/
tasks.py
157 lines (118 loc) · 4.45 KB
/
tasks.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import re
import os
import glob
import plistlib
import logging
from invoke import task, Collection
import semver
_logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def yes(question: str):
"""Prompt user to input y[es]/n[o] to a yes or no question.
Args:
question: Question to answer
Returns: True if yes, False if no.
"""
answer = input(question + " (y/n): ").lower().strip()
while answer not in ['y', 'n', 'yes', 'no']:
print("Input yes or no")
answer = input(question + "(y/n):").lower().strip()
if answer.startswith('y'):
return True
else:
return False
def _git_modified_files():
"""Check for modified files on branch.
Returns: True if modified, False otherwise
"""
return os.system('git diff-index --quiet HEAD --') != 0
def _get_current_version(filename: str) -> str:
"""Gets current version from podspec file.
Args:
filename: name of podspec file.
Returns: semvar version in podspec
"""
with open(filename, 'r') as f:
version_lines = [line for line in f if 's.version = ' in line]
if len(version_lines) > 1:
# The Fritz.podspec file shouldn't change for this to be a problem,
# but this is definitely brittle. If it breaks often, we can figure
# out a less hacky way.
raise Exception("Found multiple lines with version. "
"There can only be one.")
line = version_lines[0]
match = re.match(".*'(.*)'.*", line)
return match.groups(0)[0]
def _update_podspec(filename: str, current_version: str, new_version: str):
_logger.info('Updating %s from %s -> %s', filename, current_version,
new_version)
final_lines = []
with open(filename, 'r') as f:
for line in f:
if 's.version = ' in line:
line = line.replace(current_version, new_version)
final_lines.append(line)
with open(filename, 'w') as f:
f.write(''.join(final_lines))
def _update_info_plists(new_version: str):
"""Update Info.plist files for all frameworks.
Args:
new_version: Version to update plist files with.
"""
for filename in glob.glob('Frameworks/*.framework/Info.plist'):
with open(filename, 'rb') as f:
plist = plistlib.load(f)
current_version = plist['CFBundleShortVersionString']
plist['CFBundleShortVersionString'] = new_version
_logger.info('Updating %s from %s -> %s', filename, current_version,
new_version)
with open(filename, 'wb') as f:
plistlib.dump(plist, f)
def _update_changelog(new_version: str):
"""Update CHANGELOG.md with user inputted updates.
Args:
new_version: semver new version to update.
"""
if not yes("Update Changelog file?"):
_logger.info("Not updating Changelog.")
return
changes = []
changes.append(input("[" + str(len(changes) + 1) + "] "))
while yes("Add another note to CHANGELOG.md?"):
changes.append(input("[" + str(len(changes) + 1) + "] "))
template = [
'',
'## [{version}]({github_base}/releases/tag/{version})',
'',
*['{}. {}'.format(i + 1, change) for i, change in enumerate(changes)],
''
]
template = '\n'.join(template).format(
version=new_version,
github_base='https://github.com/fritzlabs/swift-framework'
)
with open('CHANGELOG.md') as f:
lines = f.readlines()
for i, line in enumerate(lines):
if line == '---\n':
lines = [*lines[:i + 1], template, *lines[i + 1:]]
break
with open('CHANGELOG.md', 'w') as f:
f.write(''.join(lines))
@task
def update_version(ctx):
"""Update version number in Podspec and Frameworks."""
if _git_modified_files():
_logger.error('Branch must be clean before updating version spec. '
'Clean up modified files and try again.')
return
current_version = _get_current_version('Fritz.podspec')
new_version = semver.bump_prerelease(current_version, token='beta')
_update_changelog(new_version)
_update_podspec('Fritz.podspec', current_version, new_version)
_update_info_plists(new_version)
os.system('git commit -am "Bump to version {}"'.format(new_version))
@task(default=True)
def list_tasks(ctx):
"""Default: Lists all available tasks"""
ctx.run("invoke --list")