2
2
* Copyright © Magento, Inc. All rights reserved.
3
3
* See COPYING.txt for license details.
4
4
*/
5
+
5
6
package com .magento .idea .magento2plugin .actions .generation .generator ;
6
7
7
8
import com .google .gson .JsonElement ;
8
9
import com .google .gson .JsonParser ;
9
10
import com .intellij .openapi .project .Project ;
11
+ import com .intellij .openapi .util .Pair ;
10
12
import com .intellij .openapi .vfs .VirtualFile ;
11
13
import com .intellij .psi .PsiFile ;
12
14
import com .magento .idea .magento2plugin .actions .generation .data .ModuleComposerJsonData ;
16
18
import com .magento .idea .magento2plugin .indexes .ModuleIndex ;
17
19
import com .magento .idea .magento2plugin .magento .files .ComposerJson ;
18
20
import com .magento .idea .magento2plugin .util .CamelCaseToHyphen ;
19
- import com .intellij .openapi .util .Pair ;
20
- import org .jetbrains .annotations .NotNull ;
21
21
import java .io .FileNotFoundException ;
22
22
import java .io .FileReader ;
23
- import java .util .Properties ;
24
23
import java .util .List ;
24
+ import java .util .Properties ;
25
+ import org .jetbrains .annotations .NotNull ;
25
26
26
27
public class ModuleComposerJsonGenerator extends FileGenerator {
27
28
28
29
private final ModuleComposerJsonData moduleComposerJsonData ;
29
30
private final FileFromTemplateGenerator fileFromTemplateGenerator ;
30
31
private final DirectoryGenerator directoryGenerator ;
31
32
private final CamelCaseToHyphen camelCaseToHyphen ;
32
- private final Project project ;
33
33
private final ModuleIndex moduleIndex ;
34
34
35
- public ModuleComposerJsonGenerator (@ NotNull ModuleComposerJsonData moduleComposerJsonData , Project project ) {
35
+ /**
36
+ * Constructor.
37
+ *
38
+ * @param moduleComposerJsonData ModuleComposerJsonData
39
+ * @param project Project
40
+ */
41
+ public ModuleComposerJsonGenerator (
42
+ final @ NotNull ModuleComposerJsonData moduleComposerJsonData ,
43
+ final Project project
44
+ ) {
36
45
super (project );
37
46
this .moduleComposerJsonData = moduleComposerJsonData ;
38
47
this .fileFromTemplateGenerator = FileFromTemplateGenerator .getInstance (project );
39
48
this .directoryGenerator = DirectoryGenerator .getInstance ();
40
49
this .camelCaseToHyphen = CamelCaseToHyphen .getInstance ();
41
- this .project = project ;
42
50
this .moduleIndex = ModuleIndex .getInstance (project );
43
51
}
44
52
45
- public PsiFile generate (String actionName ) {
53
+ @ Override
54
+ public PsiFile generate (final String actionName ) {
46
55
if (moduleComposerJsonData .getCreateModuleDirs ()) {
47
- ModuleDirectoriesData moduleDirectoriesData = directoryGenerator .createOrFindModuleDirectories (moduleComposerJsonData .getPackageName (), moduleComposerJsonData .getModuleName (), moduleComposerJsonData .getBaseDir ());
48
- return fileFromTemplateGenerator .generate (ComposerJson .getInstance (), getAttributes (), moduleDirectoriesData .getModuleDirectory (), actionName );
56
+ final ModuleDirectoriesData moduleDirectoriesData =
57
+ directoryGenerator .createOrFindModuleDirectories (
58
+ moduleComposerJsonData .getPackageName (),
59
+ moduleComposerJsonData .getModuleName (),
60
+ moduleComposerJsonData .getBaseDir ()
61
+ );
62
+ return fileFromTemplateGenerator .generate (
63
+ ComposerJson .getInstance (),
64
+ getAttributes (),
65
+ moduleDirectoriesData .getModuleDirectory (),
66
+ actionName
67
+ );
49
68
}
50
- return fileFromTemplateGenerator .generate (ComposerJson .getInstance (), getAttributes (), moduleComposerJsonData .getBaseDir (), actionName );
69
+ return fileFromTemplateGenerator .generate (
70
+ ComposerJson .getInstance (),
71
+ getAttributes (),
72
+ moduleComposerJsonData .getBaseDir (),
73
+ actionName
74
+ );
51
75
}
52
76
53
- protected void fillAttributes (Properties attributes ) {
77
+ @ Override
78
+ protected void fillAttributes (final Properties attributes ) {
54
79
attributes .setProperty ("PACKAGE" , moduleComposerJsonData .getPackageName ());
55
80
attributes .setProperty ("MODULE_NAME" , moduleComposerJsonData .getModuleName ());
56
81
attributes .setProperty ("MODULE_DESCRIPTION" , moduleComposerJsonData .getModuleDescription ());
57
- attributes .setProperty ("COMPOSER_PACKAGE_NAME" , moduleComposerJsonData .getComposerPackageName ());
82
+ attributes .setProperty (
83
+ "COMPOSER_PACKAGE_NAME" ,
84
+ moduleComposerJsonData .getComposerPackageName ()
85
+ );
58
86
attributes .setProperty ("MODULE_VERSION" , moduleComposerJsonData .getModuleVersion ());
59
- attributes .setProperty ("LICENSE" , this .getLicensesString (moduleComposerJsonData .getModuleLicense ()));
60
- attributes .setProperty ("DEPENDENCIES" , this .getDependenciesString (moduleComposerJsonData .getModuleDependencies ()));
87
+ attributes .setProperty (
88
+ "LICENSE" ,
89
+ this .getLicensesString (moduleComposerJsonData .getModuleLicense ())
90
+ );
91
+ attributes .setProperty (
92
+ "DEPENDENCIES" ,
93
+ this .getDependenciesString (moduleComposerJsonData .getModuleDependencies ())
94
+ );
61
95
}
62
96
63
- protected String getLicensesString (List licensesList ) {
97
+ protected String getLicensesString (final List licensesList ) {
64
98
String license = "[\n " ;
65
- Object [] licenses = licensesList .toArray ();
99
+ final Object [] licenses = licensesList .toArray ();
66
100
67
101
for (int i = 0 ; i < licenses .length ; i ++) {
68
102
license = license .concat ("\" " );
69
103
license = license .concat (licenses [i ].toString ());
70
104
license = license .concat ("\" " );
71
105
72
- if (licenses .length != (i + 1 )) license = license .concat ("," );
106
+ if (licenses .length != i + 1 ) {
107
+ license = license .concat ("," );
108
+ }
73
109
74
110
license = license .concat ("\n " );
75
111
}
@@ -79,11 +115,14 @@ protected String getLicensesString(List licensesList) {
79
115
return license ;
80
116
}
81
117
82
- private String getDependenciesString (List dependenciesList ) {
118
+ private String getDependenciesString (final List dependenciesList ) {
83
119
String result = "" ;
84
- Object [] dependencies = dependenciesList .toArray ();
120
+ final Object [] dependencies = dependenciesList .toArray ();
85
121
result = result .concat (ComposerJson .DEFAULT_DEPENDENCY );
86
- boolean noDependency = dependencies .length == 1 && dependencies [0 ].equals (ComposerJson .NO_DEPENDENCY_LABEL );
122
+ final boolean noDependency =
123
+ dependencies .length == 1 && dependencies [0 ].equals (
124
+ ComposerJson .NO_DEPENDENCY_LABEL
125
+ );
87
126
if (dependencies .length == 0 || noDependency ) {
88
127
result = result .concat ("\n " );
89
128
} else {
@@ -95,14 +134,14 @@ private String getDependenciesString(List dependenciesList) {
95
134
}
96
135
97
136
for (int i = 0 ; i < dependencies .length ; i ++) {
98
- String dependency = dependencies [i ].toString ();
99
- Pair <String , String > dependencyData = getDependencyData (dependency );
137
+ final String dependency = dependencies [i ].toString ();
138
+ final Pair <String , String > dependencyData = getDependencyData (dependency );
100
139
result = result .concat ("\" " );
101
140
result = result .concat (dependencyData .getFirst ());
102
141
result = result .concat ("\" " );
103
142
result = result .concat (": \" " + dependencyData .getSecond () + "\" " );
104
143
105
- if (dependencies .length != ( i + 1 ) ) {
144
+ if (dependencies .length != i + 1 ) {
106
145
result = result .concat ("," );
107
146
}
108
147
@@ -112,20 +151,23 @@ private String getDependenciesString(List dependenciesList) {
112
151
return result ;
113
152
}
114
153
115
- private Pair <String , String > getDependencyData (String dependency ) {
154
+ private Pair <String , String > getDependencyData (
155
+ final String dependency
156
+ ) {
116
157
String version = "*" ;
117
158
String moduleName = camelCaseToHyphen .convert (dependency ).replace ("_-" , "/" );
118
159
try {
119
- VirtualFile virtualFile = moduleIndex .getModuleDirectoryByModuleName (dependency )
160
+ final VirtualFile virtualFile = moduleIndex .getModuleDirectoryByModuleName (dependency )
120
161
.findFile (ComposerJson .FILE_NAME )
121
- .getVirtualFile ();
162
+ .getVirtualFile ();//NOPMD
122
163
if (virtualFile .exists ()) {
123
- JsonElement jsonElement = new JsonParser ().parse (new FileReader (virtualFile .getPath ()));
124
- JsonElement versionJsonElement = jsonElement .getAsJsonObject ().get ("version" );
125
- JsonElement nameJsonElement = jsonElement .getAsJsonObject ().get ("name" );
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" );
126
168
if (versionJsonElement != null ) {
127
169
version = versionJsonElement .getAsString ();
128
- int minorVersionSeparator = version .lastIndexOf ("." );
170
+ final int minorVersionSeparator = version .lastIndexOf ('.' );
129
171
version = new StringBuilder (version )
130
172
.replace (minorVersionSeparator + 1 , version .length (),"*" )
131
173
.toString ();
@@ -134,7 +176,7 @@ private Pair<String, String> getDependencyData(String dependency) {
134
176
moduleName = nameJsonElement .getAsString ();
135
177
}
136
178
}
137
- } catch (FileNotFoundException e ) {
179
+ } catch (FileNotFoundException e ) { //NOPMD
138
180
// It's fine
139
181
}
140
182
0 commit comments