3
3
namespace ecoAPM \LibYear ;
4
4
5
5
use cli \Table ;
6
+ use Garden \Cli \Cli ;
7
+ use Exception ;
8
+ use InvalidArgumentException ;
6
9
7
10
class App
8
11
{
@@ -11,14 +14,21 @@ class App
11
14
12
15
/** @var resource */
13
16
private $ output ;
17
+ private Cli $ cli ;
14
18
15
19
/**
20
+ * @param Cli $cli
16
21
* @param Calculator $calculator
17
22
* @param ComposerFile $composer
18
23
* @param resource $output
19
24
*/
20
- public function __construct (Calculator $ calculator , ComposerFile $ composer , $ output )
25
+ public function __construct (Cli $ cli , Calculator $ calculator , ComposerFile $ composer , $ output )
21
26
{
27
+ $ this ->cli = $ cli ->description ('libyear: a simple measure of dependency freshness -- calculates the total number of years behind their respective newest versions for all dependencies listed in a composer.json file. ' )
28
+ ->opt ('quiet:q ' , 'only display outdated dependencies ' , false , 'boolean ' )
29
+ ->opt ('update:u ' , 'update composer.json with newest versions ' , false , 'boolean ' )
30
+ ->opt ('verbose:v ' , 'display network debug information ' , false , 'boolean ' )
31
+ ->arg ('path ' , 'the directory containing composer.json and composer.lock files ' );
22
32
$ this ->calculator = $ calculator ;
23
33
$ this ->composer = $ composer ;
24
34
$ this ->output = $ output ;
@@ -29,18 +39,21 @@ public function __construct(Calculator $calculator, ComposerFile $composer, $out
29
39
*/
30
40
public function run (array $ args ): void
31
41
{
32
- if (in_array ('-h ' , $ args ) || in_array ('--help ' , $ args ))
33
- {
34
- $ this ->showHelp ();
42
+ try {
43
+ $ arguments = $ this ->cli ->parse ($ args , false );
44
+ } catch (Exception $ e ) {
45
+ $ error = $ e ->getMessage ();
46
+ fwrite ($ this ->output , "{$ error }\n" );
47
+ if (!str_starts_with ($ error , "usage: " )) {
48
+ $ this ->showHelp ();
49
+ }
35
50
return ;
36
51
}
37
52
38
- $ quiet_mode = in_array ('-q ' , $ args ) || in_array ('--quiet ' , $ args );
39
- $ update_mode = in_array ('-u ' , $ args ) || in_array ('--update ' , $ args );
40
- $ verbose_mode = in_array ('-v ' , $ args ) || in_array ('--verbose ' , $ args );
41
- $ known_options = ['-q ' , '--quiet ' , '-u ' , '--update ' , '-v ' , '--verbose ' ];
42
- $ other_args = array_filter (array_slice ($ args , 1 ), fn ($ a ) => !in_array ($ a , $ known_options ));
43
- $ dir = !empty ($ other_args ) ? array_values ($ other_args )[0 ] : '. ' ;
53
+ $ quiet_mode = $ arguments ->getOpt ('quiet ' ) !== null ;
54
+ $ update_mode = $ arguments ->getOpt ('update ' ) !== null ;
55
+ $ verbose_mode = $ arguments ->getOpt ('verbose ' ) !== null ;
56
+ $ dir = $ arguments ->getArg ('path ' ) ?? '. ' ;
44
57
45
58
$ real_dir = realpath ($ dir );
46
59
fwrite ($ this ->output , "Gathering information for $ real_dir... \n" );
@@ -58,6 +71,7 @@ public function run(array $args): void
58
71
if ($ update_mode ) {
59
72
$ this ->composer ->update ($ dir , $ dependencies );
60
73
fwrite ($ this ->output , "composer.json updated \n" );
74
+ fwrite ($ this ->output , "A manual run of \"composer update \" is required to actually update dependencies \n" );
61
75
}
62
76
}
63
77
@@ -83,10 +97,10 @@ private function showTable(array $dependencies): void
83
97
fn (Dependency $ dependency ): array => [
84
98
$ dependency ->name ,
85
99
$ dependency ->current_version ->version_number ,
86
- isset ($ dependency ->current_version ->released ) ? $ dependency ->current_version ->released ->format ('Y-m-d ' ) : "" ,
87
- isset ($ dependency ->newest_version ->version_number ) ? $ dependency ->newest_version ->version_number : "" ,
88
- isset ($ dependency ->newest_version ->released ) ? $ dependency ->newest_version ->released ->format ('Y-m-d ' ) : "" ,
89
- $ dependency ->getLibyearsBehind () !== null ? number_format ($ dependency ->getLibyearsBehind (), 2 ) : ""
100
+ isset ($ dependency ->current_version ->released ) ? $ dependency ->current_version ->released ->format ('Y-m-d ' ) : '' ,
101
+ isset ($ dependency ->newest_version ->version_number ) ? $ dependency ->newest_version ->version_number : '' ,
102
+ isset ($ dependency ->newest_version ->released ) ? $ dependency ->newest_version ->released ->format ('Y-m-d ' ) : '' ,
103
+ $ dependency ->getLibyearsBehind () !== null ? number_format ($ dependency ->getLibyearsBehind (), 2 ) : ''
90
104
],
91
105
$ dependencies
92
106
)
@@ -98,25 +112,14 @@ private function showTable(array $dependencies): void
98
112
}
99
113
}
100
114
101
- private function showHelp (): void
115
+ /**
116
+ * @return void
117
+ */
118
+ public function showHelp (): void
102
119
{
103
- $ output = 'libyear: a simple measure of dependency freshness ' . PHP_EOL
104
- . PHP_EOL
105
- . 'Calculates the total number of years behind their respective newest versions for all '
106
- . ' dependencies listed in composer.json. ' . PHP_EOL
107
- . PHP_EOL
108
- . 'Usage: libyear <path> [-q|--quiet] [-u|--update] [-v|--verbose] ' . PHP_EOL
109
- . PHP_EOL
110
- . 'Arguments: ' . PHP_EOL
111
- . '- path (required) the directory containing composer.json and composer.lock files ' . PHP_EOL
112
- . PHP_EOL
113
- . 'Options: ' . PHP_EOL
114
- . '--help (-h) show this message and exit ' . PHP_EOL
115
- . '--quiet (-q) only display outdated dependencies ' . PHP_EOL
116
- . '--update (-u) update composer.json with newest versions ' . PHP_EOL
117
- . '--verbose (-v) display network debug information ' . PHP_EOL
118
- . PHP_EOL ;
119
-
120
- fwrite ($ this ->output , $ output );
120
+ ob_start ();
121
+ $ this ->cli ->writeHelp ();
122
+ $ output = ob_get_clean ();
123
+ fwrite ($ this ->output , $ output );
121
124
}
122
125
}
0 commit comments