Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

The heck is this?

Basically, this takes an ant build manifest for GreenTurtle, fetches the chunks remotely and concats all the things into designated files locally. Sure I could have just downloaded ant and used it directly, but why? :)

Caveats

The bootleg AMD wrapper strategy here won't provide the correct exports for RDFaProcessor*.js; for that, replace the wrapper's return with:

return {{
  default: RDFaProcessor,
  RDFaProcessor: RDFaProcessor
}};
#!/usr/bin/env python3
"""
compile_greenturtle_from_github.py
Pulls the build manifest and source chunks from Github and compiles them into a local file
in a mini-AMD wrapper.
USAGE
Change the BASE_URL to whichever version you need.
$ ./compile_greenturtle_from_github.py
"""
from collections import defaultdict
import urllib.request
import xml.etree.ElementTree as XML
import os.path
BASE_URL = 'https://raw.githubusercontent.com/alexmilowski/green-turtle/master' #/v1.3.0
SRC_PREFIX = 'src'
OUTPUT_DIR = os.path.dirname(__file__)
_cache = {}
def main():
properties = generate_properties(BASE_URL, SRC_PREFIX, OUTPUT_DIR)
for job in fetch_manifest(properties):
contents = [process_task(task, properties) for task in job]
destination = generate_filepath(job.get('destfile'), properties)
compile_amd(destination, contents, properties)
def compile_amd(filepath, contents, properties):
print('compile_amd: `{}`'.format(filepath))
with open(filepath, 'w') as fp:
fp.write('''\
define('green-turtle', [], function() {{
{0}
return {{
default: GreenTurtle,
GreenTurtle: GreenTurtle
}};
}});
'''.format('\n'.join(contents)))
def fetch(uri, properties):
resolved_url = resolve('${{context}}/{0}'.format(uri), properties)
if resolved_url not in _cache:
print('fetch: `{}`'.format(resolved_url))
with urllib.request.urlopen(resolved_url) as response:
_cache[resolved_url] = response.read().decode()
return _cache[resolved_url]
def fetch_manifest(properties):
document = XML.fromstring(fetch('build.xml', properties))
return document.findall('target[@name="web"]/concat')
def fetch_version_label(properties):
lines = fetch('version.properties', properties).split()
version = dict([line.replace('version.', '').split('=') for line in lines])
return '{major}.{minor}.{release}'.format(**version)
def generate_filepath(value, properties):
normalized_template = value.replace('.${version.label}', '-${version.label}').lower()
return resolve(normalized_template, properties)
def generate_properties(base_url, src_prefix, output_dir):
properties = {}
properties['context'] = base_url
properties['src.dir'] = src_prefix
properties['dist.dir'] = output_dir
properties['version.label'] = fetch_version_label(properties)
return properties
def process_task(task, properties):
if task.tag == 'file':
contents = fetch(task.get('file'), properties)
elif task.tag == 'string':
contents = resolve(task.text, properties)
return contents.strip()
def resolve(value, properties):
for key in properties:
value = value.replace('${{{0}}}'.format(key), properties[key])
return value
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.