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.ByteArrayOutputStream;
23 import java.io.File;
24 import java.io.PrintStream;
25 import java.nio.charset.Charset;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 import org.apache.commons.io.IOUtils;
30 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
31
32 public class XCodeVersionUtil
33 {
34
35 public static String getXCodeVersionString() throws XCodeException
36 {
37 PrintStream out = null;
38 try {
39 ByteArrayOutputStream bos = new ByteArrayOutputStream();
40 out = new PrintStream(bos, true, Charset.defaultCharset().name());
41 int exitCode = Forker.forkProcess(out, new File("."), new String[] { "xcodebuild", "-version" });
42 if (exitCode == 0) {
43 return bos.toString(Charset.defaultCharset().name());
44 }
45 else {
46 throw new XCodeException(
47 "Could not get xcodebuild version (exit code = " + exitCode + ")");
48 }
49 }
50 catch (Exception e) {
51 throw new XCodeException(
52 "Could not get xcodebuild version");
53 }
54 finally {
55 IOUtils.closeQuietly(out);
56 }
57 }
58
59 public static DefaultArtifactVersion getVersion(String xCodeVersionString) throws XCodeException
60 {
61 Pattern versionPattern = Pattern.compile("Xcode (\\d+(\\.\\d+)+)", Pattern.CASE_INSENSITIVE);
62 Matcher versionMatcher = versionPattern.matcher(xCodeVersionString);
63 if (versionMatcher.find()) {
64 return new DefaultArtifactVersion(versionMatcher.group(1));
65 }
66 throw new XCodeException("Could not get xcodebuild version");
67 }
68
69 public static String getBuildVersion(String xCodeVersionString) throws XCodeException
70 {
71 Pattern buildPattern = Pattern.compile("Build version (\\w+)", Pattern.CASE_INSENSITIVE);
72 Matcher buildMatcher = buildPattern.matcher(xCodeVersionString);
73 if (buildMatcher.find()) {
74 return buildMatcher.group(1);
75 }
76 throw new XCodeException("Could not get xcodebuild build version");
77 }
78
79
80 public static boolean checkVersions(DefaultArtifactVersion version, final String MIN_XCODE_VERSION)
81 {
82 DefaultArtifactVersion minXcodeVersion = new DefaultArtifactVersion(MIN_XCODE_VERSION);
83 return version.compareTo(minXcodeVersion) >= 0;
84 }
85
86 }