name: unity-project
description: "Run a Unity command using a project's version and a build target."
inputs:
  projectPath:
    description: "Path to the Unity project."
    required: true
    default: "."
  platform:
    description: "Unity Platform. Options: windows, windows32bit, mac, linux, android"
    required: true
  serial:
    description: "Unity license serial number. Or 'personal' for a personal license."
    required: true
    default: personal
  email:
    description: "Unity email."
    required: true
  password:
    description: "Unity password."
    required: true
  command:
    description: "Unity command to run."
    required: false
  unityBuilder:
    description: "Whether or not to use the UnityBuilder instead of a Unity command."
    required: false
  noGraphics:
    description: "Whether or not to use the graphics device when running Unity."
    required: false
    default: "true"
  sshPublicKey:
    description: "Public SSH key to use for git package restoration."
    required: false
  sshPrivateKey:
    description: "Private SSH key to use for git package restoration."
    required: false
  downloadKeystore:
    description: "Whether or not to download the keystore for Android builds. Defaults to true if the platform is android."
    required: false
    default: ""
runs:
  using: "composite"
  steps:
  - name: "Replace spaces in project path."
    id: path
    run: |
      PROJECT_PATH=${{ inputs.projectPath }}
      PROJECT_PATH=${PROJECT_PATH// /\\ }
      echo "projectPath=$PROJECT_PATH" >> "$GITHUB_OUTPUT"

      DOWNLOAD_KEYSTORE=${{ inputs.downloadKeystore }}
      if [ -z "$DOWNLOAD_KEYSTORE" ]; then
        shopt -s nocasematch
        if [[ "${{ inputs.platform }}" = "android" ]]; then
          DOWNLOAD_KEYSTORE=true
        else
          DOWNLOAD_KEYSTORE=false
        fi
        shopt -u nocasematch
      fi
      echo "downloadKeystore=$DOWNLOAD_KEYSTORE" >> "$GITHUB_OUTPUT"
    shell: bash
  - name: "Download Keystore."
    if: ${{ steps.path.outputs.downloadKeystore == 'true' }}
    uses: act/common/minio/mc-cp@master
    with:
      alias: minio
      target: minio/artifacts/studiowhy.keystore
      dest: ${{ github.workspace }}
      accessKey: ${{ env.MINIO_ACCESS_KEY }}
      secretKey: ${{ env.MINIO_SECRET_KEY }}
      url: ${{ env.MINIO_URL }}
  - name: "Get Unity Version."
    id: getVersion
    uses: act/unity/unity-get-version@master
    with:
      projectPath: ${{ steps.path.outputs.projectPath }}
  - name: "Get Unity buildTarget."
    id: getTarget
    uses: act/unity/unity-get-buildtarget@master
    with:
      platform: ${{ inputs.platform }}
  - name: "Restore NugetForUnity packages if they exist."
    uses: act/common/dotnet/dotnet-nugetforunity-restore@master
    with:
      projectPath: ${{ steps.path.outputs.projectPath }}
  - name: "Run Unity command."
    uses: act/unity/unity-command@master
    with:
      platform: ${{ inputs.platform }}
      version: ${{ steps.getVersion.outputs.projectVersion }}
      serial: ${{ inputs.serial }}
      email: ${{ inputs.email }}
      password: ${{ inputs.password }}
      sshPublicKey: ${{ inputs.sshPublicKey }}
      sshPrivateKey: ${{ inputs.sshPrivateKey }}
      noGraphics: ${{ inputs.noGraphics }}
      command: -projectPath ${{ steps.path.outputs.projectPath }} -buildTarget ${{ steps.getTarget.outputs.buildTarget }} ${{ inputs.command }}
      unityBuilder: ${{ inputs.unityBuilder }}

