Skip to content

Commit f821a05

Browse files
authored
Merge pull request #98 from PoshCode/jaykul/fix/93
BREAKING CHANGE: addresses #65 and changes output path calculation
2 parents acd9976 + c2fb49e commit f821a05

21 files changed

+703
-346
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"presentation": {
1818
"echo": true,
19-
"reveal": "silent",
19+
"reveal": "always",
2020
"focus": false,
2121
"panel": "shared",
2222
"showReuseMessage": true,

Install-RequiredModule.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Push-Location $PSScriptRoot
2+
try {
3+
Install-Script Install-RequiredModule
4+
Install-RequiredModule
5+
} finally {
6+
Pop-Location
7+
}

PotentialContribution/GitHub.psm1

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
Write-Debug "Path: $path"
2727
Push-Location
2828
Set-Location $Path
29-
29+
3030
#not safe!
3131
#populate what info we can from the path
3232
$username, $repo = ((git remote -v | ?{$_ -like "*(push)"}).replace('.git','') -split '\s+')[1].split("/") | select -last 2
@@ -37,7 +37,7 @@
3737
Write-Debug "Username: $username"
3838
Write-Debug "Repository: $repo"
3939
Write-Debug "Branch: $branch"
40-
40+
4141
$status = git status --short|?{$_ -match '(.)(.)\s+(.+)'} | %{[pscustomobject]@{X=$matches[1];Y=$matches[2];File=$matches[3]}}
4242
if($status)
4343
{
@@ -48,13 +48,13 @@
4848
}
4949
}
5050
Write-Verbose "Status: Committed and up to date"
51-
51+
5252
## update version and commit?
5353
## this will be replaced, just a test for commit purposes
5454
$psd1 = gi ((gi $path).name + ".psd1")
5555
Write-Debug "PSD1: $psd1"
5656

57-
57+
5858
$psd1data = (gc $psd1) | %{
5959
if($_ -match 'moduleversion\s*=(.+)')
6060
{
@@ -65,7 +65,7 @@
6565
$versionfull = "$versionshort.$(Get-Date -Format "yyyyMMdd.HHmm")"
6666
"ModuleVersion='$versionfull'"
6767
Write-Verbose "New Versioin: $versionfull"
68-
68+
6969
}
7070
else{$_}
7171
}
@@ -78,49 +78,49 @@
7878
Write-Verbose "Commiting version data"
7979
git commit -a -m "Release Commit - Version Tag $versionfull" | Out-Null
8080
git push origin $branch
81-
81+
8282
Write-Verbose "Creating tag"
8383
$tag = "V$versionshort"
8484
git tag -a $tag -m "Auto Release version $versionfull"
8585
git push --tags
8686

8787
Write-Verbose "Getting Github auth token"
8888
$token = Get-GitToken -Credential $Credential
89-
89+
9090
Write-Verbose "Creating the release"
9191
$GHRelease = New-GitHubRelease -username $username -repo $repo -token $token -tag $tag -branch $branch -Title $title -Description $Description -draft:$draft.IsPresent -Prerelease:$Prerelease.IsPresent
92-
92+
9393
## create a release folder, maybe clear it first?
94-
94+
9595
$releaseFolder = join-path $path Release
9696
Write-Verbose "Creating release folder"
97-
mkdir $releaseFolder -ea 0 | out-null
98-
97+
New-Item -ItemType Directory $releaseFolder -ea 0 | out-null
98+
9999
#create a folder for module files
100100
Write-Verbose "Creating copy of module"
101101
$modtemp = join-path $releaseFolder $moduleName
102102
Write-Debug "creating temp folder: $modtemp"
103-
mkdir $modtemp -ea 0 | Out-Null
103+
New-Item -ItemType Directory $modtemp -ea 0 | Out-Null
104104
#get all files
105105
$filenames = git ls-tree -r $branch --name-only | ? {$_ -notlike ".*"}
106106
$files = $filenames | %{join-path . $_} | gi
107107
#copy files to module temp/release location
108108
copy $files $modtemp
109-
110-
Write-Verbose "Signing moved files"
109+
110+
Write-Verbose "Signing moved files"
111111
if($Certificate)
112112
{
113113
gci $modtemp | Set-AuthenticodeSignature -Certificate $Certificate | Out-Null
114114
}
115115

116-
116+
117117
## create packages
118118
#poshcode/nuget/chocolatey
119119
ipmo $modtemp -Global
120120
sleep 1
121121
Write-Verbose "Creating nuget package"
122122
$nugetfiles = Compress-Module -Module $moduleName -OutputPath $releaseFolder -Force
123-
123+
124124
#exclude xml
125125
#todo: move packageinfo/nuspec to repo add/overwite etc
126126
#todo: rename nuget file to remove extend version info
@@ -191,7 +191,7 @@ function New-GitHubRepo {
191191
# The name of the Repository to create
192192
[Parameter(Mandatory=$true)]
193193
[String]$Name,
194-
# A short description of the repository
194+
# A short description of the repository
195195
[String]$Description,
196196
# A URL with more information about the repository
197197
[String]$Homepage,
@@ -298,4 +298,4 @@ function Get-GitToken {
298298
Write-Error "An unexpected error occurred (bad user/password?) $($Error[0])"
299299
}
300300
}
301-
}
301+
}

PotentialContribution/ModuleCreation.ps1

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ $helpPath = Join-Path $modulePath en-US
88

99
$HelpFile = Join-Path $helpPath "$moduleName.psm1-help.xml"
1010

11-
mkdir $helpPath -Force #create both module folder and help folder
11+
New-Item -ItemType Directory $helpPath -Force #create both module folder and help folder
1212

1313
New-ModuleManifest $modulePath -RootModule "$moduleName.psm1" -ModuleVersion 0.0.0.1
1414

15-
16-
1715
New-Item -Path $modulePath -Name "${moduleName}_${guid}_HelpInfo.xml" -ItemType File -Value "" ## templated, get guid from psd1
1816
New-Item -Path $helpPath -Name "$moduleName.psm1-help.xml" -ItemType File -Value "" #pull value from template some place
1917

@@ -23,4 +21,4 @@ New-Item -Path $helpPath -Name "$moduleName.psm1-help.xml" -ItemType File -Value
2321
## assumed variables based on Replace: $moduleName, $ShortDescription
2422

2523
iex ("@`"`n{0}`n`"@" -f (gc .\ReadMe.md -Raw)) | Out-File ReadMe.md
26-
cp .\module.psm1 $moduleName.psm1
24+
cp .\module.psm1 $moduleName.psm1

ReadMe.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,18 @@ We have a few modules which are required for building. They're listed in `Requir
4040

4141
#### 3. Run the `build.ps1` script.
4242

43+
By default, the build script uses [gitversion](/gittols/gitversion) to calculate the version of the build automatically:
44+
4345
```powershell
4446
.\build.ps1
4547
```
4648

49+
If you don't have gitversion handy, you can just specify a version for the `-Semver` parameter:
50+
51+
```powershell
52+
.\build.ps1 -Semver 2.0.0-beta
53+
```
54+
4755
#### 4. Make the compiled module available to Powershell
4856

4957
The `.\build.ps1` process will output the path to the folder named with the current version number, like "1.0.0" -- the compiled psm1 and psd1 files are in that folder. In order for PowerShell to find them when you ask it to import, they need to be in the PSModulePath. PowerShell expects to find modules in a folder with a matching name that sits in one of the folders in your PSModulePath.

Source/Private/GetRelativePath.ps1

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
function GetRelativePath {
2+
<#
3+
.SYNOPSIS
4+
Returns the relative path, or $Path if the paths don't share the same root.
5+
For backward compatibility, this is [System.IO.Path]::GetRelativePath for .NET 4.x
6+
#>
7+
[OutputType([string])]
8+
[CmdletBinding()]
9+
param(
10+
# The source path the result should be relative to. This path is always considered to be a directory.
11+
[Parameter(Mandatory)]
12+
[string]$RelativeTo,
13+
14+
# The destination path.
15+
[Parameter(Mandatory)]
16+
[string]$Path
17+
)
18+
19+
# This giant mess is because PowerShell drives aren't valid filesystem drives
20+
$Drive = $Path -replace "^([^\\/]+:[\\/])?.*", '$1'
21+
if ($Drive -ne ($RelativeTo -replace "^([^\\/]+:[\\/])?.*", '$1')) {
22+
Write-Verbose "Paths on different drives"
23+
return $Path # no commonality, different drive letters on windows
24+
}
25+
$RelativeTo = $RelativeTo -replace "^[^\\/]+:[\\/]", [IO.Path]::DirectorySeparatorChar
26+
$Path = $Path -replace "^[^\\/]+:[\\/]", [IO.Path]::DirectorySeparatorChar
27+
$RelativeTo = [IO.Path]::GetFullPath($RelativeTo).TrimEnd('\/') -replace "^[^\\/]+:[\\/]", [IO.Path]::DirectorySeparatorChar
28+
$Path = [IO.Path]::GetFullPath($Path) -replace "^[^\\/]+:[\\/]", [IO.Path]::DirectorySeparatorChar
29+
30+
$commonLength = 0
31+
while ($Path[$commonLength] -eq $RelativeTo[$commonLength]) {
32+
$commonLength++
33+
}
34+
if ($commonLength -eq $RelativeTo.Length -and $RelativeTo.Length -eq $Path.Length) {
35+
Write-Verbose "Equal Paths"
36+
return "." # The same paths
37+
}
38+
if ($commonLength -eq 0) {
39+
Write-Verbose "Paths on different drives?"
40+
return $Drive + $Path # no commonality, different drive letters on windows
41+
}
42+
43+
Write-Verbose "Common base: $commonLength $($RelativeTo.Substring(0,$commonLength))"
44+
# In case we matched PART of a name, like C:\Users\Joel and C:\Users\Joe
45+
while ($commonLength -gt $RelativeTo.Length -and ($RelativeTo[$commonLength] -ne [IO.Path]::DirectorySeparatorChar)) {
46+
$commonLength--
47+
}
48+
49+
Write-Verbose "Common base: $commonLength $($RelativeTo.Substring(0,$commonLength))"
50+
# create '..' segments for segments past the common on the "$RelativeTo" path
51+
if ($commonLength -lt $RelativeTo.Length) {
52+
$result = @('..') * @($RelativeTo.Substring($commonLength).Split([IO.Path]::DirectorySeparatorChar).Where{ $_ }).Length -join ([IO.Path]::DirectorySeparatorChar)
53+
}
54+
(@($result, $Path.Substring($commonLength).TrimStart([IO.Path]::DirectorySeparatorChar)).Where{ $_ } -join ([IO.Path]::DirectorySeparatorChar))
55+
}

Source/Private/InitializeBuild.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ function InitializeBuild {
3333
) -join ' ')"
3434
$BuildInfo = GetBuildInfo -BuildManifest $BuildManifest -BuildCommandInvocation $BuildCommandInvocation
3535

36+
# Override VersionedOutputDirectory with UnversionedOutputDirectory
37+
if ($BuildInfo.UnversionedOutputDirectory -and $BuildInfo.VersionedOutputDirectory) {
38+
$BuildInfo.VersionedOutputDirectory = $false
39+
}
40+
3641
# Finally, add all the information in the module manifest to the return object
3742
if ($ModuleInfo = ImportModuleManifest $BuildInfo.SourcePath) {
3843
# Update the module manifest with our build configuration and output it
Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,80 @@
11
function ResolveOutputFolder {
22
[CmdletBinding()]
33
param(
4+
# The name of the module to build
5+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
6+
[Alias("Name")]
7+
[string]$ModuleName,
8+
9+
# Where to resolve the $OutputDirectory from when relative
10+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
11+
[Alias("ModuleBase")]
12+
[string]$Source,
13+
414
# Where to build the module.
515
# Defaults to an \output folder, adjacent to the "SourcePath" folder
616
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
717
[string]$OutputDirectory,
818

9-
# If set (true) adds a folder named after the version number to the OutputDirectory
10-
[Parameter(ValueFromPipelineByPropertyName)]
11-
[switch]$VersionedOutputDirectory,
12-
1319
# specifies the module version for use in the output path if -VersionedOutputDirectory is true
14-
[Parameter(ValueFromPipelineByPropertyName)]
20+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
1521
[Alias("ModuleVersion")]
1622
[string]$Version,
1723

18-
# Where to resolve the $OutputDirectory from when relative
24+
# If set (true) adds a folder named after the version number to the OutputDirectory
25+
[Parameter(ValueFromPipelineByPropertyName)]
26+
[Alias("Force")]
27+
[switch]$VersionedOutputDirectory,
28+
29+
# Controls whether or not there is a build or cleanup performed
1930
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
20-
[string]$ModuleBase
31+
[ValidateSet("Clean", "Build", "CleanBuild")]
32+
[string]$Target = "CleanBuild"
2133
)
2234
process {
2335
Write-Verbose "Resolve OutputDirectory path: $OutputDirectory"
2436

2537
# Ensure the OutputDirectory makes sense (it's never blank anymore)
2638
if (!(Split-Path -IsAbsolute $OutputDirectory)) {
2739
# Relative paths are relative to the ModuleBase
28-
$OutputDirectory = Join-Path $ModuleBase $OutputDirectory
40+
$OutputDirectory = Join-Path $Source $OutputDirectory
41+
}
42+
# If they passed in a path with ModuleName\Version on the end...
43+
if ((Split-Path $OutputDirectory -Leaf).EndsWith($Version) -and (Split-Path (Split-Path $OutputDirectory) -Leaf) -eq $ModuleName) {
44+
# strip the version (so we can add it back)
45+
$VersionedOutputDirectory = $true
46+
$OutputDirectory = Split-Path $OutputDirectory
47+
}
48+
# Ensure the OutputDirectory is named "ModuleName"
49+
if ((Split-Path $OutputDirectory -Leaf) -ne $ModuleName) {
50+
# If it wasn't, add a "ModuleName"
51+
$OutputDirectory = Join-Path $OutputDirectory $ModuleName
52+
}
53+
# Ensure the OutputDirectory is not a parent of the SourceDirectory
54+
$RelativeOutputPath = GetRelativePath $OutputDirectory $Source
55+
if (-not $RelativeOutputPath.StartsWith("..") -and $RelativeOutputPath -ne $Source) {
56+
Write-Verbose "Added Version to OutputDirectory path: $OutputDirectory"
57+
$OutputDirectory = Join-Path $OutputDirectory $Version
58+
}
59+
# Ensure the version number is on the OutputDirectory if it's supposed to be
60+
if ($VersionedOutputDirectory -and -not (Split-Path $OutputDirectory -Leaf).EndsWith($Version)) {
61+
Write-Verbose "Added Version to OutputDirectory path: $OutputDirectory"
62+
$OutputDirectory = Join-Path $OutputDirectory $Version
2963
}
3064

31-
# Make sure the OutputDirectory exists (relative to ModuleBase or absolute)
32-
$OutputDirectory = New-Item $OutputDirectory -ItemType Directory -Force | Convert-Path
33-
if ($VersionedOutputDirectory -and $OutputDirectory.TrimEnd("/\") -notmatch "\d+\.\d+\.\d+$") {
34-
$OutputDirectory = New-Item (Join-Path $OutputDirectory $Version) -ItemType Directory -Force | Convert-Path
35-
Write-Verbose "Added ModuleVersion to OutputDirectory path: $OutputDirectory"
65+
if (Test-Path $OutputDirectory -PathType Leaf) {
66+
throw "Unable to build. There is a file in the way at $OutputDirectory"
67+
}
68+
69+
if ($Target -match "Clean") {
70+
Write-Verbose "Cleaning $OutputDirectory"
71+
if (Test-Path $OutputDirectory -PathType Container) {
72+
Remove-Item $OutputDirectory -Recurse -Force
73+
}
74+
}
75+
if ($Target -match "Build") {
76+
# Make sure the OutputDirectory exists (relative to ModuleBase or absolute)
77+
New-Item $OutputDirectory -ItemType Directory -Force | Convert-Path
3678
}
37-
$OutputDirectory
3879
}
39-
}
80+
}

Source/Private/SetModuleContent.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function SetModuleContent {
4848
Write-Verbose "Adding $SourceName"
4949
$SetContent.Process("#Region '$SourceName' 0")
5050
Get-Content $SourceName -OutVariable source | ForEach-Object { $SetContent.Process($_) }
51-
$SetContent.Process("#EndRegion '$SourceName' $($Source.Count)")
51+
$SetContent.Process("#EndRegion '$SourceName' $($Source.Count+1)")
5252
} else {
5353
if(!$ContentStarted) {
5454
$SetContent.Process("#Region 'PREFIX' 0")

0 commit comments

Comments
 (0)