Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InitCommand - If the new key and <file> don't match, prompt for confirmation #236

Merged
merged 3 commits into from
Feb 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions src/CRM/CivixBundle/Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,29 @@ protected function configure() {
Services::templating();
$this
->setName('generate:module')
->setDescription('Create a new CiviCRM Module-Extension (Regenerate module.civix.php if ext.name not specified)')
->addArgument('key', InputArgument::OPTIONAL, "Extension identifier (Ex: \"foo-bar\" or \"org.example.foo-bar\")")
->setDescription('Create a new CiviCRM Module-Extension (Regenerate module.civix.php if \"key\" not specified)')
->addArgument('key', InputArgument::OPTIONAL, "Extension identifier (Ex: \"foo_bar\" or \"org.example.foo-bar\")")
->addOption('enable', NULL, InputOption::VALUE_REQUIRED, 'Whether to auto-enable the new module (yes/no/ask)', 'ask')
->addOption('license', NULL, InputOption::VALUE_OPTIONAL, 'License for the extension (' . implode(', ', $this->getLicenses()) . ')', $this->getDefaultLicense())
->addOption('author', NULL, InputOption::VALUE_REQUIRED, 'Name of the author', $this->getDefaultAuthor())
->addOption('email', NULL, InputOption::VALUE_OPTIONAL, 'Email of the author', $this->getDefaultEmail())
->setHelp(
"Create a new CiviCRM Module-Extension (Regenerate module.civix.php if ext.name not specified)\n" .
"Create a new CiviCRM Module-Extension (Regenerate module.civix.php if \"key\" is not specified)\n" .
"\n" .
"<comment>Identification:</comment>\n" .
" Keys must be lowercase alphanumeric (with dashes and underscores allowed).\n" .
" Keys should be lowercase alphanumeric with underscores. Dots and dashes may be used with caveats.\n" .
"\n" .
" Optionally, you may use a Java-style prefix (reverse domain name).\n" .
" CiviCRM extensions formally have two names, the \"key\" and the \"file\".\n" .
" Some APIs use the \"key\" name, and other APIs use the \"file\" name.\n" .
" The \"key\" allows a Java-style prefix (reverse domain name), but \"file\" does not.\n" .
"\n" .
" However, the prefix is mostly cosmetic. The base part of the key should be globally unique.\n" .
" If you use a Java-style prefix (dots and dashes), then the extension will have split names.\n" .
"\n" .
" If you omit a Java-style prefix (dots and dashes), then the extension will have a single (matching) name.\n" .
"\n" .
"<comment>Examples:</comment>\n" .
" civix generate:module foo-bar\n" .
" civix generate:module foo-bar --license=AGPL-3.0 --author=\"Alice\" --email=\"alice@example.org\"\n" .
" civix generate:module foo_bar\n" .
" civix generate:module foo_bar --license=AGPL-3.0 --author=\"Alice\" --email=\"alice@example.org\"\n" .
" civix generate:module org.example.foo-bar \n" .
"\n"
);
Expand Down Expand Up @@ -103,6 +107,30 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$output->writeln('<error>Unrecognized license (' . $ctx['license'] . ')</error>');
return;
}

if ($ctx['fullName'] !== $ctx['mainFile']) {
$output->writeln("");
$output->writeln("<error>ALERT:</error> <comment>The requested command requires split-naming.</comment>");
$output->writeln("");
$output->writeln("CiviCRM extensions formally have two names, the \"key\" and the \"file\".");
$output->writeln("Some APIs use the \"key\" name, and other APIs use the \"file\" name.");
$output->writeln("");
$output->writeln(" <comment>\"Key\":</comment> Appears in many strings+indices. Allows Java-style prefix.");
$output->writeln(" <comment>Requested Value:</comment> {$ctx['fullName']}");
$output->writeln(" <comment>Example Usage:</comment> addStyleFile('{$ctx['fullName']}', 'example.css')");
$output->writeln(" <comment>\"File\"</comment>: Appears in PHP files+functions. No Java-style prefix.");
$output->writeln(" <comment>Requested Value:</comment> {$ctx['mainFile']}");
$output->writeln(" <comment>Example Usage:</comment> function {$ctx['mainFile']}_civicrm_config() {}");
$output->writeln("");
$output->writeln("Many developers find it easier to use matching names, but the");
$output->writeln("requested command requires splitting the names. You may continue with");
$output->writeln("split names, or you may cancel and try again with a simpler name.");
$output->writeln("");
if (!$this->confirm($input, $output, "Continue with current (split) name? [Y/n] ")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work on windows but that's come up elsewhere so isn't something new, just is new in this command. The workaround is just don't use dots.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, curious. I don't have a good setup to try it out. FWIW, that's a thin wrapper around Symfony ConfirmationQuestion. Theories:

  • The command somehow gets option--yes.
  • There's a discrepancy in the version of symfony/console. (Ex: My local has v4.4.34)
  • There's an upstream issue with ConfirmationQuestion in the active version of symfony/console.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh - symfony/console version. I must have not run composer install anytime recently.

return 1;
}
}

$ext = new Collection();

$output->writeln("<info>Initalize module " . $ctx['fullName'] . "</info>");
Expand Down