Skip to content

Commit

Permalink
refactor: Add dev container
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
joshooaj committed Nov 8, 2023
1 parent 0dc17c9 commit 5603d7d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
10 changes: 10 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -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;
27 changes: 27 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -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]
}
21 changes: 15 additions & 6 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)"

Expand All @@ -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 = @{}
Expand Down Expand Up @@ -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
}

Expand All @@ -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.
Expand All @@ -92,28 +100,29 @@ 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
$newCommands = Get-Command -Module ImportExcel -CommandType Function, Cmdlet | Where-Object Name -NotIn $existingHelp
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)
Expand Down

0 comments on commit 5603d7d

Please sign in to comment.