Skip to content

Instantly share code, notes, and snippets.

@Strubbl
Last active August 29, 2015 14:14
Show Gist options
  • Save Strubbl/fe21f6fae05afa6f08d2 to your computer and use it in GitHub Desktop.
Save Strubbl/fe21f6fae05afa6f08d2 to your computer and use it in GitHub Desktop.
just another Cordova before_compile hook, to use app version from config.xml in your app
{
"version" : {
"MAJOR" : "0",
"MINOR" : "1",
"PATCH" : "0"
}
}
#!/usr/bin/env python
import json
import xml.etree.ElementTree as ET
file_json = 'www/js/version.json'
if __name__ == "__main__":
with open(file_json, 'r') as f:
data = f.read()
json_data = json.loads(data)
f.closed
with open('config.xml', 'r') as f:
data = f.read()
tree = ET.fromstring(data)
xml_version = tree.get('version')
f.closed
xml_version_splitted = xml_version.split(".")
json_version = json_data['version']
if(xml_version_splitted[0] == json_version['MAJOR'] and xml_version_splitted[1] == json_version['MINOR'] and xml_version_splitted[2] == json_version['PATCH']):
exit(0)
else:
print('new version detected in config.xml, updating ', file_json)
json_data['version']['MAJOR'] = xml_version_splitted[0]
json_data['version']['MINOR'] = xml_version_splitted[1]
json_data['version']['PATCH'] = xml_version_splitted[2]
with open(file_json, 'w') as of:
json.dump(json_data, of)
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment