Skip to content

Instantly share code, notes, and snippets.

@kijuky
Last active December 5, 2015 14:54
Show Gist options
  • Select an option

  • Save kijuky/52d79ff21e3acf34fce3 to your computer and use it in GitHub Desktop.

Select an option

Save kijuky/52d79ff21e3acf34fce3 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
# http://docs.python.jp/3/library/argparse.html
def parsearg():
import argparse
parser = argparse.ArgumentParser('Create xcworkspace')
parser.add_argument('name')
parser.add_argument('--xcodeproj', nargs='+', default=[])
parser.add_argument('--use-cocoapods', action='store_const', const=True, default=False)
return parser.parse_args()
# http://docs.python.jp/3/library/xml.etree.elementtree.html#treebuilder-objects
def createcontents(args):
import xml.etree.ElementTree as ET
builder = ET.TreeBuilder()
builder.start('Workspace', {'version':'1.0'})
for xcodeproj in args.xcodeproj:
builder.start('FileRef', {'location':'group:{0}'.format(xcodeproj)})
builder.end('FileRef')
if args.use_cocoapods:
builder.start('FileRef', {'location':'group:Pods/Pods.xcodeproj'})
builder.end('FileRef')
builder.end('Workspace')
return builder.close()
# http://docs.python.jp/3.3/library/os.html#files-and-directories
def createworkspace(args):
import os
xcworkspace = '{0}.xcworkspace'.format(args.name)
os.mkdir(xcworkspace)
#
contents = createcontents(args)
with open('{0}/contents.xcworkspacedata'.format(xcworkspace), 'w') as f:
f.write(prettify(contents))
# http://ja.pymotw.com/2/xml/etree/ElementTree/create.html
# http://docs.python.jp/2/library/xml.dom.minidom.html
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
from xml.etree import ElementTree
from xml.dom import minidom
rough_string = ElementTree.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ", encoding='utf-8')
# http://d.hatena.ne.jp/s-n-k/20080512/1210611374
if __name__ == '__main__':
args = parsearg()
createworkspace(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment