/
deploy-script-inject.ps1
76 lines (60 loc) · 2.68 KB
/
deploy-script-inject.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
param (
[string]$scriptUrl = $null
)
if ($scriptUrl.Length -eq 0) {
Write-Host "Parameter -scriptUrl was not provided, and is required."
return
}
Write-Host "Downloading deploy script $scriptUrl"
$WebClient=New-Object net.webclient
$deployScriptContents = $WebClient.DownloadString($scriptUrl)
Write-Host "Deploy script downloaded"
$csprojFiles = Get-ChildItem -Path . -Include @("*.csproj", "*.vbproj") -Recurse
foreach ($file in $csprojFiles) {
[xml]$xmlFile = Get-Content $file.FullName
$projNode = $xmlFile.SelectSingleNode('//*[local-name()="Project"]')
if ($projNode -eq $null) {
Write-Host "Project node is null, can't process $($file.Name)"
continue
}
$deployPath = Join-Path -Path $file.Directory.FullName -ChildPath "deploy.ps1"
if (Test-Path $deployPath) {
Write-Host "deploy.ps1 already exists, not adding"
} else {
Write-Host "Writing deploy.cs1 to $deployPath"
$deployScriptContents | Out-File $deployPath
}
$targetBeforeBuildNodes = $projNode.SelectNodes('*[local-name()="Target" and @Name="BeforeBuild"]')
#Write-Host "$($file.FullName) count is $($targetBeforeBuildNodes.Count)"
$targetNode = $null
if ($targetBeforeBuildNodes.Count -eq 0) {
# add the node
Write-Host "Adding Target BeforeBuild node to project node..."
$targetNode = $xmlFile.CreateElement("Target", $xmlFile.DocumentElement.NamespaceURI)
$targetNode.SetAttribute("Name", "BeforeBuild")
$projNode.AppendChild($targetNode)
} else {
$targetNode = $targetBeforeBuildNodes[0]
Write-Host "Found existing Target BeforeBuild node..."
}
$itemGroupNodes = $targetNode.SelectNodes('*[local-name()="ItemGroup"]')
$itemGroupNode = $null
if ($itemGroupNodes.Count -eq 0) {
Write-Host "Adding ItemGroup node to Target BeforeBuild node..."
$itemGroupNode = $xmlFile.CreateElement("ItemGroup", $xmlFile.DocumentElement.NamespaceURI)
$targetNode.AppendChild($itemGroupNode)
} else {
$itemGroupNode = $itemGroupNodes[0]
Write-Host "Found existing ItemGroup node..."
}
$contentNode = $itemGroupNode.SelectSingleNode('*[local-name()="Content" and @Include="deploy.ps1"]')
if ($contentNode -eq $null) {
Write-Host "Adding Content Include deploy.ps1 node to ItemGroup node..."
$contentNode = $xmlFile.CreateElement("Content", $xmlFile.DocumentElement.NamespaceURI)
$itemGroupNode.AppendChild($contentNode)
$contentNode.SetAttribute("Include", "deploy.ps1")
$xmlFile.Save($file.FullName)
} else {
Write-Host "Content Include deploy.ps1 node already exists, not modifying"
}
}