-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathResolveOutputFolder.ps1
More file actions
80 lines (72 loc) · 3.64 KB
/
ResolveOutputFolder.ps1
File metadata and controls
80 lines (72 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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,
# specifies the module version for use in the output path if -VersionedOutputDirectory is true
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[Alias("ModuleVersion")]
[string]$Version,
# 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)]
[ValidateSet("Clean", "Build", "CleanBuild")]
[string]$Target
)
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 $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
}
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
}
}
}