lbry-desktop/build/set_version.py

53 lines
1.4 KiB
Python
Raw Normal View History

2017-02-09 20:09:31 -06:00
"""Set the package version to the output of `git describe`"""
2017-02-16 12:19:35 -06:00
import argparse
2017-02-09 20:09:31 -06:00
import json
import os.path
import re
2017-02-09 20:09:31 -06:00
import subprocess
import sys
def main():
2017-02-16 12:19:35 -06:00
parser = argparse.ArgumentParser()
parser.add_argument('--version', help="defaults to the output of `git describe`")
args = parser.parse_args()
if args.version:
version = args.version
else:
tag = subprocess.check_output(['git', 'describe']).strip()
2017-02-22 14:54:09 -06:00
try:
version = get_version_from_tag(tag)
except InvalidVersionTag:
# this should be an error but its easier to handle here
# than in the calling scripts.
print 'Tag cannot be converted to a version, Exitting'
return
2017-02-16 12:19:35 -06:00
set_version(version)
2017-02-22 14:54:09 -06:00
class InvalidVersionTag(Exception):
pass
def get_version_from_tag(tag):
match = re.match('v([\d.]+)', tag)
if match:
return match.group(1)
else:
2017-02-22 14:54:09 -06:00
raise InvalidVersionTag('Failed to parse version from tag {}'.format(tag))
2017-02-16 12:19:35 -06:00
def set_version(version):
2017-04-11 10:38:32 -04:00
root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
package_file = os.path.join(root_dir, 'app', 'package.json')
2017-02-09 20:09:31 -06:00
with open(package_file) as fp:
package_data = json.load(fp)
package_data['version'] = version
with open(package_file, 'w') as fp:
json.dump(package_data, fp, indent=2, separators=(',', ': '))
if __name__ == '__main__':
sys.exit(main())