From 5603d7df1f499da6c3dab09810aeedf188b9a3f0 Mon Sep 17 00:00:00 2001 From: Josh Hendricks Date: Tue, 7 Nov 2023 15:50:02 -0800 Subject: [PATCH] refactor: Add dev container - Add `devcontainer.json` and `Dockerfile` with all dependencies necessary to build and test the module, and work on docs with local web server provided by mkdocs. - Install platyPS during build if module not available in a local or CI build. --- .devcontainer/Dockerfile | 10 ++++++++++ .devcontainer/devcontainer.json | 27 +++++++++++++++++++++++++++ build.ps1 | 21 +++++++++++++++------ 3 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 00000000..a95894f8 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,10 @@ +FROM mcr.microsoft.com/powershell:lts-debian-11 + +SHELL [ "pwsh", "-NoLogo", "-NoProfile", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; " ] +RUN apt update && apt install -y --no-install-recommends libc6-dev python3-pip mkdocs; \ + pip install mkdocs-material mkdocs-awesome-pages-plugin -q --no-input; \ + Install-Module Pester -RequiredVersion 5.5.0 -Force; \ + Install-Module psake -RequiredVersion 4.9.0 -Force; \ + Install-Module PSReadLine -RequiredVersion 2.3.4 -Force; \ + Install-Module PSScriptAnalyzer -RequiredVersion 1.21.0 -Force; \ + Install-Module platyPS -RequiredVersion 0.14.2 -Force; diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000..7a362986 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,27 @@ +{ + "name": "PowerShell", + "dockerFile": "Dockerfile", + "features": { + "ghcr.io/devcontainers/features/common-utils:2": { + "installZsh": "true", + "username": "vscode", + "upgradePackages": "false", + "nonFreePackages": "true" + } + }, + "postCreateCommand": "sudo chsh vscode -s \"$(which pwsh)\"", + "customizations": { + "vscode": { + "settings": { + "powershell.powerShellDefaultVersion": "PowerShell", + "terminal.integrated.defaultProfile.linux": "pwsh" + }, + "extensions": [ + "ms-vscode.powershell", + "redhat.vscode-yaml", + "eamodio.gitlens" + ] + } + }, + "forwardPorts": [8000] +} diff --git a/build.ps1 b/build.ps1 index c813bc49..a5ce52bc 100644 --- a/build.ps1 +++ b/build.ps1 @@ -8,6 +8,12 @@ param() $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' + +if ($null -eq (Get-Module -ListAvailable -Name platyPS | Where-Object Version -eq '0.14.2')) { + Write-Host "Installing platyPS" -ForegroundColor Green + Install-Module -Name platyPS -RequiredVersion '0.14.2' -Force -Scope CurrentUser -ErrorAction Stop +} + $manifest = Import-PowerShellDataFile -Path $PSScriptRoot/ImportExcel/ImportExcel.psd1 $outputFolder = Join-Path -Path $PSScriptRoot -ChildPath "Output/ImportExcel/$($manifest.ModuleVersion)" @@ -26,11 +32,13 @@ $Build = @{ } # Stage module in output directory +Write-Host "Copying ./ImportExcel/* to $($Build.Output.Directory)" -ForegroundColor Green $null = New-Item -Path $Build.Output.Directory -ItemType Directory -Force Get-ChildItem -Path $Build.Output.Directory | Remove-Item -Recurse Get-ChildItem -Path ./ImportExcel/ | Copy-Item -Destination $Build.Output.Directory -Recurse # Embed dot-sourced functions in the PSM1 file +Write-Host "Merging .PS1 files into ImportExcel.psm1" -ForegroundColor Green try { Push-Location -Path $Build.Output.Directory $usings = @{} @@ -59,14 +67,14 @@ try { if ($usings.Count) { $usings.Keys | Sort-Object | ForEach-Object { - $null = $newPSM1.AppendLine($_) + $null = $newPSM1.Append("$_`r`n") } $usings.Clear() } if ($_ -eq '#region Dot-Sourced Functions') { $insideDotSourcedRegion = $true - $null = $newPSM1.AppendLine($content) + $null = $newPSM1.Append("$content`r`n") return } @@ -78,7 +86,7 @@ try { return } - $null = $newPSM1.AppendLine($_) + $null = $newPSM1.Append("$_`r`n") } if ($content.Length) { throw "An error occurred while embedding files from directories referenced in dot-sources.txt. @@ -92,6 +100,7 @@ try { } # Update docs +Write-Host "Generating / updating markdown help" -ForegroundColor Green Import-Module $Build.Output.ManifestPath -Force $null = New-Item -Path $Build.Docs.Directory -ItemType Directory -Force $existingHelp = (Get-ChildItem -Path "$($Build.Docs.Directory)/*.md").BaseName @@ -99,21 +108,21 @@ $newCommands = Get-Command -Module ImportExcel -CommandType Function, Cmdlet | W if ($existingHelp) { $null = Update-MarkdownHelp -Path $Build.Docs.Directory -AlphabeticParamsOrder -ExcludeDontShow } -if ($true) { +if ($newCommands) { $newHelpArgs = @{ Module = 'ImportExcel' OutputFolder = $Build.Docs.Directory Locale = $Build.Docs.Locale AlphabeticParamsOrder = $true ExcludeDontShow = $true - WithModulePage = $true ErrorAction = 'SilentlyContinue' } $null = New-MarkdownHelp @newHelpArgs } # Add online help URL for all commands -$onlineversionpattern = [regex]::new('^online version:.*$', ([RegexOptions]::IgnoreCase, [RegexOptions]::Multiline)) +Write-Host "Updating online help URLs" -ForegroundColor Green +$onlineversionpattern = [regex]::new('(?<=\n)online version:.*?(?=[\r\n])', ([RegexOptions]::IgnoreCase, [RegexOptions]::Multiline)) foreach ($path in [io.directory]::EnumerateFiles($Build.Docs.Directory, '*.md')) { $baseName = ([fileinfo]$path).BaseName $content = [file]::ReadAllText($path)