Skip to content

Instantly share code, notes, and snippets.

@nicerobot
Created January 29, 2012 05:30
Show Gist options
  • Save nicerobot/1697386 to your computer and use it in GitHub Desktop.
Save nicerobot/1697386 to your computer and use it in GitHub Desktop.
Xcode 4.3+ license template creator.

This process copies /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates files in the CWD and updates them with the specified license.

TODO: The TemplateInfo.plist files for the newly created templates doesn't currently change Apple's identifier so the newly created templates might be hidden, otherwise, they will hide the default templates. The <string>com.apple.dt.unit.*</string> Identifiers need to be changed to a unique name.

User Templates go into ${HOME}/Library/Developer/Xcode/Templates:

mkdir ${HOME}/Library/Developer/Xcode/Templates
cd ${HOME}/Library/Developer/Xcode/Templates
  • To generate templates with GNU GPLv3 licenses:

      curl -ks https://raw.github.com/gist/1697386/Xclict.py | LICENSE_TYPE='1697386/GNU-GPLv3' python
    
  • The LICENSE_TYPE "basename", like the above GNU-GPLv3, must be valid filename characters as it'll be prepended to the .xctemplate directories.

      curl -ks https://raw.github.com/gist/1697386/Xclict.py | LICENSE_TYPE='1697386/MPLv2' python
    
This file is part of ___PROJECTNAME___.
___PROJECTNAME___ is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
___PROJECTNAME___ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ___PROJECTNAME___. If not, see <http://www.gnu.org/licenses/>.
This file is part of ___PACKAGENAME___.
___PACKAGENAME___ is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
___PACKAGENAME___ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ___PACKAGENAME___. If not, see <http://www.gnu.org/licenses/>.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this file,
You can obtain one at http://mozilla.org/MPL/2.0/.
#!/usr/bin/env python
import os,sys,re
import fnmatch
import shutil
import urllib2
def main():
lre = re.compile('^(([^C]+)Copyright [(]c[)] ___YEAR___ ___ORGANIZATIONNAME___[.] All rights reserved[.][^\n]*\n)',re.M)
lcm = re.compile('(^)',re.M)
lxc = re.compile('/([^/]+[.]xctemplate)$')
if 'LICENSE_TYPE' not in os.environ:
license_type = '1697386/GNU-GPLv3'
else:
license_type = os.environ['LICENSE_TYPE']
licenses = dict()
for f in ['File','Proj']:
req = urllib2.Request('https://raw.github.com/gist/%s.%s.txt' % (license_type,f))
try:
response = urllib2.urlopen(req)
licenses[f] = response.read()
except:
pass
if not len(licenses):
return 1
if 'File' not in licenses:
licenses['File'] = licenses['Proj']
elif 'Proj' not in licenses:
licenses['Proj'] = licenses['File']
name_prefix = os.path.basename(license_type)
comments = dict()
templates='/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates'
if not os.path.isdir(templates):
templates='/Developer/Library/Xcode/Templates'
# Get each of the directories from the Templates/
for d in [os.path.join(templates, name) for name in os.listdir(templates) if os.path.isdir(os.path.join(templates, name))]:
# Walk the directory
for root, dirnames, filenames in os.walk(d):
if not lxc.search(root):
continue
# Extract the [:4] of the directory to use as hash key.
# This is used to correlate the fetched license and the saved comment style.
untemplated = root.replace('%s/'%templates,'')
template_type = untemplated[:4]
dst=os.path.join(os.path.dirname(untemplated),"%s %s" % (name_prefix,os.path.basename(untemplated)))
shutil.copytree(root,dst)
# Iterate the files in the walked directories
for xcroot, xcdirnames, xcfilenames in os.walk(dst):
for filename in [os.path.join(xcroot,fn) for fn in xcfilenames]:
# Slurp the file
f = os.path.join(root, filename)
text = file(filename).read()
# Check if the file has the Copyright template
m = lre.search(text)
if m:
# Save the commenting style of the Copyright line
style = m.group(2)
# If the style hasn't already been seen, then save it.
if style not in comments:
comments[style] = dict()
# Save the license template with the comment style updated.
if template_type not in comments[style]:
comments[style][template_type] = lcm.sub('%s\\1' % style,licenses[template_type])
# Write the template with the updated license.
f = None
try:
f = open(filename, 'w')
f.write(lre.sub('\\1%s\n%s\n' % (style,comments[style][template_type]),text))
finally:
if f:
f.close()
return 0
#
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment