Skip to content

Commit

Permalink
Add the clone command and adjust output
Browse files Browse the repository at this point in the history
  • Loading branch information
borkweb committed May 2, 2024
1 parent ea878f9 commit c37b9bb
Show file tree
Hide file tree
Showing 24 changed files with 315 additions and 80 deletions.
1 change: 1 addition & 0 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function __construct( string $version ) {
$this->add( new Commands\Build() );
$this->add( new Commands\Check() );
$this->add( new Commands\Clean() );
$this->add( new Commands\CloneCommand() );
$this->add( new Commands\GetVersion() );
$this->add( new Commands\Help() );
$this->add( new Commands\I18n() );
Expand Down
13 changes: 12 additions & 1 deletion src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use StellarWP\Pup\App;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;

abstract class Command extends SymfonyCommand {
/**
Expand All @@ -18,6 +20,13 @@ abstract class Command extends SymfonyCommand {
*/
protected $should_validate_puprc = true;

public function __construct( string $name = null ) {
parent::__construct( $name );

// Declare options that we want to be able to use globally in workflows without declaring it in each command.
$this->addOption( 'branch', null, InputOption::VALUE_REQUIRED, 'The branch to use.' );
}

/**
* Runs the wrapping execute command.
*
Expand Down Expand Up @@ -47,6 +56,8 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
* @return void
*/
protected function initialize( InputInterface $input, OutputInterface $output ) {
$output->getFormatter()->setStyle( 'info', new OutputFormatterStyle( 'blue' ) );
$output->getFormatter()->setStyle( 'success', new OutputFormatterStyle( 'green' ) );
$this->io = new Io( $input, $output );
}

Expand All @@ -65,4 +76,4 @@ protected function getIO(): Io {
public function setShouldNotValidatePuprc() {
$this->should_validate_puprc = false;
}
}
}
2 changes: 1 addition & 1 deletion src/Commands/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
chdir( $config->getWorkingDir() );
}

$io->writeln( '<info>Build complete.</info>' );
$io->writeln( '<fg=green>✓</> <success>Build complete.</success>' );
return 0;
}
}
2 changes: 1 addition & 1 deletion src/Commands/Checks/AbstractCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,4 @@ public function shouldBailOnFailureDev(): bool {
protected function writeln( string $message ) {
$this->getIO()->writeln( $message );
}
}
}
6 changes: 3 additions & 3 deletions src/Commands/Checks/Tbd.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ protected function checkExecute( InputInterface $input, Io $output ): int {
$output->writeln( '' );
}
} else {
$output->writeln( '<info>No TBDs found!</info>' );
$output->writeln( '<success>No TBDs found!</success>' );
$output->writeln( '' );
}
$output->writeln( '' );

if ( $found_tbds ) {
$output->writeln( "<fg=red>TBDs have been found!</>" );
} else {
$output->writeln( '<info>Success! No TBDs found.</info>' );
$output->writeln( '<success>Success! No TBDs found.</success>' );
}

return $found_tbds ? 1 : 0;
Expand Down Expand Up @@ -168,4 +168,4 @@ protected function scanDir( string $root, string $current_dir, string $scan_dir,

return $matched_lines;
}
}
}
4 changes: 2 additions & 2 deletions src/Commands/Checks/VersionConflict.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected function checkExecute( InputInterface $input, Io $output ): int {
return 1;
}

$output->writeln( '<info>No version conflicts found.</info>' );
$output->writeln( '<success>No version conflicts found.</success>' );
return 0;
}
}
}
14 changes: 6 additions & 8 deletions src/Commands/Clean.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,22 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
$clean_steps = $config->getCleanCommands();
$io = $this->getIO();

$output->writeln( '<fg=yellow>Cleaning up...</>' );
$this->io->section( '<fg=yellow>Cleaning up...</>' );

$output->write( "* Removing zip dir..." );
if ( file_exists( $zip_dir ) && DirectoryUtils::rmdir( $zip_dir ) !== 0 ) {
throw new \Exception( "Could not remove {$zip_dir}." );
}
$output->write( 'Complete.' . PHP_EOL );
$output->writeln( "<fg=green>✓</> Removing zip dir...Complete." );

$output->write( "* Removing build dir..." );
if ( file_exists( $build_dir ) && DirectoryUtils::rmdir( $build_dir ) !== 0 ) {
throw new \Exception( "Could not remove {$build_dir}." );
}
$output->write( 'Complete.' . PHP_EOL );
$output->writeln( "<fg=green>✓</> Removing build dir...Complete." );

$pup_distfiles = $config->getWorkingDir() . '.pup-distfiles';
if ( file_exists( $pup_distfiles ) ) {
if ( unlink( $pup_distfiles ) ) {
$output->writeln( 'Removing .pup-distfiles...Complete.' );
$output->writeln( '<fg=green>✓</> Removing .pup-distfiles...Complete.' );
} else {
throw new \Exception( "Could not remove {$build_dir}." );
}
Expand All @@ -58,7 +56,7 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
$pup_distignore = $config->getWorkingDir() . '.pup-distignore';
if ( file_exists( $pup_distignore ) ) {
if ( unlink( $pup_distignore ) ) {
$output->writeln( 'Removing .pup-distignore...Complete.' );
$output->writeln( '<fg=green>✓</> Removing .pup-distignore...Complete.' );
} else {
throw new \Exception( "Could not remove {$build_dir}." );
}
Expand All @@ -67,7 +65,7 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
$pup_distinclude = $config->getWorkingDir() . '.pup-distinclude';
if ( file_exists( $pup_distinclude ) ) {
if ( unlink( $pup_distinclude ) ) {
$output->writeln( 'Removing .pup-distinclude...Complete.' );
$output->writeln( '<fg=green>✓</> Removing .pup-distinclude...Complete.' );
} else {
throw new \Exception( "Could not remove {$build_dir}." );
}
Expand Down
60 changes: 60 additions & 0 deletions src/Commands/CloneCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace StellarWP\Pup\Commands;

use StellarWP\Pup\App;
use StellarWP\Pup\Command\Command;
use StellarWP\Pup\Utils\Directory as DirectoryUtils;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Clone a git repository.
*
* This command is named CloneCommand because Clone is a reserved word in PHP.
*/
class CloneCommand extends Command {
/**
* @inheritDoc
*
* @return void
*/
protected function configure() {
$this->setName( 'clone' )
->setDescription( 'Clone a git repository.' )
->setHelp( 'Clone a git repository.' );
}

/**
* @inheritDoc
*/
protected function execute( InputInterface $input, OutputInterface $output ) {
parent::execute( $input, $output );
$config = App::getConfig();
$build_dir = $config->getBuildDir();
$branch = $input->getOption( 'branch' );

$branch_arg = '';
if ( $branch ) {
$branch_arg = "-b {$branch}";
}

$repo = App::getConfig()->getRepo();

if ( file_exists( $build_dir ) ) {
$build_dir_basename = basename( $build_dir );
$output->writeln( "The {$build_dir_basename} already exists." );
$output->write( "Removing build dir..." );
if ( file_exists( $build_dir ) && DirectoryUtils::rmdir( $build_dir ) !== 0 ) {

Check failure on line 48 in src/Commands/CloneCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Left side of && is always true.
throw new \Exception( "Could not remove {$build_dir}." );
}
$output->write( 'Complete.' . PHP_EOL );
}

$output->writeln( '<comment>Cloning the ' . $repo . ' repo into ' . App::getConfig()->getBuildDir( false ) . '...</comment>' );
system( 'git clone --quiet --recurse-submodules -j8 --shallow-submodules --depth 1 ' . $branch_arg . ' ' . $repo . ' ' . App::getConfig()->getBuildDir( false ) );
$output->writeln( '<fg=green>✓</> Clone complete.' );

return 0;
}
}
16 changes: 8 additions & 8 deletions src/Commands/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
$config = App::getConfig();
$zip_name = $config->getZipName();

$output->writeln( '<comment>Packaging zip...</comment>' );
$this->io->section( '<comment>Packaging zip...</comment>' );

$buffer = new BufferedOutput();
$application = $this->getApplication();
Expand All @@ -77,39 +77,39 @@ protected function execute( InputInterface $input, OutputInterface $output ) {

$zip_filename = "{$full_zip_name}.zip";

$output->write( '* Updating version files...' );
$output->writeln( '<fg=gray>- Updating version files...</>' );
if ( $version !== 'unknown' ) {
$this->updateVersionsInFiles( $version );
}
$output->write( 'Complete.' . PHP_EOL );
$output->writeln( '<fg=green>✓</> Updating version files...Complete.' );

$output->write( '* Synchronizing files to zip directory...' );
$output->writeln( '<fg=gray>- Synchronizing files to zip directory...</>' );
$pup_zip_dir = $config->getZipDir();

DirectoryUtils::rmdir( $pup_zip_dir );

mkdir( $pup_zip_dir );

$results = $this->syncFiles( $root, $pup_zip_dir );
$output->write( 'Complete.' . PHP_EOL );
$output->writeln( '<fg=green>✓</> Synchronizing files to zip directory...Complete.' );

if ( $results !== 0 ) {
$this->undoChanges();
return $results;
}

$output->write( '* Zipping...' );
$output->writeln( '<fg=gray>- Zipping...</>' );
$results = $this->createZip( $pup_zip_dir, $zip_filename, $zip_name );

if ( $results !== 0 ) {
$this->undoChanges();
return $results;
}
$output->write( 'Complete.' . PHP_EOL );
$output->writeln( '<fg=green>✓</> Zipping...Complete.' );

$this->undoChanges();

$this->output->writeln( "<info>Zip {$zip_filename} created!</info>" );
$this->output->writeln( PHP_EOL . "<fg=green>✓</> <success>Zip {$zip_filename} created!</success>" . PHP_EOL );

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
chdir( $config->getWorkingDir() );
}

$io->writeln( '<info>Workflow complete.</info>' );
$io->writeln( '<success>Workflow complete.</success>' );
}

return 0;
Expand Down
39 changes: 30 additions & 9 deletions src/Commands/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,11 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
$branch = $this->input->getArgument( 'branch' );

if ( ! $this->input->getOption( 'no-clone' ) ) {
$branch_arg = '';
if ( $branch ) {
$branch_arg = "-b {$branch}";
$results = $this->runClone();
if ( $results !== 0 ) {
$output->writeln( '<error>The clone step of `pup zip` failed.</error>' );
return $results;
}

$repo = App::getConfig()->getRepo();

$output->writeln( '<comment>Cloning the ' . $repo . ' repo into ' . App::getConfig()->getBuildDir( false ) . '...</comment>' );
system( 'git clone --quiet --recurse-submodules -j8 --shallow-submodules --depth 1 ' . $branch_arg . ' ' . $repo . ' ' . App::getConfig()->getBuildDir( false ) );
$output->writeln( 'Clone complete.' );
} elseif ( $branch ) {
system( 'git checkout --quiet ' . $branch );
}
Expand Down Expand Up @@ -164,6 +159,32 @@ protected function runCheck(): int {
return $command->run( $command_input, $this->output );
}

/**
* Run the clone command.
*
* @throws \Symfony\Component\Console\Exception\ExceptionInterface
*
* @return int
*/
protected function runClone(): int {
$application = $this->getApplication();
if ( ! $application ) {
return 1;
}

$command = $application->find( 'clone' );
$arguments = [];

$branch = $this->input->getArgument( 'branch' );

if ( $branch ) {
$arguments['--branch'] = $this->input->getArgument( 'branch' );
}

$command_input = new ArrayInput( $arguments );
return $command->run( $command_input, $this->output );
}

/**
* Run the get-version command.
*
Expand Down
Loading

0 comments on commit c37b9bb

Please sign in to comment.