Created
July 22, 2014 16:39
-
-
Save cybertk/d13146972b32b785bb65 to your computer and use it in GitHub Desktop.
Generate xcassets from file system
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/env python | |
import shutil | |
import os | |
import glob | |
import json | |
import sys | |
def CreateXcassets(name): | |
# Cleanup siliently first. | |
shutil.rmtree(name, ignore_errors=True) | |
# Create new. | |
os.mkdir(name) | |
def AddToXcassets(xcassets, filepath): | |
def GenerateContentJson(asset_dir): | |
contents = {} | |
contents['images'] = list() | |
contents['info'] = {} | |
for f in glob.glob(os.path.join(asset_dir, '*.png')): | |
image = {} | |
image['idiom'] = 'universal' | |
image['scale'] = '2x' | |
image['filename'] = os.path.basename(f) | |
info = {} | |
info['version'] = 1 | |
info['author'] = 'Bot' | |
contents['images'].append(image) | |
contents['info'] = info | |
with open(os.path.join(asset_dir, 'Contents.json'), 'wb') as f: | |
f.write(json.dumps(contents, indent=4)) | |
asset_name = os.path.splitext(os.path.basename(filepath))[0] | |
# Remove '@2x' suffix | |
asset_name = asset_name.replace('@2x', '') | |
# Create imageset | |
asset_dir = os.path.join(xcassets, asset_name + '.imageset') | |
os.mkdir(asset_dir) | |
shutil.copyfile( | |
filepath, os.path.join(asset_dir, os.path.basename(filepath))) | |
GenerateContentJson(asset_dir) | |
print 'Add', asset_name, 'into', xcassets | |
def Main(): | |
xc = 'Images.xcassets' | |
CreateXcassets(xc) | |
# Walk the tree. | |
for root, directories, files in os.walk('image/assets'): | |
for filename in files: | |
# Join the two strings in order to form the full filepath. | |
AddToXcassets(xc, os.path.join(root, filename)) | |
if __name__ == '__main__': | |
sys.exit(Main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment