Skip to content

Commit cd74e9a

Browse files
authored
MCLOUD-5665: Improve git installation add support for packages with different structure (magento#708)
1 parent 3acc50c commit cd74e9a

File tree

2 files changed

+64
-25
lines changed

2 files changed

+64
-25
lines changed

src/Command/Dev/UpdateComposer/ComposerGenerator.php

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class ComposerGenerator
1818
{
1919
const REPO_TYPE_SINGLE_PACKAGE = 'single-package';
2020

21+
/**
22+
* Type for packages with modules in the root directory such as Inventory
23+
*/
24+
const REPO_TYPE_FLAT_STRUCTURE = 'flat-structure';
25+
2126
/**
2227
* @var DirectoryList
2328
*/
@@ -128,7 +133,7 @@ private function getBaseComposer(array $repoOptions): array
128133
$preparePackagesScripts = [];
129134

130135
foreach ($repoOptions as $repoName => $gitOption) {
131-
if ($this->isSinglePackage($gitOption)) {
136+
if ($this->isSinglePackage($gitOption) || $this->isFlatStructurePackage($gitOption)) {
132137
continue;
133138
}
134139

@@ -204,51 +209,68 @@ private function getBaseComposer(array $repoOptions): array
204209
* @param array $composer
205210
* @return array
206211
* @codeCoverageIgnore
212+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
207213
*/
208214
private function addModules(array $repoOptions, array $composer): array
209215
{
210-
$add = function ($dir, $version = null) use (&$composer) {
211-
if (!$this->file->isExists($dir . '/composer.json')) {
212-
return 0;
213-
}
214-
215-
$dirComposer = json_decode($this->file->fileGetContents($dir . '/composer.json'), true);
216-
$composer['repositories'][$dirComposer['name']] = [
217-
'type' => 'path',
218-
'url' => ltrim(str_replace($this->directoryList->getMagentoRoot(), '', $dir), '/'),
219-
'options' => [
220-
'symlink' => false,
221-
],
222-
];
223-
$composer['require'][$dirComposer['name']] = $version ?? $dirComposer['version'] ?? '*';
224-
};
225-
226216
foreach ($repoOptions as $repoName => $gitOption) {
227217
$baseRepoFolder = $this->directoryList->getMagentoRoot() . '/' . $repoName;
228218
if ($this->isSinglePackage($gitOption)) {
229-
$add($baseRepoFolder, '*');
219+
$this->addModule($baseRepoFolder, $composer, '*');
220+
continue;
221+
}
222+
223+
if ($this->isFlatStructurePackage($gitOption)) {
224+
foreach (glob($baseRepoFolder . '/*') as $dir) {
225+
$this->addModule($dir, $composer);
226+
}
230227
continue;
231228
}
232229

233230
foreach (glob($baseRepoFolder . '/app/code/Magento/*') as $dir) {
234-
$add($dir);
231+
$this->addModule($dir, $composer);
235232
}
236233
foreach (glob($baseRepoFolder . '/app/design/*/Magento/*/') as $dir) {
237-
$add($dir);
234+
$this->addModule($dir, $composer);
238235
}
239236
foreach (glob($baseRepoFolder . '/app/design/*/Magento/*/') as $dir) {
240-
$add($dir);
237+
$this->addModule($dir, $composer);
241238
}
242239
if ($this->file->isDirectory($baseRepoFolder . '/lib/internal/Magento/Framework/')) {
243240
foreach (glob($baseRepoFolder . '/lib/internal/Magento/Framework/*') as $dir) {
244-
$add($dir);
241+
$this->addModule($dir, $composer);
245242
}
246243
}
247244
}
248245

249246
return $composer;
250247
}
251248

249+
/**
250+
* Add single module to composer json
251+
*
252+
* @param string $dir
253+
* @param array $composer
254+
* @param string|null $version
255+
* @throws \Magento\MagentoCloud\Filesystem\FileSystemException
256+
*/
257+
private function addModule(string $dir, array &$composer, string $version = null): void
258+
{
259+
if (!$this->file->isExists($dir . '/composer.json')) {
260+
return;
261+
}
262+
263+
$dirComposer = json_decode($this->file->fileGetContents($dir . '/composer.json'), true);
264+
$composer['repositories'][$dirComposer['name']] = [
265+
'type' => 'path',
266+
'url' => ltrim(str_replace($this->directoryList->getMagentoRoot(), '', $dir), '/'),
267+
'options' => [
268+
'symlink' => false,
269+
],
270+
];
271+
$composer['require'][$dirComposer['name']] = $version ?? $dirComposer['version'] ?? '*';
272+
}
273+
252274
/**
253275
* @param array $repoOptions
254276
* @return bool
@@ -257,4 +279,15 @@ private function isSinglePackage(array $repoOptions): bool
257279
{
258280
return isset($repoOptions['type']) && $repoOptions['type'] == self::REPO_TYPE_SINGLE_PACKAGE;
259281
}
282+
283+
/**
284+
* Checks that package has option type and it equal to @see ComposerGenerator::REPO_TYPE_FLAT_STRUCTURE
285+
*
286+
* @param array $repoOptions
287+
* @return bool
288+
*/
289+
private function isFlatStructurePackage(array $repoOptions): bool
290+
{
291+
return isset($repoOptions['type']) && $repoOptions['type'] == self::REPO_TYPE_FLAT_STRUCTURE;
292+
}
260293
}

src/Test/Unit/Command/Dev/UpdateComposer/ComposerGeneratorTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class ComposerGeneratorTest extends TestCase
3636
'branch' => '3.0.0',
3737
'type' => 'single-package'
3838
],
39+
'repo4' => [
40+
'repo' => 'path_to_repo4',
41+
'branch' => '1.1.0',
42+
'type' => 'flat-structure'
43+
]
3944
];
4045

4146
/**
@@ -88,9 +93,9 @@ public function testGenerate()
8893
$this->magentoVersionMock->expects($this->once())
8994
->method('getVersion')
9095
->willReturn('2.2');
91-
$this->fileMock->expects($this->exactly(5))
96+
$this->fileMock->expects($this->exactly(6))
9297
->method('isExists')
93-
->willReturn(true);
98+
->willReturn(true, true, true, true, false, true);
9499
$this->fileMock->expects($this->exactly(5))
95100
->method('fileGetContents')
96101
->willReturnOnConsecutiveCalls(
@@ -147,10 +152,11 @@ private function assertInstallFromGitScripts(array $actual)
147152
$this->assertEquals(
148153
[
149154
'php -r"@mkdir(__DIR__ . \'/app/etc\', 0777, true);"',
150-
'rm -rf repo1 repo2 repo3',
155+
'rm -rf repo1 repo2 repo3 repo4',
151156
'git clone -b 1.0.0 --single-branch --depth 1 path_to_repo1 repo1',
152157
'git clone -b 1.0.0 --single-branch --depth 1 path_to_repo2 repo2',
153158
'git clone -b 3.0.0 --single-branch --depth 1 path_to_repo3 repo3',
159+
'git clone -b 1.1.0 --single-branch --depth 1 path_to_repo4 repo4',
154160
],
155161
$actual
156162
);

0 commit comments

Comments
 (0)