name: compare-nupkg
description: "Compare the contents of a nupkg file to an expected value."
inputs:
  lhs:
    description: "Left hand side nupkg."
    required: true
  rhs:
    description: "Right hand side nupkg."
    required: true
  compareNuspec:
    description: "Whether or not to compare the .nuspec file when comparing versions."
    required: true
    default: "false"
  exitOnFail:
    description: "Should the program exit on a failure."
    required: true
    default: "false"
  tmpDir:
    description: "Temporary directory. Default: _tmp"
    required: true
    default: _tmp
outputs:
  success:
    description: "The result of the comparison."
    value: ${{ steps.compare.outputs.success == 'true' && ( inputs.compareNuspec == 'false' || steps.compare-nuspec.outputs.success == 'true' ) }}
runs:
  using: "composite"
  steps:
  - name: "Make temporary directory for lhs extraction"
    id: mktemp-lhs
    uses: act/common/utils/mktemp@master
    with:
      outputType: dir
      tmpDir: ${{ inputs.tmpDir }}
  - name: "Make temporary directory for rhs extraction"
    id: mktemp-rhs
    uses: act/common/utils/mktemp@master
    with:
      outputType: dir
      tmpDir: ${{ inputs.tmpDir }}
  - name: "Extract lhs."
    uses: act/common/utils/extract@master
    with:
      file: ${{ inputs.lhs }}
      outputDir: ${{ steps.mktemp-lhs.outputs.tmp }}
  - name: "Extract rhs."
    uses: act/common/utils/extract@master
    with:
      file: ${{ inputs.rhs }}
      outputDir: ${{ steps.mktemp-rhs.outputs.tmp }}
  - name: "Disassemble lhs dlls."
    uses: act/common/mono/ikdasm-files@master
    with:
      directory: ${{ steps.mktemp-lhs.outputs.tmp }}/lib
      deleteAssemblies: true
      removeComments: true
  - name: "Disassemble rhs dlls."
    uses: act/common/mono/ikdasm-files@master
    with:
      directory: ${{ steps.mktemp-rhs.outputs.tmp }}/lib
      deleteAssemblies: true
      removeComments: true
  - name: "Get shasum for lhs."
    id: lhs-shasum
    uses: act/common/utils/shasum-files@master
    with:
      directory: ${{ steps.mktemp-lhs.outputs.tmp }}/lib
      pattern: "*"
  - name: "Get shasum for rhs."
    id: rhs-shasum
    uses: act/common/utils/shasum-files@master
    with:
      directory: ${{ steps.mktemp-rhs.outputs.tmp }}/lib
      pattern: "*"
  - name: "Compare lhs and rhs."
    id: compare
    uses: act/common/utils/compare@master
    with:
      expected: ${{ steps.lhs-shasum.outputs.sums }}
      actual: ${{ steps.rhs-shasum.outputs.sums }}
      exitOnFail: ${{ inputs.exitOnFail }}
  #TODO: Use a yq expression to remove the git sha and version before comparing.
  - name: "Compare nuspec files."
    id: compare-nuspec
    if: ${{ inputs.compareNuspec == 'true' }}
    uses: act/common/utils/compare-files@master
    with:
      expected: ${{ steps.mktemp-lhs.outputs.tmp }}
      expectedPattern: /*.nuspec
      actual: ${{ steps.mktemp-rhs.outputs.tmp }}
      actualPattern: /*.nuspec
      exitOnFail: ${{ inputs.exitOnFail }}
  - name: "Take ownership of the artifacts."
    uses: act/common/utils/chown@master
    with:
      file: ${{ inputs.tmpDir }}
  - name: "Remove temporary files."
    run: rm -rf "${{ inputs.tmpDir }}"
    shell: bash
