22
33namespace Phug \Split \Command ;
44
5+ use Exception ;
56use Phug \Split ;
67use Phug \Split \Command \Options \GitProgram ;
78use Phug \Split \Command \Options \HashPrefix ;
89use SimpleCli \SimpleCli ;
9- use Traversable ;
1010
1111/**
1212 * Copy files from the mono-repository to a sub-package at the current linked revision.
@@ -43,7 +43,9 @@ class Copy extends CommandBase
4343 public $ filters = 'composer.json ' ;
4444
4545 /**
46- * @param Split $cli
46+ * @param Split|SimpleCli $cli
47+ *
48+ * @throws Exception
4749 *
4850 * @return bool
4951 */
@@ -80,111 +82,4 @@ public function run(SimpleCli $cli): bool
8082
8183 return true ;
8284 }
83-
84- protected function calculatePackagesTree (Split $ cli ): bool
85- {
86- $ this ->directory = realpath ($ this ->directory );
87-
88- if (!$ this ->directory ) {
89- return $ cli ->error ('Input directory not found. ' );
90- }
91-
92- $ cli ->chdir ($ this ->directory );
93-
94- if (!file_exists ($ this ->composerFile )) {
95- return $ cli ->error ('Root project directory should contains a ' .$ this ->composerFile .' file. ' );
96- }
97-
98- $ data = json_decode (file_get_contents ($ this ->composerFile ), true );
99- $ vendorDirectory = ($ data ['config ' ] ?? [])['vendor-dir ' ] ?? 'vendor ' ;
100-
101- $ cli ->writeLine ($ data ['name ' ]);
102- $ this ->ast = $ this ->mapDirectories ('. ' , function (string $ path , string $ element ) use ($ vendorDirectory ) {
103- if ($ element === $ vendorDirectory ) {
104- return null ;
105- }
106-
107- return $ this ->scanDirectories ($ path );
108- });
109-
110- return true ;
111- }
112-
113- protected function getPackages (): iterable
114- {
115- if ($ this ->ast instanceof Traversable) {
116- $ this ->ast = iterator_to_array ($ this ->ast );
117- }
118-
119- return $ this ->ast ;
120- }
121-
122- protected function dumpPackagesTree (Split $ cli , iterable $ packages , int $ level = 0 ): bool
123- {
124- $ count = count ($ packages );
125-
126- foreach ($ packages as $ index => $ package ) {
127- $ symbol = $ index === $ count - 1 ? '└ ' : '├ ' ;
128- $ cli ->writeLine (str_repeat (' ' , $ level ).' ' .$ symbol .' ' .$ package ['name ' ], 'light_cyan ' );
129- $ this ->dumpPackagesTree ($ cli , $ package ['children ' ]);
130- }
131-
132- return true ;
133- }
134-
135- protected function mapDirectories (string $ directory , callable $ callback ): iterable
136- {
137- foreach (scandir ($ directory ) as $ element ) {
138- if (substr ($ element , 0 , 1 ) === '. ' ) {
139- continue ;
140- }
141-
142- $ path = $ directory .DIRECTORY_SEPARATOR .$ element ;
143-
144- if (is_dir ($ path )) {
145- $ result = $ callback ($ path , $ element );
146-
147- if ($ result !== null ) {
148- foreach ($ result as $ item ) {
149- yield $ item ;
150- }
151- }
152- }
153- }
154- }
155-
156- protected function getPackage (string $ directory , array $ data ): array
157- {
158- return [
159- 'name ' => $ data ['name ' ],
160- 'children ' => [],
161- ];
162- }
163-
164- protected function scanDirectories (string $ directory ): iterable
165- {
166- $ mainPackage = null ;
167- $ composerPath = $ directory .DIRECTORY_SEPARATOR .$ this ->composerFile ;
168-
169- if (file_exists ($ composerPath )) {
170- $ data = json_decode (file_get_contents ($ composerPath ), true );
171- $ mainPackage = $ this ->getPackage ($ directory , $ data );
172- }
173-
174- foreach ($ this ->mapDirectories ($ directory , function (string $ path ) {
175- return $ this ->scanDirectories ($ path );
176- }) as $ package ) {
177- if ($ mainPackage ) {
178- $ mainPackage ['children ' ][] = $ package ;
179-
180- continue ;
181- }
182-
183- yield $ package ;
184- }
185-
186- if ($ mainPackage ) {
187- yield $ mainPackage ;
188- }
189- }
190- }
85+ }
0 commit comments