name: unity-command
description: "Run a Unity command using a specified version of Unity."
inputs:
  platform:
    description: "Unity Platform. Options: windows, windows32bit, mac, linux, android"
    required: true
  version:
    description: "Unity Version."
    required: true
  imageTag:
    description: "Tag to use for the temporary version of Unity. Must refer to an existing public Docker image. Must be the same is in the Dockerfile of the called action."
    required: true
    default: unityci/base:latest
  serial:
    description: "Unity license serial number. Or 'public' for a public license."
    required: true
    default: public
  email:
    description: "Unity email."
    required: true
  password:
    description: "Unity password."
    required: true
  command:
    description: "Unity command to run."
    required: false
  noGraphics:
    description: "Whether or not to use the graphics device when running Unity."
    required: false
    default: "true"
  catchErrors:
    description: "Whether or not errors should be handled."
    required: false
  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
  unityBuilder:
    description: "Whether or not to use the UnityBuilder instead of a Unity command."
    required: false
runs:
  using: "composite"
  steps:
  - name: "Print Inputs"
    run: |
      NAME="Unity Command"
      echo "::group::$NAME - Inputs"
      echo "${{ toJSON(inputs) }}"
      echo "::endgroup::"
    shell: bash
  - name: "Get Unity container name."
    id: getContainer
    uses: act/unity/unity-get-container@master
    with:
      version: ${{ inputs.version }}
      platform: ${{ inputs.platform }}
  - name: "Pull Unity container."
    run: |
      CONTAINER="${{ steps.getContainer.outputs.container }}"

      CACHED_CONTAINER="$CONTAINER-activated"
      # Activate the license on build.
      # DOCKER_BUILDKIT=1 \
      # SERIAL="${{ inputs.serial }}" USERNAME="${{ inputs.email }}" PASSWORD="${{ inputs.password }}" \
      # docker build --secret id=SERIAL,env=SERIAL --secret id=USERNAME,env=USERNAME --secret id=PASSWORD,env=PASSWORD \
      # -t $CACHED_CONTAINER --build-arg IMAGE=$CONTAINER ${{ github.action_path }}
      docker build -t $CACHED_CONTAINER --build-arg IMAGE=$CONTAINER ${{ github.action_path }}

      docker tag $CACHED_CONTAINER ${{ inputs.imageTag }}
    shell: bash
  - name: "Get Unity Command."
    id: command
    run: |
      COMMAND="${{ inputs.command }}"
      NO_GRAPHICS="${{ inputs.noGraphics }}"

      if [[ "$NO_GRAPHICS" == "true" ]]; then
        COMMAND="-nographics $COMMAND"
      fi
      echo "command=$COMMAND" >> "$GITHUB_OUTPUT"
    shell: bash
  - name: "Run Unity command."
    uses: act/unity/unity@master
    env:
      SSH_PUBLIC_KEY: ${{ inputs.sshPublicKey }}
      SSH_PRIVATE_KEY: ${{ inputs.sshPrivateKey }}
      CATCH_ERRORS: ${{ inputs.catchErrors }}
    with:
      serial: ${{ inputs.serial }}
      # serial: "activated"
      email: ${{ inputs.email }}
      password: ${{ inputs.password }}
      command: ${{ steps.command.outputs.command }}
      unityBuilder: ${{ inputs.unityBuilder }}

