|
| 1 | +from fnmatch import fnmatch |
| 2 | +from itertools import chain |
| 3 | +from zipfile import ZipFile |
| 4 | +from zipfile import ZIP_DEFLATED |
| 5 | +import argparse |
| 6 | +import glob |
| 7 | +import json |
| 8 | +import os |
| 9 | + |
| 10 | + |
| 11 | +THIS_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 12 | +PROJECT_ROOT = os.path.dirname(THIS_DIR) |
| 13 | +RESERVED = ['manifest.json', 'dist'] |
| 14 | + |
| 15 | + |
| 16 | +parser = argparse.ArgumentParser( |
| 17 | + description="Builds .sublime-package archives.") |
| 18 | +parser.add_argument('-d', dest='target_dir', default='./dist', |
| 19 | + help="output directory") |
| 20 | +parser.add_argument('--release', dest='release', default='dev', |
| 21 | + help="type of build (e.g. 'dev', 'release'...)") |
| 22 | + |
| 23 | + |
| 24 | +def get_manifest(): |
| 25 | + path = os.path.join(PROJECT_ROOT, 'manifest.json') |
| 26 | + with open(path) as f: |
| 27 | + return json.load(f) |
| 28 | + |
| 29 | + |
| 30 | +def unwanted(fn, pats): |
| 31 | + return any(fnmatch(fn, pat) for pat in pats + RESERVED) |
| 32 | + |
| 33 | + |
| 34 | +def ifind_files(patterns): |
| 35 | + for fn in (fn for (pat, exclude) in patterns |
| 36 | + for fn in glob.iglob(pat) |
| 37 | + if not unwanted(fn, exclude)): |
| 38 | + yield fn |
| 39 | + |
| 40 | + |
| 41 | +def build(target_dir="dist", release="dev"): |
| 42 | + manifest = get_manifest() |
| 43 | + name = manifest['name'] + '.sublime-package' |
| 44 | + |
| 45 | + target_dir = os.path.join(PROJECT_ROOT, target_dir) |
| 46 | + if not os.path.exists(target_dir): |
| 47 | + os.mkdir(target_dir) |
| 48 | + |
| 49 | + target_file = os.path.join(target_dir, name) |
| 50 | + if os.path.exists(target_file): |
| 51 | + os.unlink(target_file) |
| 52 | + |
| 53 | + with ZipFile(target_file, 'a', compression=ZIP_DEFLATED) as package: |
| 54 | + for fn in ifind_files(manifest['include'][release]): |
| 55 | + package.write(fn) |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == '__main__': |
| 59 | + args = parser.parse_args() |
| 60 | + build(args.target_dir, args.release) |
0 commit comments