Skip to content

Commit

Permalink
Merge pull request #7 from mattsqd/feature/6-standards-option-remove
Browse files Browse the repository at this point in the history
resolves #6: standards option remove
  • Loading branch information
mattsqd authored May 30, 2023
2 parents b654715 + d0a5c8e commit 8d7d0b4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 70 deletions.
32 changes: 14 additions & 18 deletions robo.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,24 @@ command:
validate:
# Used in 'validate:coding-standards' and 'validate:branch-name'
options:
# This is used as a token replacement. It is most useful for 'pattern' so
# you can ensure that commit and branch names have a issue number in them
# so that Jira, GitHub, or Gitlab know which commits and branches belong
# to which issue.
# For example, if you bitbucket issue was XZY-1234, you can make project-id
# XYZ.
project-id: ''
# This is used as a token replacement. It is most useful for 'pattern' so
# you can ensure that commit and branch names have an issue number in them
# so that Jira, GitHub, or Gitlab know which commits and branches belong
# to which issue.
# For example, if you bitbucket issue was XZY-1234, you can make project-id
# XYZ.
project-id: ''
# Used in 'validate:coding-standards'
# Requires https://github.com/squizlabs/PHP_CodeSniffer be installed.
coding-standards:
options:
# This is initially configured for Drupal. However, this can be any PHPCS standard.
# Individual PHPCS commands cannot use more than one standard, so this will cause
# the same command to be run for each standard for each path.
standards:
- Drupal
- DrupalPractice
# This is required for non-Drupal projects. It will be in the form of:
# path/to/files/a:
# extensions: 'php,module,inc'
# ignore: '*node_modules/*,*bower_components/*,*vendor/*,*.min.js,*.min.css'
# path/to/files/b: {}
# path/to/files/c:
# standard:
# - psr2
# standard: 'Drupal'
# The key is used as the path, and the value are the options passed to PHPCS.
# If no options are given, as in 'b', then 'similar-options' will be used.
# If any options are given, then similar options will not be used, so you must
Expand All @@ -38,9 +31,10 @@ command:
# If 'standard' is only given, as in 'c' 'similar-options' WILL still be applied.
paths: {}
# These are the options passed to PHPCS if 'paths' does not set any options.
# Please see https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options.
# Please see https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options
# and https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage.
similar-options:
colors: ''
standard: 'Drupal,DrupalPractice'
extensions: 'php,module,inc,install,test,profile,theme,css,info'
ignore: '*node_modules/*,*bower_components/*,*vendor/*,*.min.js,*.min.css'
# Used in 'validate:composer-lock'
Expand Down Expand Up @@ -79,7 +73,9 @@ command:
pattern: '/^feature\/{$project_id}-([\d]{1,})-(?!.*--)([a-z\d]{1})([a-z\d-]{3,})([a-z\d]{1})$/'
# Show help messages if the branch name does not match.
custom-help:
- 'feature/{$project_id}-* where * can be:'
- 'feature/{$project_id}-x-y'
- ' Where x is the ticket number.'
- ' And y is:'
- ' - Always lower case.'
- ' - Starts and end with a letter or integer.'
- ' - Contains letters, integers, or dashes (non-consecutive).'
Expand Down
2 changes: 1 addition & 1 deletion robo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ command:
paths:
# Robo projects use psr2 standards.
src:
extensions: 'php'
extensions: php
standard: psr2
commit-messages:
options:
Expand Down
76 changes: 25 additions & 51 deletions src/Robo/Plugin/Commands/ValidateCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,9 @@ public function validateAll(): ResultData
*/
public function validateCodingStandards(
array $opts = [
'standards' => ['Drupal', 'DrupalPractice'],
'paths' => [],
'similar-options' => [
'standard' => '',
'colors' => '',
'standard' => 'Drupal,DrupalPractice',
'extensions' => 'php,module,inc,install,test,profile,theme,css,info',
'ignore' => '*node_modules/*,*bower_components/*,*vendor/*,*.min.js,*.min.css',
],
Expand All @@ -218,16 +216,14 @@ public function validateCodingStandards(
$paths,
$similar_options,
] = $this->getOptions([
'paths',
'similar-options',
], $opts, false);
'paths',
'similar-options',
], $opts, false);
[
$standards,
$composer_json_path,
] = $this->getOptions([
'standards',
'composer-json-path',
], $opts);
'composer-json-path',
], $opts);
$this->sayWithWrapper('Checking for coding standards issues.');
// Load composer.json and determine where web root and paths are.
if (empty($paths)) {
Expand All @@ -238,11 +234,10 @@ public function validateCodingStandards(
file_get_contents($composer_json_path),
true
);
$web_root = $composer['extra']['drupal-scaffold']['locations']['web-root'];
if (!empty($composer['extra']['installer-paths'])) {
foreach ($composer['extra']['installer-paths'] as $key => $installer_paths) {
if (!empty($installer_paths)) {
switch ($installer_paths[0]) {
switch ($installer_paths[0] ?? '') {
case 'type:drupal-custom-module':
$custom_modules_path = str_replace(
'/{$name}',
Expand Down Expand Up @@ -271,36 +266,27 @@ public function validateCodingStandards(
}
}
}
$web_root = $web_root ?? 'web/';
$web_root = $composer['extra']['drupal-scaffold']['locations']['web-root'] ?? 'web/';
// Ensure web root ends in "/".
$web_root = !str_ends_with(
$web_root,
'/'
) ? $web_root.'/' : $web_root;

// Set defaults if not found in composer.json.
$custom_modules_path = $custom_modules_path ?? $web_root.'modules/custom';
$custom_profiles_path = $custom_profiles_path ?? $web_root.'profiles/custom';
$custom_theme_path = $custom_theme_path ?? $web_root.'themes/custom';
$custom_modules_path = $custom_modules_path ?? $web_root . 'modules/custom';
$custom_profiles_path = $custom_profiles_path ?? $web_root . 'profiles/custom';
$custom_theme_path = $custom_theme_path ?? $web_root . 'themes/custom';

$paths = [
$custom_modules_path => $similar_options,
$custom_profiles_path => $similar_options,
$custom_theme_path => $similar_options,
$custom_modules_path => [],
$custom_profiles_path => [],
$custom_theme_path => [],
];
} else {
// Go through each explicitly configured path and set the options for
// each if not given.
foreach ($paths as &$options) {
// Save the explicit standards.
$standard = $options['standard'] ?? [];
unset($options['standard']);
if (empty($options)) {
$options = $similar_options;
}
// Add the explicit standards back.
$options['standard'] = (array) $standard;
}
}
// Set similar option defaults on the paths.
foreach ($paths as &$options) {
$options += $similar_options;
}
unset($options);
// Remove any path that does not actually exist.
Expand All @@ -318,24 +304,12 @@ public function validateCodingStandards(
}
$one_failed = false;
foreach ($paths as $path => $path_options) {
// If standards is not given for a path, use the --standards option.
if (empty($path_options['standard'])) {
$standards = (array) $standards;
} else {
$standards = (array) $path_options['standard'];
}
// This is a virtual option since phpcs only supports one
// standard at a time.
unset($path_options['standard']);
foreach ($standards as $standard) {
$success = $this->taskExec('./vendor/bin/phpcs')
->options($path_options, '=')
->option('standard', $standard, '=')
->arg($path)
->run()->wasSuccessful();
if (!$one_failed && !$success) {
$one_failed = true;
}
$success = $this->taskExec('./vendor/bin/phpcs')
->options($path_options, '=')
->arg($path)
->run()->wasSuccessful();
if (!$one_failed && !$success) {
$one_failed = true;
}
}
if ($one_failed) {
Expand Down Expand Up @@ -624,7 +598,7 @@ public function validateBranchName(
case 'custom':
if (empty(array_filter($custom_help))) {
$this->printError("If 'custom' branch type is used then --custom-help must be given. Otherwise,"
. " pass custom --valid-branch-names without 'custom'.");
. " pass custom --valid-branch-names without 'custom'.");
return new ResultData(ResultData::EXITCODE_ERROR);
}
foreach ($custom_help as &$item) {
Expand Down

0 comments on commit 8d7d0b4

Please sign in to comment.