-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathbuildFunctions.psm1
More file actions
215 lines (179 loc) · 7.87 KB
/
buildFunctions.psm1
File metadata and controls
215 lines (179 loc) · 7.87 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#modular functions to help with building the site in powershell in windows
function Get-Python {
[CmdletBinding()]
param (
)
process {
"Ensuring Python is installed...." | out-host;
if (!(Get-Command "python.exe" -ea 0)) {
Write-Warning "Python not detected in path! Attempting to install with chocolatey package manager"
Write-Warning "May we install/use chocolatey package manager and install python? This will require admin rights in an elevated shell and the package will handle updating path variables"
$installCHoco = Read-Host -Prompt "Install choco and python with choco? (Y/N)"
if ($installCHoco -eq "Y") {
if (!(Get-Command 'choco' -ea 0)) {
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
}
choco upgrade python312 -y --no-progress;
Import-Module C:\ProgramData\chocolatey\helpers\chocolateyInstaller.psm1 -force -ea 0;
Update-SessionEnvironment
} else {
Write-Warning "Python not detected in path! Attempting to Install with winget via msstore or winget version and adding to the path"
if (Get-Command "winget" -ea 0) {
try {
winget.exe install "Python 3.12" -s msstore
} catch {
"There was an error with winget install, trying winget source" | Out-Host
winget.exe install "Python 3.12" -s winget
}
} else {
Write-Error "Please manually install python and add it to the environment path then re-run this build/make script";
pause;
exit;
}
}
#python should now be installed, make sure it is in $ENV:PATH as well as the script subdir for putting mkdocs in the path
if (!(Get-Command "python.exe" -ea 0)) {
"searching for python.exe..."
$pythonPth = (Get-ChildItem -Filter "python.exe" -Recurse -file -Path "\" -force -ea 0).FullName
if ($null -ne $pythonPth.count) {
if ($pythonPth.count -gt 1) {
$pythonPath = $pythonPth | Where-Object {
Test-path ("$(Split-Path $($_) -Parent)\Scripts")
}
$pythonPth = $pythonPath[0];
}
}
"python found at $($pythonPth), adding parent folder to path, and scripts folder to path" | out-host;
$ENV:PATH += ";$(Split-Path $($pythonPth[0]) -Parent)"
$ENV:PATH += ";$(Split-Path $($pythonPth[0]) -Parent)\Scripts"
Update-SessionEnvironment
}
}
}
}
function Install-Requirements {
[CmdletBinding()]
param (
$requirements = ".\requirements.txt"
)
process {
"Installing Pre-requisites if needed..." | out-host;
$log = ".\.lastprereqrun"
$requirementsLastUpdate = (Get-item $requirements).LastWriteTime;
if (Test-Path $log) {
if ((Get-item $log).LastWriteTime -lt $requirementsLastUpdate) {
$shouldUpdate = $true;
} else {
$shouldUpdate = $false;
}
} else {
$shouldUpdate = $true;
}
if ($shouldUpdate) {
$results = New-Object -TypeName 'System.collections.generic.List[System.Object]';
$result = & python.exe -m pip install --upgrade pip
$results.add(($result))
Get-Content $requirements | Where-Object { $_ -notmatch "#"} | ForEach-Object {
$result = pip install $_;
$results.add(($result))
}
New-Item $log -ItemType File -force -Value "requirements last installed with pip on $(Get-date)`n`n$($results | out-string)";
} else {
"Requirements already met, insuring they're up to date" | out-host;
$results = New-Object -TypeName 'System.collections.generic.List[System.Object]';
$result = & python.exe -m pip install --upgrade pip
$results.add(($result))
Get-Content $requirements | Where-Object { $_ -notmatch "#"} | ForEach-Object {
$result = pip install --upgrade $_;
$results.add(($result))
}
New-Item "$log-upgrades" -ItemType File -force -Value "requirements last installed with pip on $(Get-date)`n`n$($results | out-string)";
}
return (Get-Content $log)
}
}
function Start-MkDocsBuild {
[CmdletBinding()]
param (
$SOURCEDIR=".",
$BUILDDIR="$SOURCEDIR\_build"
)
process {
Set-Location $SOURCEDIR
if (!(Test-Path $BUILDDIR)) {
mkdir $BUILDDIR;
}
try {
& mkdocs build -d $BUILDDIR | Out-Null
} catch {
try {
$mkDocs = (Get-ChildItem -Filter "mkdocs.exe" -Recurse -file -Path "\" -force -ea 0).FullName;
if ( !($mkdocs.count -eq 1) ) {
Write-Warning "Multiple versions of mkdocs.exe found in system, will try first one $($mkdocs | out-string). "
$mkDocs = $mkdocs[0]
}
& $mkDocs build -d $BUILDDIR
} catch {
"" | Out-Host
"The 'mkdocs.exe' command was not found. Make sure you have mkdocs installed with pip python package manager and that the path to python scripts is added to your path" | Out-Host
"This should have been done automatically with the make.ps1 make/build script" | Out-Host
return "mkdocs not found";
}
}
start "http:\\localhost:8000"
& mkdocs.exe serve
# "$buildDir\index.html"
return "local dev server version opened in default browser"
}
}
function Add-PagesToSiteMap {
<#
.SYNOPSIS
This needs more work, copy pasted from another build script I've written that needs to be adapted. Basically, we can dynamically build the page tree in the mkdocs.yml in local builds.
Making this also work in cloud builds will be another story
.DESCRIPTION
Long description
.EXAMPLE
An example
.NOTES
General notes
#>
[CmdletBinding()]
param (
$docsPth
)
process {
$mkdocsYml = "$PSScriptRoot\mkdocs.yml";
$mkdocs = @"
nav:
- Home: index.md
"@
$mkdocs += "`n";
$index = "# FogAPI`n`n"
Get-ChildItem "$docsPth" -Recurse | Where-Object name -NotMatch 'index' | Foreach-Object {
# if is directory make category
if ($_.Attributes -contains "Directory") {
"- $($_.BaseName): "
}
#if is
$name = $_.Name;
$baseName = $_.BaseName
$file = $_.FullName;
$link = "https://fogapi.readthedocs.io/en/latest/commands/$basename";
#insert in onlineVersion at top
$content = Get-Content $file;
$onlineStr = ($content | Select-String "online version: *" -Raw).tostring();
$newOnlineStr = "$onlineStr $link";
$content = $content.replace($onlineStr,$newOnlineStr);
Set-Content -Path $file -Value $content;
#Update commands index
$index += "## [$basename]($name)`n`n"
#Update readthedocs nav index
$mkdocs += " - '$basename': 'commands/$name'`n";
#maybe later add something that converts any functions in .link to related links
}
$mkdocs += "`ntheme: readthedocs"
Set-Content $mkdocsYml -value $mkdocs;
Set-Content $indexFile -Value $index;
}
}