-
Notifications
You must be signed in to change notification settings - Fork 3
/
mm-docs.build.ps1
97 lines (80 loc) · 3.47 KB
/
mm-docs.build.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
<# .SYNOPSIS
Invoke-Build build script
#>
param (
# Tag to use when building a new docker image, by default 'latest'
[string] $aTag = 'latest',
# Use latest versions of all included components
[switch] $aLatestModules
)
Enter-Build {
Write-Host "If you are behind the proxy use http(s)_proxy environment variables"
$script:ImageName = "majkinetor/mm-docs"
$script:ImageFullName = if (!$aTag) { $ImageName } else { "${ImageName}:$aTag" }
}
function Get-PlantUmlVersion {
function Get-GitHubReleaseUrl( $GitHubRepositoryUrl, $Pattern='\.exe$') {
$latestReleases = "$GitHubRepositoryUrl/releases/latest"
$latestPage = Invoke-WebRequest -Uri $latestReleases -UseBasicParsing
$latestPage.Content -match '(?<=src=")[^"]+expanded_assets[^"]+' | Out-Null
$assetsUrl = $Matches[0]
if (!$assetsUrl) { throw "Can't find assets URL" }
$domain = $GitHubRepositoryUrl -split '(?<=//.+)/' | Select-Object -First 1
$assetsPage = Invoke-WebRequest -Uri $assetsUrl -UseBasicParsing
$assetsPage.links | ? href -match $Pattern | Select-Object -expand href | % { $domain + $_ }
}
$GitHubRepositoryUrl = 'https://github.com/plantuml/plantuml'
$url = Get-GitHubReleaseUrl $GitHubRepositoryUrl 'plantuml-[0-9.]+\.jar$' | select -Last 1
$version = $url -split '-|.jar' | select -First 1 -Skip 1
$version
}
# Synopsis: Build docker image
task Build {
if ($aLatestModules) {
if (!(Test-Path requirements.txt.bak)) { Copy-Item requirements.txt requirements.txt.bak }
Write-Host "Removing versions from requirements.txt file" -ForegroundColor yellow
((Get-Content requirements.txt).Trim() -replace '=.+') + " "*(Get-Random 10) | Set-Content -Encoding Ascii requirements.txt
$plantuml_version = Get-PlantUmlVersion
Write-Host "Setting latest PlantUML version:" $plantuml_version -ForegroundColor yellow
(Get-Content Dockerfile) -replace '(PLANTUML_VERSION)=(.+)', "`$1=$plantuml_version" | Set-Content .\Dockerfile
}
$params = @(
'build'
if ($Env:http_proxy) { '--build-arg', "http_proxy=$Env:http_proxy" }
if ($Env:https_proxy) { '--build-arg', "https_proxy=$Env:https_proxy" }
'-t', $ImageFullName
'.'
)
Write-Host "Cmd: " docker $params -ForegroundColor green
exec { docker $params }
}
# Synopsis: Update docker image with latest dependencies
task Update { $script:aLatestModules = $true }, Build, GetVersions
# Synopsis: Run interactive session
task RunShell { docker run -it --rm $ImageFullName sh }
# Synopsis: Publish docker
task Publish {
if ($aTag -eq 'latest') { throw "aTag is required" }
exec {
docker tag majkinetor/mm-docs:latest majkinetor/mm-docs:$aTag
docker login
docker push majkinetor/mm-docs:$aTag
}
}
# Synopsis: Generate latest python requirement versions
task GetVersions {
Write-Host "Setting container versions in requirements.txt file" -ForegroundColor yellow
$cVersions = exec {
docker run -it --rm $ImageFullName pip list -l --format=json --disable-pip-version-check
}
$cVersions = $cVersions | ConvertFrom-Json
(Get-Content requirements.txt) | % {
$version = $cVersions | ? name -eq $_ | % version
if ($version) { "$_==$version" } else { $_ }
} | Set-Content -Encoding Ascii requirements.txt
Get-Content requirements.txt
}
# Synopsis: Remove docker images
task Clean {
docker rmi $script:ImageName
}