name: nuget-install
description: "Download the desired .nupkg file."
inputs:
  name:
    description: "Name of the .nupkg file."
    required: true
  version:
    description: "Version of the .nupkg file."
    required: false
    default: ""
  prerelease:
    description: "Install prerelease packages. Values: true, false."
    required: false
    default: "false"
  outputDirectory:
    description: "Directory for the output file."
    required: true
    default: "."
  directDownload:
    description: "Download the .nupkg file directly from the source."
    required: true
    default: "true"
  excludeVersion:
    description: "Exclude the version when downloading the .nuget package."
    required: true
    default: "false"
  nupkgOnly:
    description: "Only output the .nupkg file directly to the output directory."
    required: true
    default: "false"
  additionalArgs:
    description: "Additional arguments to use when dowloading the .nupkg file."
    required: false
  source:
    description: "Name of the source to use."
    required: true
    default: lewdorg
  url:
    description: "Url of the NuGet repository."
    required: true
    default: ${{ github.server_url }}/_registry/nuget/${{ github.repository_owner }}/index.json
  username:
    description: "Username for the nuget repository to search."
    required: true
    default: ${{ github.actor }}
  password:
    description: "Password or ApiKey of the nuget repository to search."
    required: true
    default: ${{ github.token }}
  tmpDir:
    description: "Temporary directory. Default: _tmp"
    required: true
    default: _tmp
  cleanTmp:
    description: "Remove the temporary directory after use. Default: true"
    required: true
    default: "true"
outputs:
  nupkg:
    description: "The path to the .nupkg file."
    value: ${{ steps.move.outputs.nupkg }}
runs:
  using: "composite"
  steps:
  - name: "Make temporary folder if we are using .nupkg only."
    id: mktmp
    uses: act/common/utils/mktemp@master
    with:
      tmpDir: ${{ inputs.tmpDir }}
      outputType: dir
  - name: "Create Output Directory"
    run: |
      OUTPUT_DIR="${{ inputs.outputDirectory }}"
      mkdir "$OUTPUT_DIR" -p
    shell: bash
  - name: "Build nuget command."
    id: command
    run: |
      COMMAND="install ${{ inputs.name }} -OutputDirectory \"${{ steps.mktmp.outputs.tmp }}\" ${{ inputs.additionalArgs }}"
      VERSION="${{ inputs.version }}"
      PRERELEASE="${{ inputs.prerelease }}"
      DIRECT="${{ inputs.directDownload }}"

      if [ "$DIRECT" == "true" ]; then
        COMMAND="$COMMAND -DirectDownload"
      fi
      if [ -n "$VERSION" ]; then
        COMMAND="$COMMAND -Version $VERSION"
      fi
      if [ "$PRERELEASE" == "true" ]; then
        COMMAND="$COMMAND -Prerelease"
      fi
      echo "command=$COMMAND" >> "$GITHUB_OUTPUT"
    shell: bash
  - name: "Download the .nupkg file."
    uses: act/common/nuget/nuget@master
    with:
      command: ${{ steps.command.outputs.command }}
      username: ${{ inputs.username }}
      password: ${{ inputs.password }}
      source: ${{ inputs.source }}
      url: ${{ inputs.url }}
  - name: "Take ownership of the artifacts."
    uses: act/common/utils/chown@master
    with:
      file: ${{ steps.mktmp.outputs.tmp }}
  - name: "Copy contents to output folder."
    id: move
    run: |
      OUTPUT_DIR="${{ inputs.outputDirectory }}"
      TMP_DIR="${{ steps.mktmp.outputs.tmp }}"
      EXCLUDE_VERSION="${{ inputs.excludeVersion }}"
      NUPKG_ONLY="${{ inputs.nupkgOnly }}"

      if [ "$EXCLUDE_VERSION" == "true" ]; then
        NUPKG_NAME="${{ inputs.name }}.nupkg"
      else
        # Get the subdirectory name
        OUTDIR=($TMP_DIR/*)
        BASE_NAME=$(basename "$OUTDIR")
        NUPKG_NAME="${BASE_NAME}.nupkg"
      fi

      if [ "$NUPKG_ONLY" == "true" ]; then
        mv "$OUTDIR/$NUPKG_NAME" "$OUTPUT_DIR"
        echo "nupkg=$OUTPUT_DIR/$NUPKG_NAME" >> "$GITHUB_OUTPUT"
      else
        mv "$TMP_DIR"/* "$OUTPUT_DIR"
        echo "nupkg=$OUTPUT_DIR/$OUTDIR/$NUPKG_NAME" >> "$GITHUB_OUTPUT"
      fi
    shell: bash
  - name: "Remove temporary files."
    if: ${{ inputs.cleanTmp == 'true' }}
    run: rm -rf "${{ inputs.tmpDir }}"
    shell: bash