Skip to content

Commit f45b3a9

Browse files
author
Vitaliy
authored
Merge pull request #278 from coderimus/272-fix-error-create-module
272: fix composer.json generation with module which doesn't have composer.json
2 parents 0f34ec7 + 7814a3b commit f45b3a9

File tree

9 files changed

+194
-55
lines changed

9 files changed

+194
-55
lines changed

src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleComposerJsonGenerator.java

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,18 @@ private String getDependenciesString(final List dependenciesList) {
136136
for (int i = 0; i < dependencies.length; i++) {
137137
final String dependency = dependencies[i].toString();
138138
final Pair<String, String> dependencyData = getDependencyData(dependency);
139-
result = result.concat("\"");
140-
result = result.concat(dependencyData.getFirst());
141-
result = result.concat("\"");
142-
result = result.concat(": \"" + dependencyData.getSecond() + "\"");
139+
if (!dependencyData.getFirst().isEmpty()) {
140+
result = result.concat("\"");
141+
result = result.concat(dependencyData.getFirst());
142+
result = result.concat("\"");
143+
result = result.concat(": \"" + dependencyData.getSecond() + "\"");
144+
}
143145

144-
if (dependencies.length != i + 1) {
146+
if (!dependencyData.getFirst().isEmpty() && dependencies.length != i + 1) {
145147
result = result.concat(",");
148+
result = result.concat("\n");
146149
}
147150

148-
result = result.concat("\n");
149151
}
150152

151153
return result;
@@ -155,26 +157,36 @@ private Pair<String, String> getDependencyData(
155157
final String dependency
156158
) {
157159
String version = "*";
158-
String moduleName = camelCaseToHyphen.convert(dependency).replace("_-", "/");
160+
String moduleName = camelCaseToHyphen.convert(dependency).replace(
161+
"_-", "/"
162+
);
159163
try {
160-
final VirtualFile virtualFile = moduleIndex.getModuleDirectoryByModuleName(dependency)
161-
.findFile(ComposerJson.FILE_NAME)
162-
.getVirtualFile();//NOPMD
163-
if (virtualFile.exists()) {
164-
final JsonElement jsonElement =
165-
new JsonParser().parse(new FileReader(virtualFile.getPath()));//NOPMD
166-
final JsonElement versionJsonElement = jsonElement.getAsJsonObject().get("version");
167-
final JsonElement nameJsonElement = jsonElement.getAsJsonObject().get("name");
168-
if (versionJsonElement != null) {
169-
version = versionJsonElement.getAsString();
170-
final int minorVersionSeparator = version.lastIndexOf('.');
171-
version = new StringBuilder(version)
172-
.replace(minorVersionSeparator + 1, version.length(),"*")
173-
.toString();
174-
}
175-
if (nameJsonElement != null) {
176-
moduleName = nameJsonElement.getAsString();
164+
final PsiFile virtualFile = moduleIndex.getModuleDirectoryByModuleName(dependency)
165+
.findFile(ComposerJson.FILE_NAME);
166+
167+
if (virtualFile != null) { //NOPMD
168+
final VirtualFile composerJsonFile = virtualFile.getVirtualFile();
169+
if (composerJsonFile.exists()) {
170+
final JsonElement jsonElement =
171+
new JsonParser().parse(
172+
new FileReader(composerJsonFile.getPath())//NOPMD
173+
);
174+
final JsonElement versionJsonElement =
175+
jsonElement.getAsJsonObject().get("version");
176+
final JsonElement nameJsonElement = jsonElement.getAsJsonObject().get("name");
177+
if (versionJsonElement != null) {
178+
version = versionJsonElement.getAsString();
179+
final int minorVersionSeparator = version.lastIndexOf('.');
180+
version = new StringBuilder(version)
181+
.replace(minorVersionSeparator + 1, version.length(),"*")
182+
.toString();
183+
}
184+
if (nameJsonElement != null) {
185+
moduleName = nameJsonElement.getAsString();
186+
}
177187
}
188+
} else {
189+
return Pair.create("", "");
178190
}
179191
} catch (FileNotFoundException e) { //NOPMD
180192
// It's fine

testData/actions/generation/generator/ModuleComposerJsonGenerator/generateFileInRoot/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"registration.php"
1818
],
1919
"psr-4": {
20-
"Test\\Module\\": ""
20+
"TestWithDependencies\\Module\\": ""
2121
}
2222
}
2323
}

testData/actions/generation/generator/ModuleComposerJsonGenerator/generateModuleFile/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"registration.php"
1818
],
1919
"psr-4": {
20-
"Test\\Module\\": ""
20+
"TestWithDependencies\\Module\\": ""
2121
}
2222
}
2323
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "test/module",
3+
"version": "1.0.0-dev",
4+
"description": "test-description",
5+
"type": "magento2-module",
6+
"require": {
7+
"magento/framework": "*"
8+
},
9+
"license": [
10+
"Test License 1",
11+
"Test License 2"
12+
],
13+
"autoload": {
14+
"files": [
15+
"registration.php"
16+
],
17+
"psr-4": {
18+
"TestWithoutDependencies\\Module\\": ""
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "test/module",
3+
"version": "1.0.0-dev",
4+
"description": "test-description",
5+
"type": "magento2-module",
6+
"require": {
7+
"magento/framework": "*"
8+
},
9+
"license": [
10+
"Test License 1",
11+
"Test License 2"
12+
],
13+
"autoload": {
14+
"files": [
15+
"registration.php"
16+
],
17+
"psr-4": {
18+
"Test\\Module\\": ""
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "test/module",
3+
"version": "1.0.0-dev",
4+
"description": "test-description",
5+
"type": "magento2-module",
6+
"require": {
7+
"magento/framework": "*"
8+
},
9+
"license": [
10+
"Test License 1",
11+
"Test License 2"
12+
],
13+
"autoload": {
14+
"files": [
15+
"registration.php"
16+
],
17+
"psr-4": {
18+
"Test\\Module\\": ""
19+
}
20+
}
21+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" ?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3+
<module name="Foo_BarWithoutComposer"/>
4+
</config>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
\Magento\Framework\Component\ComponentRegistrar::register(
3+
\Magento\Framework\Component\ComponentRegistrar::MODULE,
4+
'Foo_BarWithOutComposer',
5+
__DIR__
6+
);

tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleComposerJsonGeneratorTest.java

Lines changed: 83 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.actions.generation.generator;
67

78
import com.intellij.openapi.project.Project;
@@ -15,50 +16,103 @@
1516

1617
public class ModuleComposerJsonGeneratorTest extends BaseGeneratorTestCase {
1718

19+
/**
20+
* Test for the module composer.json generation with dependencies.
21+
*/
1822
public void testGenerateModuleFile() {
19-
String filePath = this.getFixturePath(ComposerJson.FILE_NAME);
20-
PsiFile expectedFile = myFixture.configureByFile(filePath);
21-
PsiDirectory projectDir = getProjectDirectory();
23+
final String filePath = this.getFixturePath(ComposerJson.FILE_NAME);
24+
final PsiFile expectedFile = myFixture.configureByFile(filePath);
25+
final PsiDirectory projectDir = getProjectDirectory();
2226

23-
PsiFile composerJson = generateComposerJson(true, projectDir);
27+
final String expectedDirectory =
28+
projectDir.getVirtualFile().getPath() + "/TestWithDependencies/Module";
29+
final PsiFile composerJson = generateComposerJson(
30+
true,
31+
projectDir,
32+
true,
33+
"TestWithDependencies");
2434

2535
assertGeneratedFileIsCorrect(
26-
expectedFile,
27-
projectDir.getVirtualFile().getPath() + "/Test/Module",
28-
composerJson
36+
expectedFile,
37+
expectedDirectory,
38+
composerJson
2939
);
3040
}
3141

42+
/**
43+
* Test for generation the composer.json with dependencies in the root directory.
44+
*/
3245
public void testGenerateFileInRoot() {
33-
String filePath = this.getFixturePath(ComposerJson.FILE_NAME);
34-
PsiFile expectedFile = myFixture.configureByFile(filePath);
35-
PsiDirectory projectDir = getProjectDirectory();
46+
final String filePath = this.getFixturePath(ComposerJson.FILE_NAME);
47+
final PsiFile expectedFile = myFixture.configureByFile(filePath);
48+
final PsiDirectory projectDir = getProjectDirectory();
49+
50+
final String composerJsonDirPath = projectDir.getVirtualFile().getPath();
51+
final PsiFile composerJson = generateComposerJson(
52+
false,
53+
projectDir,
54+
true,
55+
"TestWithDependencies");
3656

37-
PsiFile composerJson = generateComposerJson(false, projectDir);
57+
assertGeneratedFileIsCorrect(expectedFile, composerJsonDirPath, composerJson);
58+
}
59+
60+
/**
61+
* Test case for the composer.json generation without dependencies.
62+
*/
63+
public void testGenerateModuleFileWithoutDependencies() {
64+
final String filePath = this.getFixturePath(ComposerJson.FILE_NAME);
65+
final PsiFile expectedFile = myFixture.configureByFile(filePath);
66+
final PsiDirectory projectDir = getProjectDirectory();
67+
final String expectedDirectory = projectDir.getVirtualFile().getPath()
68+
+ "/TestWithoutDependencies/Module";
69+
final PsiFile composerJson = generateComposerJson(
70+
true,
71+
projectDir,
72+
false,
73+
"TestWithoutDependencies");
3874

3975
assertGeneratedFileIsCorrect(
40-
expectedFile,
41-
projectDir.getVirtualFile().getPath(),
42-
composerJson
76+
expectedFile,
77+
expectedDirectory,
78+
composerJson
4379
);
4480
}
4581

46-
private PsiFile generateComposerJson(boolean createModuleDirectories, PsiDirectory projectDir) {
47-
Project project = myFixture.getProject();
48-
List<String> dependencies = new ArrayList<>(Arrays.asList("Foo_Bar", "Magento_Backend"));
49-
List<String> licenses = new ArrayList<>(Arrays.asList("Test License 1", "Test License 2"));
50-
ModuleComposerJsonData composerJsonData = new ModuleComposerJsonData(
51-
"Test",
52-
"Module",
53-
projectDir,
54-
"test-description",
55-
"test/module",
56-
"1.0.0-dev",
57-
licenses,
58-
dependencies,
59-
createModuleDirectories
82+
/**
83+
* Generate composer.json file for tests.
84+
*
85+
* @param createModuleDirectories create module directory flag
86+
* @param projectDir project directory
87+
* @param withDependencies generate composer.json with dependencies or not
88+
* @param packageName the package name of the test module
89+
* @return PsiFile
90+
*/
91+
private PsiFile generateComposerJson(
92+
final boolean createModuleDirectories,
93+
final PsiDirectory projectDir,
94+
final boolean withDependencies,
95+
final String packageName) {
96+
final Project project = myFixture.getProject();
97+
final List<String> dependencies = withDependencies
98+
? new ArrayList<>(Arrays.asList("Foo_Bar", "Magento_Backend"))
99+
: new ArrayList<>(Arrays.asList("Foo_BarWithOutComposer"));
100+
final List<String> licenses = new ArrayList<>(
101+
Arrays.asList("Test License 1", "Test License 2")
102+
);
103+
final ModuleComposerJsonData composerJsonData = new ModuleComposerJsonData(
104+
packageName,
105+
"Module",
106+
projectDir,
107+
"test-description",
108+
"test/module",
109+
"1.0.0-dev",
110+
licenses,
111+
dependencies,
112+
createModuleDirectories
60113
);
61-
ModuleComposerJsonGenerator composerJsonGenerator = new ModuleComposerJsonGenerator(composerJsonData, project);
114+
final ModuleComposerJsonGenerator composerJsonGenerator =
115+
new ModuleComposerJsonGenerator(composerJsonData, project);
62116
return composerJsonGenerator.generate("test");
63117
}
64118
}

0 commit comments

Comments
 (0)