Skip to content

Commit 981047c

Browse files
authored
MCLOUD-6376: Fixed: single-module repo files are copied to project root (magento#754)
1 parent b0a8802 commit 981047c

File tree

4 files changed

+75
-36
lines changed

4 files changed

+75
-36
lines changed

src/Command/Dev/UpdateComposer/ComposerGenerator.php

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
*/
1818
class ComposerGenerator
1919
{
20+
/**
21+
* Types of supported Magento component packages
22+
*/
23+
private const COMPONENT_PACKAGE_TYPES = [
24+
'magento2-module',
25+
'magento2-theme',
26+
'magento2-language',
27+
'magento2-library'
28+
];
29+
2030
/**
2131
* @var DirectoryList
2232
*/
@@ -81,10 +91,13 @@ public function generate(array $repoOptions): array
8191
$baseRepoFolder = $this->directoryList->getMagentoRoot() . '/' . $repoDir;
8292

8393
$dirComposerJson = $baseRepoFolder . '/composer.json';
94+
$isModuleRoot = false;
8495
if ($this->file->isExists($dirComposerJson)) {
8596
$dirPackageInfo = json_decode($this->file->fileGetContents($dirComposerJson), true);
86-
if (isset($dirPackageInfo['type']) && $dirPackageInfo['type'] == 'project') {
97+
if ($this->isProjectPackage($dirPackageInfo)) {
8798
$composer['require'] = array_merge($composer['require'], $dirPackageInfo['require']);
99+
} elseif ($this->isComponentPackage($dirPackageInfo)) {
100+
$isModuleRoot = true;
88101
}
89102
}
90103

@@ -99,12 +112,16 @@ public function generate(array $repoOptions): array
99112
];
100113
$composer['require'][$packageName] = '*@dev';
101114
}
102-
$excludeRepoStr = empty($repoPackages) ? '' : "--exclude='" . join("' --exclude='", $repoPackages) . "' ";
103-
$preparePackagesScripts[] = sprintf(
104-
"rsync -azhm --stats $excludeRepoStr--exclude='dev/tests' --exclude='.git' " .
105-
"--exclude='composer.json' --exclude='composer.lock' ./%s/ ./",
106-
$repoDir
107-
);
115+
if (!$isModuleRoot) {
116+
$excludeRepoStr = empty($repoPackages)
117+
? ''
118+
: "--exclude='" . join("' --exclude='", $repoPackages) . "' ";
119+
$preparePackagesScripts[] = sprintf(
120+
"rsync -azhm --stats $excludeRepoStr--exclude='dev/tests' --exclude='.git' " .
121+
"--exclude='composer.json' --exclude='composer.lock' ./%s/ ./",
122+
$repoDir
123+
);
124+
}
108125
}
109126
$composer['scripts']['prepare-packages'] = $preparePackagesScripts;
110127
$composer['scripts']['post-install-cmd'] = ['@prepare-packages'];
@@ -208,7 +225,6 @@ private function getBaseComposer(array $repoOptions): array
208225
private function findPackages(string $path)
209226
{
210227
$path = rtrim($path, '\\/');
211-
$packageTypes = ['magento2-module', 'magento2-theme', 'magento2-language', 'magento2-library'];
212228
$pathLength = strlen($path . '/');
213229

214230
$dirIterator = $this->file->getRecursiveFileIterator(
@@ -219,11 +235,33 @@ private function findPackages(string $path)
219235
$packages = [];
220236
foreach ($dirIterator as $currentFileInfo) {
221237
$packageInfo = json_decode($this->file->fileGetContents($currentFileInfo->getPathName()), true);
222-
if (isset($packageInfo['type']) && in_array($packageInfo['type'], $packageTypes)) {
238+
if ($this->isComponentPackage($packageInfo)) {
223239
$packages[$packageInfo['name']] = substr($currentFileInfo->getPath(), $pathLength);
224240
}
225241
}
226242

227243
return $packages;
228244
}
245+
246+
/**
247+
* Check if provided package info belongs to a Magento component package
248+
*
249+
* @param array $packageInfo
250+
* @return bool
251+
*/
252+
private function isComponentPackage(array $packageInfo): bool
253+
{
254+
return isset($packageInfo['type']) && in_array($packageInfo['type'], self::COMPONENT_PACKAGE_TYPES);
255+
}
256+
257+
/**
258+
* Check if provided package info belongs to a Magento project package
259+
*
260+
* @param array $packageInfo
261+
* @return bool
262+
*/
263+
private function isProjectPackage(array $packageInfo): bool
264+
{
265+
return isset($packageInfo['type']) && $packageInfo['type'] == 'project';
266+
}
229267
}

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

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,19 @@ class ComposerGeneratorTest extends TestCase
3434
],
3535
'repo3' => [
3636
'repo' => 'path_to_repo3',
37-
'ref' => 'test',
37+
'ref' => 'ref3',
3838
'branch' => '3.0.0',
3939
],
4040
'repo4' => [
4141
'repo' => 'path_to_repo4',
42-
'ref' => 'test',
43-
]
42+
'ref' => 'ref4',
43+
'branch' => '4.0.0',
44+
],
45+
'repo5' => [
46+
'repo' => 'path_to_repo5',
47+
'ref' => 'ref5',
48+
'branch' => '5.0.0',
49+
],
4450
];
4551

4652
/**
@@ -82,7 +88,9 @@ protected function setUp()
8288

8389
public function testGetInstallFromGitScripts()
8490
{
85-
$this->assertInstallFromGitScripts($this->composerGenerator->getInstallFromGitScripts($this->repoOptions));
91+
$expected = include(__DIR__ . '/_files/expected_composer.php');
92+
$actual = $this->composerGenerator->getInstallFromGitScripts($this->repoOptions);
93+
$this->assertEquals($expected['scripts']['install-from-git'], $actual);
8694
}
8795

8896
public function testGenerate(): void
@@ -95,24 +103,4 @@ public function testGenerate(): void
95103
$this->assertEquals($value, $composer[$key]);
96104
}
97105
}
98-
99-
/**
100-
* @param array $actual
101-
*
102-
* @return void
103-
*/
104-
private function assertInstallFromGitScripts(array $actual): void
105-
{
106-
$this->assertEquals(
107-
[
108-
'php -r"@mkdir(__DIR__ . \'/app/etc\', 0777, true);"',
109-
'rm -rf repo1 repo2 repo3 repo4',
110-
'git clone -b 1.0.0 --single-branch --depth 1 path_to_repo1 repo1',
111-
'git clone -b 2.0.0 --single-branch --depth 1 path_to_repo2 repo2',
112-
'git clone path_to_repo3 "repo3" && git --git-dir="repo3/.git" --work-tree="repo3" checkout test',
113-
'git clone path_to_repo4 "repo4" && git --git-dir="repo4/.git" --work-tree="repo4" checkout test',
114-
],
115-
$actual
116-
);
117-
}
118106
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "vendor/module-module5",
3+
"type": "magento2-module"
4+
}

src/Test/Unit/Command/Dev/UpdateComposer/_files/expected_composer.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,30 @@
3333
'symlink' => false,
3434
],
3535
],
36+
'vendor/module-module5' => [
37+
'type' => 'path',
38+
'url' => 'repo5/',
39+
'options' => [
40+
'symlink' => false,
41+
],
42+
],
3643
],
3744
'require' => [
3845
'package' => '*',
3946
'vendor/library1' => '*@dev',
4047
'vendor/theme1' => '*@dev',
4148
'vendor/module-module1' => '*@dev',
49+
'vendor/module-module5' => '*@dev',
4250
],
4351
'scripts' =>[
4452
'install-from-git' => [
4553
'php -r"@mkdir(__DIR__ . \'/app/etc\', 0777, true);"',
46-
'rm -rf repo1 repo2 repo3 repo4',
54+
'rm -rf repo1 repo2 repo3 repo4 repo5',
4755
'git clone -b 1.0.0 --single-branch --depth 1 path_to_repo1 repo1',
4856
'git clone -b 2.0.0 --single-branch --depth 1 path_to_repo2 repo2',
49-
'git clone path_to_repo3 "repo3" && git --git-dir="repo3/.git" --work-tree="repo3" checkout test',
50-
'git clone path_to_repo4 "repo4" && git --git-dir="repo4/.git" --work-tree="repo4" checkout test',
57+
'git clone path_to_repo3 "repo3" && git --git-dir="repo3/.git" --work-tree="repo3" checkout ref3',
58+
'git clone path_to_repo4 "repo4" && git --git-dir="repo4/.git" --work-tree="repo4" checkout ref4',
59+
'git clone path_to_repo5 "repo5" && git --git-dir="repo5/.git" --work-tree="repo5" checkout ref5',
5160
],
5261
'pre-install-cmd' => [
5362
'@install-from-git',

0 commit comments

Comments
 (0)