Skip to content

Commit

Permalink
#6 Add --project option to InitCommand class
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas committed Jul 22, 2015
1 parent c48b6ee commit 5ef30c7
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions src/Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class InitCommand extends Command
'destination' => 'drupal/config.yml',
],
[
'source' => 'php/config.yml',
'source' => '/../phpqa.yml',
'destination' => 'php/config.yml',
],
[
'source' => 'symfony/config.yml',
'source' => '/../phpqa.yml',
'destination' => 'symfony/config.yml',
],
[
Expand All @@ -36,6 +36,17 @@ class InitCommand extends Command
],
];

private $projects = [
'php',
'symfony',
'drupal'
];

private $dirs = [
'home',
'current'
];

protected function configure()
{
$this
Expand All @@ -45,7 +56,19 @@ protected function configure()
'dir',
null,
InputOption::VALUE_REQUIRED,
'Directory to copy file(s) valid options home, current'
sprintf(
'Directory to copy file(s) valid options (%s)',
implode($this->dirs)
)
)
->addOption(
'project',
null,
InputOption::VALUE_OPTIONAL,
sprintf(
'Project name to copy config from, must be (%s).',
implode(',', $this->projects)
)
)
->addOption(
'override',
Expand All @@ -62,9 +85,22 @@ protected function execute(InputInterface $input, OutputInterface $output)

$dir = $input->getOption('dir');

if (!$dir) {
if (!$dir || !in_array($dir, $this->dirs)) {
throw new \Exception(
'You must provide a valid dir value (home or current)'
sprintf(
'You must provide a valid dir value (%s)',
implode(',', $this->dirs)
)
);
}

$project = $input->getOption('project');
if ($project && !in_array($project, $this->projects)) {
throw new \Exception(
sprintf(
'You must provide a valid project value (%s)',
implode(',', $this->projects)
)
);
}

Expand All @@ -74,20 +110,26 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

if ($dir === 'current') {
$this->copyCurrentDirectory($output, $config, $override);
$this->copyCurrentDirectory($output, $config, $override, $project);
}

if ($dir === 'home') {
$this->copyHomeDirectory($output, $config, $override);
}
}

private function copyCurrentDirectory($output, $config, $override)
private function copyCurrentDirectory($output, $config, $override, $project)
{
$baseConfigDirectory = $config->getBaseConfigDirectory();
$currentDirectory = $config->getApplicationDirectory();

$source = $baseConfigDirectory.'/../phpqa.yml';

$configFile = $baseConfigDirectory.$project.'/config.yml';
if (file_exists($configFile)) {
$source = $configFile;
}

$destination = $currentDirectory.'phpqa.yml';

if ($this->copyFile($source, $destination, $override)) {
Expand Down

0 comments on commit 5ef30c7

Please sign in to comment.