From 53eb19131530111045d7c910c0175c95587013f0 Mon Sep 17 00:00:00 2001 From: eeue56 Date: Wed, 4 May 2016 05:57:15 +0200 Subject: [PATCH] woo --- update_elm_package.py | 65 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/update_elm_package.py b/update_elm_package.py index 5bb3680..ce4a052 100644 --- a/update_elm_package.py +++ b/update_elm_package.py @@ -7,6 +7,11 @@ import requests import argparse import sys +import re +import os +import glob + +module_pattern = r'module\s+?(\S+)\s*?(.*?)\s+?where' KNOWN_MOVES = { 'evancz/elm-html' : 'elm-lang/html' @@ -20,6 +25,27 @@ def upgrade_elm_version(version): return "0.17.0 <= v < 0.18.0" +def upgrade_module_syntax(module_name, exposing): + module_name = module_name.strip() + exposing = exposing.strip() + + if not exposing: + exposing = '(..)' + + return 'module {name} exposing {exposing}'.format(name=module_name, exposing=exposing) + + +def replace_module_line(text, replacement): + return re.sub(module_pattern, replacement, text, count=1) + +def get_module_name_and_exposing(text): + matches = re.search(module_pattern, text) + + if not matches: + return ('', '') + + return matches.groups() + def new_packages(): r = requests.get("http://159.203.88.24:8000/new-packages") return r.json() @@ -45,9 +71,10 @@ def update_elm_package(root_folder, dry=False): errors.append('Package {name} has been removed'.format(name=package)) else: notes.append('Package {name} renamed to {new_name}'.format(name=package, new_name=new_name)) - package = new_name + version = '1.0.0 <= v <= 1.0.0' - upgradable_packages[package] = version + upgradable_packages[new_name] = version + del package_data['dependencies'][package] continue @@ -69,6 +96,7 @@ def update_elm_package(root_folder, dry=False): update_to = upgrader.newest_version(suggestions) except Exception as e: notes.append('package {} already updated'.format(dep)) + package_data['dependencies'][dep] = upgradable_packages[dep] continue notes.append('updating {} to {}'.format(dep, update_to)) @@ -95,8 +123,39 @@ def update_elm_package(root_folder, dry=False): print('There were errors that need to be handled manually!') sys.exit(1) +def upgrade_elm_files_in_folder(folder, root_folder): + if folder[0] != '.': + folder = root_folder + '/' + folder + + for file in glob.glob('{}/**/*.elm'.format(folder), recursive=True): + with open(file, 'r') as f: + text = f.read() + + (name, exposing) = get_module_name_and_exposing(text) + new_line = upgrade_module_syntax(name, exposing) + + new_text = replace_module_line(text, new_line) + + with open(file, 'w') as f: + f.write(new_text) + + + +def upgrade_elm_files(root_folder): + with open(root_folder + '/elm-package.json') as f: + package_data = json.load(f, object_pairs_hook=OrderedDict) + + dirs = package_data['source-directories'] + + for dir in dirs: + upgrade_elm_files_in_folder(dir, root_folder) +def run_elm_make(root_folder): + from subprocess import call + os.chdir(root_folder) + call(["elm-package", "install", "--yes"]) + call(["elm-make"]) def main(): @@ -107,6 +166,8 @@ def main(): args = parser.parse_args() update_elm_package(args.package_dir, dry=args.dry) + upgrade_elm_files(args.package_dir) + run_elm_make(args.package_dir) if __name__ == '__main__':