diff --git a/.github/workflows/UpdateMoose.yml b/.github/workflows/UpdateMoose.yml new file mode 100644 index 0000000000..b1c999e5cb --- /dev/null +++ b/.github/workflows/UpdateMoose.yml @@ -0,0 +1,91 @@ +# This workflow updates the Moose.lua file in all miz files. +name: Build Demo Missions + +# Controls when the workflow will run +on: + + # Triggers the workflow on push or pull request events for the "main" and "develop" branches + push: + branches: + - master + - develop + + # Triggers the workflow on pull request events for the "main" and "develop" branches + pull_request: + branches: + - master + - develop + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + + # This workflow contains a single job called "Update" + Update: + + # Operating system + runs-on: ubuntu-latest + + # Set environment variable (not used) + env: + BRANCH: main + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + + # Say Mission branch name + - name: Mission branch + run: echo ${GITHUB_REF_NAME} + + # Check if master + - name: Check if master + if: ${{ github.ref_name=='master' }} + run: echo "MOOSE_BRANCH=master" >> $GITHUB_ENV + + # Check if develop + - name: Check if develop + if: ${{ github.ref_name=='develop' }} + run: echo "MOOSE_BRANCH=develop" >> $GITHUB_ENV + + # Say Moose branch name. + - name: Moose branch name + run: echo $MOOSE_BRANCH + + # Checks-out MOOSE_INCLUDE to ./MooseFiles + - name: Checkout MOOSE_INCLUDE + uses: actions/checkout@v3 + with: + repository: FlightControl-Master/MOOSE_INCLUDE + ref: ${{ env.MOOSE_BRANCH }} + path: ./MooseFiles + + # Checks-out repository to ./self + - name: Checkout Missions + uses: actions/checkout@v3 + with: + #ref: ${{ env.BRANCH }} + path: ./self + + # Install python + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' # install the python version needed + + # Run python script to update Moose.lua in all miz files + - name: Run UpdateMoose.py + run: | + cd ./self + python .scripts/UpdateMoose.py ../MooseFiles/Moose_Include_Static/ ./ + + # Commit changes + - name: Commit Changes + run: | + cd ./self + git add * + git config --global user.name 'funkyfranky' + git config --global user.email 'frank@inter-zone.de' + git commit -am "Updated Moose.lua" + git push diff --git a/.github/workflows/demomissionspacked.yml b/.github/workflows/demomissionspacked.yml index 793e2d5763..4651caa25a 100644 --- a/.github/workflows/demomissionspacked.yml +++ b/.github/workflows/demomissionspacked.yml @@ -5,13 +5,13 @@ name: Build Demo Missions (packed) # Controls when the action will run. Workflow runs when manually triggered using the UI # or API. on: - schedule: - - cron: '0 7 * * 6' + #schedule: + # - cron: '0 7 * * 6' - push: - branches: - - master - - develop + #push: + # branches: + # - master + # - develop workflow_dispatch: inputs: diff --git a/.scripts/UpdateMoose.py b/.scripts/UpdateMoose.py new file mode 100644 index 0000000000..06f39b8927 --- /dev/null +++ b/.scripts/UpdateMoose.py @@ -0,0 +1,109 @@ +""" +This script finds all miz files and updates the contained Moose.lua from a given one. +It also extracts the contained mission script and places it next to the miz file. +Here we assume that the stem of the file name is the same as the directory name, e.g. +"Auftrag - 10 - Arty.lua" if the miz file name is "Auftrag - 10 - Arty.miz" + +This script is supposed to be run from, e.g., github actions when a new demo mission is +uploaded. +""" + +from multiprocessing.util import is_exiting +from pathlib import Path +from zipfile import ZipFile +from shutil import rmtree, copy +import argparse + +def update(f: Path, MooseLua: Path, Temp: Path): + """ + Update the Moose.lua file in given file. + """ + # Print file name. + print(f"Updating file: {f}") + + # Extract all the contents of zip file in different directory + with ZipFile(f, mode='r') as miz: + miz.extractall(Temp) + + # Folder where script is located + ScriptDir=Temp / "l10n/DEFAULT/" + + # Script file. + ScriptFile=ScriptDir / Path(f.stem + ".lua") + + #Copy script file to directory. + if ScriptFile.is_file(): + print(f"Copying script file {ScriptFile} to {f.parent}") + copy(ScriptFile, f.parent) + else: + print(f"Warning: expected script file {ScriptFile} does NOT exist in miz file!") + + # Copy Moose.lua to temp dir. + copy(MooseLua, ScriptDir/"Moose.lua") + + # Create new miz file + with ZipFile(f, mode='w') as archive: + for file_path in Temp.rglob("*"): + archive.write(file_path, arcname=file_path.relative_to(Temp)) + + # Remove temp dir. + try: + rmtree(Temp) + except: + pass + + # Debug info. + if False: + with ZipFile(f, mode='r') as zipObj: + zipObj.printdir() + #for filename in zipObj.namelist(): + # print(filename) + + # Done. + print("--------------") + +if __name__ == '__main__': + + # Command line argument parser. + parser = argparse.ArgumentParser(description='Blabla.') + + # Add argument for Moose path. + parser.add_argument('MoosePath', metavar='moose', type=str, help='path to Moose.lua file', default="./") + + # Add argument for Moose path. + parser.add_argument('MissionPath', metavar='missions', type=str, help='path to missions', default="./") + + + # Execute the parse_args() method + args = parser.parse_args() + + # Path to Moose.lua + Moose=Path(args.MoosePath) + + # Moose.lua file + MooseLua=Moose/"Moose_.lua" + + # Check that Moose.lua exists + if MooseLua.exists(): + print("Moose_.lua exists") + with open(MooseLua) as myfile: + head = [next(myfile) for x in range(1)] + print(head) + else: + print(f"{MooseLua.name} does not exist") + quit() + + # Path to search for mission (miz) files + Missions=Path(args.MissionPath) + + # Temp directory. + Temp=Path("temp/") + + # Try to delete temp folder. + if Temp.is_dir(): + rmtree(Temp) + + # Loop over all miz files (recursively) + print("\nMiz files:\n----------") + for f in Missions.rglob("*.miz"): + update(f, MooseLua, Temp)