Created
November 2, 2019 10:30
-
-
Save steliodibello/4cb47563ce43b922b943a000ec072853 to your computer and use it in GitHub Desktop.
Powershell Publish for helix like solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[Parameter(Mandatory=$true)][string]$solutionPath | |
) | |
Function GetJsonConfig{ | |
$jsonConfig = Join-Path -Path $solutionPath -ChildPath "Configuration" | Join-Path -ChildPath "PostPublishConfig.json" | |
if ((Test-Path $jsonConfig) -eq $true){$jsonObject = Get-Content $jsonConfig | ConvertFrom-Json} | |
else{throw [System.ArgumentException] "$jsonConfig does not exist."} | |
return $jsonObject | |
} | |
Function Find-MsBuild([string] $path) | |
{ | |
$rootPath = "C:\Program Files (x86)\Microsoft Visual Studio\2019" | |
$buildPart = "MSBuild\\Current\\Bin\\msbuild.exe" | |
$agentPath = "$rootPath$vsPart\BuildTools\$buildPart" | |
If (Test-Path $agentPath) { return $agentPath } | |
$agentPath = "$rootPath$vsPart\Enterprise\$buildPart" | |
If (Test-Path $agentPath) { return $agentPath } | |
$agentPath = "$rootPath$vsPart\Professional\$buildPart" | |
If (Test-Path $agentPath) { return $agentPath } | |
$agentPath = "$rootPath$vsPart\Community\$buildPart" | |
If (Test-Path $agentPath) { return $agentPath } | |
$rootPath = "C:\Program Files\Microsoft Visual Studio\2017" | |
$agentPath = "$rootPath$vsPart\BuildTools\$buildPart" | |
If (Test-Path $agentPath) { return $agentPath } | |
$agentPath = "$rootPath$vsPart\Enterprise\$buildPart" | |
If (Test-Path $agentPath) { return $agentPath } | |
$agentPath = "$rootPath$vsPart\Professional\$buildPart" | |
If (Test-Path $agentPath) { return $agentPath } | |
$agentPath = "$rootPath$vsPart\Community\$buildPart" | |
If (Test-Path $agentPath) { return $agentPath } | |
return $path | |
} | |
# Config values from JSON file | |
$jsonObject = GetJsonConfig | |
[String]$msBuildExe = Find-MsBuild($jsonObject.config.msBuildExe) | |
[String]$siteName = $jsonObject.config.siteName | |
[bool]$prune = $jsonObject.config.prune | |
#Get the paths to the site, based on site inputname | |
$appCmd = "$Env:SystemRoot\system32\inetsrv\appcmd.exe" | |
[xml]$site = & $appCmd list vdir "$siteName/" /config | |
[String]$publishTarget = $site.virtualDirectory.physicalPath | |
Write-Host "starting publishing solution $publishTarget " -foregroundcolor Magenta | |
if($prune) { | |
[String]$appConfigInclude = Join-Path -Path $publishTarget -ChildPath "App_Config" | Join-Path -ChildPath "Include" | |
$files = Get-ChildItem $appConfigInclude | |
foreach ($file in $files) { | |
Write-Host "Remove File $file.FullName " | |
Remove-Item $file.FullName -Recurse | |
} | |
} | |
# Publish all Foundation projects | |
$foundationPath = Join-Path -Path $solutionPath -ChildPath "src/Foundation" | |
Write-Host "publishTarget $foundationPath " | |
if ((Test-Path $foundationPath) -eq $True) { | |
$foundationProjects = Get-ChildItem $foundationPath -Recurse -Include *.csproj | |
foreach ($projectFile in $foundationProjects) { | |
if ($projectFile -notlike "*Tests*") | |
{ | |
Write-Host "Foundation $projectFile " | |
& "$($msBuildExe)" "$($projectFile)" /m /nr:false /p:DeployOnBuild=true /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:Configuration=debug /p:PublishUrl=$publishTarget -v:q -nologo -clp:NoSummary > null | |
if ($LastExitCode -ne 0) { | |
Write-Host "Build failed for" $projectFile -foregroundcolor Magenta | |
exit | |
} | |
} | |
} | |
} | |
# Publish all Feature projects | |
$featurePath = Join-Path -Path $solutionPath -ChildPath "src/Feature" | |
Write-Host "FeaturePath $featurePath " | |
if ((Test-Path $featurePath) -eq $True) { | |
$featureProjects = Get-ChildItem $featurePath -Recurse -Include *.csproj | |
foreach ($projectFile in $featureProjects) { | |
if ($projectFile -notlike "*Tests*") | |
{ | |
Write-Host "Feature $projectFile " | |
& "$($msBuildExe)" "$($projectFile)" /m /nr:false /p:DeployOnBuild=true /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:Configuration=debug /p:PublishUrl=$publishTarget -v:q -nologo -clp:NoSummary > null | |
if ($LastExitCode -ne 0) { | |
Write-Host "Build failed for" $projectFile -foregroundcolor Magenta | |
exit | |
} | |
} | |
} | |
} | |
$webconfigPath = Join-Path -Path $solutionPath -ChildPath "src\Project\XXXXXXXX\code\web.config" | |
Write-Host "mainsite config $webconfigPath " | |
Copy-Item -Path $webconfigPath -Destination $publishTarget |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"__comment_config": "This configuration file is used by PostPublish.ps1.", | |
"config": { | |
"__comment_msBuildExe": "The script will look for your msbuild.exe in visualstudio 2019 default path. msBuildExe is a optional fallback value if no msbuild.exe is found", | |
"msBuildExe": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Current.0\\Bin\\msbuild.exe", | |
"siteName": "XXXXX", | |
"__comment__prune": "Only use with sitecore > 9 due to folder structure. Removes all files and folders in /App_Config/Include", | |
"prune": true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment