forked from rayh/xcode-hudson-plugin
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathXcodeProjectParser.java
More file actions
344 lines (327 loc) · 14.1 KB
/
XcodeProjectParser.java
File metadata and controls
344 lines (327 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package au.com.rayh;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.text.ParseException;
import hudson.FilePath;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
//import javax.xml.parsers.ParserException;
import javax.xml.parsers.ParserConfigurationException;
import java.lang.InterruptedException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSArray;
import com.dd.plist.NSObject;
import com.dd.plist.PropertyListFormatException;
import com.dd.plist.PropertyListParser;
/**
* Analyze the Xcode project file and obtain the information necessary for building the application. (Only combinations of UUID and BundleID used for CodeSign now)
* @author Kazuhide Takahashi
*/
public class XcodeProjectParser {
/**
* Retrieve all Xcode scheme file from project directory.
* @param projectLocation Xcode project file location (directory path)
* @return the list of schema files found in the project directory and the result (ProjectScheme) of analysis of the contents as a HashMap. If analysis fails, it is empty
*/
public static HashMap<String, ProjectScheme> listXcodeSchemes(FilePath projectLocation) {
String currentUser = System.getProperty("user.name") ;
HashMap<String, ProjectScheme> schemeList = new HashMap<String, ProjectScheme>();
List<FilePath> schemeFilesDirList = new ArrayList<FilePath>();
try {
if ( projectLocation.child("xcuserdata/" + currentUser + ".xcuserdatad/xcschemes").exists() ) {
schemeFilesDirList.add(projectLocation.child("xcuserdata/" + currentUser + ".xcuserdatad/xcschemes"));
}
if ( projectLocation.child("xcshareddata/xcschemes").exists() ) {
schemeFilesDirList.add(projectLocation.child("xcshareddata/xcschemes"));
}
}
catch ( IOException ex ) {
ex.printStackTrace();
schemeList = null;
}
catch ( InterruptedException ex ) {
ex.printStackTrace();
schemeList = null;
}
for ( FilePath schemeFilesDir : schemeFilesDirList ) {
try {
List<FilePath> files = schemeFilesDir.list(new XcodeSchemeFileFilter());
for ( FilePath file : files ) {
ProjectScheme scheme = parseXcodeScheme(file);
String schemeName = file.getBaseName().replaceAll(".xcscheme$", "");
schemeList.put(schemeName, scheme);
}
}
catch ( IOException ex ) {
ex.printStackTrace();
schemeList = null;
}
catch ( InterruptedException ex ) {
ex.printStackTrace();
schemeList = null;
}
}
return schemeList;
}
/**
* @param schemeFile Xcode schieme file location
* @return analysis result of Xcode projectscheme file. If analysis fails, it is null
*/
public static ProjectScheme parseXcodeScheme(FilePath schemeFile) {
ProjectScheme projectScheme = new ProjectScheme();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
String FEATURE = null;
FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
factory.setFeature(FEATURE, true);
FEATURE = "http://xml.org/sax/features/external-general-entities";
factory.setFeature(FEATURE, false);
FEATURE = "http://xml.org/sax/features/external-parameter-entities";
factory.setFeature(FEATURE, false);
FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
factory.setFeature(FEATURE, false);
factory.setXIncludeAware(false);
factory.setExpandEntityReferences(false);
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document document = documentBuilder.parse(schemeFile.read());
Element root = document.getDocumentElement();
if ( root.getNodeName().equals("Scheme") ) {
NodeList schemeNodes = root.getChildNodes();
for ( int i = 0; i < schemeNodes.getLength(); i++ ) {
Node node = schemeNodes.item(i);
if ( node.getNodeType() == Node.ELEMENT_NODE ) {
Element element = (Element)node;
if ( element.getNodeName().equals("BuildAction") ) {
//projectScheme.parallelizeBuildables = element.getAttribute("parallelizeBuildables");
//projectScheme.buildImplicitDependencies = element.getAttribute("buildImplicitDependencies");
NodeList buildActionNodes = element.getChildNodes();
for ( int j = 0; j < buildActionNodes.getLength(); j++ ) {
node = buildActionNodes.item(j);
if ( node.getNodeType() == Node.ELEMENT_NODE ) {
element = (Element)node;
if ( element.getNodeName().equals("BuildActionEntries") ) {
NodeList buildActionEntriesNodes = element.getChildNodes();
for ( int k = 0; k < buildActionEntriesNodes.getLength(); k++ ) {
node = buildActionEntriesNodes.item(k);
if ( node.getNodeType() == Node.ELEMENT_NODE ) {
element = (Element)node;
if ( element.getNodeName().equals("BuildActionEntry") ) {
//projectScheme.buildForTesting = element.getAttribute("buildForTesting");
//projectScheme.buildForRunning = element.getAttribute("buildForRunning");
//projectScheme.buildForProfiling = element.getAttribute("buildForProfiling");
//projectScheme.buildForArchiving = element.getAttribute("buildForArchiving");
//projectScheme.buildForAnalyzing = element.getAttribute("buildForAnalyzing");
NodeList buildActionEntryNodes = element.getChildNodes();
for ( int l = 0; l < buildActionEntryNodes.getLength(); l++ ) {
node = buildActionEntryNodes.item(l);
if ( node.getNodeType() == Node.ELEMENT_NODE ) {
element = (Element)node;
if ( element.getNodeName().equals("BuildableReference") ) {
//projectScheme.buildableIdentifier = element.getAttribute("BuildableIdentifier");
//projectScheme.blueprintIdentifier = element.getAttribute("BlueprintIdentifier");
//projectScheme.buildableName = element.getAttribute("BuildableName");
projectScheme.blueprintName = element.getAttribute("BlueprintName");
projectScheme.referencedContainer = element.getAttribute("ReferencedContainer");
}
}
}
}
}
}
}
}
}
}
}
}
}
}
catch (SAXException ex) {
ex.printStackTrace();
projectScheme = null;
}
catch (IOException ex) {
ex.printStackTrace();
projectScheme = null;
}
catch (ParserConfigurationException ex) {
ex.printStackTrace();
projectScheme = null;
}
catch ( InterruptedException ex ) {
ex.printStackTrace();
projectScheme = null;
}
return projectScheme;
}
/**
* @param workspaceFileLocation Xcode workspace file location (directory)
* @return list of project files obtained as a result of analyzing workspaceFile. If analysis fails, it is empty
*/
public static List<String> parseXcodeWorkspace(FilePath workspaceFileLocation) {
List<String> projectList = new ArrayList<>();
try {
FilePath workspaceFilePath = workspaceFileLocation.child("contents.xcworkspacedata");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
String FEATURE = null;
FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
factory.setFeature(FEATURE, true);
FEATURE = "http://xml.org/sax/features/external-general-entities";
factory.setFeature(FEATURE, false);
FEATURE = "http://xml.org/sax/features/external-parameter-entities";
factory.setFeature(FEATURE, false);
FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
factory.setFeature(FEATURE, false);
factory.setXIncludeAware(false);
factory.setExpandEntityReferences(false);
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document document = documentBuilder.parse(workspaceFilePath.read());
Element root = document.getDocumentElement();
if ( root.getNodeName().equals("Workspace") ) {
NodeList rootChildren = root.getChildNodes();
for ( int i = 0; i < rootChildren.getLength(); i++ ) {
Node node = rootChildren.item(i);
if ( node.getNodeType() == Node.ELEMENT_NODE ) {
Element element = (Element)node;
if ( element.getNodeName().equals("FileRef") ) {
String projectLocation = element.getAttribute("location");
if ( projectLocation.startsWith("group:") ) {
projectLocation = projectLocation.replaceAll("^group:", "");
projectList.add(projectLocation);
}
}
}
}
}
}
catch (SAXException ex) {
ex.printStackTrace();
projectList = null;
}
catch (IOException ex) {
ex.printStackTrace();
projectList = null;
}
catch (ParserConfigurationException ex) {
ex.printStackTrace();
projectList = null;
}
catch ( InterruptedException ex ) {
ex.printStackTrace();
projectList = null;
}
return projectList;
}
/**
* @param infoPlistFile Xcode Info.plist file location
* @return analysis result of Info.plist file. If analysis fails, it is null
*/
public static InfoPlist parseInfoPlist(FilePath infoPlistFile) {
InfoPlist infoPlist = null;
try {
NSDictionary rootDict = (NSDictionary)PropertyListParser.parse(infoPlistFile.read());
String cfBundleIdentifier = rootDict.objectForKey("CFBundleIdentifier").toString();
String cfBundleVersion = rootDict.objectForKey("CFBundleVersion").toString();
String cfBundleShortVersionString = rootDict.objectForKey("CFBundleShortVersionString").toString();
infoPlist = new InfoPlist(infoPlistFile, cfBundleIdentifier, cfBundleVersion, cfBundleShortVersionString);
}
catch ( Exception ex ) {
ex.printStackTrace();
}
return infoPlist;
}
/**
* @param projectLocation Xcode project file location (directory)
* @return analysis result of Xcode project file. If analysis fails, it is null
*/
public static XcodeProject parseXcodeProject(FilePath projectLocation) {
XcodeProject project = new XcodeProject();
FilePath xcodeProjectFile = projectLocation.child("project.pbxproj");
try {
NSDictionary rootDict = (NSDictionary)PropertyListParser.parse(xcodeProjectFile.read());
String rootObjectsUUID = rootDict.objectForKey("rootObject").toString();
NSDictionary objectsDict = ((NSDictionary)rootDict.objectForKey("objects"));
NSDictionary pbxProjectSectionDict = ((NSDictionary)objectsDict.objectForKey(rootObjectsUUID));
NSObject[] projectTargetUUIDs = ((NSArray)pbxProjectSectionDict.objectForKey("targets")).getArray();
// In case Project has buildConfigurationList.
// Parse each targets.
for ( NSObject projectTargetUUID:projectTargetUUIDs ) {
ProjectTarget target = new ProjectTarget();
NSDictionary projectTargetDict = ((NSDictionary)objectsDict.objectForKey(projectTargetUUID.toString()));
target.uuid = projectTargetUUID.toString();
String targetName = projectTargetDict.objectForKey("name").toString();
// Target has buildConfigurationList.
String buildConfigurationListUUID = projectTargetDict.objectForKey("buildConfigurationList").toString();
target.productType = projectTargetDict.objectForKey("productType").toString();
if ( target.productType.equals("com.apple.product-type.application") ||
target.productType.equals("com.apple.product-type.bundle.unit-test") ||
target.productType.equals("com.apple.product-type.bundle.ui-testing") ||
target.productType.equals("com.apple.product-type.watchkit-extension") ||
target.productType.equals("com.apple.product-type.application.watchapp") ||
target.productType.equals("com.apple.product-type.watchkit2-extension") ||
target.productType.equals("com.apple.product-type.application.watchapp2") ) {
NSDictionary attributesDict = ((NSDictionary)pbxProjectSectionDict.objectForKey("attributes"));
NSDictionary targetAttributesDict = ((NSDictionary)attributesDict.objectForKey("TargetAttributes"));
NSDictionary attributeDict = ((NSDictionary)targetAttributesDict.objectForKey(target.uuid));
if ( attributeDict.objectForKey("ProvisioningStyle") != null ) {
target.provisioningStyle = attributeDict.objectForKey("ProvisioningStyle").toString();
}
else {
// Default code signing style is "Automatic"
target.provisioningStyle = "Automatic";
}
if ( attributeDict.objectForKey("TestTargetID") != null ) {
target.testTargetID = attributeDict.objectForKey("TestTargetID").toString();
}
NSDictionary buildConfigurationList = ((NSDictionary)objectsDict.objectForKey(buildConfigurationListUUID));
if ( buildConfigurationList != null ) {
if ( buildConfigurationList.objectForKey("defaultConfigurationName") != null ) {
target.defaultConfigurationName = buildConfigurationList.objectForKey("defaultConfigurationName").toString();
}
NSObject[] buildConfigurationUUIDs = ((NSArray)buildConfigurationList.objectForKey("buildConfigurations")).getArray();
// Parse each build configurations.
for ( NSObject buildConfigurationUUID : buildConfigurationUUIDs ) {
BuildConfiguration buildConfiguration = new BuildConfiguration(objectsDict, buildConfigurationUUID.toString(), target.provisioningStyle.equals("Automatic"));
target.buildConfiguration.put(buildConfiguration.name, buildConfiguration);
}
}
project.projectTarget.put(targetName, target);
}
else if ( target.productType.equals("com.apple.product-type.framework" ) ) {
}
}
}
catch ( IOException ex ) {
ex.printStackTrace();
project = null;
}
catch ( PropertyListFormatException ex ) {
ex.printStackTrace();
project = null;
}
catch ( ParseException ex ) {
ex.printStackTrace();
project = null;
}
catch ( ParserConfigurationException ex ) {
ex.printStackTrace();
project = null;
}
catch ( SAXException ex ) {
ex.printStackTrace();
project = null;
}
catch ( InterruptedException ex ) {
ex.printStackTrace();
project = null;
}
return project;
}
}