org.openbakery.xcode.Xcode.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xcode-plugin Show documentation
Show all versions of xcode-plugin Show documentation
XCode-Plugin is a plugin to allow custom XCode projects to build as generated by CMake
package org.openbakery.xcode
import org.openbakery.CommandRunner
import org.slf4j.Logger
import org.slf4j.LoggerFactory
class Xcode {
private static Logger logger = LoggerFactory.getLogger(Xcode.class)
CommandRunner commandRunner
String xcodePath
Version version = null
public Xcode(CommandRunner commandRunner) {
this(commandRunner, null)
}
public Xcode(CommandRunner commandRunner, String version) {
logger.debug("create xcode with version {}", version)
this.commandRunner = commandRunner
if (version != null) {
setVersionFromString(version)
}
}
void setVersionFromString(String version) {
Version versionToCompare = new Version(version)
String installedXcodes = commandRunner.runWithResult("mdfind", "kMDItemCFBundleIdentifier=com.apple.dt.Xcode")
for (String xcode : installedXcodes.split("\n")) {
File xcodeBuildFile = new File(xcode, "Contents/Developer/usr/bin/xcodebuild");
if (xcodeBuildFile.exists()) {
Version xcodeVersion = getXcodeVersion(xcodeBuildFile.absolutePath)
if (xcodeVersion.suffix != null && versionToCompare.suffix != null) {
if (xcodeVersion.suffix.equalsIgnoreCase(versionToCompare.suffix)) {
xcodePath = xcode
this.version = xcodeVersion
return
}
} else if (xcodeVersion.toString().startsWith(versionToCompare.toString())) {
xcodePath = xcode
this.version = xcodeVersion
return
}
}
}
throw new IllegalStateException("No Xcode found with build number " + version);
}
Version getXcodeVersion(String xcodebuildCommand) {
String xcodeVersion = commandRunner.runWithResult(xcodebuildCommand, "-version");
def VERSION_PATTERN = ~/Xcode\s([^\s]*)\nBuild\sversion\s([^\s]*)/
def matcher = VERSION_PATTERN.matcher(xcodeVersion)
if (matcher.matches()) {
Version version = new Version(matcher[0][1])
version.suffix = matcher[0][2]
return version
}
return null
}
Version getVersion() {
if (this.version == null) {
this.version = getXcodeVersion(getXcodebuild())
}
return this.version
}
String getPath() {
if (xcodePath == null) {
String result = commandRunner.runWithResult("xcode-select", "-p")
xcodePath = result - "/Contents/Developer"
}
return xcodePath
}
String getXcodebuild() {
if (xcodePath != null) {
return xcodePath + "/Contents/Developer/usr/bin/xcodebuild"
}
return "xcodebuild"
}
String getAltool() {
return getPath() + "/Contents/Applications/Application Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Support/altool"
}
String getXcrun() {
return getPath() + "/Contents/Developer/usr/bin/xcrun"
}
String getSimctl() {
return getPath() + "/Contents/Developer/usr/bin/simctl"
}
@Override
public String toString() {
return "Xcode{" +
"xcodePath='" + xcodePath + '\'' +
", version=" + version +
'}';
}
}