Skip to content

Commit

Permalink
refactor: switch to using an executable object to interact with composer
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Dec 24, 2024
1 parent 77b2ec3 commit 9f1940a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/Command/InstallPluginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
use Symfony\Component\Finder\Finder;
use Ymir\Cli\ApiClient;
use Ymir\Cli\CliConfiguration;
use Ymir\Cli\Executable\ComposerExecutable;
use Ymir\Cli\GitHubClient;
use Ymir\Cli\Process\Process;
use Ymir\Cli\ProjectConfiguration\ProjectConfiguration;
use Ymir\Cli\Support\Arr;

Expand All @@ -33,6 +33,13 @@ class InstallPluginCommand extends AbstractProjectCommand
*/
public const NAME = 'install-plugin';

/**
* The Composer executable.
*
* @var ComposerExecutable
*/
private $composerExecutable;

/**
* The file system.
*
Expand All @@ -57,10 +64,11 @@ class InstallPluginCommand extends AbstractProjectCommand
/**
* Constructor.
*/
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, Filesystem $filesystem, GitHubClient $gitHubClient, ProjectConfiguration $projectConfiguration, string $projectDirectory)
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, ComposerExecutable $composerExecutable, Filesystem $filesystem, GitHubClient $gitHubClient, ProjectConfiguration $projectConfiguration, string $projectDirectory)
{
parent::__construct($apiClient, $cliConfiguration, $projectConfiguration);

$this->composerExecutable = $composerExecutable;
$this->filesystem = $filesystem;
$this->gitHubClient = $gitHubClient;
$this->projectDirectory = rtrim($projectDirectory, '/');
Expand Down Expand Up @@ -90,7 +98,7 @@ protected function perform()

if ('bedrock' === $projectType) {
$this->output->info($message.' using Composer');
Process::runShellCommandline('composer require ymirapp/wordpress-plugin');
$this->composerExecutable->require('ymirapp/wordpress-plugin');
} elseif ('wordpress' === $projectType) {
$this->output->info($message.' from GitHub');
$this->installFromGitHub();
Expand Down
14 changes: 11 additions & 3 deletions src/Command/Project/InitializeProjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
use Ymir\Cli\Command\Docker\CreateDockerfileCommand;
use Ymir\Cli\Command\InstallPluginCommand;
use Ymir\Cli\Command\Provider\ConnectProviderCommand;
use Ymir\Cli\Executable\ComposerExecutable;
use Ymir\Cli\Executable\DockerExecutable;
use Ymir\Cli\Executable\WpCliExecutable;
use Ymir\Cli\Process\Process;
use Ymir\Cli\ProjectConfiguration\ProjectConfiguration;
use Ymir\Cli\Support\Arr;

Expand All @@ -46,6 +46,13 @@ class InitializeProjectCommand extends AbstractCommand
*/
public const NAME = 'project:init';

/**
* The Composer executable.
*
* @var ComposerExecutable
*/
private $composerExecutable;

/**
* Docker executable.
*
Expand Down Expand Up @@ -77,10 +84,11 @@ class InitializeProjectCommand extends AbstractCommand
/**
* Constructor.
*/
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, DockerExecutable $dockerExecutable, Filesystem $filesystem, ProjectConfiguration $projectConfiguration, string $projectDirectory, WpCliExecutable $wpCliExecutable)
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, ComposerExecutable $composerExecutable, DockerExecutable $dockerExecutable, Filesystem $filesystem, ProjectConfiguration $projectConfiguration, string $projectDirectory, WpCliExecutable $wpCliExecutable)
{
parent::__construct($apiClient, $cliConfiguration, $projectConfiguration);

$this->composerExecutable = $composerExecutable;
$this->dockerExecutable = $dockerExecutable;
$this->filesystem = $filesystem;
$this->projectDirectory = rtrim($projectDirectory, '/');
Expand Down Expand Up @@ -213,7 +221,7 @@ private function checkForWordPress(string $projectType)

if ('bedrock' === $projectType) {
$this->output->info('Creating new Bedrock project');
Process::runShellCommandline('composer create-project roots/bedrock .');
$this->composerExecutable->createProject('roots/bedrock');
} elseif ('wordpress' === $projectType) {
$this->output->info('Downloading WordPress using WP-CLI');
$this->wpCliExecutable->downloadWordPress();
Expand Down
49 changes: 49 additions & 0 deletions src/Executable/ComposerExecutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/*
* This file is part of Ymir command-line tool.
*
* (c) Carl Alexander <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Ymir\Cli\Executable;

class ComposerExecutable extends AbstractExecutable
{
/**
* Create a new project from the given package into the given directory.
*/
public function createProject(string $package, string $directory = '.')
{
$this->run(sprintf('create-project %s %s', $package, $directory));
}

/**
* {@inheritdoc}
*/
public function getDisplayName(): string
{
return 'Composer';
}

/**
* {@inheritdoc}
*/
public function getExecutable(): string
{
return 'composer';
}

/**
* Add the given package to the project's "composer.json" file and install it.
*/
public function require(string $package)
{
$this->run(sprintf('require %s', $package));
}
}

0 comments on commit 9f1940a

Please sign in to comment.