1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.sap.prd.mobile.ios.mios;
21
22 import java.io.File;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.Set;
26 import java.util.logging.LogManager;
27
28 import org.apache.maven.plugin.AbstractMojo;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.project.MavenProject;
31
32
33
34
35
36 public abstract class AbstractXCodeMojo extends AbstractMojo
37 {
38
39
40
41
42
43
44
45
46 private static XCodePluginLogger logger = null;
47
48 static {
49
50 if(null == LogManager.getLogManager().getLogger(XCodePluginLogger.getLoggerName())) {
51
52 logger = new XCodePluginLogger();
53 LogManager.getLogManager().addLogger(logger);
54 logger.finest(String.format("XCode plugin logger has been created: %s", logger.getName()));
55 }
56 }
57
58
59
60
61
62
63
64
65
66
67 private File checkoutDirectory;
68
69
70
71
72
73
74 private File xcodeCompileDirectory;
75
76
77
78
79
80
81 protected MavenProject project;
82
83
84
85
86
87
88
89
90
91
92 private Set<String> configurations;
93
94
95
96
97
98
99 protected String packaging;
100
101
102
103
104
105
106
107
108
109
110 private Set<String> sdks;
111
112
113
114
115
116
117
118
119
120
121 private String defaultAppConfigurations;
122
123
124
125
126
127
128
129
130
131
132 private String defaultLibConfigurations;
133
134
135
136
137
138
139
140
141
142 private String defaultAppSdks;
143
144
145
146
147
148
149
150
151
152 private String defaultLibSdks;
153
154 protected Set<String> getSDKs()
155 {
156 if (sdks == null || sdks.isEmpty()) {
157
158 try {
159
160 PackagingType packagingType = PackagingType.getByMavenType(packaging);
161
162 if (packagingType == PackagingType.APP) {
163
164 getLog().info(
165 "No SDKs in POM set. Using default configurations for applications: " + defaultAppSdks);
166 return commaSeparatedStringToSet(defaultAppSdks);
167 }
168 else if (packagingType == PackagingType.LIB || packagingType == PackagingType.FRAMEWORK) {
169 getLog().info(
170 "No SDKs in POM set. Using default configurations for libraries: " + defaultLibSdks);
171 return commaSeparatedStringToSet(defaultLibSdks);
172 }
173 }
174 catch (PackagingType.UnknownPackagingTypeException e) {
175 getLog().info("Unknown PackagingType found.", e);
176 return Collections.emptySet();
177 }
178 }
179 getLog().info("SDKs have been explicitly set in POM: " + sdks);
180 return sdks;
181 }
182
183 protected Set<String> getConfigurations()
184 {
185 if (configurations == null || configurations.isEmpty()) {
186
187 try {
188 PackagingType packagingType = PackagingType.getByMavenType(packaging);
189
190 if (packagingType == PackagingType.APP) {
191 getLog().info(
192 "No configurations in POM set. Using default configurations for applications: "
193 + defaultAppConfigurations);
194 return commaSeparatedStringToSet(defaultAppConfigurations);
195 }
196 else if (packagingType == PackagingType.LIB || packagingType == PackagingType.FRAMEWORK) {
197 getLog()
198 .info(
199 "No configurations in POM set. Using default configurations for libraries: "
200 + defaultLibConfigurations);
201 return commaSeparatedStringToSet(defaultLibConfigurations);
202 }
203 }
204 catch (PackagingType.UnknownPackagingTypeException e) {
205 getLog().info("Unknown PackagingType found.", e);
206 return Collections.emptySet();
207 }
208 }
209 getLog().info("Configurations have been explicitly set in POM: " + configurations);
210 return configurations;
211 }
212
213 private Set<String> commaSeparatedStringToSet(String commaSeparetedValues)
214 {
215 Set<String> values = new HashSet<String>();
216 String[] valueArray = commaSeparetedValues.split(",");
217 for (String value : valueArray) {
218 value = value.trim();
219 if (!value.isEmpty()) {
220 values.add(value);
221 }
222 }
223 return Collections.unmodifiableSet(values);
224 }
225
226 protected File getCheckoutDirectory()
227 {
228 return checkoutDirectory;
229 }
230
231
232
233
234 protected File getXCodeCompileDirectory()
235 {
236 return xcodeCompileDirectory;
237 }
238
239
240
241
242
243 protected File getXCodeSourceDirectory()
244 {
245 return new File(project.getBuild().getSourceDirectory());
246 }
247
248 protected String getFixedProductName(final String productName)
249 {
250 return productName.trim().replaceAll(" ", "");
251 }
252
253 protected File getXCodeProjectFile()
254 {
255 return new File(getXCodeCompileDirectory(), project.getArtifactId() + ".xcodeproj/project.pbxproj");
256 }
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 protected File zipSubfolder(File rootDir, String zipSubFolder, String zipFileName, String archiveFolder)
275 throws MojoExecutionException
276 {
277 int resultCode = 0;
278
279 try {
280
281 File scriptDirectory = new File(project.getBuild().getDirectory(), "scripts").getCanonicalFile();
282 scriptDirectory.deleteOnExit();
283
284 if (archiveFolder != null)
285 {
286 resultCode = ScriptRunner.copyAndExecuteScript(System.out, "/com/sap/prd/mobile/ios/mios/zip-subfolder.sh",
287 scriptDirectory, rootDir.getCanonicalPath(), zipSubFolder, zipFileName, archiveFolder);
288 }
289 else
290 {
291 resultCode = ScriptRunner.copyAndExecuteScript(System.out, "/com/sap/prd/mobile/ios/mios/zip-subfolder.sh",
292 scriptDirectory, rootDir.getCanonicalPath(), zipSubFolder, zipFileName);
293 }
294 }
295 catch (Exception ex) {
296 throw new MojoExecutionException("Cannot create zip file " + zipFileName + ". Check log for details.", ex);
297 }
298 if (resultCode != 0) {
299 throw new MojoExecutionException("Cannot create zip file " + zipFileName + ". Check log for details.");
300 }
301 getLog().info("Zip file '" + zipFileName + "' created.");
302 return new File(rootDir, zipFileName);
303 }
304 }