Last active
July 14, 2024 16:52
-
-
Save Snownee/10a41a3b4a71021b68250c5c7509069e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# D:\Program Files\Git\cmd | |
# pyinstaller.exe .\publishMod.py --onefile --distpath . | |
# python (get-command publishMod.py).path cur -f -p | |
import os | |
import re | |
import sys | |
import argparse | |
import subprocess | |
def main(): | |
repo = readProperty('remote_repo') | |
if repo == '': | |
repo = readCmdResult('git config --get remote.origin.url').strip() | |
match = re.search(r"https://github\.com/([a-zA-Z0-9-]{1,39}/[a-zA-Z0-9._-]+)\.git", repo) | |
if match is None: | |
print('Not a git repository') | |
return | |
repo = match.group(1) | |
# get `mod_version` from gradle.properties | |
old_mod_version = mod_version = readProperty('mod_version') | |
old_release_type = readProperty('release_type') | |
parts = mod_version.split('.') | |
assert len(parts) == 3, 'mod_version must be in format major.minor.patch' | |
parser = argparse.ArgumentParser(description='Publish mod to CurseForge and Modrinth') | |
parser.add_argument('release_type', choices=['r', 'b', 'a'], help='release, beta or alpha') | |
parser.add_argument('publish_type', nargs='?', default='patch', choices=['major', 'minor', 'patch', 'cur'], | |
help='major, minor, patch or cur') | |
parser.add_argument('-f', '--force', action='store_true', help='force publish without checking for changes') | |
parser.add_argument('-p', '--print', action='store_true', help='print changelog') | |
args = parser.parse_args() | |
release_type = {'r': 'release', 'b': 'beta', 'a': 'alpha'}[args.release_type] | |
writeProperty('release_type', release_type) | |
publish_type = args.publish_type | |
if publish_type != 'cur': | |
publish_type = ['major', 'minor', 'patch'].index(publish_type) | |
assert publish_type >= 0, 'publish_type must be major, minor or patch' | |
parts[publish_type] = str(int(parts[publish_type]) + 1) | |
for i in range(publish_type + 1, 3): | |
parts[i] = '0' | |
mod_version = '.'.join(parts) | |
print('Publishing version: ' + mod_version) | |
# write new `mod_version` to gradle.properties | |
writeProperty('mod_version', mod_version) | |
else: | |
print('Publishing version: ' + mod_version) | |
# generate changelog from git commits | |
recent_tags = readCmdResult( | |
'''git for-each-ref refs/tags --sort=creatordate --format='%(refname:lstrip=2)' --count=6 --merged''').strip().split('\n') | |
changelog = [] | |
if len(recent_tags) > 1: | |
commits = readCmdResult('git log ' + recent_tags[0].replace("'", '') + '..HEAD --pretty=format:"%s"').strip().split('\n') | |
first_line = True | |
is_empty = False | |
changelog.append('## ' + mod_version) | |
changelog.append('') | |
for line in commits: | |
line = line.strip() | |
if line.startswith('Publish version'): | |
if first_line and not args.force: | |
print('No changes since last publish') | |
writeProperty('mod_version', old_mod_version) | |
return | |
if is_empty: | |
changelog.append('- No changelog provided') | |
if not first_line: | |
changelog.append('') | |
changelog.append(line.replace('Publish version', '##')) | |
changelog.append('') | |
is_empty = True | |
elif line == '.': | |
continue | |
elif line.find('[skip changelog]') != -1: | |
continue | |
else: | |
changelog.append('- ' + re.sub(r"([a-zA-Z0-9-]{1,39}\/[a-zA-Z0-9._-]+)?#(\d+)", lambda m: convertIssueUrl(m, repo), line, 0, | |
re.MULTILINE)) | |
is_empty = False | |
first_line = False | |
changelog.append('') | |
changelog = '\n'.join(changelog) | |
with open('CHANGELOG.md', 'w', encoding='utf-8') as f: | |
f.write(changelog) | |
# print changelog | |
if args.print: | |
print(changelog) | |
# run gradle task to publish mod | |
command = readProperty('build_command') | |
if command == '': | |
command = 'gradlew publishUnified' | |
result = os.system(command) | |
if result != 0: | |
print('Publishing failed') | |
writeProperty('mod_version', old_mod_version) | |
writeProperty('release_type', old_release_type) | |
return 1 | |
# get current branch name | |
branch = readCmdResult('git rev-parse --abbrev-ref HEAD').strip() | |
platform = branch.split('-')[1] | |
tag = platform + '-' + mod_version | |
# push a new tag to git | |
os.system('git add gradle.properties') | |
os.system('git commit --allow-empty -m "Publish version ' + mod_version + '"') | |
os.system('git tag ' + tag) | |
os.system('git push') | |
os.system('git push --tags') | |
def readProperty(key): | |
path = os.path.join(os.getcwd(), 'gradle.properties') | |
file = open(path, 'r', encoding='utf-8') | |
text = file.read() | |
pattern = re.compile(r'' + key + '\s?=\s?(.*)') | |
values = pattern.findall(text) | |
file.close() | |
return len(values) > 0 and values[0] or '' | |
def writeProperty(key, value): | |
path = os.path.join(os.getcwd(), 'gradle.properties') | |
file = open(path, 'r', encoding='utf-8') | |
text = file.read() | |
pattern = re.compile(r'' + key + '\s?=\s?(.*)') | |
text = pattern.sub(key + '=' + value, text) | |
file.close() | |
file = open(path, 'w', encoding='utf-8') | |
file.write(text) | |
file.close() | |
def convertIssueUrl(match, default_repo): | |
repo = match.group(1) or default_repo | |
issue = match.group(2) | |
return "[#%s](https://github.com/%s/issues/%s)" % (issue, repo, issue) | |
def readCmdResult(cmd): | |
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, text=True, encoding='utf-8') | |
return result.stdout | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment