-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.py
executable file
·84 lines (65 loc) · 2.82 KB
/
build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
import logging as log
import subprocess
import os
from typing import Tuple
import yaml
import tempfile
from argparse import ArgumentParser, Namespace
log.basicConfig(
format="%(asctime)s %(levelname)s: %(message)s", level=log.INFO)
def getBuildCount() -> int:
output = subprocess.check_output(["git", "rev-list", "--count", "HEAD"])
return int(output)
def makeOptions() -> Namespace:
arg = ArgumentParser(description="Script to build the app.")
arg.add_argument("-t", "--type", choices=['apk', 'ios', 'appbundle', 'ipa', 'web'],
type=str, default='apk', # const='apk', nargs='?',
help="Type of build to make. (Default: '%(default)s')")
arg.add_argument("--install", type=str, default=None,
help="install to device d. (Default: '%(default)s')")
# arg.add_argument("-n", action='store_true',
# help="Do not make a new commit with tag. (Default: '%(default)s')")
out = arg.parse_args()
return out
def getVersion() -> Tuple[str, bool]:
last_commit = subprocess.check_output(
["git", "log", "-1", "--oneline"])
if last_commit.decode('utf-8').split(' ', 1)[1].startswith("updated build number"):
with open("pubspec.yaml", 'rt') as f:
yamlData = yaml.safe_load(f)
return yamlData['version'], False
version = "1.0.0(0)"
with tempfile.TemporaryFile('w+t') as tmpFile:
with open("pubspec.yaml", 'rt') as f:
yamlData = yaml.safe_load(f)
f.seek(0)
tmpFile.writelines(f.readlines())
tmpFile.seek(0)
version = yamlData['version'].split('+')[0]
version = f"{version}+{getBuildCount()}"
yamlData['version'] = version
with open("pubspec.yaml", 'wt') as f:
for line in tmpFile.readlines():
if line.strip().startswith("version:"):
f.write(f"version: {version}\n")
else:
f.write(line)
return version, True
if __name__ == '__main__':
args = makeOptions()
version, toCommit = getVersion()
# Build the project
log.info(f"Building: v{version}")
if toCommit:
subprocess.call(["git", "commit", "-m", "updated build number", "pubspec.yaml"],
env=os.environ.copy(), shell=False, cwd=".")
subprocess.call(['git', 'tag', f'v{version}'],
env=os.environ.copy(), shell=False, cwd=".")
log.info(f"Building: {args.type}")
subprocess.call(["flutter", "build", args.type],
env=os.environ.copy(), shell=False, cwd=".")
log.info("Building: Done")
if args.install is not None:
subprocess.call(["flutter", "install", "-d", args.install],
env=os.environ.copy(), shell=False, cwd=".")