Skip to content

Instantly share code, notes, and snippets.

Last active Dec 19, 2015
What would you like to do?
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.
import sys, os, re
import getpass, getopt
import xml.etree.ElementTree as ET
pref_xcode4_loc = "/Applications/"
pref_xcode5_loc = "/Applications/"
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
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))
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: <xcode-version>\nBe sure to only have 1 csproj file in the current directory\n"
if __name__ == "__main__":
if len(sys.argv) != 2:
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 == "":
print "Updated AppleSdkRoot to %s" % (version_xcode)
csproj_paths = find_files_with_ext(".csproj")
if csproj_paths == None or len(csproj_paths) == 0:
if len(csproj_paths) != 1:
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\ ./" + sln)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment