-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-functions.psm1
161 lines (133 loc) · 4.98 KB
/
build-functions.psm1
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
function Get-FunctionFileContent {
[CmdletBinding()]
[OutputType([System.String[]])]
param (
[parameter(Mandatory = $true)]
[string]$Path
)
try {
$fileContent = Get-Content -Path $Path
$parserErrors = $null
if ([string]::IsNullOrEmpty($fileContent)) {
$parsedFileFunctions = @()
}
else {
$parsedFileFunctions = [System.Management.Automation.PSParser]::Tokenize($fileContent, [ref]$parserErrors)
}
$parsedFunctions = ($parsedFileFunctions | Where-Object { $_.Type -eq "Keyword" -and $_.Content -like 'function' })
if ($parsedFunctions.Count -gt 1) {
throw "Too many functions in file, file is invalid"
}
if ($parsedFunctions.Count -eq 1) {
if ($fileContent.Count -gt 1) {
foreach ($function in $parsedFunctions) {
$startLine = ($function.StartLine)
for ($line = $fileContent.Count; $line -gt $function.StartLine; $line--) {
if ($fileContent[$line] -like "*}*") {
$endLine = $line
break
}
}
for ($line = $startLine; $line -lt $endLine; $line++) {
$parsedFileContent += $fileContent[$line]
if ($line -ne ($fileContent.Count - 1)) {
$parsedFileContent += "`r`n"
}
}
}
}
else {
[int]$startBracket = $fileContent.IndexOf('{')
[int]$endBracket = $fileContent.LastIndexOf('}')
$parsedFileContent = $fileContent.substring($startBracket + 1, $endBracket - 1 - $startBracket)
}
}
else {
if ($fileContent.Count -gt 1) {
for ($line = 0; $line -lt $fileContent.Count; $line++) {
$parsedFileContent += $fileContent[$line]
if ($line -ne ($fileContent.Count - 1)) {
$parsedFileContent += "`r`n"
}
}
}
else {
$parsedFileContent = $fileContent
}
}
}
catch {
throw
}
return $parsedFileContent
}
function Install-BuiltModule {
[CmdletBinding()]
[OutputType([System.Void])]
param (
[parameter(Mandatory = $true)]
[string]$ModuleName
)
Install-Module -Name $ModuleName -Repository "$ModuleName-local" -Force
}
function Publish-BuiltModule {
[CmdletBinding()]
[OutputType([System.Void])]
param (
[parameter(Mandatory = $true)]
[string]$ModuleName,
[parameter(Mandatory = $true)]
[string]$ArtifactsFolder,
[parameter(Mandatory = $true)]
[string]$SourceFolder,
[parameter(Mandatory = $true)]
[string]$BuildFolder,
[switch]$Clean
)
$version = (Import-PowerShellDataFile -Path "$SourceFolder\$ModuleName\$ModuleName.psd1").ModuleVersion
$repositoryName = "$moduleName-local"
if ($null -ne (Get-InstalledModule -Name $ModuleName -ErrorAction SilentlyContinue)) {
Uninstall-Module -Name $ModuleName
}
if ($null -ne (Get-PSRepository -Name $repositoryName -ErrorAction SilentlyContinue)) {
Unregister-PSRepository -Name $repositoryName
}
$artifact = Join-Path -Path $ArtifactsFolder -ChildPath "$($ModuleName).$($version).nupkg"
if ($PSBoundParameters.ContainsKey('Clean')) {
if ((Test-Path -Path $artifact -ErrorAction SilentlyContinue)) {
Remove-Item -Path $artifact -Force
}
}
Register-PSRepository -Name $repositoryName -SourceLocation $ArtifactsFolder -InstallationPolicy Trusted
Publish-Module -Path "$BuildFolder\$ModuleName\$version" -Repository $repositoryName -NuGetApiKey 'use real NuGetApiKey for real nuget server here'
}
function Uninstall-BuiltModule {
[CmdletBinding()]
[OutputType([System.Void])]
param (
[parameter(Mandatory = $true)]
[string]$ModuleName
)
Uninstall-Module -Name $ModuleName
}
function Unpublish-BuiltModule {
[CmdletBinding()]
[OutputType([System.Void])]
param (
[parameter(Mandatory = $true)]
[string]$ModuleName,
[parameter(Mandatory = $true)]
[string]$SourceFolder,
[parameter(Mandatory = $true)]
[string]$ArtifactsFolder
)
$repositoryName = "$moduleName-local"
$version = (Import-PowerShellDataFile -Path "$SourceFolder\$ModuleName\$ModuleName.psd1").ModuleVersion
$artifact = Join-Path -Path $ArtifactsFolder -ChildPath "$ModuleName.$version.nupkg"
if ((Test-Path -Path $artifact -ErrorAction SilentlyContinue)) {
Remove-Item -Path $artifact -Force
}
if ($null -ne (Get-PSRepository -Name $repositoryName -ErrorAction SilentlyContinue)) {
Unregister-PSRepository -Name $repositoryName
}
}