Skip to content
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
5 changes: 3 additions & 2 deletions .publishignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ PesterTestResults.xml
tools/*
Tests/*
img/*
Docs/*.md
cab/*
Docs/*
cab/*
en-US/InstallModuleFromGit-help.xml
2 changes: 1 addition & 1 deletion InstallModuleFromGit.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Author = 'iricigor@outlook.com'
# Copyright = '(c) 2019 Igor. All rights reserved.'

# Description of the functionality provided by this module
Description = 'Installs module from Git repository (i.e. GitHUb or Azure DevOps)'
Description = 'Installs module from Git repository (i.e. GitHub or Azure DevOps aka VSTS)'

# Minimum version of the Windows PowerShell engine required by this module
# PowerShellVersion = ''
Expand Down
89 changes: 89 additions & 0 deletions tools/PublishModule-VSTS.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#
# Script should be executed inside of VSTS pipeline
#

$ModuleName = 'InstallModuleFromGit'

if ($Env:TF_BUILD -ne 'True') {
throw 'This script should be executed only from VSTS. Please use the other publishing script.'
}


# check running folder
if (Test-Path "..\s\$ModuleName.psd1") {
"Checking module $(Resolve-Path ".\$ModuleName.psd1")"
} else {
throw "We are not in correct folder, please run this tool as .\tools\PublishModule.ps1"
}


# test manifest
try {
$Module = Test-ModuleManifest "$ModuleName.psd1" -ea Stop
"Module $ModuleName.psd1 is OK"
} catch {
throw 'Module manifest not in proper format'
}


# test version, must be x.y.z
if (($Module.Version.ToString() -split '\.').Count -lt 3) {
throw "Module version must have three segments at least, currently it is $($Module.Version.ToString())"
} else {
"Module version tag '$($Module.Version.ToString())' is OK"
}


# test if the module is not already published with same version number
"Checking for module with version $($Module.Version) online..."
if (Find-Module -Name $ModuleName -RequiredVersion ($Module.Version) -Repository PSGallery -ea 0) {
throw 'Module with same version already exists'
} else {
"No module with version $($Module.Version) found online"
}


# get publishing key from pipeline or directly
if ($Env:NugetKey) {
"NugetKey found"
} else {
throw 'Please define NugetKey (secret) variable'
}

# copy entire folder to temp location
if ($IsLinux -or $IsMacOS) {$Destination = '/tmp'} else {$Destination = $Env:TEMP}
$Destination2 = Join-Path $Destination $ModuleName
if (Test-Path $Destination2) {Remove-Item $Destination2 -Recurse -Force}

"Copying to $Destination2"
New-Item -Path $Destination2 -ItemType Directory | Out-Null
Get-ChildItem -Force | Copy-Item -Destination $Destination2 -Container -Recurse
"`nverifying content of $Destination2"
Get-ChildItem $Destination2 -Force


# remove not needed files (as per .publishignore)
"Removing not needed files"
$pwdLength = (Get-Item $Destination2).FullName.Length + 1 # expands 'C:\Users\VSSADM~1\AppData\Local\Temp'
foreach ($line in (Get-Content '.publishignore'| where {$_ -notlike '#*'})) {
"Checking files like $line"
foreach ($file in (Get-ChildItem -Path $Destination2 -Recurse -Force -File)) {
$relativeName = $file.FullName.Substring($pwdLength) -replace '\\','/'
#"$relativeName"
if ($relativeName -like $line) {
"Removing $relativeName"
Remove-Item $file.FullName -Recurse -Force
}
}
}


# publish
"Publishing total of $((Get-ChildItem $Destination2 -Recurse -File).Count) files"
Get-ChildItem $Destination2 -Recurse -File
if ($Env:ModuleVersionToPublish -eq $Module.Version) {
Publish-Module -Path $Destination2 -Repository PSGallery -NuGetApiKey $Env:NugetKey -Verbose
"Module $ModuleName published to PowerShell Gallery"
} else {
throw "Mismatching module versions $($Env:ModuleVersionToPublish) and $($Module.Version), please update pipeline variable ModuleVersionToPublish"
}
40 changes: 27 additions & 13 deletions tools/PublishModule.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
#
# Script should be executed manually by developer
#

$ModuleName = 'InstallModuleFromGit'

if ($Env:TF_BUILD -eq 'True') {
throw 'This script should not be executed from VSTS. Please use dedicated script for that.'
}


# check running folder
if (!(Test-Path "..\$ModuleName\$ModuleName.psd1")) {
throw "We are not in correct folder, please run this tool as .\tools\PublishModule.ps1"
if (Test-Path "..\$ModuleName\$ModuleName.psd1") {
"Checking module $(Resolve-Path ".\$ModuleName.psd1")"
} else {
"Checking module $(Resolve-Path "..\$ModuleName\$ModuleName.psd1")"
throw "We are not in correct folder, please run this tool as .\tools\PublishModule.ps1"
}


# test manifest
try {
$Module = Test-ModuleManifest "$ModuleName.psd1" -ea Stop
Expand All @@ -16,42 +25,46 @@ try {
throw 'Module manifest not in proper format'
}


# test version, must be x.y.z
if (($Module.Version.ToString() -split '\.').Count -lt 3) {
throw "Module version must have three segments at least, currently it is $($Module.Version.ToString())"
} else {
"Module version $($Module.Version.ToString()) is OK"
"Module version tag '$($Module.Version.ToString())' is OK"
}

# test if remote is not the same

# test if the module is not already published with same version number
"Checking for module with version $($Module.Version) online..."
if (Find-Module -Name $ModuleName -RequiredVersion ($Module.Version) -Repository PSGallery -ea 0) {
throw 'Module with same version already exists'
} else {
"No module with version $($Module.Version) found online"
}

# get nuget key from somewhere?

# get publishing key
if ($NugetKey) {
"NugetKey found"
} else {
throw 'Please define $NugetKey variable (run $NugetKey = Read-Host)'
}

# copy entire folder to temp location
if ($IsLinux -or $IsMacOS) {$Destination = '/tmp'}
else {$Destination = $Env:TEMP}

# copy entire folder to temp location
if ($IsLinux -or $IsMacOS) {$Destination = '/tmp'} else {$Destination = $Env:TEMP}
$Destination2 = Join-Path $Destination $ModuleName
"Copying to $Destination2"
if (Test-Path $Destination2) {Remove-Item $Destination2 -Recurse -Force}

"Copying to $Destination2"
Copy-Item -Path . -Destination $Destination -Recurse # it creates folder $ModuleName


# remove not needed files (as per .publishignore)
"Removing not needed files"
$pwdLength = $Destination2.Length + 1
$pwdLength = (Get-Item $Destination2).FullName.Length + 1 # expands 'C:\Users\VSSADM~1\AppData\Local\Temp'
foreach ($line in (Get-Content '.publishignore'| where {$_ -notlike '#*'})) {
#"Checking files like $line"
"Checking files like $line"
foreach ($file in (Get-ChildItem -Path $Destination2 -Recurse -Force -File)) {
$relativeName = $file.FullName.Substring($pwdLength) -replace '\\','/'
#"$relativeName"
Expand All @@ -63,6 +76,7 @@ foreach ($line in (Get-Content '.publishignore'| where {$_ -notlike '#*'})) {
}

# publish
"Publishing total of $((Get-ChildItem $Destination2 -Recurse -File).Count) files"
Read-Host "All prerequisites check. Press Enter to Publish module or Ctrl+C to abort"
Publish-Module -Path $Destination2 -Repository PSGallery -NuGetApiKey $NugetKey -Verbose
"Module $ModuleName published to PowerShell Gallery"
"Module $ModuleName published to PowerShell Gallery"