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

BREAKING CHANGE: addresses #65 and changes output path calculation #98

Merged
merged 10 commits into from
Sep 27, 2020
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"presentation": {
"echo": true,
"reveal": "silent",
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
Expand Down
7 changes: 7 additions & 0 deletions Install-RequiredModule.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Push-Location $PSScriptRoot
try {
Install-Script Install-RequiredModule
Install-RequiredModule
} finally {
Pop-Location
}
36 changes: 18 additions & 18 deletions PotentialContribution/GitHub.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
Write-Debug "Path: $path"
Push-Location
Set-Location $Path

#not safe!
#populate what info we can from the path
$username, $repo = ((git remote -v | ?{$_ -like "*(push)"}).replace('.git','') -split '\s+')[1].split("/") | select -last 2
Expand All @@ -37,7 +37,7 @@
Write-Debug "Username: $username"
Write-Debug "Repository: $repo"
Write-Debug "Branch: $branch"

$status = git status --short|?{$_ -match '(.)(.)\s+(.+)'} | %{[pscustomobject]@{X=$matches[1];Y=$matches[2];File=$matches[3]}}
if($status)
{
Expand All @@ -48,13 +48,13 @@
}
}
Write-Verbose "Status: Committed and up to date"

## update version and commit?
## this will be replaced, just a test for commit purposes
$psd1 = gi ((gi $path).name + ".psd1")
Write-Debug "PSD1: $psd1"


$psd1data = (gc $psd1) | %{
if($_ -match 'moduleversion\s*=(.+)')
{
Expand All @@ -65,7 +65,7 @@
$versionfull = "$versionshort.$(Get-Date -Format "yyyyMMdd.HHmm")"
"ModuleVersion='$versionfull'"
Write-Verbose "New Versioin: $versionfull"

}
else{$_}
}
Expand All @@ -78,49 +78,49 @@
Write-Verbose "Commiting version data"
git commit -a -m "Release Commit - Version Tag $versionfull" | Out-Null
git push origin $branch

Write-Verbose "Creating tag"
$tag = "V$versionshort"
git tag -a $tag -m "Auto Release version $versionfull"
git push --tags

Write-Verbose "Getting Github auth token"
$token = Get-GitToken -Credential $Credential

Write-Verbose "Creating the release"
$GHRelease = New-GitHubRelease -username $username -repo $repo -token $token -tag $tag -branch $branch -Title $title -Description $Description -draft:$draft.IsPresent -Prerelease:$Prerelease.IsPresent

## create a release folder, maybe clear it first?

$releaseFolder = join-path $path Release
Write-Verbose "Creating release folder"
mkdir $releaseFolder -ea 0 | out-null
New-Item -ItemType Directory $releaseFolder -ea 0 | out-null

#create a folder for module files
Write-Verbose "Creating copy of module"
$modtemp = join-path $releaseFolder $moduleName
Write-Debug "creating temp folder: $modtemp"
mkdir $modtemp -ea 0 | Out-Null
New-Item -ItemType Directory $modtemp -ea 0 | Out-Null
#get all files
$filenames = git ls-tree -r $branch --name-only | ? {$_ -notlike ".*"}
$files = $filenames | %{join-path . $_} | gi
#copy files to module temp/release location
copy $files $modtemp
Write-Verbose "Signing moved files"

Write-Verbose "Signing moved files"
if($Certificate)
{
gci $modtemp | Set-AuthenticodeSignature -Certificate $Certificate | Out-Null
}


## create packages
#poshcode/nuget/chocolatey
ipmo $modtemp -Global
sleep 1
Write-Verbose "Creating nuget package"
$nugetfiles = Compress-Module -Module $moduleName -OutputPath $releaseFolder -Force

#exclude xml
#todo: move packageinfo/nuspec to repo add/overwite etc
#todo: rename nuget file to remove extend version info
Expand Down Expand Up @@ -191,7 +191,7 @@ function New-GitHubRepo {
# The name of the Repository to create
[Parameter(Mandatory=$true)]
[String]$Name,
# A short description of the repository
# A short description of the repository
[String]$Description,
# A URL with more information about the repository
[String]$Homepage,
Expand Down Expand Up @@ -298,4 +298,4 @@ function Get-GitToken {
Write-Error "An unexpected error occurred (bad user/password?) $($Error[0])"
}
}
}
}
6 changes: 2 additions & 4 deletions PotentialContribution/ModuleCreation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ $helpPath = Join-Path $modulePath en-US

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

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

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



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

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

iex ("@`"`n{0}`n`"@" -f (gc .\ReadMe.md -Raw)) | Out-File ReadMe.md
cp .\module.psm1 $moduleName.psm1
cp .\module.psm1 $moduleName.psm1
8 changes: 8 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,18 @@ We have a few modules which are required for building. They're listed in `Requir

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

By default, the build script uses [gitversion](/gittols/gitversion) to calculate the version of the build automatically:

```powershell
.\build.ps1
```

If you don't have gitversion handy, you can just specify a version for the `-Semver` parameter:

```powershell
.\build.ps1 -Semver 2.0.0-beta
```

#### 4. Make the compiled module available to Powershell

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.
Expand Down
55 changes: 55 additions & 0 deletions Source/Private/GetRelativePath.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
function GetRelativePath {
<#
.SYNOPSIS
Returns the relative path, or $Path if the paths don't share the same root.
For backward compatibility, this is [System.IO.Path]::GetRelativePath for .NET 4.x
#>
[OutputType([string])]
[CmdletBinding()]
param(
# The source path the result should be relative to. This path is always considered to be a directory.
[Parameter(Mandatory)]
[string]$RelativeTo,

# The destination path.
[Parameter(Mandatory)]
[string]$Path
)

# This giant mess is because PowerShell drives aren't valid filesystem drives
$Drive = $Path -replace "^([^\\/]+:[\\/])?.*", '$1'
if ($Drive -ne ($RelativeTo -replace "^([^\\/]+:[\\/])?.*", '$1')) {
Write-Verbose "Paths on different drives"
return $Path # no commonality, different drive letters on windows
}
$RelativeTo = $RelativeTo -replace "^[^\\/]+:[\\/]", [IO.Path]::DirectorySeparatorChar
$Path = $Path -replace "^[^\\/]+:[\\/]", [IO.Path]::DirectorySeparatorChar
$RelativeTo = [IO.Path]::GetFullPath($RelativeTo).TrimEnd('\/') -replace "^[^\\/]+:[\\/]", [IO.Path]::DirectorySeparatorChar
$Path = [IO.Path]::GetFullPath($Path) -replace "^[^\\/]+:[\\/]", [IO.Path]::DirectorySeparatorChar

$commonLength = 0
while ($Path[$commonLength] -eq $RelativeTo[$commonLength]) {
$commonLength++
}
if ($commonLength -eq $RelativeTo.Length -and $RelativeTo.Length -eq $Path.Length) {
Write-Verbose "Equal Paths"
return "." # The same paths
}
if ($commonLength -eq 0) {
Write-Verbose "Paths on different drives?"
return $Drive + $Path # no commonality, different drive letters on windows
}

Write-Verbose "Common base: $commonLength $($RelativeTo.Substring(0,$commonLength))"
# In case we matched PART of a name, like C:\Users\Joel and C:\Users\Joe
while ($commonLength -gt $RelativeTo.Length -and ($RelativeTo[$commonLength] -ne [IO.Path]::DirectorySeparatorChar)) {
$commonLength--
}

Write-Verbose "Common base: $commonLength $($RelativeTo.Substring(0,$commonLength))"
# create '..' segments for segments past the common on the "$RelativeTo" path
if ($commonLength -lt $RelativeTo.Length) {
$result = @('..') * @($RelativeTo.Substring($commonLength).Split([IO.Path]::DirectorySeparatorChar).Where{ $_ }).Length -join ([IO.Path]::DirectorySeparatorChar)
}
(@($result, $Path.Substring($commonLength).TrimStart([IO.Path]::DirectorySeparatorChar)).Where{ $_ } -join ([IO.Path]::DirectorySeparatorChar))
}
5 changes: 5 additions & 0 deletions Source/Private/InitializeBuild.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ function InitializeBuild {
) -join ' ')"
$BuildInfo = GetBuildInfo -BuildManifest $BuildManifest -BuildCommandInvocation $BuildCommandInvocation

# Override VersionedOutputDirectory with UnversionedOutputDirectory
if ($BuildInfo.UnversionedOutputDirectory -and $BuildInfo.VersionedOutputDirectory) {
$BuildInfo.VersionedOutputDirectory = $false
}

# Finally, add all the information in the module manifest to the return object
if ($ModuleInfo = ImportModuleManifest $BuildInfo.SourcePath) {
# Update the module manifest with our build configuration and output it
Expand Down
71 changes: 56 additions & 15 deletions Source/Private/ResolveOutputFolder.ps1
Original file line number Diff line number Diff line change
@@ -1,39 +1,80 @@
function ResolveOutputFolder {
[CmdletBinding()]
param(
# The name of the module to build
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[Alias("Name")]
[string]$ModuleName,

# Where to resolve the $OutputDirectory from when relative
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[Alias("ModuleBase")]
[string]$Source,

# Where to build the module.
# Defaults to an \output folder, adjacent to the "SourcePath" folder
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]$OutputDirectory,

# If set (true) adds a folder named after the version number to the OutputDirectory
[Parameter(ValueFromPipelineByPropertyName)]
[switch]$VersionedOutputDirectory,

# specifies the module version for use in the output path if -VersionedOutputDirectory is true
[Parameter(ValueFromPipelineByPropertyName)]
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[Alias("ModuleVersion")]
[string]$Version,

# Where to resolve the $OutputDirectory from when relative
# If set (true) adds a folder named after the version number to the OutputDirectory
[Parameter(ValueFromPipelineByPropertyName)]
[Alias("Force")]
[switch]$VersionedOutputDirectory,

# Controls whether or not there is a build or cleanup performed
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]$ModuleBase
[ValidateSet("Clean", "Build", "CleanBuild")]
[string]$Target = "CleanBuild"
)
process {
Write-Verbose "Resolve OutputDirectory path: $OutputDirectory"

# Ensure the OutputDirectory makes sense (it's never blank anymore)
if (!(Split-Path -IsAbsolute $OutputDirectory)) {
# Relative paths are relative to the ModuleBase
$OutputDirectory = Join-Path $ModuleBase $OutputDirectory
$OutputDirectory = Join-Path $Source $OutputDirectory
}
# If they passed in a path with ModuleName\Version on the end...
if ((Split-Path $OutputDirectory -Leaf).EndsWith($Version) -and (Split-Path (Split-Path $OutputDirectory) -Leaf) -eq $ModuleName) {
# strip the version (so we can add it back)
$VersionedOutputDirectory = $true
$OutputDirectory = Split-Path $OutputDirectory
}
# Ensure the OutputDirectory is named "ModuleName"
if ((Split-Path $OutputDirectory -Leaf) -ne $ModuleName) {
# If it wasn't, add a "ModuleName"
$OutputDirectory = Join-Path $OutputDirectory $ModuleName
}
# Ensure the OutputDirectory is not a parent of the SourceDirectory
$RelativeOutputPath = GetRelativePath $OutputDirectory $Source
if (-not $RelativeOutputPath.StartsWith("..") -and $RelativeOutputPath -ne $Source) {
Write-Verbose "Added Version to OutputDirectory path: $OutputDirectory"
$OutputDirectory = Join-Path $OutputDirectory $Version
}
# Ensure the version number is on the OutputDirectory if it's supposed to be
if ($VersionedOutputDirectory -and -not (Split-Path $OutputDirectory -Leaf).EndsWith($Version)) {
Write-Verbose "Added Version to OutputDirectory path: $OutputDirectory"
$OutputDirectory = Join-Path $OutputDirectory $Version
}

# Make sure the OutputDirectory exists (relative to ModuleBase or absolute)
$OutputDirectory = New-Item $OutputDirectory -ItemType Directory -Force | Convert-Path
if ($VersionedOutputDirectory -and $OutputDirectory.TrimEnd("/\") -notmatch "\d+\.\d+\.\d+$") {
$OutputDirectory = New-Item (Join-Path $OutputDirectory $Version) -ItemType Directory -Force | Convert-Path
Write-Verbose "Added ModuleVersion to OutputDirectory path: $OutputDirectory"
if (Test-Path $OutputDirectory -PathType Leaf) {
throw "Unable to build. There is a file in the way at $OutputDirectory"
}

if ($Target -match "Clean") {
Write-Verbose "Cleaning $OutputDirectory"
if (Test-Path $OutputDirectory -PathType Container) {
Remove-Item $OutputDirectory -Recurse -Force
}
}
if ($Target -match "Build") {
# Make sure the OutputDirectory exists (relative to ModuleBase or absolute)
New-Item $OutputDirectory -ItemType Directory -Force | Convert-Path
}
$OutputDirectory
}
}
}
2 changes: 1 addition & 1 deletion Source/Private/SetModuleContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function SetModuleContent {
Write-Verbose "Adding $SourceName"
$SetContent.Process("#Region '$SourceName' 0")
Get-Content $SourceName -OutVariable source | ForEach-Object { $SetContent.Process($_) }
$SetContent.Process("#EndRegion '$SourceName' $($Source.Count)")
$SetContent.Process("#EndRegion '$SourceName' $($Source.Count+1)")
} else {
if(!$ContentStarted) {
$SetContent.Process("#Region 'PREFIX' 0")
Expand Down
Loading