From d75b709f23689f2601ecaba2cb62c4844370c1f8 Mon Sep 17 00:00:00 2001 From: Imple Lee <80144331+ImpleLee@users.noreply.github.com> Date: Mon, 1 Nov 2021 02:12:32 +0800 Subject: [PATCH] Move Python code from `updateVersion.py` into `action.yml`s directly (#423) * test python3 * try use `python` instead of `python3` * use `update-version` everywhere * fix variable injection * fix python2vs3 * move `updateVersion.py` into action files --- .github/actions/build-android/action.yml | 32 ++++++- .github/actions/build-macos/action.yml | 18 +++- .github/actions/build-windows/action.yml | 13 ++- .github/actions/snapshot-update/action.yml | 12 --- .github/actions/update-version/action.yml | 33 +++++++ .github/workflows/dev.yml | 15 ++-- .github/workflows/release.yml | 32 ++----- .github/workflows/test.yml | 15 ++-- .github/workflows/updateVersion.py | 100 --------------------- 9 files changed, 118 insertions(+), 152 deletions(-) delete mode 100644 .github/actions/snapshot-update/action.yml create mode 100644 .github/actions/update-version/action.yml delete mode 100644 .github/workflows/updateVersion.py diff --git a/.github/actions/build-android/action.yml b/.github/actions/build-android/action.yml index 38dd0e4c..889cdc42 100644 --- a/.github/actions/build-android/action.yml +++ b/.github/actions/build-android/action.yml @@ -59,11 +59,41 @@ runs: shell: bash run: | 7z a -tzip love-android/app/src/main/assets/game.love libAndroid + - name: update Android information + shell: python + run: | + if '${{ inputs.type }}' == 'Release': + appName = 'Techmino' + packageName = 'org.love2d.MrZ.Techmino' + edition = 'release' + elif '${{ inputs.type }}' == 'Snapshot': + appName = 'Techmino_Snapshot' + packageName = 'org.love2d.MrZ.Techmino.Snapshot' + edition = 'snapshot' + with open('./love-android/app/src/main/AndroidManifest.xml', "r+", encoding='utf-8') as file: + data = file.read() + data = data\ + .replace('@appName', appName)\ + .replace('@edition', edition) + file.seek(0) + file.truncate() + file.write(data) + with open("./love-android/app/build.gradle", "r+", encoding='utf-8') as file: + data = file.read() + data = data\ + .replace('@packageName', packageName)\ + .replace('@versionCode', '${{ inputs.apkCode }}')\ + .replace('@versionName', '${{ inputs.name }}')\ + .replace('@storePassword', '${{ inputs.KEY_STORE_PASSWORD }}')\ + .replace('@keyAlias', '${{ inputs.ALIAS }}')\ + .replace('@keyPassword', '${{ inputs.KEY_PASSWORD }}') + file.seek(0) + file.truncate() + file.write(data) - name: Build Techmino shell: bash run: | echo "${{ inputs.SIGNING_KEY }}" | base64 -d > love-android/app/android.keystore - python3 .github/workflows/updateVersion.py -T Android${{ inputs.type }} -C ${{ inputs.apkCode }} -N ${{ inputs.name }} -S ${{ inputs.KEY_STORE_PASSWORD }} -A ${{ inputs.ALIAS }} -K ${{ inputs.KEY_PASSWORD }} chmod 777 love-android/gradlew cd love-android/ ./gradlew assembleRelease diff --git a/.github/actions/build-macos/action.yml b/.github/actions/build-macos/action.yml index 5fb7eb37..dcf1ed8c 100644 --- a/.github/actions/build-macos/action.yml +++ b/.github/actions/build-macos/action.yml @@ -59,12 +59,22 @@ runs: MATCH_PASSWORD: '${{ inputs.FASTLANE_MATCH_PWD }}' MATCH_TOKEN: '${{ inputs.FASTLANE_MATCH_TOKEN }}' - name: Modify template + shell: python + run: | + import datetime + from io import open + thisYear = str(datetime.datetime.today().year) + with open('./.github/build/macOS/info.plist.template', 'r', encoding='utf-8') as file: + data = file.read() + data = data\ + .replace('@versionName', '${{ inputs.name }}'[1:])\ + .replace('@thisYear', thisYear)\ + .replace('@bundleId', '${{ inputs.APPLE_APP_IDENTIFIER }}') + with open('./Techmino-macOS/Techmino.app/Contents/info.plist', 'w+', encoding='utf-8') as file: + file.write(data) + - name: Pack shell: bash run: | - python3 .github/workflows/updateVersion.py -T macOS \ - -N ${{ inputs.name }} \ - -B ${{ inputs.APPLE_APP_IDENTIFIER }} - mv Techmino.love Techmino-macOS/Techmino.app/Contents/Resources mv CCloader.dylib Techmino-macOS/Techmino.app/Contents/Frameworks mv ${{ inputs.icon }} Techmino-macOS/Techmino.app/Contents/Resources/iconfile.icns diff --git a/.github/actions/build-windows/action.yml b/.github/actions/build-windows/action.yml index 09c51bc9..546d3a94 100644 --- a/.github/actions/build-windows/action.yml +++ b/.github/actions/build-windows/action.yml @@ -30,6 +30,18 @@ runs: with: url: http://www.angusj.com/resourcehacker/resource_hacker.zip - uses: ./.github/actions/build-love + - name: update Windows template + shell: python + run: | + Version = '${{ inputs.version }}'.replace('V', '') + FileVersion = (f"{Version.replace('.', ',')},0") + with open('./.github/build/Windows/Techmino.rc.template', 'r', encoding='utf8') as file: + data = file.read() + data = data\ + .replace('@FileVersion', FileVersion)\ + .replace('@Version', Version) + with open('Techmino.rc', 'w+', encoding='utf8') as file: + file.write(data) - name: Pack Techmino shell: pwsh run: | @@ -42,7 +54,6 @@ runs: del .\love\readme.txt move .\cold_clear.dll .\love move .\CCloader.dll .\love - python .\.github\workflows\updateVersion.py -T Windows -N ${{ inputs.version }} cmd /c '.\ResourceHacker.exe -open .\love\Techmino.exe -save .\love\Techmino.exe -action delete -mask ICONGROUP,,' cmd /c '.\ResourceHacker.exe -open .\Techmino.rc -save .\Techmino.res -action compile' cmd /c '.\ResourceHacker.exe -open .\love\Techmino.exe -save .\love\Techmino.exe -action addoverwrite -res "${{ inputs.icon }}" -mask ICONGROUP,1,' diff --git a/.github/actions/snapshot-update/action.yml b/.github/actions/snapshot-update/action.yml deleted file mode 100644 index c59777a4..00000000 --- a/.github/actions/snapshot-update/action.yml +++ /dev/null @@ -1,12 +0,0 @@ -name: 'update for a snapshot' -description: 'common update logic for snapshot' -inputs: - commit: - required: true -runs: - using: "composite" - steps: - - shell: bash - run: | - python3 .github/workflows/updateVersion.py -T Conf - python3 .github/workflows/updateVersion.py -T Version -H ${{ inputs.commit }} diff --git a/.github/actions/update-version/action.yml b/.github/actions/update-version/action.yml new file mode 100644 index 00000000..ac7c53cb --- /dev/null +++ b/.github/actions/update-version/action.yml @@ -0,0 +1,33 @@ +name: 'update version' +description: 'common update logic for snapshot and release' +inputs: + commit: + required: false + type: + required: true +runs: + using: "composite" + steps: + - shell: python + name: update snapshot saving folder + run: | + from io import open + if '${{ inputs.type }}'.lower() != 'snapshot': + exit(0) + with open('conf.lua', 'r+', encoding='utf-8') as file: + data = file.read() + data = data.replace("t.identity='Techmino'--Saving folder", "t.identity='Techmino_Snapshot'--Saving folder") + file.seek(0) + file.truncate() + file.flush() + file.write(data) + - shell: python + run: | + from io import open + with open('version.lua', 'r+', encoding='utf-8') as file: + data = file.read() + data = data.replace('@DEV', '@${{ inputs.commit }}'[0:4] if '${{ inputs.commit }}' != '' else '') + file.seek(0) + file.truncate() + file.flush() + file.write(data) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 9bc600c3..09f74b29 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -32,9 +32,10 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - uses: ./.github/actions/snapshot-update + - uses: ./.github/actions/update-version with: commit: ${{ needs.get-info.outputs.commit }} + type: snapshot - uses: ./.github/actions/build-windows with: love-url: https://github.com/love2d/love/releases/download/11.3/love-11.3-win64.zip @@ -53,9 +54,10 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - uses: ./.github/actions/snapshot-update + - uses: ./.github/actions/update-version with: commit: ${{ needs.get-info.outputs.commit }} + type: snapshot - uses: ./.github/actions/build-linux with: icon: .github/build/Linux/icon_snapshot.png @@ -70,9 +72,10 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - uses: ./.github/actions/snapshot-update + - uses: ./.github/actions/update-version with: commit: ${{ needs.get-info.outputs.commit }} + type: snapshot - uses: ./.github/actions/build-android with: type: Snapshot @@ -94,9 +97,10 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - uses: ./.github/actions/snapshot-update + - uses: ./.github/actions/update-version with: commit: ${{ needs.get-info.outputs.commit }} + type: snapshot - uses: ./.github/actions/build-macos with: name: ${{ needs.get-info.outputs.name }} @@ -120,9 +124,10 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - uses: ./.github/actions/snapshot-update + - uses: ./.github/actions/update-version with: commit: ${{ needs.get-info.outputs.commit }} + type: snapshot - uses: ./.github/actions/build-ios with: name: ${{ needs.get-info.outputs.name }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 97fb1b15..00eaf781 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -35,9 +35,7 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - name: Update Version - run: | - python .\.github\workflows\updateVersion.py -T Version + - uses: ./.github/actions/update-version - uses: ./.github/actions/build-windows with: love-url: https://github.com/love2d/love/releases/download/11.3/love-11.3-win64.zip @@ -58,9 +56,7 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - name: Update Version - run: | - python .\.github\workflows\updateVersion.py -T Version + - uses: ./.github/actions/update-version - uses: ./.github/actions/build-windows with: love-url: https://github.com/love2d/love/releases/download/11.3/love-11.3-win32.zip @@ -81,9 +77,7 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - name: Update Version - run: | - python3 .github/workflows/updateVersion.py -T Version + - uses: ./.github/actions/update-version - uses: ./.github/actions/build-linux with: file-path: Techmino.a${{ needs.get-info.outputs.release }}.AppImage @@ -99,9 +93,7 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - name: Update Version - run: | - python3 .github/workflows/updateVersion.py -T Version + - uses: ./.github/actions/update-version - uses: ./.github/actions/build-android with: type: Release @@ -123,9 +115,7 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - name: Update Version - run: | - python3 .github/workflows/updateVersion.py -T Version + - uses: ./.github/actions/update-version - name: remove media run: | rm -rf media/music media/effect media/vocal @@ -150,9 +140,7 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - name: Update Version - run: | - python3 .github/workflows/updateVersion.py -T Version + - uses: ./.github/actions/update-version - uses: ./.github/actions/build-macos with: name: ${{ needs.get-info.outputs.name }} @@ -179,9 +167,7 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - uses: ./.github/actions/snapshot-update - with: - commit: ${{ needs.get-info.outputs.commit }} + - uses: ./.github/actions/update-version - uses: ./.github/actions/build-ios with: name: ${{ needs.get-info.outputs.name }} @@ -215,9 +201,7 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - name: Update Version - run: | - python3 .github/workflows/updateVersion.py -T Version + - uses: ./.github/actions/update-version - uses: ./.github/actions/build-love with: file-path: Techmino.a${{ needs.get-info.outputs.release }}.love diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 206c8005..0ad19bdd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,9 +33,10 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - uses: ./.github/actions/snapshot-update + - uses: ./.github/actions/update-version with: commit: ${{ needs.get-info.outputs.commit }} + type: snapshot - uses: ./.github/actions/build-windows with: love-url: https://github.com/love2d/love/releases/download/11.3/love-11.3-win64.zip @@ -54,9 +55,10 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - uses: ./.github/actions/snapshot-update + - uses: ./.github/actions/update-version with: commit: ${{ needs.get-info.outputs.commit }} + type: snapshot - uses: ./.github/actions/build-linux with: icon: .github/build/Linux/icon_snapshot.png @@ -71,9 +73,10 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - uses: ./.github/actions/snapshot-update + - uses: ./.github/actions/update-version with: commit: ${{ needs.get-info.outputs.commit }} + type: snapshot - uses: ./.github/actions/build-android with: type: Snapshot @@ -95,9 +98,10 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - uses: ./.github/actions/snapshot-update + - uses: ./.github/actions/update-version with: commit: ${{ needs.get-info.outputs.commit }} + type: snapshot - uses: ./.github/actions/build-macos with: name: ${{ needs.get-info.outputs.name }} @@ -121,9 +125,10 @@ jobs: needs: get-info steps: - uses: actions/checkout@v2 - - uses: ./.github/actions/snapshot-update + - uses: ./.github/actions/update-version with: commit: ${{ needs.get-info.outputs.commit }} + type: snapshot - uses: ./.github/actions/build-ios with: name: ${{ needs.get-info.outputs.name }} diff --git a/.github/workflows/updateVersion.py b/.github/workflows/updateVersion.py deleted file mode 100644 index 8a61ac9e..00000000 --- a/.github/workflows/updateVersion.py +++ /dev/null @@ -1,100 +0,0 @@ -import argparse -import re - -def updateConf(): #更新存档位置 - with open('conf.lua', 'r+', encoding='utf-8') as file: - data = file.read() - data = data.replace("t.identity='Techmino'--Saving folder", "t.identity='Techmino_Snapshot'--Saving folder") - file.seek(0) - file.truncate() - file.flush() - file.write(data) - -def updateVersion(args): #更新版本号 - with open('version.lua', 'r+', encoding='utf-8') as file: - data = file.read() - if args.Hash != False: - data = data.replace('@DEV', f'@{args.Hash[0:4]}') - else: - data = data.replace('@DEV', '') - file.seek(0) - file.truncate() - file.flush() - file.write(data) - -def updateMacOS(args): #更新macOS打包信息 - import datetime - thisYear = str(datetime.datetime.today().year) - with open('./.github/build/macOS/info.plist.template', 'r', encoding='utf-8') as file: - data = file.read() - data = data\ - .replace('@versionName', args.Name[1:])\ - .replace('@thisYear', thisYear)\ - .replace('@bundleId', args.Bundle) - with open('./Techmino-macOS/Techmino.app/Contents/info.plist', 'w+', encoding='utf-8') as file: - file.write(data) - -def updateWindows(args): #更新Windows打包信息 - Version = (args.Name).replace('V', '') - FileVersion = (f"{Version.replace('.', ',')},0") - with open('./.github/build/Windows/Techmino.rc.template', 'r', encoding='utf8') as file: - data = file.read() - data = data\ - .replace('@FileVersion', FileVersion)\ - .replace('@Version', Version) - with open('Techmino.rc', 'w+', encoding='utf8') as file: - file.write(data) - -def updateAndroid(args, edition): #更新Android打包信息 - if edition == 'Release': - appName = 'Techmino' - packageName = 'org.love2d.MrZ.Techmino' - edition = 'release' - elif edition == 'Snapshot': - appName = 'Techmino_Snapshot' - packageName = 'org.love2d.MrZ.Techmino.Snapshot' - edition = 'snapshot' - with open('./love-android/app/src/main/AndroidManifest.xml', "r+", encoding='utf-8') as file: - data = file.read() - data = data\ - .replace('@appName', appName)\ - .replace('@edition', edition) - file.seek(0) - file.truncate() - file.write(data) - with open("./love-android/app/build.gradle", "r+", encoding='utf-8') as file: - data = file.read() - data = data\ - .replace('@packageName', packageName)\ - .replace('@versionCode', args.Code)\ - .replace('@versionName', args.Name)\ - .replace('@storePassword', args.Store)\ - .replace('@keyAlias', args.Alias)\ - .replace('@keyPassword', args.Key) - file.seek(0) - file.truncate() - file.write(data) - -if __name__ == '__main__': - parser = argparse.ArgumentParser(description='用于CI更新程序各类信息') - parser.add_argument('-T', '--Type', type=str, help = '更新的种类') - parser.add_argument('-B', '--Bundle', type=str, help = '应用包名') - parser.add_argument('-H', '--Hash', type=str, default = False, help = 'Github提交Hash') - parser.add_argument('-C', '--Code', type=str, default = False, help = 'versionCode') - parser.add_argument('-N', '--Name', type=str, default = False, help = 'versionName') - parser.add_argument('-S', '--Store', type=str, default = False, help = 'storePassword') - parser.add_argument('-A', '--Alias', type=str, default = False, help = 'keyAlias') - parser.add_argument('-K', '--Key', type=str, default = False, help = 'keyPassword') - args = parser.parse_args() - if args.Type == 'Conf': - updateConf() - elif args.Type == 'Version': - updateVersion(args) - elif args.Type == 'Windows': - updateWindows(args) - elif args.Type == 'macOS': - updateMacOS(args) - elif args.Type == 'AndroidRelease': - updateAndroid(args, 'Release') - elif args.Type == 'AndroidSnapshot': - updateAndroid(args, 'Snapshot')