diff --git a/elm_deps_upgrade.py b/elm_deps_upgrade.py index e611fcd..6098f34 100755 --- a/elm_deps_upgrade.py +++ b/elm_deps_upgrade.py @@ -7,15 +7,23 @@ import struct import argparse -def load_all_packages(elm_version): - payload = requests.get("http://package.elm-lang.org/all-packages?elm-package-version={elm_version}".format( +def load_all_packages(elm_version, url=None): + if url is None: + url = "http://package.elm-lang.org/all-packages?elm-package-version=" + + payload = requests.get("{url}{elm_version}".format( + url=url, elm_version=elm_version )) return { item['name'] : item for item in payload.json() } -def load_versions(package_name): - payload = requests.get("http://package.elm-lang.org/versions?name={package_name}".format( +def load_versions(package_name, url=None): + if url is None: + url = "http://package.elm-lang.org/versions?name=" + + payload = requests.get("{url}{package_name}".format( + url=url, package_name=package_name )) @@ -27,13 +35,30 @@ def load_local_packages(elm_package): return json.load(f)['dependencies'] def top_range(field): - print(field) only_end = field[field.index('v'):] if '=' in only_end: return only_end.split('=')[-1].strip() if '<' in only_end: - return only_end.split('<')[-1].strip() + + number = only_end.split('<')[-1].strip() + + if patch(number) == 0: + if minor(number) == 0: + return '{maj}.{min}.{pat}'.format( + maj = major(number) - 1, + min = 9999999, + pat = 0 ) + return '{maj}.{min}.{pat}'.format( + maj = major(number) - 1, + min = minor(number) - 1, + pat = 0 ) + return '{maj}.{min}.{pat}'.format( + maj = major(number) - 1, + min = minor(number) - 1, + pat = patch(number) - 1 ) + + def major(version): return int(version.split('.')[0]) @@ -63,9 +88,8 @@ def get_patch_upgrades(top, versions): return [ version for version in versions if major(version) == major_top and minor_top == minor(version) and patch_top > patch(version) ] -def print_newer_versions(local_deps, remote_deps): - - upgrade_suggestions = [] +def find_newer_versions(local_deps, remote_deps): + upgrade_suggestions = {} for (dep, version) in local_deps.items(): current_version = top_range(version) @@ -73,6 +97,30 @@ def print_newer_versions(local_deps, remote_deps): minors = get_minor_upgrades(current_version, remote_deps[dep]['versions']) majors = get_major_upgrades(current_version, remote_deps[dep]['versions']) + upgrade_suggestions[dep] = { + 'patches': patches, + 'minors': minors, + 'majors': majors + } + + return upgrade_suggestions + +def newest_version(suggestions): + if suggestions['majors']: + return suggestions['majors'][-1] + elif suggestions['minors']: + return suggestions['majors'][-1] + else: + return suggestions['patches'][-1] + +def print_newer_versions(local_deps, remote_deps): + upgrade_suggestions = [] + + for (dep, suggestions) in find_newer_versions(local_deps, remote_deps).items(): + patches = suggestions['patches'] + minors = suggestions['minors'] + majors = suggestions['majors'] + if len(patches) > 0: upgrade_suggestions.append( 'Patches available for {dep}: [{patches}]'.format(dep=dep, patches=', '.join(patches))