Last active
December 19, 2015 03:28
-
-
Save xeb/5890019 to your computer and use it in GitHub Desktop.
A quick script to automatically change the Xamarin Studio Apple SDK from Xcode4 (for iOS 5.1 development) to Xcode5 (for iOS 7.0 development). The script will also automatically update the first (and only) csproj in the current directory and finally launch Xamarin Studio with the desired configuration.
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
#!/usr/bin/python | |
import sys, os, re | |
import getpass, getopt | |
import xml.etree.ElementTree as ET | |
pref_xcode4_loc = "/Applications/Xcode.app" | |
pref_xcode5_loc = "/Applications/Xcode5-DP.app" | |
pref_loc = "/Users/%s/Library/Preferences/XamarinStudio-4.0/MonoDevelopProperties.xml" % (getpass.getuser()) | |
pref_key = "MonoDevelop.MacDev.AppleSdkRoot" | |
csproj_xcode4_val = "6.1" | |
csproj_xcode5_val = "7.0" | |
csproj_sdk_ele = "MtouchSdkVersion" | |
def update_prefs(version_xcode): | |
tree = ET.parse(pref_loc) | |
root = tree.getroot() | |
prop = root.findall("Property[@key='%s']" % (pref_key))[0] | |
prop.attrib["value"] = version_xcode | |
tree.write(pref_loc) | |
def update_csproj(csproj_path, csproj_val): | |
# determine the old value of the SDK based on what the new one is | |
old_xcode = csproj_xcode4_val | |
if(csproj_val == csproj_xcode4_val): | |
old_xcode = csproj_xcode5_val | |
# just do some old school regex to avoid MS XML namespaces | |
with open(csproj_path) as f: | |
tmp_csproj_path = csproj_path + ".tmp" | |
out = open(tmp_csproj_path, "w") | |
pattern = "\<{0}\>{1}\<\/{0}\>".format(csproj_sdk_ele, old_xcode) | |
replace = "<{0}>{1}</{0}>".format(csproj_sdk_ele, csproj_val) | |
for line in f: | |
out.write(re.sub(pattern, replace, line)) | |
out.close() | |
os.rename(tmp_csproj_path, csproj_path) | |
def find_files_with_ext(extension): | |
return [f for f in os.listdir('.') if os.path.isfile(f) and os.path.splitext(f)[1] == extension] | |
def usage(): | |
print "usage: xamarian_switch.py <xcode-version>\nBe sure to only have 1 csproj file in the current directory\n" | |
sys.exit(2) | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
usage() | |
version_xcode = "" | |
csproj_val = "" | |
if sys.argv[1].lower() in [ "4", "xcode4" ]: | |
version_xcode = pref_xcode4_loc | |
csproj_val = csproj_xcode4_val | |
if sys.argv[1].lower() in [ "5", "xcode5" ]: | |
version_xcode = pref_xcode5_loc | |
csproj_val = csproj_xcode5_val | |
if version_xcode == "" or csproj_val == "": | |
usage() | |
update_prefs(version_xcode) | |
print "Updated AppleSdkRoot to %s" % (version_xcode) | |
csproj_paths = find_files_with_ext(".csproj") | |
if csproj_paths == None or len(csproj_paths) == 0: | |
usage() | |
if len(csproj_paths) != 1: | |
usage() | |
update_csproj(csproj_paths[0], csproj_val) | |
print "Updated %s's MtouchSdkVersion to SDK Version of %s" % (csproj_paths[0], csproj_val) | |
sln = find_files_with_ext(".sln")[0] | |
print "Opening %s with Xamarin Studio using %s" % (sln, version_xcode) | |
os.system("open -n /Applications/Xamarin\ Studio.app ./" + sln) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment