Skip to content

Commit

Permalink
Merge pull request #13 from max-ieremenko/build/clean-up
Browse files Browse the repository at this point in the history
use Invoke-Build instead of exec
  • Loading branch information
max-ieremenko authored Apr 13, 2024
2 parents 91689a8 + 3bf6b26 commit 3e89ca0
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 122 deletions.
10 changes: 5 additions & 5 deletions build/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
[CmdletBinding()]
param (
[Parameter()]
[ValidateSet("LocalBuild", "CIBuild")]
[ValidateSet('LocalBuild', 'CIBuild')]
[string]
$Task = "LocalBuild"
$Task = 'LocalBuild'
)

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'

$file = Join-Path $PSScriptRoot "scripts/build-tasks.ps1"
$file = Join-Path $PSScriptRoot 'scripts/build-tasks.ps1'
Invoke-Build -File $file -Task $Task
30 changes: 15 additions & 15 deletions build/install-dependencies.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
[CmdletBinding()]
param (
[Parameter()]
[ValidateSet(".net", "InvokeBuild", "Pester")]
[ValidateSet('.net', 'InvokeBuild', 'Pester')]
[string[]]
$List
)

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$ErrorActionPreference = 'Stop'

function Get-ModuleVersion {
param (
Expand All @@ -18,7 +18,7 @@ function Get-ModuleVersion {
$Name
)

$sources = Get-Content (Join-Path $PSScriptRoot "build.ps1") -Raw
$sources = Get-Content (Join-Path $PSScriptRoot 'build.ps1') -Raw
$tokens = $null
$errors = $null
$modules = [Management.Automation.Language.Parser]::ParseInput($sources, [ref]$tokens, [ref]$errors).ScriptRequirements.RequiredModules
Expand All @@ -32,22 +32,22 @@ function Get-ModuleVersion {
}

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$ErrorActionPreference = 'Stop'

if (-not $List -or (".net" -in $List)) {
$version = (Get-Content -Raw (Join-Path $PSScriptRoot "../sources/global.json") | ConvertFrom-Json).sdk.version
$script = Join-Path $PSScriptRoot "scripts/step-install-dotnet.ps1"
if (-not $List -or ('.net' -in $List)) {
$version = (Get-Content -Raw (Join-Path $PSScriptRoot '../sources/global.json') | ConvertFrom-Json).sdk.version
$script = Join-Path $PSScriptRoot 'scripts/step-install-dotnet.ps1'
& $script $version
}

if (-not $List -or ("InvokeBuild" -in $List)) {
$script = Join-Path $PSScriptRoot "scripts/step-install-module.ps1"
$version = Get-ModuleVersion "InvokeBuild"
& $script "InvokeBuild" $version
if (-not $List -or ('InvokeBuild' -in $List)) {
$script = Join-Path $PSScriptRoot 'scripts/step-install-module.ps1'
$version = Get-ModuleVersion 'InvokeBuild'
& $script 'InvokeBuild' $version
}

if (-not $List -or ("Pester" -in $List)) {
$script = Join-Path $PSScriptRoot "scripts/step-install-module.ps1"
$version = Get-ModuleVersion "Pester"
& $script "Pester" $version
if (-not $List -or ('Pester' -in $List)) {
$script = Join-Path $PSScriptRoot 'scripts/step-install-module.ps1'
$version = Get-ModuleVersion 'Pester'
& $script 'Pester' $version
}
78 changes: 38 additions & 40 deletions build/scripts/build-tasks.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ task DefaultBuild Initialize, Clean, Build, PackPowerShellModule, UnitTest

Enter-Build {
$settings = @{
sources = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "../../sources"))
bin = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "../../bin"))
tests = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "../../tests"))
sources = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot '../../sources'))
bin = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot '../../bin'))
tests = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot '../../tests'))
version = $(
$buildProps = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "../../sources/Directory.Build.props"))
$packageVersion = (Select-Xml -Path $buildProps -XPath "Project/PropertyGroup/Version").Node.InnerText
assert $packageVersion "Version not found"
$buildProps = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot '../../sources/Directory.Build.props'))
$packageVersion = (Select-Xml -Path $buildProps -XPath 'Project/PropertyGroup/Version').Node.InnerText
assert $packageVersion 'Version not found'
$packageVersion
)
repositoryUrl = $(
$url = Exec { git config --get remote.origin.url }
if ($url.EndsWith(".git")) {
if ($url.EndsWith('.git')) {
$url = $url.Substring(0, $url.Length - 4)
}

Expand All @@ -37,7 +37,7 @@ task Clean {
}

task Build {
$solutionFile = Join-Path $settings.sources "ZipAsFolder.sln"
$solutionFile = Join-Path $settings.sources 'ZipAsFolder.sln'

Exec { dotnet restore $solutionFile }

Expand All @@ -50,48 +50,48 @@ task Build {
}

task UnitTest {
Exec { .\step-unit-test.ps1 $settings.bin "net8.0" }
Invoke-Build -File .\step-unit-test.ps1 `
-BinPath $settings.bin `
-Framework 'net8.0'
}

task PackPowerShellModule {
Exec {
.\step-pack-ps-module.ps1 `
-BinPath (Join-Path $settings.bin "module") `
-OutPath (Join-Path $settings.bin "pwsh-module.zip") `
-ModuleVersion $settings.version `
-RepositoryUrl $settings.repositoryUrl
}
Invoke-Build -File .\step-pack-ps-module.ps1 `
-BinPath (Join-Path $settings.bin 'module') `
-OutPath (Join-Path $settings.bin 'pwsh-module.zip') `
-ModuleVersion $settings.version `
-RepositoryUrl $settings.repositoryUrl
}

task LocalPsCoreTest {
# show-powershell-images.ps1
$images = $(
"mcr.microsoft.com/powershell:7.0.0-ubuntu-18.04"
, "mcr.microsoft.com/powershell:7.0.1-ubuntu-18.04"
, "mcr.microsoft.com/powershell:7.0.2-ubuntu-18.04"
, "mcr.microsoft.com/powershell:7.0.3-ubuntu-18.04"
, "mcr.microsoft.com/powershell:7.1.0-ubuntu-18.04"
, "mcr.microsoft.com/powershell:7.1.1-ubuntu-20.04"
, "mcr.microsoft.com/powershell:7.1.2-ubuntu-20.04"
, "mcr.microsoft.com/powershell:7.1.3-ubuntu-20.04"
, "mcr.microsoft.com/powershell:7.1.4-ubuntu-20.04"
, "mcr.microsoft.com/powershell:7.1.5-ubuntu-20.04"
, "mcr.microsoft.com/powershell:7.2.0-ubuntu-20.04"
, "mcr.microsoft.com/powershell:7.2.1-ubuntu-20.04"
, "mcr.microsoft.com/powershell:7.2.2-ubuntu-20.04"
, "mcr.microsoft.com/powershell:7.3-ubuntu-20.04"
, "mcr.microsoft.com/powershell:7.4-ubuntu-20.04"
, "mcr.microsoft.com/powershell:preview-7.5-ubuntu-20.04")
'mcr.microsoft.com/powershell:7.0.0-ubuntu-18.04'
, 'mcr.microsoft.com/powershell:7.0.1-ubuntu-18.04'
, 'mcr.microsoft.com/powershell:7.0.2-ubuntu-18.04'
, 'mcr.microsoft.com/powershell:7.0.3-ubuntu-18.04'
, 'mcr.microsoft.com/powershell:7.1.0-ubuntu-18.04'
, 'mcr.microsoft.com/powershell:7.1.1-ubuntu-20.04'
, 'mcr.microsoft.com/powershell:7.1.2-ubuntu-20.04'
, 'mcr.microsoft.com/powershell:7.1.3-ubuntu-20.04'
, 'mcr.microsoft.com/powershell:7.1.4-ubuntu-20.04'
, 'mcr.microsoft.com/powershell:7.1.5-ubuntu-20.04'
, 'mcr.microsoft.com/powershell:7.2.0-ubuntu-20.04'
, 'mcr.microsoft.com/powershell:7.2.1-ubuntu-20.04'
, 'mcr.microsoft.com/powershell:7.2.2-ubuntu-20.04'
, 'mcr.microsoft.com/powershell:7.3-ubuntu-20.04'
, 'mcr.microsoft.com/powershell:7.4-ubuntu-20.04'
, 'mcr.microsoft.com/powershell:preview-7.5-ubuntu-20.04')

$builds = @()
foreach ($image in $images) {
exec { docker pull --quiet $image }

$builds += @{
File = "step-test-ps-module-docker.ps1"
Task = "Test"
File = 'step-test-ps-module-docker.ps1'
Task = 'Test'
TestsPath = $settings.tests
ModulePath = (Join-Path $settings.bin "pwsh-module.zip")
ModulePath = (Join-Path $settings.bin 'pwsh-module.zip')
ImageName = $image
}
}
Expand All @@ -100,9 +100,7 @@ task LocalPsCoreTest {
}

task CIPsCoreTest {
Exec {
.\step-test-ps-module.ps1 `
-TestsPath $settings.tests `
-ModulePath (Join-Path $settings.bin "pwsh-module.zip")
}
Invoke-Build -File .\step-test-ps-module.ps1 `
-TestsPath $settings.tests `
-ModulePath (Join-Path $settings.bin 'pwsh-module.zip')
}
10 changes: 5 additions & 5 deletions build/scripts/step-install-dotnet.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ param (
$versions = dotnet --list-sdks
foreach ($installedVersion in $versions) {
# 6.0.401 [C:\Program Files\dotnet\sdk]
$test = ($installedVersion -split " ")[0]
$test = ($installedVersion -split ' ')[0]

if ($test -eq $Version) {
Write-Output ".net sdk $version is alredy installed"
return
}
}

$installDir = "C:\Program Files\dotnet"
$installScript = "dotnet-install.ps1"
$installDir = 'C:\Program Files\dotnet'
$installScript = 'dotnet-install.ps1'

if ($IsLinux) {
$installDir = "/usr/share/dotnet"
$installScript = "dotnet-install.sh"
$installDir = '/usr/share/dotnet'
$installScript = 'dotnet-install.sh'
}

$downloadDir = [System.IO.Path]::GetTempPath()
Expand Down
2 changes: 1 addition & 1 deletion build/scripts/step-install-module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ param (
$Version
)

$test = Get-InstalledModule -Name $Name -MinimumVersion $Version -ErrorAction "SilentlyContinue"
$test = Get-InstalledModule -Name $Name -MinimumVersion $Version -ErrorAction 'SilentlyContinue'
if ($test) {
Write-Output "$Name $Version is alredy installed"
return
Expand Down
92 changes: 54 additions & 38 deletions build/scripts/step-pack-ps-module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,71 @@ param (
$RepositoryUrl
)

$temp = Join-Path (Split-Path $OutPath -Parent) "tmp"
remove $temp
remove $OutPath
task Default Copy, UpdateManifest, CleanUpManifest, Release

Copy-Item -Path $BinPath -Destination $temp -Recurse
Enter-Build {
$temp = Join-Path (Split-Path $OutPath -Parent) 'tmp'
remove $temp
remove $OutPath
}

$psdFile = Get-Item (Join-Path $temp "*.psd1")
assert ($psdFile) ".psd1 not found"
Exit-Build {
remove $temp
}

Update-ModuleManifest `
-Path $psdFile.FullName `
-ModuleVersion $ModuleVersion `
-LicenseUri "$RepositoryUrl/blob/main/LICENSE" `
-ProjectUri $RepositoryUrl `
-ReleaseNotes "$RepositoryUrl/releases"
task Copy {
Copy-Item -Path $BinPath -Destination $temp -Recurse
Get-ChildItem $temp -Include *.pdb -Recurse | Remove-Item
}

Get-ChildItem $temp -Include *.pdb -Recurse | Remove-Item
task UpdateManifest {
$psdFile = Get-Item (Join-Path $temp '*.psd1')
assert ($psdFile) '.psd1 not found'

Update-ModuleManifest `
-Path $psdFile.FullName `
-ModuleVersion $ModuleVersion `
-LicenseUri "$RepositoryUrl/blob/main/LICENSE" `
-ProjectUri $RepositoryUrl `
-ReleaseNotes "$RepositoryUrl/releases/tag/$ModuleVersion"
}

# remove comments and empty lines
$draftContent = Get-Content $psdFile
$cleanContent = @()
$descriptionBegin = $false
for ($i = 0; $i -lt $draftContent.Count; $i++) {
$line = $draftContent[$i]
$test = $line.Trim()
task CleanUpManifest {
$psdFile = Get-Item (Join-Path $temp '*.psd1')
assert ($psdFile) '.psd1 not found'

if ($test.StartsWith("Description =")) {
$descriptionBegin = $true
}
elseif ($test.StartsWith("# Minimum version") -or $test.StartsWith("PowerShellVersion =")) {
$descriptionBegin = $false
}
# remove comments and empty lines
$draftContent = Get-Content $psdFile
$cleanContent = @()
$descriptionBegin = $false
for ($i = 0; $i -lt $draftContent.Count; $i++) {
$line = $draftContent[$i]
$test = $line.Trim()

if (-not $descriptionBegin) {
if ([string]::IsNullOrWhiteSpace($test) -or $test.StartsWith("#")) {
continue
if ($test.StartsWith('Description =')) {
$descriptionBegin = $true
}
elseif ($test.StartsWith('# Minimum version') -or $test.StartsWith('PowerShellVersion =')) {
$descriptionBegin = $false
}

if (-not $descriptionBegin) {
if ([string]::IsNullOrWhiteSpace($test) -or $test.StartsWith('#')) {
continue
}

$index = $line.LastIndexOf(" #")
if ($index -gt 0) {
$line = $line.Substring(0, $index)
$index = $line.LastIndexOf(' #')
if ($index -gt 0) {
$line = $line.Substring(0, $index)
}
}

$cleanContent += $line
}

$cleanContent += $line
Set-Content $psdFile $cleanContent
}

Set-Content $psdFile $cleanContent

Compress-Archive -Path (Join-Path $temp "*") -DestinationPath $OutPath

remove $temp
task Release {
Compress-Archive -Path (Join-Path $temp '*') -DestinationPath $OutPath
}
2 changes: 1 addition & 1 deletion build/scripts/step-test-ps-module-docker-entrypoint.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$ProgressPreference = "SilentlyContinue"
$ProgressPreference = 'SilentlyContinue'

Expand-Archive /module.zip /root/.local/share/powershell/Modules/ZipAsFolder

Expand Down
4 changes: 2 additions & 2 deletions build/scripts/step-test-ps-module-docker.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ task Test {
$pester = Get-Module Pester
$pesterPath = Split-Path $pester.Path -Parent

$entryPoint = Join-Path $PSScriptRoot "step-test-ps-module-docker-entrypoint.ps1"
$entryPoint = Join-Path $PSScriptRoot 'step-test-ps-module-docker-entrypoint.ps1'
Exec {
docker run `
-it --rm `
Expand All @@ -30,6 +30,6 @@ task Test {
-v "${entryPoint}:/entrypoint.ps1" `
--entrypoint pwsh `
$ImageName `
"/entrypoint.ps1"
'/entrypoint.ps1'
}
}
12 changes: 7 additions & 5 deletions build/scripts/step-test-ps-module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ param (
$ModulePath
)

$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([guid]::NewGuid().ToString())
Expand-Archive $ModulePath $tempDir

$script = Join-Path $TestsPath "scripts/Run-TestsLocally.ps1"
& $script -ModulePath $tempDir
task Default {
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([guid]::NewGuid().ToString())
Expand-Archive $ModulePath $tempDir

$script = Join-Path $TestsPath 'scripts/Run-TestsLocally.ps1'
Exec { & $script -ModulePath $tempDir }
}
Loading

0 comments on commit 3e89ca0

Please sign in to comment.