View Javadoc

1   /*
2    * #%L
3    * xcode-maven-plugin
4    * %%
5    * Copyright (C) 2012 SAP AG
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
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  }