-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathPost_Build.ps1
197 lines (156 loc) · 7.31 KB
/
Post_Build.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#
# Copies contents of _Deploy folders into the various deployment sites, as specified within deploy_user.json
#
Param([string]$projectfilter)
# Load dependents scripts
. "$PSScriptRoot\Deploy_Functions.ps1"
# Load configuration files
$deployConfig = Get-Content "$PSScriptRoot\deploy.json" | ConvertFrom-Json
if(Test-Path -Path "$PSScriptRoot\deploy.user.json") {
$userConfig = Get-Content "$PSScriptRoot\deploy.user.json" | ConvertFrom-Json
}
if(!$userConfig) {
Write-Host "No userconfig found. Applying default settings."
$userConfig = [PSCustomObject]@{
"sites" = @(
[PSCustomObject]@{
"path" = Join-Path -Path (Get-Item -Path $PSScriptRoot).Parent.FullName -ChildPath "docker\deploy"
}
)
}
}
if($userConfig) {
if(!$userConfig.sites) {
$userConfig.sites = @(
[PSCustomObject]@{
"path" = Join-Path -Path (Get-Item -Path $PSScriptRoot).Parent.FullName -ChildPath "docker\deploy"
}
)
}
}
# Set source folder
$sourceFolder = $PSScriptRoot
# Convert the variables into a hashtable for replacement
$variables = @{
"%%sourceFolder%%" = $sourceFolder
}
Write-Host -ForegroundColor Cyan "*******************************************"
Write-Host -ForegroundColor Cyan " Sitecore PowerShell Extensions Deployment"
Write-Host -ForegroundColor Cyan "*******************************************"
Write-Host
if ($projectFilter) {
Write-Host "Applying project filter: $projectFilter"
}
Write-Host
Write-Host -ForegroundColor Blue "Building user configuration with variable replacements"
Write-Host
# First copy user configuration to a deploy folder
$userConfigPath = Join-Path $sourceFolder $deployConfig.userConfigurationFolder
$deployFolderFullPath = Join-Path $sourceFolder $deployConfig.deployFolder
$deployUserConfigPath = Join-Path $deployFolderFullPath $deployConfig.userConfigurationFolder
Get-ChildItem $userConfigPath -Recurse | ? { $_.Extension -eq ".config" } | % {
$targetFile = $deployUserConfigPath + $_.FullName.SubString($userConfigPath.Length);
Copy-ItemWithStructure $_.FullName $targetFile
Write-Host "--- Copied $targetFile"
}
# Perform variable replacement on configuration files in deploy folder
$variablesRegexKeys = $variables.keys | % { [System.Text.RegularExpressions.Regex]::Escape($_) }
$variablesRegex = [regex]($variablesRegexKeys -join '|')
$regexCallback = { $variables[$args[0].Value] }
Get-ChildItem $deployUserConfigPath -Recurse -Include *.config | ForEach-Object {
$file = [System.IO.File]::ReadAllText($_.FullName)
$file = $variablesRegex.Replace($file, $regexCallback)
Set-Content -Path $_ -Value $file
}
Write-Host
Write-Host -ForegroundColor Blue "User configuration build complete"
Write-Host -ForegroundColor Blue "Processing sites"
Write-Host
# Loop over the sites to deploy
foreach ( $site in $userConfig.sites ) {
Write-Host "Site: $($site)"
$site = Update-FromDefaultSite $site
# Get folders to deploy to from configuration and based on the destination site's version
$deployProjects = $deployConfig.deployProjects | Filter-ProjectsForSite -version $site.version -projectFilter $projectFilter
if ($deployProjects)
{
Write-Host
Write-Host -ForegroundColor Cyan "Deploying $(@($deployProjects).Count) projects to $($site.path)"
Write-Host
foreach ($deployProject in $deployProjects)
{
$deployFolder = Get-DeployFolder $sourceFolder $deployConfig $deployProject.project
if (!(Test-Path $deployFolder))
{
Write-Error "Cannot find deploy folder for project: $deployFolder"
continue
}
$projectFolder = Get-ProjectFolder $sourceFolder $deployProject.project
Write-Host
Write-Host -ForegroundColor Green "Copying from $deployFolder to $($site.path)"
Write-Host
$filesToCopy = Get-ChildItem $deployFolder -Recurse | Where-Object { $_.PSIsContainer -eq $False }
if ($site.junction -and $deployProject.junctionPoints -ne $null) {
# Deploy any files that are not included in junction-folders
$filesToCopy = $filesToCopy | Filter-JunctionPoints $deployProject
# Create the junction points
$deployProject.junctionPoints | % {
$sourcePath = Join-Path $projectFolder $_
$sitePath = Join-Path $site.path $_
Create-Junction $sitePath $sourcePath
Write-Host "--- Created junction at $sitePath"
}
}
$filesToCopy | % {
$targetFile = Join-Path $site.path $_.FullName.SubString($deployFolder.Length);
Copy-ItemWithStructure $_.FullName $targetFile
Write-Host "--- Copied $targetFile"
}
Write-Host "Processing configs in $($site.path)"
foreach($action in $userConfig.files) {
foreach($file in $action.remove) {
Write-Host "- Removing $($file)"
$path = Join-Path -Path $site.path -ChildPath $file
Get-Item -Path $path -ErrorAction SilentlyContinue | Remove-Item
}
foreach($file in $action.disable) {
Write-Host "- Disabling $($file)"
$path = Join-Path -Path $site.path -ChildPath $file
if(Test-Path -Path ("$($path).disabled")) {
Remove-Item -Path ("$($path).disabled")
}
Get-Item -Path $path -ErrorAction SilentlyContinue | Rename-Item -NewName { $PSItem.Name + ".disabled" }
}
foreach($file in $action.enable) {
Write-Host "- Enabling $($file)"
$path = Join-Path -Path $site.path -ChildPath $file
if(Test-Path -Path $path) {
if(Test-Path -Path ("$($path).disabled")) {
Remove-Item -Path $path
}
if(Test-Path -Path ("$($path).example")) {
Remove-Item -Path $path
}
Get-Item -Path ("$($path).disabled") -ErrorAction SilentlyContinue | Rename-Item -NewName { $PSItem.Name -replace ".disabled","" } -Force
Get-Item -Path ("$($path).example") -ErrorAction SilentlyContinue | Rename-Item -NewName { $PSItem.Name -replace ".example","" } -Force
}
}
}
}
}
else
{
Write-Host
Write-Host -ForegroundColor Cyan "No valid deployment sites for this project"
Write-Host
}
# Deploy UserConfiguration
Write-Host
Write-Host -ForegroundColor Blue "Copying user configuration to $($site.path)"
Write-Host
Get-ChildItem $deployUserConfigPath -Recurse | ? { $_.Extension -eq ".config" } | % {
$targetFile = Join-Path $site.path $_.FullName.SubString($deployUserConfigPath.Length);
Copy-ItemWithStructure $_.FullName $targetFile
Write-Host "--- Copied $targetFile"
}
}