#SCRIPT AUTO-BUILD # - Author: Nicola Carminati # - Version: 1.0 # - Date: 07/12/2020 # # #--------------------------------------------------------------------------------------------------------------------------- # recupera timestamp x logging function Get-TimeStamp { return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date) } function getFileLog() { return "{0:yyy_MM_dd}_{0:HH_mm}" -f (Get-Date) + "_$pid.log" } function ExecuteLog($txt2log) { Write-Output "-------------------------------------------------------------------------------------------------------------------------------------------------" | Out-File -FilePath "$filelog" -Append Write-Output "$(Get-TimeStamp) $txt2log" | Out-File -FilePath "$filelog" -Append Invoke-Expression "$txt2log 2>&1 | Out-File -FilePath $filelog -Append" if( $LASTEXITCODE -ne 0) { Write-Host "Error during cmmand: $txt2log" -ForegroundColor Red ; Write-Host "Exit...." -ForegroundColor Red ; Set-Location $PSScriptRoot exit } } function askConfirmorDie($message) { if ($silent -eq 0) { while ($confirmation -ne 'y') { $confirmation = Read-Host $message if ($confirmation -eq 'n') { Write-Host "As you like...." -ForegroundColor Red ; Write-Host "Bye...." -ForegroundColor Red ; Set-Location $PSScriptRoot exit } } } } function ExitOk() { Write-Host "Done!" -ForegroundColor Green exit } function ExitERR($msg) { Write-Host "Error: $msg" -ForegroundColor Red Write-Host "Bye...." -ForegroundColor Red ; exit } function getAssemblyInfo($file) { $regex = "\[assembly\: AssemblyVersion\(""([\.\d+]*)""\)\]" $row = ((Get-Content $file ) | Select-String $regex) | Out-String $found1 = $row.IndexOf("""") $found2 = $row.LastIndexOf("""") RETURN $row.substring($found1+1, $found2 - $found1 -1) } function ChangeAssemblyVersion($file,$version) { $regex = "\[assembly\: AssemblyVersion\(""([\.\d+]*)""\)\]" (Get-Content $file) -replace $regex, "[assembly: AssemblyVersion(""$version"")]" | Set-Content $file } function CheckNugetScm() { $url = 'https://repository.scmgroup.com/repository/mconnect-nuget/' $ngt = nuget sources -Format Detailed | Out-String $ind = $ngt.LastIndexOf($url) if($ind -lt 0) { ExitERR("Nuget package Scm not found: $url. Add this Repository `r`n -USR: guest`r`n -PSW: mB6SQvj6`r`n") } } function CheckSingleCommand($cmd) { if ($null -eq (Get-Command $cmd -ErrorAction SilentlyContinue)) { ExitERR("Command ""$cmd"" not found in your PATH") } } function CheckAllCommand() { CheckSingleCommand "git" CheckSingleCommand "nuget" CheckSingleCommand "devenv" CheckSingleCommand "npm" CheckSingleCommand "7z" CheckSingleCommand "iscc" } #--------------------------------------------------------------------------------------------------------------------------- # Main Function #Setup Variables $outputdir = $PSScriptRoot + "\Thermo.Active\bin" $outputServer = $outputdir + "\Thermo.Active.exe" $outputClient = $outputdir + "\Client\Active_Client.exe" $assemblyInfoFile = $PSScriptRoot + "\Thermo.Active\Properties\AssemblyInfo.cs" $installerFilePath = $PSScriptRoot + "\Thermo.Active\SetupActive_Auto.iss" $outBuildPath = "C:\CMS\AutoBuild\" $branch = "master" $version = "" $branchNeedsToBeSetted = 1; $versionNeedsToBeSetted = 1; $zipOutput = 0; $silent = 0; $excludeClient = 0; $excludeServer = 0; $excludeFrontend = 0; $excludeNodeUpdate = 0; $excludeNugetUpdate = 0; $excludeGit = 0; $filelog = $PSScriptRoot + "\" + $filelog $version = getAssemblyInfo $assemblyInfoFile $outBuild = $outBuildPath + $version $outLogs = $outBuildPath + "_Logs\" #Setup Arguments for ( $i = 0; $i -lt $args.count; $i++ ) { if($args[$i] -eq "-branch") { if($args[$i+1].StartsWith("-")) { $err = $args[$i+1] ExitERR "Invalid Branch Name" } else { $branch = $args[$i +1] $i++; $branchNeedsToBeSetted = 0; } } elseif($args[$i] -eq "-version") { if($args[$i+1].StartsWith("-")) { $err = $args[$i+1] ExitERR "Invalid Version Name" } else { $version = $args[$i +1] $outBuild = $outBuildPath + $version $i++; $versionNeedsToBeSetted = 0; } } elseif($args[$i] -eq "-silent") { $silent = 1; } elseif($args[$i] -eq "-portable") { $zipOutput = 1; } elseif($args[$i] -eq "-excludeclient") { $excludeClient = 1; } elseif($args[$i] -eq "-excludenodeupdate") { $excludeNodeUpdate = 1; } elseif($args[$i] -eq "-excludenugetupdate") { $excludeNugetUpdate = 1; } elseif($args[$i] -eq "-excludegit") { $excludeGit = 1; } elseif($args[$i] -eq "-excludeserver") { $excludeServer = 1; } elseif($args[$i] -eq "-excludefrontend") { $excludeFrontend = 1; } else { $err = $args[$i] ExitERR "Invalid parameter: $err" } } #Operations... CheckAllCommand Write-Host "CMS-Active Builder...." -ForegroundColor Green $tempname = getFileLog $filelog = $outLogs + $tempname New-Item -ItemType "directory" -Path "$outLogs" -Force | Out-Null Write-Host "Log File: " $filelog -ForegroundColor Green if (($silent -eq 0) -and ($branchNeedsToBeSetted -eq 1)) { $branch = git branch --show-current $response = Read-Host "insert the name of the Git Branch [$branch]" if ($response -ne '') { $branch = $response } } if (($silent -eq 0) -and ($versionNeedsToBeSetted -eq 1)) { $response = Read-Host "insert the name of the New Version [$version]" if ($response -ne '') { $version = $response $outBuild = $outBuildPath + $version } } Write-Host "-------------------------------" if ($excludeGit -ne 1) { Write-Host "Git Feching" ExecuteLog "git fetch --all" Write-Host "Moving to branch '$branch'" ExecuteLog "git checkout $branch" $mail = git log -1 --pretty=format:'%ae' $author = git log -1 --pretty=format:'%an' Write-Host "This is your last commit message from '$author' [$mail]:" git log --oneline -n 1 HEAD askConfirmorDie "`r`nAre you sure do you want to continue [y,n]" ExecuteLog "git checkout ." Write-Host "Pulling" ExecuteLog "git pull" } Write-Host "-------------------------------" Write-Host "Clean the Solution" ExecuteLog "devenv Thermo.Active.sln /Clean" $outFile = Test-Path $outputdir if($outFile -eq "True") { Remove-Item $outputdir -Recurse -Force } if ($excludeNugetUpdate -ne 1) { Write-Host "Update the Nuget Packages" CheckNugetScm ExecuteLog "nuget restore Thermo.Active.sln " } if ($version -ne "") { ExecuteLog "ChangeAssemblyVersion $assemblyInfoFile $version" } if ($excludeServer -ne 1) { Write-Host "Build the Server APP" ExecuteLog "devenv Thermo.Active.sln /Build Release /Projectconfig ""Release"" /project ""Thermo.Active\Thermo.Active.csproj""" $newv = (Get-Item $outputServer).VersionInfo.FileVersion $outBuild = $outBuildPath + $newv $outFile = Test-Path $outputServer if($outFile -ne "True") { ExitERR "Error while building Server APP (Exe not created)" } } if ($excludeClient -ne 1) { Write-Host "Build the Client APP" ExecuteLog "devenv Thermo.Active.sln /Build Release /Projectconfig ""Release|x64"" /project ""Client2020\Client2020.csproj""" ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\bin\Client\x64"" -Destination ""$PSScriptRoot\tmp"" -Recurse" ExecuteLog "Remove-Item -Path ""$PSScriptRoot\Thermo.Active\bin\Client\x64*"" -Force -Recurse" ExecuteLog "Copy-Item -Path ""$PSScriptRoot\tmp\*"" -Destination ""$PSScriptRoot\Thermo.Active\bin\Client"" -Recurse" $outFile = Test-Path $outputClient if($outFile -ne "True") { ExitERR "Error while building Client APP (Exe not created)" } } Write-Host "-------------------------------" if ($excludeFrontend -ne 1) { Set-Location .\Thermo.Active\wwwroot\ if ($excludeNodeUpdate -ne 1) { Write-Host "Update the Node dependencies" ExecuteLog "npm install" } Write-Host "Build the Frontend" ExecuteLog "npm run build" Set-Location $PSScriptRoot } Write-Host "-------------------------------" Write-Host "Create the output directory" ExecuteLog "New-Item -ItemType ""directory"" -Path $outBuild -Force" ExecuteLog "Remove-Item $outBuild -Recurse -Force" if ($zipOutput -ne 1) { Write-Host "-------------------------------" Write-Host "Create the installer" ExecuteLog "iscc ""$installerFilePath""" ExecuteLog "Remove-Item -Path ""$PSScriptRoot\tmp"" -Force -Recurse" } if ($zipOutput -eq 1) { Write-Host "-------------------------------" Write-Host "Create the Zip file" $outFile = Test-Path $outputdir if($outFile -eq "True") { ExecuteLog "7z a ""$outBuild\Active_Portable_$version.zip"" $outputdir\* " } Write-Host "Create the view Folder" ExecuteLog "New-Item -ItemType ""directory"" -Path view -Force" ExecuteLog "Remove-Item -Path ""view\*"" -Force -Recurse" Write-Host "Copy the files in View Folder" ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\dist"" -Destination ""view"" -Recurse" ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\Scripts"" -Destination ""view"" -Recurse" ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\libs"" -Destination ""view"" -Recurse" ExecuteLog "New-Item -ItemType ""directory"" -Path view\Assets\styles -Force" ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\Assets\fonts"" -Destination ""view\Assets"" -Recurse" ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\Assets\images"" -Destination ""view\Assets"" -Recurse" ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\Assets\icons"" -Destination ""view\Assets"" -Recurse" ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\Assets\svg"" -Destination ""view\Assets"" -Recurse" ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\Assets\animations"" -Destination ""view\Assets"" -Recurse" ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\Assets\styles\style.css"" -Destination ""view\Assets\styles"" " ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\Assets\styles\style.css.map"" -Destination ""view\Assets\styles"" " ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\Assets\styles\iziToast.min.css"" -Destination ""view\Assets\styles"" " ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\index.html"" -Destination ""view"" -Recurse" ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\favicon.ico"" -Destination ""view"" -Recurse" ExecuteLog "Copy-Item -Path ""$PSScriptRoot\Thermo.Active\wwwroot\config.development.json"" -Destination ""view"" -Recurse" Write-Host "Copy view in Zip file" $outFile = Test-Path ".\View" if($outFile -eq "True") { ExecuteLog "7z a ""$outBuild\Active_Portable_$version.zip"" "".\view"" " ExecuteLog "Remove-Item "".\View"" -Recurse -Force" ExecuteLog "Remove-Item -Path ""$PSScriptRoot\tmp"" -Force -Recurse" } } Write-Host "Version created: $version" -ForegroundColor Green Write-Host "Output dir: $outBuild" if ($silent -eq 0) { explorer.exe $outBuild } ExitOk