Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated dependencies #283

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions modules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"dependencies": {},
"devDependencies": {
"Pester": {
"version": "4.10.1"
},
"platyPS": {
"version": "0.14.2"
},
"PSScriptAnalyzer": {
"version": "1.21.0"
}
}
}
53 changes: 8 additions & 45 deletions pipeline.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ task CopyModule {
task BuildModule BuildDotNet, CopyModule, VersionModule

# Synopsis: Build help
task BuildHelp BuildModule, PlatyPS, {
task BuildHelp BuildModule, Dependencies, {
# Avoid YamlDotNet issue in same app domain
exec {
$pwshPath = (Get-Process -Id $PID).Path;
Expand Down Expand Up @@ -177,50 +177,22 @@ task VersionModule {
}
}

task ReleaseModule VersionModule, {
$modulePath = (Join-Path -Path $ArtifactPath -ChildPath 'PSDocs');
Write-Verbose -Message "[ReleaseModule] -- Checking module path: $modulePath";

if (!(Test-Path -Path $modulePath)) {
Write-Error -Message "[ReleaseModule] -- Module path does not exist";
}
elseif (![String]::IsNullOrEmpty($ApiKey)) {
Publish-Module -Path $modulePath -NuGetApiKey $ApiKey;
}
}

# Synopsis: Install NuGet provider
task NuGet {
if ($Null -eq (Get-PackageProvider -Name NuGet -ErrorAction Ignore)) {
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser;
}
}

# Synopsis: Install Pester module
task Pester NuGet, {
if ($Null -eq (Get-InstalledModule -Name Pester -RequiredVersion 4.10.1 -ErrorAction Ignore)) {
Install-Module -Name Pester -RequiredVersion 4.10.1 -Scope CurrentUser -Force -SkipPublisherCheck;
}
Import-Module -Name Pester -RequiredVersion 4.10.1 -Verbose:$False;
}

# Synopsis: Install PSScriptAnalyzer module
task PSScriptAnalyzer NuGet, {
if ($Null -eq (Get-InstalledModule -Name PSScriptAnalyzer -MinimumVersion 1.18.1 -ErrorAction Ignore)) {
Install-Module -Name PSScriptAnalyzer -MinimumVersion 1.18.1 -Scope CurrentUser -Force;
}
Import-Module -Name PSScriptAnalyzer -Verbose:$False;
}

# Synopsis: Install PlatyPS module
task platyPS {
if ($Null -eq (Get-InstalledModule -Name PlatyPS -MinimumVersion 0.14.0 -ErrorAction Ignore)) {
Install-Module -Name PlatyPS -Scope CurrentUser -MinimumVersion 0.14.0 -Force;
}
task Dependencies NuGet, {
Import-Module $PWD/scripts/dependencies.psm1;
Install-Dependencies -Path $PWD/modules.json -Dev;
}

# Synopsis: Test the module
task TestModule Pester, PSScriptAnalyzer, {
task TestModule Dependencies, {
Import-Module Pester -RequiredVersion 4.10.1 -Force;

# Run Pester tests
$pesterParams = @{ Path = $PWD; OutputFile = 'reports/pester-unit.xml'; OutputFormat = 'NUnitXml'; PesterOption = @{ IncludeVSCodeMarker = $True }; PassThru = $True; };

Expand Down Expand Up @@ -255,17 +227,10 @@ task Benchmark {
}

# Synopsis: Run script analyzer
task Analyze Build, PSScriptAnalyzer, {
task Analyze Build, Dependencies, {
Invoke-ScriptAnalyzer -Path out/modules/PSDocs;
}

# Synopsis: Add shipit build tag
task TagBuild {
if ($Null -ne $Env:BUILD_DEFINITIONNAME) {
Write-Host "`#`#vso[build.addbuildtag]shipit";
}
}

# Synopsis: Remove temp files.
task Clean {
Remove-Item -Path out,reports -Recurse -Force -ErrorAction SilentlyContinue;
Expand All @@ -275,7 +240,5 @@ task Build Clean, BuildModule, VersionModule, BuildHelp

task Test Build, TestDotNet, TestModule

task Release ReleaseModule, TagBuild

# Synopsis: Build and test. Entry point for CI Build stage
task . Build, TestDotNet
152 changes: 152 additions & 0 deletions scripts/dependencies.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# Note:
# Handles dependencies updates.

function Update-Dependencies {
[CmdletBinding()]
param (
[Parameter(Mandatory = $False)]
[String]$Path = (Join-Path -Path $PWD -ChildPath 'modules.json'),

[Parameter(Mandatory = $False)]
[String]$Repository = 'PSGallery'
)
process {
$modules = Get-Content -Path $Path -Raw | ConvertFrom-Json -AsHashtable;
$dependencies = CheckVersion $modules.dependencies -Repository $Repository;
$devDependencies = CheckVersion $modules.devDependencies -Repository $Repository -Dev;

$modules = [Ordered]@{
dependencies = $dependencies
devDependencies = $devDependencies
}
$modules | ConvertTo-Json -Depth 10 | Set-Content -Path $Path;

$updates = @(git status --porcelain);
if ($Null -ne $Env:WORKING_BRANCH -and $Null -ne $updates -and $updates.Length -gt 0) {
git add modules.json;
git commit -m "Update $path";
git push --force -u origin $Env:WORKING_BRANCH;

$existingBranch = @(gh pr list --head $Env:WORKING_BRANCH --state open --json number | ConvertFrom-Json);
if ($Null -eq $existingBranch -or $existingBranch.Length -eq 0) {
gh pr create -B 'main' -H $Env:WORKING_BRANCH -l 'dependencies' -t 'Bump PowerShell dependencies' -F 'out/updates.txt';
}
else {
$pr = $existingBranch[0].number
gh pr edit $pr -F 'out/updates.txt';
}
}
}
}

function Install-Dependencies {
[CmdletBinding()]
param (
[Parameter(Mandatory = $False)]
[String]$Path = (Join-Path -Path $PWD -ChildPath 'modules.json'),

[Parameter(Mandatory = $False)]
[String]$Repository = 'PSGallery',

[Parameter(Mandatory = $False)]
[Switch]$Dev
)
process {
$modules = Get-Content -Path $Path -Raw | ConvertFrom-Json;
InstallVersion $modules.dependencies -Repository $Repository;
if ($Dev) {
InstallVersion $modules.devDependencies -Repository $Repository -Dev;
}
}
}

function CheckVersion {
[CmdletBinding()]
[OutputType([System.Collections.Specialized.OrderedDictionary])]
param (
[Parameter(Mandatory = $True)]
[Hashtable]$InputObject,

[Parameter(Mandatory = $True)]
[String]$Repository,

[Parameter(Mandatory = $False)]
[Switch]$Dev,

[Parameter(Mandatory = $False)]
[String]$OutputPath = 'out/'
)
begin {
$group = 'Dependencies';
if ($Dev) {
$group = 'DevDependencies';
}
if (!(Test-Path -Path $OutputPath)) {
$Null = New-Item -Path $OutputPath -ItemType Directory -Force;
}
$changeNotes = Join-Path -Path $OutputPath -ChildPath 'updates.txt';
}
process {
$dependencies = [Ordered]@{ };
$InputObject.GetEnumerator() | Sort-Object -Property Name | ForEach-Object {
$dependencies[$_.Name] = $_.Value
}
foreach ($module in $dependencies.GetEnumerator()) {
Write-Host -Object "[$group] -- Checking $($module.Name)";
$installParams = @{}
$installParams += $module.Value;
$installParams.MinimumVersion = $installParams.version;
$installParams.Remove('version');
$available = @(Find-Module -Repository $Repository -Name $module.Name @installParams -ErrorAction Ignore);
foreach ($found in $available) {
if (([Version]$found.Version) -gt ([Version]$module.Value.version)) {
Write-Host -Object "[$group] -- Newer version found $($found.Version)";
$dependencies[$module.Name].version = $found.Version;
$Null = Add-Content -Path $changeNotes -Value "Bump $($module.Name) to v$($found.Version).";
}
else {
Write-Host -Object "[$group] -- Already up to date.";
}
}
}
return $dependencies;
}
}

function InstallVersion {
[CmdletBinding()]
[OutputType([void])]
param (
[Parameter(Mandatory = $True)]
[PSObject]$InputObject,

[Parameter(Mandatory = $True)]
[String]$Repository,

[Parameter(Mandatory = $False)]
[Switch]$Dev
)
begin {
$group = 'Dependencies';
if ($Dev) {
$group = 'DevDependencies';
}
}
process {
foreach ($module in $InputObject.PSObject.Properties.GetEnumerator()) {
Write-Host -Object "[$group] -- Installing $($module.Name) v$($module.Value.version)";
$installParams = @{ RequiredVersion = $module.Value.version };
if ($Null -eq (Get-InstalledModule -Name $module.Name @installParams -ErrorAction Ignore)) {
Install-Module -Name $module.Name @installParams -Force -Repository $Repository;
}
}
}
}

Export-ModuleMember -Function @(
'Update-Dependencies'
'Install-Dependencies'
)
Loading