Skip to content

Commit

Permalink
Generate YAML file from scratch on add
Browse files Browse the repository at this point in the history
If the specified file does not exist, ask to generate it, and request
minimal information to fill the organization.
  • Loading branch information
marvil07-adapt committed Feb 26, 2021
1 parent 4cd89fb commit 40ed3fb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Ideas

- Provide a dynamic way to find the YAML to process.
- generate YAML file from scratch via cli
- project example for composer
- publish on commit?
52 changes: 51 additions & 1 deletion src/Console/Command/AddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;

/**
Expand Down Expand Up @@ -50,14 +51,54 @@ protected function configure() {
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$yml_file = $input->getArgument('contributions-yml');
$this->fillContributions($yml_file);
try {
$this->fillContributions($yml_file);
}
catch (\InvalidArgumentException $exception) {
// File does not exist yet.
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion(sprintf('The "%s" file does not exist yet. Do you want to generate initialize it? (y/n) ', $yml_file), false);
if (!$helper->ask($input, $output, $question)) {
// Nothing else to do, cannot continue, hence fail.
$output->writeln('<error>A contributions YAML file is needed to continue. See an examples directory or accept to generate it while runnind add command.</error>');
return Command::FAILURE;
}
$this->generateMinimalYaml();
$this->getOrganization($input, $output);
}
$project = $this->getProject($input, $output);
$contribution = $this->getContribution($project, $input, $output);
$this->contributions['contributions'][] = $contribution;
$this->writeYaml($yml_file, $output);
return Command::SUCCESS;
}

/**
* Helper to get organization.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* Input object.
* @param \Symfony\Component\Console\Output\OutputInterface $output
* Output object.
*
* @return array
* A map with two keys, name and url, for the organization.
*/
protected function getOrganization(InputInterface $input, OutputInterface $output) {
$helper = $this->getHelper('question');
$question = new Question('[1/2] What is the name of the organization? ');
$question->setValidator([self::class, 'isNotEmpty']);
$name = $helper->ask($input, $output, $question);
$question = new Question('What is main URL for the organization? ');
$question->setValidator([self::class, 'isNotEmpty']);
$url = $helper->ask($input, $output, $question);
$this->contributions['organization'] = [
'name' => $name,
'url' => $url,
];
return $this->contributions['organization'];
}

/**
* Helper to get related project.
*
Expand Down Expand Up @@ -227,4 +268,13 @@ public static function cleanEmpty($value) {
return $lines;
}

protected function generateMinimalYaml() {
$this->contributions = [
'organization' => [],
'people' => [],
'projects' => [],
'contributions' => [],
];
}

}

0 comments on commit 40ed3fb

Please sign in to comment.