name: CD

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v2
    - name: Build
      id: base_init
      run: |
        $(new-object System.Net.WebClient).DownloadFile("https://www.github.com/JFronny/UpTool2/releases/latest/download/Tools.zip", "$($(pwd).Path)\Tools.zip")
        & "C:\Program Files\7-Zip\7z.exe" x .\Tools.zip
        rm Tools.zip
        rm Install.bat
        rm Remove.bat
        dotnet restore
        & "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe" -noLogo -verbosity:m -p:Configuration=Release
        cp *\bin\Release\net461\package.zip .
        [timespan]$span = [System.DateTime]::Now - [datetime]'01/01/2000'
        [version]$ver = [version]("1.0." + $span.Days.ToString() + "." + ($span.Seconds / 2).ToString())
        $asmver = $ver.ToString()
        echo "::set-output name=vers::$asmver"
    - name: Create Release
      id: create_release
      uses: actions/create-release@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ steps.base_init.outputs.vers }}
        release_name: Release ${{ steps.base_init.outputs.vers }}
        draft: false
        prerelease: false
    - name: Upload Release Asset
      id: upload_release_asset
      uses: actions/upload-release-asset@v1.0.2
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: ./package.zip
        asset_name: package.zip
        asset_content_type: application/zip
    - name: Generate XML
      run: |
        $asm = $([Reflection.Assembly]::LoadFile($(Get-Item $(Resolve-Path *\bin\Release\net461\*.exe).Path).FullName))
        [System.XML.XMLDocument]$xml=New-Object System.XML.XMLDocument
        [System.XML.XMLElement]$app=$xml.CreateElement("app")
        $xml.appendChild($app)
        $app.appendChild($xml.CreateElement("Name")).InnerText = $asm.GetName().Name
        $app.appendChild($xml.CreateElement("Description")).InnerText = "A basic GUI for multiple common programming languages including c# and HTML"
        $app.appendChild($xml.CreateElement("Version")).InnerText = "${{ steps.base_init.outputs.vers }}"
        $app.appendChild($xml.CreateElement("ID")).InnerText = "63d64672-3ca3-4760-96a3-443b262d326f"
        $app.appendChild($xml.CreateElement("File")).InnerText = "${{ steps.upload_release_asset.outputs.browser_download_url }}"
        $app.appendChild($xml.CreateElement("Hash")).InnerText = $(Get-FileHash .\package.zip).Hash
        $app.appendChild($xml.CreateElement("MainFile")).InnerText = $file.Name
        echo NULL > app.xml
        $xml.save($(gi .\app.xml).Fullname)
    - name: Upload XML
      uses: actions/upload-release-asset@v1.0.2
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: ./app.xml
        asset_name: app.xml
        asset_content_type: text/xml
