Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
woo
Browse files Browse the repository at this point in the history
  • Loading branch information
eeue56 committed May 4, 2016
1 parent a53662a commit 53eb191
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions update_elm_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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()
Expand All @@ -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

Expand All @@ -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))
Expand All @@ -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():

Expand All @@ -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__':
Expand Down

0 comments on commit 53eb191

Please sign in to comment.