Skip to content

Instantly share code, notes, and snippets.

@tastyone
Created November 17, 2019 09:42
Show Gist options
  • Save tastyone/1e4695edfe27121c29d16e5b5fd2471c to your computer and use it in GitHub Desktop.
Save tastyone/1e4695edfe27121c29d16e5b5fd2471c to your computer and use it in GitHub Desktop.
#! /usr/bin/python
# This will generate a build number from GIT repository rev-count
# and Set the build number to iOS and Android project file in the React Native Project
#
# Use this script on the base directory of your React Native Project.
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
import re
import subprocess
def git_rev_count():
cmd = ['git', 'rev-list', '--all', '--count']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
return out
def findXcodeProj():
files = []
# r=root, d=directories, f = files
path = dir_path + '/ios'
for r, d, f in os.walk(path):
for ff in f:
if 'project.pbxproj' == ff:
if 'Pods' in r:
continue
files.append(os.path.join(r, ff))
if len(files) != 1:
print(bcolors.FAIL + 'Error: the number of pbxproj is not 1' + bcolors.ENDC)
raise SystemExit
return files[0]
# update_ios
def update_ios(new_build_number):
changed = ''
xcodeproj = findXcodeProj()
with open(xcodeproj, 'r') as f:
content = f.read()
s = r'CURRENT_PROJECT_VERSION = ([0-9]+);'
r = r'CURRENT_PROJECT_VERSION = %d;' % new_build_number
m = re.search(s, content)
print(('[ios] set build number: %s -> ' + bcolors.OKGREEN + '%d' + bcolors.ENDC) % (m.group(1), new_build_number))
changed = re.sub(s, r, content)
if len(changed) < 1:
print(bcolors.FAIL + 'Error: No changed string' + bcolors.ENDC)
raise SystemExit
with open(xcodeproj, 'w') as f:
f.write(changed)
# update_android
def update_android(new_build_number):
changed = ''
buildgradle = dir_path + '/android/app/build.gradle'
with open(buildgradle, 'r') as f:
content = f.read()
s = r'versionCode ([0-9]+)'
r = r'versionCode %d' % new_build_number
m = re.search(s, content)
print(('[android] set build number: %s -> ' + bcolors.OKGREEN + '%d' + bcolors.ENDC) % (m.group(1), new_build_number))
changed = re.sub(s, r, content)
if len(changed) < 1:
print(bcolors.FAIL + 'Error: No changed string' + bcolors.ENDC)
raise SystemExit
with open(buildgradle, 'w') as f:
f.write(changed)
# run
n = int(git_rev_count())
update_ios(n)
update_android(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment