Skip to content

Commit

Permalink
Add yes option to push subtree (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
gseidel authored Apr 25, 2021
1 parent c048f74 commit d8f3400
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/Command/PushSubtreeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protected function configure()
->addOption('tag', null, InputOption::VALUE_REQUIRED, 'Tag')
->addOption('branch', null, InputOption::VALUE_REQUIRED, 'Branch name')
->addOption('force', null, InputOption::VALUE_NONE, 'Force push')
->addOption('yes', null, InputOption::VALUE_NONE, 'Always yes')
;
}

Expand All @@ -28,8 +29,9 @@ public function execute(InputInterface $input, OutputInterface $output)
$force = $input->getOption('force');
$branch = $input->getOption('branch');
$tag = $input->getOption('tag');
$yes = $input->getOption('yes');
$configuration = (new Factory())->create();

return (new PushSubtree($input, $output, $this->getHelper('question'), $configuration, $name, $force, $branch, $tag))();
return (new PushSubtree($input, $output, $this->getHelper('question'), $configuration, $name, $force, $branch, $tag, $yes))();
}
}
17 changes: 9 additions & 8 deletions src/Subroutine/PushSubtree.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class PushSubtree extends AbstractSubroutine
/** @var bool|null */
private $force;

/** @var bool|null */
private $yes;

/**
* PushSubtree constructor.
* @param InputInterface $input
Expand All @@ -39,6 +42,7 @@ class PushSubtree extends AbstractSubroutine
* @param bool|null $force
* @param string|null $branch
* @param string|null $tag
* @param bool|null $yes
*/
public function __construct(
InputInterface $input,
Expand All @@ -48,14 +52,16 @@ public function __construct(
?string $name,
?bool $force,
?string $branch,
?string $tag
?string $tag,
?bool $yes
) {
parent::__construct($input, $output, $questionHelper);
$this->configuration = $configuration;
$this->name = $name;
$this->force = $force;
$this->branch = $branch;
$this->tag = $tag;
$this->yes = $yes;
}

public function __invoke()
Expand All @@ -65,7 +71,7 @@ public function __invoke()
return Command::FAILURE;
}

$splitShPath = (new DownloadSplitSh($this->input, $this->output, $this->questionHelper))();
$splitShPath = (new DownloadSplitSh($this->input, $this->output, $this->questionHelper, $this->yes))();
$git = new Git(getcwd());
if ($splitShPath !== null) {
$git->setSplitShBin($splitShPath);
Expand All @@ -83,16 +89,11 @@ public function __invoke()

foreach ($subtrees as $subtree) {
if (!$git->hasRemote($subtree->getName())) {
// Add remote to main repository;
$this->output->writeln(sprintf('Add remote "%s"', $subtree->getName()));
$git->addRemote($subtree->getName(), $subtree->getUrl());
}
}

$branch = $this->branch !== null ? $this->branch : $git->getCurrentBranch();
foreach ($subtrees as $subtree) {
$git->pushSubtreeBranch($subtree->getName(), $subtree->getPrefix(), $branch);
}

if ($this->tag) {
$this->pushTag($subtrees, $git);
} else {
Expand Down
37 changes: 31 additions & 6 deletions src/Task/DownloadSplitSh.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@
namespace Enhavo\Component\Cli\Task;

use Enhavo\Component\Cli\AbstractSubroutine;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Filesystem\Filesystem;

class DownloadSplitSh extends AbstractSubroutine
{
/** @var bool */
private $yes;

/**
* Interactive constructor.
* @param InputInterface $input
* @param OutputInterface $output
* @param QuestionHelper $questionHelper
* @param bool $yes
*/
public function __construct(InputInterface $input, OutputInterface $output, QuestionHelper $questionHelper, ?bool $yes)
{
parent::__construct($input, $output, $questionHelper);
$this->yes = $yes;
}

public function __invoke($overwrite = false)
{
$home = getenv("HOME");
Expand All @@ -18,12 +37,18 @@ public function __invoke($overwrite = false)
}

while(true) {
$question = new Question(sprintf('install splitsh to "%s"? [y/n]', $path), 'y');
$option = $this->questionHelper->ask($this->input, $this->output, $question);

if (strtolower($option) === 'n') {
return null;
} elseif (strtolower($option) === 'y') {
if (!$this->yes) {
$question = new Question(sprintf('install splitsh to "%s"? [y/n]', $path), 'y');
$option = $this->questionHelper->ask($this->input, $this->output, $question);

if (strtolower($option) === 'n') {
return null;
} elseif (strtolower($option) === 'y') {
self::download($path, $overwrite);
return $path;
}
} else {
$this->output->writeln(sprintf('install splitsh to "%s"', $path));
self::download($path, $overwrite);
return $path;
}
Expand Down

0 comments on commit d8f3400

Please sign in to comment.