3
3
4
4
$ opts = parseOpts ();
5
5
6
- $ prod = diff ('packages ' , $ opts ['from ' ], $ opts ['to ' ]);
7
- $ dev = diff ('packages-dev ' , $ opts ['from ' ], $ opts ['to ' ]);
6
+ $ prod = diff ('packages ' , $ opts ['from ' ], $ opts ['to ' ], $ opts [ ' path ' ] );
7
+ $ dev = diff ('packages-dev ' , $ opts ['from ' ], $ opts ['to ' ], $ opts [ ' path ' ] );
8
8
9
9
if ($ opts ['json ' ]) {
10
10
$ opts = ($ opt ['pretty ' ]) ? JSON_PRETTY_PRINT : 0 ;
@@ -15,17 +15,17 @@ if ($opts['json']) {
15
15
print tableize ('Production Changes ' , $ prod );
16
16
print tableize ('Dev Changes ' , $ dev );
17
17
18
- function diff ($ key , $ from , $ to ) {
18
+ function diff ($ key , $ from , $ to, $ base_path ) {
19
19
20
20
$ pkgs = array ();
21
21
22
- $ data = load ($ from );
22
+ $ data = load ($ from, $ base_path );
23
23
24
24
foreach ($ data ->$ key as $ pkg ) {
25
25
$ pkgs [$ pkg ->name ] = array (version ($ pkg ), 'REMOVED ' );
26
26
}
27
27
28
- $ data = load ($ to );
28
+ $ data = load ($ to, $ base_path );
29
29
30
30
foreach ($ data ->$ key as $ pkg ) {
31
31
if (! array_key_exists ($ pkg ->name , $ pkgs )) {
@@ -96,19 +96,25 @@ function tabelizeLine($data, $widths) {
96
96
return '| ' . implode (' | ' , $ fields ) . ' | ' ;
97
97
}
98
98
99
- function load ($ fileish ) {
99
+ function load ($ fileish, $ base_path = '' ) {
100
100
$ orig = $ fileish ;
101
101
102
+ if (empty ($ base_path )) {
103
+ $ base_path = '. ' . DIRECTORY_SEPARATOR ;
104
+ } else {
105
+ $ base_path = rtrim ($ base_path , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR ;
106
+ }
107
+
102
108
if (empty ($ fileish )) {
103
- $ fileish = 'composer.lock ' ;
109
+ $ fileish = $ base_path . 'composer.lock ' ;
104
110
}
105
111
106
112
if (file_exists ($ fileish )) {
107
113
return mustDecodeJson (file_get_contents ($ fileish ), $ fileish );
108
114
}
109
115
110
- if (strpos ($ fileish , ': ' ) === false ) {
111
- $ fileish .= ':composer.lock ' ;
116
+ if (strpos ($ orig , ': ' ) === false ) {
117
+ $ fileish .= ': ' . $ base_path . ' composer.lock ' ;
112
118
}
113
119
114
120
$ lines = '' ;
@@ -135,13 +141,21 @@ function mustDecodeJson($json, $context) {
135
141
}
136
142
137
143
function parseOpts () {
138
- $ given = getopt ('h ' , array ('from: ' , 'to: ' , 'json ' , 'pretty ' , 'help ' ));
144
+ $ given = getopt ('hp: ' , array ('path: ' , 'from: ' , 'to: ' , 'json ' , 'pretty ' , 'help ' ));
145
+
146
+ foreach (array ('help ' => 'h ' , 'path ' => 'p ' ) as $ long => $ short ) {
147
+ if (array_key_exists ($ short , $ given )) {
148
+ $ given [$ long ] = $ given [$ short ];
149
+ unset($ given [$ short ]);
150
+ }
151
+ }
139
152
140
- if (array_key_exists ('h ' , $ given ) || array_key_exists ( ' help ' , $ given )) {
153
+ if (array_key_exists ('help ' , $ given )) {
141
154
usage ();
142
155
}
143
156
144
157
return array (
158
+ 'path ' => array_key_exists ('path ' , $ given ) ? $ given ['path ' ] : '' ,
145
159
'from ' => array_key_exists ('from ' , $ given ) ? $ given ['from ' ] : 'HEAD ' ,
146
160
'to ' => array_key_exists ('to ' , $ given ) ? $ given ['to ' ] : '' ,
147
161
'json ' => array_key_exists ('json ' , $ given ),
@@ -155,6 +169,8 @@ Usage: composer-lock-diff [options]
155
169
156
170
Options:
157
171
-h --help Print this message
172
+ --path, -p Base to with which to prefix paths. Default "./"
173
+ E.g. `-p app` would look for HEAD:app/composer.lock and app/composer.lock
158
174
--from The file, git ref, or git ref with filename to compare from (HEAD:composer.lock)
159
175
--to The file, git ref, or git ref with filename to compare to (composer.lock)
160
176
--json Format output as JSON
0 commit comments