forked from chocolatey/cChoco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppveyorCIScript.ps1
167 lines (145 loc) · 7.02 KB
/
AppveyorCIScript.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
$ErrorActionPreference = "Stop"
##Function used to output PSON - http://stackoverflow.com/questions/15139552/save-hash-table-in-powershell-object-notation-pson
Function ConvertTo-PSON($Object, [Int]$Depth = 9, [Int]$Layers = 1, [Switch]$Strict, [Version]$Version = $PSVersionTable.PSVersion) {
$Format = $Null
$Quote = If ($Depth -le 0) {""} Else {""""}
$Space = If ($Layers -le 0) {""} Else {" "}
If ($Object -eq $Null) {"`$Null"} Else {
$Type = "[" + $Object.GetType().Name + "]"
$PSON = If ($Object -is "Array") {
$Format = "@(", ",$Space", ")"
If ($Depth -gt 1) {For ($i = 0; $i -lt $Object.Count; $i++) {ConvertTo-PSON $Object[$i] ($Depth - 1) ($Layers - 1) -Strict:$Strict}}
} ElseIf ($Object -is "Xml") {
$Type = "[Xml]"
$String = New-Object System.IO.StringWriter
$Object.Save($String)
$Xml = "'" + ([String]$String).Replace("`'", "'") + "'"
If ($Layers -le 0) {($Xml -Replace "\r\n\s*", "") -Replace "\s+", " "} ElseIf ($Layers -eq 1) {$Xml} Else {$Xml.Replace("`r`n", "`r`n`t")}
$String.Dispose()
} ElseIf ($Object -is "DateTime") {
"$Quote$($Object.ToString('s'))$Quote"
} ElseIf ($Object -is "String") {
0..11 | ForEach {$Object = $Object.Replace([String]"```'""`0`a`b`f`n`r`t`v`$"[$_], ('`' + '`''"0abfnrtv$'[$_]))}; "$Quote$Object$Quote"
} ElseIf ($Object -is "Boolean") {
If ($Object) {"`$True"} Else {"`$False"}
} ElseIf ($Object -is "Char") {
If ($Strict) {[Int]$Object} Else {"$Quote$Object$Quote"}
} ElseIf ($Object -is "ValueType") {
$Object
} ElseIf ($Object.Keys -ne $Null) {
If ($Type -eq "[OrderedDictionary]") {$Type = "[Ordered]"}
$Format = "@{", ";$Space", "}"
If ($Depth -gt 1) {$Object.GetEnumerator() | ForEach {$_.Name + "$Space=$Space" + (ConvertTo-PSON $_.Value ($Depth - 1) ($Layers - 1) -Strict:$Strict)}}
} ElseIf ($Object -is "Object") {
If ($Version -le [Version]"2.0") {$Type = "New-Object PSObject -Property "}
$Format = "@{", ";$Space", "}"
If ($Depth -gt 1) {$Object.PSObject.Properties | ForEach {$_.Name + "$Space=$Space" + (ConvertTo-PSON $_.Value ($Depth - 1) ($Layers - 1) -Strict:$Strict)}}
} Else {$Object}
If ($Format) {
$PSON = $Format[0] + (&{
If (($Layers -le 1) -or ($PSON.Count -le 0)) {
$PSON -Join $Format[1]
} Else {
("`r`n" + ($PSON -Join "$($Format[1])`r`n")).Replace("`r`n", "`r`n`t") + "`r`n"
}
}) + $Format[2]
}
If ($Strict) {"$Type$PSON"} Else {"$PSON"}
}
} Set-Alias PSON ConvertTo-PSON -Description "Convert variable to PSON"
##Variables
$ModuleName = $env:ModuleName
$ModuleLocation = $env:APPVEYOR_BUILD_FOLDER
$PublishingNugetKey = $env:nugetKey
$Psd1Path = "./$ModuleName.psd1"
$BuildNumber = $env:APPVEYOR_BUILD_NUMBER
##Setup
#Add current directory to ps modules path so module is available
$env:psmodulepath = $env:psmodulepath + ";" + $ModuleLocation
#Install dsc resource designer to make tests available
Install-Module -Name xDSCResourceDesigner -force
##Checkout git master branch
#& git checkout master 2>$null
Start-Process -FilePath git -ArgumentList "checkout master" -Wait -NoNewWindow
##Test the resource
$DSC = Get-DscResource
write-host `n
write-host " Testing each resource in module: " -NoNewline
write-host "$ModuleName" -ForegroundColor blue -BackgroundColor darkyellow
write-host `n
##Check module exists
if (-not ($DSC | ? {$_.Module.Name -eq $ModuleName}))
{
Write-Error "Module not found: $ModuleName"
}
$ExportedDSCResources = @()
##Test the modules resources
foreach ($Resource in ($DSC | ? {$_.Module.Name -eq $ModuleName}))
{
write-host "Running Tests against $($Resource.Name) resource" -ForegroundColor Yellow
try
{
$Result = Test-xDscResource -Name $Resource.Name
switch ($Result)
{
$True
{
write-host "All tests passed for $($Resource.Name)." -ForegroundColor Green
#Add resource to array of strings, later used to update the manifest
$ExportedDSCResources += $Resource.Name
}
$False
{
Write-Error "One or more tests failed for $($Resource.Name)." -ForegroundColor Red
exit 1
}
}
write-host `n
}
catch
{
Write-Warning "The test for $($Resource.Name) failed due to an error"
Write-Error $_.Exception.Message
exit 1
}
}
#Update the manifest with included DSC Resources
#Disabled for now as only supported in Powershell5
#Update-ModuleManifest -Path $Psd1Path -DscResourcesToExport $ExportedDSCResources
##Increment version number of the module
write-host "Incrementing Module version, current version: " -NoNewline
#Load file content
$ModuleDefinitionContent = Get-Content -Path $Psd1Path | Out-String
#Invoke the contents to create hashttable we can edit.
$ModuleDefinition = Invoke-Expression $ModuleDefinitionContent
#Get current version number from hashtable
$CurrentVersion = [version]$ModuleDefinition.ModuleVersion
write-host "$CurrentVersion" -ForegroundColor blue -BackgroundColor darkyellow
#Increment the revision number
$ModuleDefinition.ModuleVersion = (New-Object -TypeName System.Version -ArgumentList $CurrentVersion.Major, $CurrentVersion.Minor, ($CurrentVersion.Build+1), $BuildNumber).ToString()
write-host "New version: " -NoNewline
write-host "$($ModuleDefinition.ModuleVersion)" -ForegroundColor blue -BackgroundColor darkyellow
#Update the module with the new version
#Todo - Find out why this isn't working in Appveyor
#$NewVersion = New-Object -TypeName System.Version -ArgumentList $CurrentVersion.Major, $CurrentVersion.Minor, $CurrentVersion.Build, ($CurrentVersion.Revision + 1)
#Update-ModuleManifest -Path $Psd1Path -ModuleVersion $NewVersion
#Workaround for Update-ModuleManifest issue
#Convert ht back to pson and write out to file
ConvertTo-PSON $ModuleDefinition -Layers 3 | Set-Content -Path $Psd1Path
##Publish the resource
write-host `n
write-host "Publishing module to Powershell Gallery: " -NoNewline
write-host "$ModuleName" -ForegroundColor blue -BackgroundColor darkyellow
write-host `n
Publish-Module -Name $ModuleName -NuGetApiKey $PublishingNugetKey
##Commit updated version back to github
$GitUpdatedFile = "$ModuleName.psd1"
git config --global credential.helper store
Add-Content "$env:USERPROFILE\.git-credentials" "https://$($env:github_access_token):[email protected]`n"
git config --global user.email "[email protected]"
git config --global user.name "AutomatedCI Build"
git config --global push.default simple
git add $GitUpdatedFile
git commit -m "Pushed to PSGallery with updated version number: $($ModuleDefinition.ModuleVersion)"
#Workaround for stderror redirect on appveyor causing build error.
Start-Process -FilePath git -ArgumentList "push" -Wait -NoNewWindow