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.7.2 Tools'
 EXE_RELEASE_FOLDER: 'c:\Projetcs\Compiled\GPW\Release'
 DEPLOY_FOLDER: 'c:\Projects\Deploy\GPW\Builds'
 VERS_MAIN: '4.1'
 NEW_REL: ''
 NEW_DEB: ''
 NEXUS_PATH: 'GMW'
 PROJ_NAME: 'GMW'
 APP_NAME: 'GMW'
 APP_CONF: 'Release'

# VARIABILI
# Tema numero versione: si potrebbe usare la variabile CI_COMMIT_TAG con $env:CI_COMMIT_TAG, in questo caso SOLO SE c'è un tag sistema versione...
# Tema compilazione x branch: $CI_COMMIT_BRANCH == "my-branch" potrebbe essere condizione if x fare alcuni sscript...
# rif: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html

# Da approfondire:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/dotNET.gitlab-ci.yml
# https://www.google.com/search?q=gitlab+variable+year+month&rlz=1C1GCEA_enIT945IT945&oq=gitlab+variable+year+month&aqs=chrome..69i57j69i64.5262j0j7&sourceid=chrome&ie=UTF-8
# https://docs.gitlab.com/ee/ci/examples/README.html
# https://docs.gitlab.com/ee/ci/quick_start/index.html
# https://docs.gitlab.com/ee/ci/yaml/

# helper x fix pacchetti nuget da repo locale nexus.steamware.net
.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 http://nexus.steamware.net/repository/nuget-group -username "`"nugetUser`"" -password "`"viaDante16`""
      } else {
         C:\Tools\nuget.exe sources Update -Name "`"Steamware Nexus`"" -Source http://nexus.steamware.net/repository/nuget-group -username "`"nugetUser`"" -password "`"viaDante16`""
      }
      echo $hasSource

# helper x fix version number
.version-fix: &version-fix
   - |
      $env:NEW_REL = $env:VERS_MAIN+"."+(get-date –format yyMM)+"."+(get-date –format ddHH)
      echo "Set vers: $env:NEW_REL"
      $contenuto = Get-Content -path 'VersGen\GMW.cs' -Raw
      $newContenuto = $contenuto -replace '1.0.0.0', $env:NEW_REL
      $newContenuto | Set-Content -Path 'VersGen\GMW.cs'

# helper creazione hash files x IIS
.hashBuild: &hashBuild
   - |
      $Target = $env:PROJ_NAME + "\Release\" + $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" 

# helper x send su NEXUS x pack
.nexusUpload: &nexusUpload
   - |
      Set-Alias mCurl  C:\Windows\system32\curl.exe
      $currentDate = get-date -format yyMM;
      $currentTime = get-date -format ddHH;
      $VersNumb = $env:NEW_REL
      echo "Curr Version: $VersNumb" 
      $FileManIn=$env:PROJ_NAME +"\Resources\manifest-original.xml"
      $FileManOut=$env:PROJ_NAME +"\Resources\manifest.xml"
      $FileCLogIn=$env:PROJ_NAME +"\Resources\ChangeLog-original.html"
      $FileCLogOut=$env:PROJ_NAME +"\Resources\ChangeLog.html"
      echo "Manifest path: $FileManOut"     
      echo "ChangeLog path: $FileCLogOut"     
      if($CI_COMMIT_BRANCH -eq "master")
      {
        $version = "stable"
      }
      else
      {
        $version = "unstable"
      }
      $manData = Get-Content $FileManIn
      $manData = $manData -replace "1.0.0.0", $VersNumb
      $manData = $manData -replace "{{DIRNAME}}", $env:NEXUS_PATH
      $manData = $manData -replace "{{BRANCHNAME}}", "$version/LAST"
      $manData = $manData -replace "{{PACKNAME}}", $env:APP_NAME
      Set-Content -Path $FileManOut -Value $manData
      $clogData = Get-Content $FileCLogIn
      $clogData = $clogData -replace "{{CURRENT-REL}}", $VersNumb
      Set-Content -Path $FileCLogOut -Value $clogData
      $File2Send = Get-ChildItem($env:PROJ_NAME + "\Release\*")
      ForEach ($File in $File2Send) {
        $FileName = Split-Path $File -leaf
        echo "mCurl -s -u GitLab:$NEXUS_PASSWD --upload-file $File https://nexus.steamware.net/repository/SWS/$env:NEXUS_PATH/$version/LAST/$FileName"
        mCurl -s -u GitLab:$NEXUS_PASSWD --upload-file $File https://nexus.steamware.net/repository/SWS/$env:NEXUS_PATH/$version/LAST/$FileName        
        echo "mCurl -s -u GitLab:$NEXUS_PASSWD --upload-file $File https://nexus.steamware.net/repository/SWS/$env:NEXUS_PATH/$version/ARCHIVE/$VersNumb/$FileName"
        mCurl -s -u GitLab:$NEXUS_PASSWD --upload-file $File https://nexus.steamware.net/repository/SWS/$env:NEXUS_PATH/$version/ARCHIVE/$VersNumb/$FileName
      }
      echo "mCurl -s -u GitLab:$NEXUS_PASSWD --upload-file $FileManOut https://nexus.steamware.net/repository/SWS/$env:NEXUS_PATH/$version/LAST/manifest.xml"
      mCurl -s -u GitLab:$NEXUS_PASSWD --upload-file $FileManOut https://nexus.steamware.net/repository/SWS/$env:NEXUS_PATH/$version/LAST/manifest.xml
      echo "mCurl -s -u GitLab:$NEXUS_PASSWD --upload-file $FileCLogOut https://nexus.steamware.net/repository/SWS/$env:NEXUS_PATH/$version/LAST/ChangeLog.html"
      mCurl -s -u GitLab:$NEXUS_PASSWD --upload-file $FileCLogOut https://nexus.steamware.net/repository/SWS/$env:NEXUS_PATH/$version/LAST/ChangeLog.html
   


stages:
 - build
 - staging
 - deploy
 - installer

GMW:build:
 stage: build
 tags:
   - win
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW.sln -verbosity quiet'
   - *version-fix
 script:    
   - '& "$env:MSBUILD_PATH" GMW/GMW.csproj -target:Build /p:Configuration=Release /p:Platform="Any CPU" /p:OutputPath=bin/ /verbosity:minimal /m'

TERM:build:
 stage: build
 tags:
   - win
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW_Terminus.sln -verbosity quiet'
   - *version-fix
 script:
   - '& "$env:MSBUILD_PATH" GMW_Term/GMW_Term.csproj -target:Build /p:Configuration=Release /p:Platform="Any CPU" /p:OutputPath=bin/ /verbosity:minimal /m'

GMW_TK:staging:
 stage: staging
 tags:
   - win
 rules:
   - if: '$CI_COMMIT_BRANCH == "develop"'
 variables:
   APP_NAME: GMW_TK
   PROJ_NAME: GMW
   NEXUS_PATH: GMW
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW.sln -verbosity quiet'  
   - *version-fix
 script: 
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /p:DeployOnBuild=true /p:Targets=Publish /p:PublishProfile=IIS01_TK.pubxml /p:RunCodeAnalysis=false /p:Configuration=Release /p:username=jenkins /p:Password=viadante16 /p:AllowUntrustedCertificate=true /p:OutputPath=bin/ /verbosity:minimal GMW/GMW.csproj'
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /T:Package /P:Configuration=TK_test /p:PublishProfile=TK_test.pubxml /p:RunCodeAnalysis=false /p:PackageLocation=Release/$env:APP_NAME.zip /p:DeployIisAppPath="Default Web Site/GMW" /p:PackageAsSingleFile=True /p:OutputPath=bin/ GMW/GMW.csproj'
   - *hashBuild
   - *nexusUpload
 needs: ["GMW:build"]
 
GMW_SP:staging:
 stage: staging
 tags:
   - win
 rules:
   - if: '$CI_COMMIT_BRANCH == "develop"'
 variables:
   APP_NAME: GMW_SP
   PROJ_NAME: GMW
   NEXUS_PATH: GMW
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW.sln -verbosity quiet'  
   - *version-fix
 script: 
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /p:DeployOnBuild=true /p:Targets=Publish /p:PublishProfile=IIS01_SP.pubxml /p:RunCodeAnalysis=false /p:Configuration=Release /p:username=jenkins /p:Password=viadante16 /p:AllowUntrustedCertificate=true /p:OutputPath=bin/ /verbosity:minimal GMW/GMW.csproj'
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /T:Package /P:Configuration=SP_test /p:PublishProfile=SP_test.pubxml /p:RunCodeAnalysis=false /p:PackageLocation=Release/$env:APP_NAME.zip /p:DeployIisAppPath="Default Web Site/GMW" /p:PackageAsSingleFile=True /p:OutputPath=bin/ GMW/GMW.csproj'
   - *hashBuild
   - *nexusUpload
 needs: ["GMW:build"]

TERM_TK:staging:
 stage: staging
 tags:
   - win
 rules:
   - if: '$CI_COMMIT_BRANCH == "develop"'
 variables:
   APP_NAME: GMWT_TK
   PROJ_NAME: GMW_Term
   NEXUS_PATH: GMWT
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW_Terminus.sln -verbosity quiet'
   - *version-fix
 script: 
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /p:DeployOnBuild=true /p:Targets=Publish /p:PublishProfile=IIS01_TK.pubxml /p:RunCodeAnalysis=false /p:Configuration=Release /p:username=jenkins /p:Password=viadante16 /p:AllowUntrustedCertificate=true /p:OutputPath=bin/ /verbosity:minimal GMW_Term/GMW_Term.csproj'
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /T:Package /P:Configuration=TK_test /p:PublishProfile=TK_test.pubxml /p:RunCodeAnalysis=false /p:PackageLocation=Release/$env:APP_NAME.zip /p:DeployIisAppPath="Default Web Site/GMWT" /p:PackageAsSingleFile=True /p:OutputPath=bin/ GMW_Term/GMW_Term.csproj'
   - *hashBuild
   - *nexusUpload
 needs: ["TERM:build"]

TERM_SP:staging:
 stage: staging
 tags:
   - win
 rules:
   - if: '$CI_COMMIT_BRANCH == "develop"'
 variables:
   APP_NAME: GMWT_SP
   PROJ_NAME: GMW_Term
   NEXUS_PATH: GMWT
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW_Terminus.sln -verbosity quiet'
   - *version-fix
 script: 
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /p:DeployOnBuild=true /p:Targets=Publish /p:PublishProfile=IIS01_SP.pubxml /p:RunCodeAnalysis=false /p:Configuration=Release /p:username=jenkins /p:Password=viadante16 /p:AllowUntrustedCertificate=true /p:OutputPath=bin/ /verbosity:minimal GMW_Term/GMW_Term.csproj'
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /T:Package /P:Configuration=SP_test /p:PublishProfile=SP_test.pubxml /p:RunCodeAnalysis=false /p:PackageLocation=Release/$env:APP_NAME.zip /p:DeployIisAppPath="Default Web Site/GMWT" /p:PackageAsSingleFile=True /p:OutputPath=bin/ GMW_Term/GMW_Term.csproj'
   - *hashBuild
   - *nexusUpload
 needs: ["TERM:build"]

GMW_TK:deploy:
 stage: deploy
 tags:
   - win
 rules:
   - if: '$CI_COMMIT_BRANCH == "master"'
 variables:
   APP_NAME: GMW
   PROJ_NAME: GMW
   NEXUS_PATH: GMW
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW.sln -verbosity quiet'  
   - *version-fix
 script: 
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /p:DeployOnBuild=true /p:Targets=Publish /p:PublishProfile=IIS02_TK.pubxml /p:RunCodeAnalysis=false /p:Configuration=Release /p:username=jenkins /p:Password=viadante16 /p:AllowUntrustedCertificate=true /p:OutputPath=bin/ /verbosity:minimal GMW/GMW.csproj'
 needs: ["GMW:build"]
 
GMW_SP:deploy:
 stage: deploy
 tags:
   - win
 rules:
   - if: '$CI_COMMIT_BRANCH == "master"'
 variables:
   APP_NAME: GMW
   PROJ_NAME: GMW
   NEXUS_PATH: GMW
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW.sln -verbosity quiet'  
   - *version-fix
 script: 
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /p:DeployOnBuild=true /p:Targets=Publish /p:PublishProfile=IIS02_SP.pubxml /p:RunCodeAnalysis=false /p:Configuration=Release /p:username=jenkins /p:Password=viadante16 /p:AllowUntrustedCertificate=true /p:OutputPath=bin/ /verbosity:minimal GMW/GMW.csproj'
 needs: ["GMW:build"]
 
TERM_TK:deploy:
 stage: deploy
 tags:
   - win
 rules:
   - if: '$CI_COMMIT_BRANCH == "master"'
 variables:
   APP_NAME: GMWT
   PROJ_NAME: GMW_Term
   NEXUS_PATH: GMWT
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW_Terminus.sln -verbosity quiet'
   - *version-fix
 script: 
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /p:DeployOnBuild=true /p:Targets=Publish /p:PublishProfile=IIS02_TK.pubxml /p:RunCodeAnalysis=false /p:Configuration=Release /p:username=jenkins /p:Password=viadante16 /p:AllowUntrustedCertificate=true /p:OutputPath=bin/ /verbosity:minimal GMW_Term/GMW_Term.csproj'
 needs: ["TERM:build"]
  
TERM_SP:deploy:
 stage: deploy
 tags:
   - win
 rules:
   - if: '$CI_COMMIT_BRANCH == "master"'
 variables:
   APP_NAME: GMWT
   PROJ_NAME: GMW_Term
   NEXUS_PATH: GMWT
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW_Terminus.sln -verbosity quiet'
   - *version-fix
 script: 
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /p:DeployOnBuild=true /p:Targets=Publish /p:PublishProfile=IIS02_SP.pubxml /p:RunCodeAnalysis=false /p:Configuration=Release /p:username=jenkins /p:Password=viadante16 /p:AllowUntrustedCertificate=true /p:OutputPath=bin/ /verbosity:minimal GMW_Term/GMW_Term.csproj'
 needs: ["TERM:build"]

GMW_TK:install:
 stage: installer
 tags:
   - win
 rules:
   - if: '$CI_COMMIT_BRANCH == "master"'
 variables:
   APP_NAME: GMW_TK
   PROJ_NAME: GMW
   NEXUS_PATH: GMW
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW.sln -verbosity quiet'  
   - *version-fix
 script: 
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /T:Package /P:Configuration=TK_prod /p:PublishProfile=TK_prod.pubxml /p:RunCodeAnalysis=false /p:PackageLocation=Release/$env:APP_NAME.zip /p:DeployIisAppPath="Default Web Site/GMW" /p:PackageAsSingleFile=True /p:OutputPath=bin/ GMW/GMW.csproj'
   - *hashBuild
   - *nexusUpload
 needs: ["GMW:build"]
 
GMW_SP:install:
 stage: installer
 tags:
   - win
 rules:
   - if: '$CI_COMMIT_BRANCH == "master"'
 variables:
   APP_NAME: GMW_SP
   PROJ_NAME: GMW
   NEXUS_PATH: GMW
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW.sln -verbosity quiet'  
   - *version-fix
 script: 
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /T:Package /P:Configuration=SP_prod /p:PublishProfile=SP_prod.pubxml /p:RunCodeAnalysis=false /p:PackageLocation=Release/$env:APP_NAME.zip /p:DeployIisAppPath="Default Web Site/GMW" /p:PackageAsSingleFile=True /p:OutputPath=bin/ GMW/GMW.csproj'
   - *hashBuild
   - *nexusUpload
 needs: ["GMW:build"]

TERM_TK:install:
 stage: installer
 tags:
   - win
 rules:
   - if: '$CI_COMMIT_BRANCH == "master"'
 variables:
   APP_NAME: GMWT_TK
   PROJ_NAME: GMW_Term
   NEXUS_PATH: GMWT
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW_Terminus.sln -verbosity quiet' 
   - *version-fix
 script: 
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /T:Package /P:Configuration=TK_prod /p:PublishProfile=TK_prod.pubxml /p:RunCodeAnalysis=false /p:PackageLocation=Release/$env:APP_NAME.zip /p:DeployIisAppPath="Default Web Site/GMWT" /p:PackageAsSingleFile=True /p:OutputPath=bin/ GMW_Term/GMW_Term.csproj'
   - *hashBuild
   - *nexusUpload
 needs: ["TERM:build"]

TERM_SP:install:
 stage: installer
 tags:
   - win
 rules:
   - if: '$CI_COMMIT_BRANCH == "master"'
 variables:
   APP_NAME: GMWT_SP
   PROJ_NAME: GMW_Term
   NEXUS_PATH: GMWT
 before_script:
   - *nuget-fix
   - '& "$env:NUGET_PATH" restore GMW_Terminus.sln -verbosity quiet' 
   - *version-fix
 script: 
   -  '& "$env:MSBUILD_PATH" /p:m=8 /p:AspnetMergePath=$env:ASPNET_MERGE_PATH /T:Package /P:Configuration=SP_prod /p:PublishProfile=SP_prod.pubxml /p:RunCodeAnalysis=false /p:PackageLocation=Release/$env:APP_NAME.zip /p:DeployIisAppPath="Default Web Site/GMWT" /p:PackageAsSingleFile=True /p:OutputPath=bin/ GMW_Term/GMW_Term.csproj'
   - *hashBuild
   - *nexusUpload
 needs: ["TERM:build"]
