From b244db0e44df0281378914f0f9b9e6e3b5c44884 Mon Sep 17 00:00:00 2001 From: Jonathan Sadowski Date: Fri, 1 Apr 2022 12:43:18 -0500 Subject: [PATCH] Update arguments parsing to use getopt, and add path option --- tools/monorepo/check-changelogger-use.php | 89 ++++++++++++----------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/tools/monorepo/check-changelogger-use.php b/tools/monorepo/check-changelogger-use.php index cfcc8bac982..04d0c42847d 100644 --- a/tools/monorepo/check-changelogger-use.php +++ b/tools/monorepo/check-changelogger-use.php @@ -14,61 +14,54 @@ function usage() { global $argv; echo << +USAGE: {$argv[0]} [--debug|-v] [--list] [-p |--path=] Checks that a monorepo commit contains a Changelogger change entry for each project touched. - --debug, -v Display verbose output. - --list Just list projects, no explanatory output. - Base git ref to compare for changed files. - Head git ref to compare for changed files. + --debug, -v Display verbose output. + --list Just list projects, no explanatory output. + --path=, Project path to check for changed files. + -p + Base git ref to compare for changed files. + Head git ref to compare for changed files. EOH; exit( 1 ); } -$idx = 0; -$verbose = false; -$list = false; -$base = null; -$head = null; -for ( $i = 1; $i < $argc; $i++ ) { - switch ( $argv[ $i ] ) { - case '-v': - case '--debug': - $verbose = true; - break; - case '--list': - $list = true; - break; - case '-h': - case '--help': - usage(); - break; - default: - if ( substr( $argv[ $i ], 0, 1 ) !== '-' ) { - switch ( $idx++ ) { - case 0: - $base = $argv[ $i ]; - break; - case 1: - $head = $argv[ $i ]; - break; - default: - fprintf( STDERR, "\e[1;31mToo many arguments.\e[0m\n" ); - usage(); - } - } else { - fprintf( STDERR, "\e[1;31mUnrecognized parameter `%s`.\e[0m\n", $argv[ $i ] ); - usage(); - } - break; - } +// Options followed by a single colon have a required value. +$short_options = 'vhp:'; +$long_options = array( + 'debug', + 'list', + 'help', + 'path:', +); +$options = getopt( $short_options, $long_options, $remain_index ); +$arg_count = count( $argv ) - $remain_index; + +if ( isset( $options['h'] ) || isset( $options['help'] ) ) { + usage(); } -if ( null === $head ) { +$list = isset( $options['l'] ) || isset( $options['list'] ); +$verbose = isset( $options['v'] ) || isset( $options['debug'] ); +$path = false; +if ( isset( $options['p'] ) || isset( $options['path'] ) ) { + $path = isset( $options['path'] ) ? $options['path'] : $options['p']; +} + +if ( $arg_count > 2 ) { + fprintf( STDERR, "\e[1;31mToo many arguments.\e[0m\n" ); + usage(); +} + +if ( $arg_count < 2 ) { fprintf( STDERR, "\e[1;31mBase and head refs are required.\e[0m\n" ); usage(); } +$base = $argv[ count( $argv ) - 2 ]; +$head = $argv[ count( $argv ) - 1 ]; + if ( $verbose ) { /** * Output debug info. @@ -106,11 +99,19 @@ if ( ! $workspace || ! is_array( $workspace['projects'] ) ) { $composer_projects = array(); foreach( $workspace['projects'] as $project => $directory ) { + if ( $path && $directory !== $path ) { + continue; + } if ( file_exists( $base_path . '/' . $directory . '/composer.json' ) ) { $composer_projects[] = $directory; } } +if ( $path && ! count( $composer_projects ) ) { + debug( sprintf( 'The provided project path, %s, did not contain a composer file.', $path ) ); + exit( 1 ); +} + // Find projects that use changelogger, and read the relevant config. $changelogger_projects = array(); foreach ( $composer_projects as $project_path ) { @@ -128,7 +129,7 @@ foreach ( $composer_projects as $project_path ) { $data = isset( $data['extra']['changelogger'] ) ? $data['extra']['changelogger'] : array(); $data += array( 'changelog' => $project_path . '/CHANGELOG.md', - 'changes-dir' => $project_patch . '/changelog', + 'changes-dir' => $project_path . '/changelog', ); $changelogger_projects[ $project_path ] = $data; }