-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnableMvcBuildViewsInCsProjFilesWhereViewsHaveChanged.ps1
78 lines (60 loc) · 2.72 KB
/
EnableMvcBuildViewsInCsProjFilesWhereViewsHaveChanged.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
# This is run as a TeamCity build step
# In TeamCity create a PowerShell build step then
# - Set "Script execution mode" to "Execute .ps1 script with -File argument"
# - Set "Script arguments" to "%system.teamcity.build.changedFiles.file% %teamcity.build.workingDir%"
$changedFileInfoPath = $args[0]
$workingDirectory = $args[1]
# Got this function from http://suhinini.blogspot.co.uk/2010/02/using-xmlpeek-and-xmlpoke-in-powershell.html
function xmlPoke($file, $xpath, $value)
{
$filePath = $file.FullName
[xml] $fileXml = Get-Content $filePath
$node = $fileXml.SelectSingleNode($xpath)
if ($node)
{
$node.InnerText = $value
$fileXml.Save($filePath)
}
}
####### Step 1 - Reset all csproj files first because TeamCity doesn't do a clean checkout unless you have checked that option https://confluence.jetbrains.com/display/TCD8/Build+Checkout+Directory
$allWebCsProjFiles = Get-ChildItem -Path $workingDirectory -Recurse -Include "*.csproj"
write-host "##teamcity[message text='Reseting all csproject files...']"
foreach ($csProjFile in $allWebCsProjFiles)
{
xmlPoke $csProjFile "//*[local-name()='MvcBuildViews']" "false"
write-host "##teamcity[message text='Reset MvcBuildViews to false in $csProjFile']"
}
####### Step 2 - Get information about changed views
$changedFileData = Get-Content $changedFileInfoPath
$csProjFilesToEnableBuildViews = @()
write-host "##teamcity[message text='Looking for view changes...']"
foreach($line in $changedFileData)
{
if($line -like "*.cshtml*")
{
# Line format [fileName]:[status]:[commitHash]
# E.g. Home/Index.cshtml:CHANGED:1400ea14c3196abbfd8de3ab6fe0a902be2ccad8
$lineBits = $line -split ":"
$fileNameChanged = $lineBits[0]
write-host "##teamcity[message text='View change found $fileNameChanged']"
$csProjRoot = $fileNameChanged -match "(.*/)Views/.*"
$csProjDirectory = join-path -path $workingDirectory -childpath $matches[1]
$csProjToChange = Get-ChildItem -Path $csProjDirectory -Recurse -Include "*.csproj"
$csProjFilesToEnableBuildViews += $csProjToChange
}
}
####### Step 3 - Set MvcBuildViews to true in all changed projects
if ($csProjFilesToEnableBuildViews.Length -gt 0)
{
$uniqueCsProjFilesToEnableBuildViews = $csProjFilesToEnableBuildViews | select -Unique
write-host "##teamcity[message text='Poking csproj files...']"
foreach ($csProjFile in $uniqueCsProjFilesToEnableBuildViews)
{
xmlPoke $csProjFile "//*[local-name()='MvcBuildViews']" "true"
write-host "##teamcity[message text='Set MvcBuildViews to true in $csProjFile']"
}
}
else
{
write-host "##teamcity[message text='No view changes found']"
}