|
| 1 | +""" |
| 2 | +A script to scan through all valid mod jars in the artifacts folder, |
| 3 | +and generate an artifact summary table for that to GitHub action step `summary` |
| 4 | +""" |
| 5 | +__author__ = 'Fallen_Breath(Original)' + '70CentsApple(Modified)' |
| 6 | +# Credits to https://github.com/Fallen-Breath/fabric-mod-template |
| 7 | + |
| 8 | +import functools |
| 9 | +import glob |
| 10 | +import hashlib |
| 11 | +import json |
| 12 | +import os |
| 13 | + |
| 14 | + |
| 15 | +def read_prop(file_name: str, key: str) -> str: |
| 16 | + with open(file_name, 'r') as prop: |
| 17 | + return next(filter( |
| 18 | + lambda l: l.split('=', 1)[0].strip() == key, |
| 19 | + prop.readlines() |
| 20 | + )).split('=', 1)[1].lstrip() |
| 21 | + |
| 22 | + |
| 23 | +def get_sha256_hash(file_path: str) -> str: |
| 24 | + sha256_hash = hashlib.sha256() |
| 25 | + with open(file_path, 'rb') as f: |
| 26 | + for buf in iter(functools.partial(f.read, 4096), b''): |
| 27 | + sha256_hash.update(buf) |
| 28 | + return sha256_hash.hexdigest() |
| 29 | + |
| 30 | +def main(): |
| 31 | + MOD_VERSION = read_prop('gradle.properties', 'mod_version') |
| 32 | + print(f'Current mod version: {MOD_VERSION}') |
| 33 | + target_subproject_env = os.environ.get('TARGET_SUBPROJECT', '') |
| 34 | + target_subprojects = list(filter(None, target_subproject_env.split(',') if target_subproject_env != '' else [])) |
| 35 | + print(f'target_subprojects: {target_subprojects}') |
| 36 | + |
| 37 | + with open('settings.json') as f: |
| 38 | + settings: dict = json.load(f) |
| 39 | + |
| 40 | + with open(os.environ['GITHUB_STEP_SUMMARY'], 'w') as f: |
| 41 | + f.write('## 🍎 Build Artifacts Summary 🍎\n\n') |
| 42 | + f.write('| Subproject | File | Size | SHA-256 |\n') |
| 43 | + f.write('| --- | --- | --- | --- |\n') |
| 44 | + |
| 45 | + warnings = [] |
| 46 | + for subproject in settings['versions']: |
| 47 | + print(f'Found: {subproject}') |
| 48 | + if len(target_subprojects) > 0 and subproject not in target_subprojects: |
| 49 | + print(f'- Skipping {subproject}') |
| 50 | + continue |
| 51 | + # file_paths = glob.glob(f'/build/libs/{MOD_VERSION}/*{subproject}*.jar') |
| 52 | + file_paths = glob.glob(f'gathered-artifacts/*{subproject}*.jar') |
| 53 | + file_paths = list(filter(lambda fp: not fp.endswith('-sources.jar') and not fp.endswith('-dev.jar') and not fp.endswith('-shadow.jar'), file_paths)) |
| 54 | + if len(file_paths) == 0: |
| 55 | + file_name = '*NOT FOUND*' |
| 56 | + file_size = '*N/A*' |
| 57 | + sha256 = '*N/A*' |
| 58 | + else: |
| 59 | + file_name = f'`{os.path.basename(file_paths[0])}`' |
| 60 | + file_size = f'{os.path.getsize(file_paths[0])} B' |
| 61 | + sha256 = f'`{get_sha256_hash(file_paths[0])}`' |
| 62 | + if len(file_paths) > 1: |
| 63 | + warnings.append(f'Found too many build files in subproject {subproject}: {", ".join(file_paths)}') |
| 64 | + |
| 65 | + f.write(f'| {subproject} | {file_name} | {file_size} | {sha256} |\n') |
| 66 | + |
| 67 | + if warnings: |
| 68 | + f.write('\n### Warnings\n\n') |
| 69 | + for warning in warnings: |
| 70 | + f.write(f'- {warning}\n') |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + main() |
0 commit comments