Last active
April 16, 2018 15:04
-
-
Save SQLDBAWithABeard/3d2e66cf86bd936adf653e12b1960cf0 to your computer and use it in GitHub Desktop.
Code for VSTS Build and release for PowerShell Gallery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ModuleName = 'BeardAnalysis' | |
$Description = "This is a demo module for demoing Plaster and TDD with Pester and CI with VSTS to the PowerShell Gallery" | |
$plaster = @{ | |
TemplatePath = "GIT:\PlasterTemplate" #(Split-Path $manifestProperties.Path) | |
DestinationPath = "Git:\$ModuleName" | |
FullName = "Rob Sewell" | |
ModuleName = $ModuleName | |
ModuleDesc = $Description | |
Version = '0.9.18' | |
GitHubUserName = "SQLDBAWithABeard" | |
GitHubRepo = $ModuleName | |
} | |
If(!(Test-Path $plaster.DestinationPath)) | |
{ | |
New-Item -ItemType Directory -Path $plaster.DestinationPath | |
} | |
Invoke-Plaster @plaster -Verbose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set location to module home path in artifacts directory | |
try { | |
Set-Location $(System.DefaultWorkingDirectory)\PowerShell_Gallery_Build\BeardAnalysis | |
} | |
catch { | |
Write-Error "Failed to set location" | |
} | |
# get the contents of the module manifest file | |
try { | |
$file = (Get-Content .\BeardAnalysis.psd1) | |
} | |
catch { | |
Write-Error "Failed to Get-Content" | |
} | |
# Use RegEx to get the Version Number and set it as a version datatype | |
# \s* - between 0 and many whitespace | |
# ModuleVersion - literal | |
# \s - 1 whitespace | |
# = - literal | |
# \s - 1 whitespace | |
# ' - literal | |
# () - capture Group | |
# \d* - between 0 and many digits | |
# ' - literal | |
# \s* between 0 and many whitespace | |
[version]$Version = [regex]::matches($file, "\s*ModuleVersion\s=\s'(\d*.\d*.\d*)'\s*").groups[1].value | |
Write-Output "Old Version - $Version" | |
# Add one to the build of the version number | |
[version]$NewVersion = $newVersion = "{0}.{1}.{2}" -f $Version.Major, $Version.Minor, ($Version.Build + 1) | |
Write-Output "New Version - $NewVersion" | |
# Replace Old Version Number with New Version number in the file | |
try { | |
(Get-Content .\BeardAnalysis.psd1) -replace $version, $NewVersion | Out-File .\BeardAnalysis.psd1 | |
Write-Output "Updated Module Version from $Version to $NewVersion" | |
} | |
catch { | |
$_ | |
Write-Error "failed to set file" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$manifest = Import-PowerShellDataFile .\BeardAnalysis.psd1 | |
[version]$version = $Manifest.ModuleVersion | |
# Add one to the build of the version number | |
[version]$NewVersion = "{0}.{1}.{2}" -f $Version.Major, $Version.Minor, ($Version.Build + 1) | |
# Update the manifest file | |
Update-ModuleManifest -Path .\BeardAnalysis.psd1 -ModuleVersion $NewVersion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git config user.email "mrrobsewell@outlook.com" | |
git config user.name "SQLDBAWithABeard" | |
git checkout master | |
git add .\BeardAnalysis.psd1 | |
git commit -m "Updated Version Number ***NO_CI***" | |
git push https://$(GitHubPAT)@github.com/SQLDBAWithABeard/BeardAnalysis.git HEAD:master | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set location to module home path in artifacts directory | |
try { | |
Set-Location $(System.DefaultWorkingDirectory)\PowerShell_Gallery_Build\BeardAnalysis | |
} | |
catch { | |
Write-Error "Failed to set location" | |
} | |
# get the contents of the module manifest file | |
try { | |
$file = (Get-Content .\BeardAnalysis.psd1 -Raw) | |
} | |
catch { | |
Write-Error "Failed to Get-Content" | |
} | |
# Replace Old Release Notes with the new ones in the file | |
try { | |
# Use RegEx to get the Release Notes | |
# \s* between 0 and many whitespace | |
# ReleaseNotes - literal | |
# \s - 1 whitespace | |
# = - literal | |
# \s - 1 whitespace | |
# ' - literal | |
# () - Capture Group | |
# [\w\s*.'!:;%&-]* - Any word, Any whitespace, any of those characters zero to many times until | |
# ' - literal | |
# \s* - between 0 and many whitespace | |
# } - literal | |
$ReleaseNotes = [regex]::matches($file, "\s*ReleaseNotes\s=\s'([\w\s*.'!:;%&-]*)'\s*").groups[1].value | |
## Use Regex to get Version - see other script for explanation | |
[version]$Version = [regex]::matches($file, "\s*ModuleVersion\s=\s'(\d*.\d*.\d*)'\s*").groups[1].value | |
$NewReleasenotes = (Get-Content .\docs\ReleaseNotes.txt -Raw) | |
## Limit Release Notes to 1000 characters | |
$NewReleasenotes = $NewReleasenotes[0..999] -join "" | |
[regex]::Replace($file, $ReleaseNotes, $NewReleasenotes)|Out-File .\BeardAnalysis.psd1 | |
Write-Output "Updated release Notes" | |
} | |
catch { | |
## Write-Error "failed to set file" | |
$_ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git config user.email "mrrobsewell@outlook.com" | |
git config user.name "SQLDBAWithABeard" | |
git checkout master | |
git add .\BeardAnalysis.psd1 | |
git commit -m "Updated Release Notes in Manifest File ***NO_CI***" | |
git push https://$(GitHubPAT)@github.com/SQLDBAWithABeard/BeardAnalysis.git HEAD:master |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Create folder for Nuget.exe | |
$NugetPath = "$(System.DefaultWorkingDirectory)\Nuget" | |
Write-Output "$NugetPath" | |
if (!(Test-Path $NugetPath)) { | |
Write-Output "Create Nuget folder" | |
try { | |
New-Item -Path $NugetPath -ItemType Directory -Verbose -ErrorAction Stop | |
Write-Output "Success - Created NuGet folder" | |
} | |
catch { | |
Write-Error "Failed To Create Nuget Folder" | |
$_ | |
break | |
} | |
} | |
else { | |
Write-Output "NuGet Folder exists" | |
} | |
## Download Nuget.exe | |
if (!(Test-Path $NugetPath\Nuget.exe)) { | |
Write-Output "Download Nuget.exe to $NugetPath" | |
try { | |
Invoke-WebRequest -Uri "http://go.microsoft.com/fwlink/?LinkID=690216&clcid=0x409" -OutFile $NugetPath\Nuget.exe -ErrorAction Stop | |
Write-Output "Success - Downloaded NuGet.exe" | |
} | |
catch { | |
Write-Error "Failed to download Nuget.exe" | |
$_ | |
break | |
} | |
} | |
else { | |
Write-Output "NuGet.exe exists" | |
} | |
## Add Nuget to Path | |
$PathEnv = [System.Environment]::GetEnvironmentVariable("path") | |
if (!($PathEnv.Contains($NugetPath))) { | |
Write-Output "Add $NugetPath as %PATH%" | |
$PathEnv = $PathEnv + ";" + $NugetPath | |
[System.Environment]::SetEnvironmentVariable("path", $PathEnv) | |
Write-Output "Set Path Env" | |
} | |
else { | |
Write-Output "Env Path contains $NugetPath" | |
} | |
## Install NuGet package Provider | |
if (!(Get-PackageProvider nuget)) { | |
Write-Output "Install NuGet package provider" | |
try { | |
Install-PackageProvider -Name NuGet -Scope CurrentUser -Force -ErrorAction Stop | |
Write-Output "Success - Installed NuGet package provider" | |
} | |
catch { | |
Write-Error "Failed to Install NuGet package provider" | |
$_ | |
break | |
} | |
} | |
else { | |
Write-Output "NuGet package provider already installed" | |
} | |
## Publish Module | |
try { | |
$path = "$(System.DefaultWorkingDirectory)\PowerShell_Gallery_Build\BeardAnalysis" | |
Publish-Module -Path $path -NuGetApiKey $(PowerShell_Gallery_APIKey) -Verbose -ErrorAction Stop | |
Write-Output "Success - Installed NuGet package provider" | |
} | |
catch { | |
$_ | |
Write-Error "Failed to Publish Module" | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment