Created
October 5, 2017 09:54
-
-
Save duraiganesh0/1462e211ead3a2d30d48cb6b6e62fdd1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.dd.plist.* | |
import org.edx.builder.TaskHelper | |
// Dependencies | |
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
classpath 'com.googlecode.plist:dd-plist:1.16' | |
} | |
} | |
apply plugin: 'edxapp' | |
edx { | |
platform = IOS | |
} | |
// Configuration | |
def workspace = 'edX.xcworkspace' | |
def scheme = 'edX' | |
class IOSHelper { | |
// Environment Variable Conveniences | |
// Only valid when our task is invoked by an Xcode build | |
def getInfoPlistPath() { | |
def env = System.getenv() | |
return env['INFOPLIST_PATH'] | |
} | |
def getBuiltProductsPath() { | |
def env = System.getenv() | |
return env['BUILT_PRODUCTS_DIR'] | |
} | |
def getWrapperName() { | |
def env = System.getenv() | |
return env['WRAPPER_NAME'] | |
} | |
def getBuiltInfoPlistPath() { | |
return builtProductsPath + '/' + infoPlistPath | |
} | |
def getBundleConfigPath() { | |
return builtProductsPath + '/' + wrapperName + '/config.plist' | |
} | |
// Saves our loaded and processed config YAML | |
// to a Plist in the app bundle | |
def saveProcessedConfig(config, toPath) { | |
def plist = [:] | |
for(c in config) { | |
plist[c.key] = c.value | |
} | |
// Save entire config | |
PropertyListParser.saveAsXML(NSObject.wrap(plist), new File(toPath)) | |
} | |
// Modify Info.plist for specific services | |
def readInfoPlist() { | |
// The configuration library doesn't know how to read | |
// binary plists so make sure it's an xml one | |
['plutil', '-convert', 'xml1', builtInfoPlistPath].execute().waitFor() | |
def plist = PropertyListParser.parse(new File(builtInfoPlistPath)).toJavaObject() | |
return plist | |
} | |
def writeInfoPlist(plist) { | |
PropertyListParser.saveAsXML(NSObject.wrap(plist), new File(builtInfoPlistPath)) | |
// Restore to binary for runtime performance | |
['plutil', '-convert', 'binary1', builtInfoPlistPath].execute().waitFor() | |
} | |
def addURLScheme(scheme, plist) { | |
def body = [ | |
'CFBundleTypeRole' : 'Editor', | |
'CFBundleURLSchemes' : [scheme] | |
] | |
def existing = plist['CFBundleURLTypes'] | |
if(existing) { | |
// make sure we don't add it more than once | |
def found = false | |
for(entry in existing){ | |
def schemes = entry['CFBundleURLSchemes'] | |
if(schemes && schemes.contains(scheme)){ | |
found = true | |
break | |
} | |
} | |
if(!found) { | |
def types = new ArrayList((ArrayList)existing) | |
types.add(body) | |
plist['CFBundleURLTypes'] = types | |
} | |
} | |
else { | |
plist["CFBundleURLTypes"] = [body] | |
} | |
} | |
def addFacebookConfig(config, plist) { | |
def facebook = config['FACEBOOK'] ?: [:] | |
def key = facebook['FACEBOOK_APP_ID'] | |
if(!key) { | |
return | |
} | |
plist["FacebookAppID"] = key | |
def scheme = "fb" + key | |
addURLScheme(scheme, plist) | |
} | |
def addFabricConfig(config, plist) { | |
def fabric = config['FABRIC'] ?: [:] | |
def key = fabric['FABRIC_KEY'] | |
if(!key) { | |
return | |
} | |
def body = [ | |
'APIKey' : key, | |
'Kits' : [ | |
[ | |
'KitInfo' : [], | |
'KitName' : 'Crashlytics' | |
] | |
] | |
] | |
plist["Fabric"] = body | |
} | |
def addGoogleConfig(config, plist) { | |
def google = config['GOOGLE'] ?: [:] | |
def key = google['GOOGLE_PLUS_KEY'] | |
if(!key) { | |
return | |
} | |
// Google login schemes are the reverse DNS of the api key | |
def scheme = key.tokenize('.').reverse().join('.') | |
addURLScheme(scheme, plist) | |
} | |
} | |
// Tasks | |
task printBuildEnvironment(type : Exec) { | |
def arguments = [ | |
'xcodebuild', | |
'-workspace', workspace, | |
'-scheme', scheme, | |
'-showBuildSettings' | |
] | |
commandLine arguments | |
} | |
task uploadDebuggingSymbols << { | |
def taskHelper = new TaskHelper() | |
def config = taskHelper.loadConfig(project) | |
def fabric = config['FABRIC'] ?: [:] | |
def key = fabric['FABRIC_KEY'] | |
def secret = fabric['FABRIC_BUILD_SECRET'] | |
def srcroot = System.getenv()['SRCROOT'] | |
if(key && secret && srcroot) { | |
[srcroot + "/Pods/Fabric/Fabric.framework/run", key, secret].execute().waitFor() | |
} | |
} | |
task applyConfig << { | |
def taskHelper = new TaskHelper() | |
def config = taskHelper.loadConfig(project) | |
def helper = new IOSHelper() | |
// Save all keys to config.plist | |
helper.saveProcessedConfig(config, helper.bundleConfigPath) | |
// Save specific fields to Info.plist | |
def plist = helper.readInfoPlist() | |
helper.addFacebookConfig(config, plist) | |
helper.addFabricConfig(config, plist) | |
helper.addGoogleConfig(config, plist) | |
helper.writeInfoPlist(plist) | |
// double check that the config file actually got made | |
def check = ["[", "-f", helper.bundleConfigPath, "]"].execute() | |
check.waitFor() | |
def result = check.exitValue() | |
assert result == 0 | |
} | |
task wrapper(type: Wrapper) { | |
gradleVersion = '2.2.1' | |
} | |
def testBuildArguments(workspace, scheme, OS, record) { | |
def args = [ | |
'xcodebuild', '-workspace', workspace, '-scheme', scheme, '-sdk', 'iphonesimulator', '-destination', 'platform=iOS Simulator,name=iPhone 5s,OS=' + OS, 'test' | |
] | |
if(record) { | |
args += ["OTHER_SWIFT_FLAGS=\$(OTHER_SWIFT_FLAGS) -D RECORD_SNAPSHOTS"] | |
} | |
return args | |
} | |
def RTLSchemeForScheme(scheme) { | |
return scheme + '-RTL' | |
} | |
def operatingSystems = ["currentOS": "10.0", "previousOS": "9.0"] | |
def directions = ["LTR": scheme, "RTL": RTLSchemeForScheme(scheme)] | |
def commands = ["test" : ["record" : false], "recordSnapshots": ["record" : true]] | |
for(OS in operatingSystems) { | |
for(direction in directions) { | |
for(command in commands) { | |
def record = command.value["record"] | |
def task = project.task(type: Exec, command.key + direction.key.capitalize() + OS.key.capitalize()) { | |
commandLine testBuildArguments(workspace, direction.value, OS.value, record) | |
} | |
} | |
} | |
} | |
task test(dependsOn: tasks.findAll { task -> task.name.startsWith('test')}) {} | |
task recordSnapshots(dependsOn: tasks.findAll { task -> task.name.startsWith('recordSnapshots')}) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment