|
|
b1b9bd |
#!/usr/bin/python
|
|
|
b1b9bd |
|
|
|
b1b9bd |
"""Set a package version in a package.json file"""
|
|
|
b1b9bd |
|
|
|
b1b9bd |
# Copyright 2018 Tom Hughes <tom@compton.nu>
|
|
|
b1b9bd |
#
|
|
|
b1b9bd |
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
b1b9bd |
# of this software and associated documentation files (the "Software"), to
|
|
|
b1b9bd |
# deal in the Software without restriction, including without limitation the
|
|
|
b1b9bd |
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
b1b9bd |
# sell copies of the Software, and to permit persons to whom the Software is
|
|
|
b1b9bd |
# furnished to do so, subject to the following conditions:
|
|
|
b1b9bd |
#
|
|
|
b1b9bd |
# The above copyright notice and this permission notice shall be included in
|
|
|
b1b9bd |
# all copies or substantial portions of the Software.
|
|
|
b1b9bd |
#
|
|
|
b1b9bd |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
b1b9bd |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
b1b9bd |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
b1b9bd |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
b1b9bd |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
b1b9bd |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
b1b9bd |
# IN THE SOFTWARE.
|
|
|
b1b9bd |
|
|
|
b1b9bd |
import json
|
|
|
b1b9bd |
import os
|
|
|
b1b9bd |
import shutil
|
|
|
b1b9bd |
import sys
|
|
|
b1b9bd |
|
|
|
b1b9bd |
if not os.path.exists('package.json~'):
|
|
|
b1b9bd |
shutil.copy2('package.json', 'package.json~')
|
|
|
b1b9bd |
|
|
|
b1b9bd |
md = json.load(open('package.json'))
|
|
|
b1b9bd |
|
|
|
b1b9bd |
if 'version' in md and sys.argv[1] != md['version']:
|
|
|
b1b9bd |
raise RuntimeError('Version is already set to {0}'.format(md['version']))
|
|
|
b1b9bd |
else:
|
|
|
b1b9bd |
md['version'] = sys.argv[1]
|
|
|
b1b9bd |
|
|
|
b1b9bd |
fh = open('package.json', 'w')
|
|
|
b1b9bd |
data = json.JSONEncoder(indent=4).encode(md)
|
|
|
b1b9bd |
fh.write(data)
|
|
|
b1b9bd |
fh.close()
|