variables:
 NUGET_PATH: 'C:\Tools\nuget.exe'
 MSBUILD_PATH: 'C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe'
 ASPNET_MERGE_PATH: 'C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools'
 EXE_RELEASE_FOLDER: 'c:\Projetcs\Compiled\GPW\Release'
 DEPLOY_FOLDER: 'c:\Projects\Deploy\GPW\Builds'
 APP_NAME: 'EgtBEAMWALL'
 APP_NAMEG: 'EgtBEAMWALL'
 NEW_REL: ''
 VERS_RD: 'Release'
 ARCH: 'x86'
 ARCH64: 'x64'
 OUTPUT_DIR: ''
 NET_SHARE: '\\10.74.82.201\Artifacts\EgtProg\EgtBEAMWALL'
 NET_USER: 'steamw\steamware'
 
# helper x mount cartella Artifacts su truenas
.ReplicaCompiled: &ReplicaCompiled
  - |
    rm -Recurse Z:\$CI_COMMIT_BRANCH\$env:ARCH\*.*
    rm -Recurse Z:\$CI_COMMIT_BRANCH\$env:ARCH\*.*
    ROBOCOPY /E $env:APP_NAME\bin\$env:ARCH\$env:VERS_RD Z:\$CI_COMMIT_BRANCH\$env:ARCH /XF *.xml /XF *.pdb
    ROBOCOPY /E $env:APP_NAME2\bin\$env:ARCH\$env:VERS_RD Z:\$CI_COMMIT_BRANCH\$env:ARCH /XF *.xml /XF *.pdb
    SLEEP 2
    net use Z: /delete

.ReplicaCompileds2: &ReplicaCompileds2
  - |
    ROBOCOPY /E $env:APP_NAME\bin\$env:ARCH64\$env:VERS_RD Z:\$CI_COMMIT_BRANCH\$env:ARCH64 /XF *.xml /XF *.pdb
    ROBOCOPY /E $env:APP_NAME2\bin\$env:ARCH64\$env:VERS_RD Z:\$CI_COMMIT_BRANCH\$env:ARCH64 /XF *.xml /XF *.pdb
    SLEEP 2
    net use Z: /delete

.MakeFolderM: &MakeFolderM
  - |
    net use Z: /delete
    new-item c:\EgtProg\EgtBEAMWALL -itemtype directory
    net use Z: $env:NET_SHARE 
    
.nuget-fix: &nuget-fix
  - |
    $hasSource = C:\Tools\nuget.exe sources list | find "`"Steamware Nexus`"" /C
    if ($hasSource -eq 0) {
        C:\Tools\nuget.exe sources Add -Name "`"Steamware Nexus`"" -Source https://nexus.steamware.net/repository/nuget-group -username "`"nugetUser`"" -password "`"$NEXUS_PASSWD`""
    } else {
        C:\Tools\nuget.exe sources Update -Name "`"Steamware Nexus`"" -Source https://nexus.steamware.net/repository/nuget-group -username "`"nugetUser`"" -password "`"$NEXUS_PASSWD`""
    }
    echo $hasSource


# helper creazione hash files
.hashBuild: &hashBuild
   - |
      $Target = "Releases\" + $CI_COMMIT_BRANCH + "\" + $env:APP_NAME + ".zip"
      $MD5 = Get-FileHash $Target -Algorithm MD5
      $SHA1 = Get-FileHash $Target -Algorithm SHA1
      New-Item $Target".md5"
      New-Item $Target".sha1"
      $MD5.Hash | Set-Content -Path $Target".md5"
      $SHA1.Hash | Set-Content -Path $Target".sha1"

      echo "Created HASH files for $Target"      

.cleanup-dir: &cleanup-dir
   - |
      rm $env:APP_NAME\bin\$env:ARCH\$env:VERS_RD\*.xml
      rm $env:APP_NAME\bin\$env:ARCH\$env:VERS_RD\*.pdb
      rm $env:APP_NAME\bin\$env:ARCH64\$env:VERS_RD\*.xml
      rm $env:APP_NAME\bin\$env:ARCH64\$env:VERS_RD\*.pdb

stages:
 - build
 - deploy
 
X86:build:
 stage: build
 tags:
   - win
 only:
   - main
   - master
   - develop
 variables:
   APP_NAME: EgtBEAMWALL.Supervisor
   APP_NAME2: EgtBEAMWALL.ViewerOptimizer
   VERS_RD: 'Release'
   ARCH: 'x86'
   ARCH64: 'x64'
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore EgtBEAMWALL.sln -verbosity quiet'
   - *MakeFolderM
 script:  
    - '& "$env:MSBUILD_PATH" "$env:APP_NAME\$env:APP_NAME.vbproj" -target:Build /p:Configuration=$env:VERS_RD /p:Platform="$env:ARCH" /p:OutputPath=bin/$env:ARCH/$env:VERS_RD/ /verbosity:minimal /m'
    - '& "$env:MSBUILD_PATH" "$env:APP_NAME2\$env:APP_NAME2.vbproj" -target:Build /p:Configuration=$env:VERS_RD /p:Platform="$env:ARCH" /p:OutputPath=bin/$env:ARCH/$env:VERS_RD/ /verbosity:minimal /m'
    - *ReplicaCompiled
   

X64:build:
 stage: build
 tags:
   - win
 only:
   - main
   - master
   - develop
 variables:
   APP_NAME: EgtBEAMWALL.Supervisor
   APP_NAME2: EgtBEAMWALL.ViewerOptimizer
   VERS_RD: 'Release'
   ARCH: 'x86'
   ARCH64: 'x64'
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore EgtBEAMWALL.sln -verbosity quiet'
   - *MakeFolderM
 script:  
    - '& "$env:MSBUILD_PATH" "$env:APP_NAME\$env:APP_NAME.vbproj" -target:Build /p:Configuration=$env:VERS_RD /p:Platform="$env:ARCH64" /p:OutputPath=bin/$env:ARCH64/$env:VERS_RD/ /verbosity:minimal /m'
    - '& "$env:MSBUILD_PATH" "$env:APP_NAME2\$env:APP_NAME2.vbproj" -target:Build /p:Configuration=$env:VERS_RD /p:Platform="$env:ARCH64" /p:OutputPath=bin/$env:ARCH64/$env:VERS_RD/ /verbosity:minimal /m'
    - *ReplicaCompileds2

