-
Notifications
You must be signed in to change notification settings - Fork 4
/
packer.ps1
91 lines (75 loc) · 2.14 KB
/
packer.ps1
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
81
82
83
84
85
86
87
88
89
90
91
#
# Vagrant Windows box factory
#
# @author Luke Carrier <[email protected]>
# @copyright 2015 Luke Carrier
# @license GPL v3
#
function Get-LargestContiguousBlock {
Param(
[Parameter(Mandatory=$true)]
[char] $Character,
[Parameter(Mandatory=$true)]
[string] $Text
)
$matches = @{}
$previousMatched = $false
$previousStart = 0
$Text.ToCharArray() | ForEach-Object {
if ($_ -eq $Character) {
if ($previousMatched) {
$matches[$previousStart] += 1
} else {
$matches[$previousStart] = 1
$previousMatched = $true
}
} else {
$previousMatched = $false
}
}
$enumerator = ($matches | Sort-Object -Descending Value).GetEnumerator()
return ([string] $Character) * ($enumerator | Select-Object -First 1).Value
}
function Get-TempDirTemplateReplacement {
Param(
[Parameter(Mandatory=$true)]
[ValidateScript({ $_ -gt 0 })]
[int] $Length
)
return -join ((65..90) + (97..122) | Get-Random -Count $Length `
| ForEach-Object { [char] $_ })
}
function New-TempDirectory {
Param(
[Parameter(Mandatory=$false)]
[ValidateScript({ $_ -contains "XXX" })]
[string] $Template = "tmp.XXXXXXXXXX",
[Parameter(Mandatory=$false)]
[ValidateScript({ Test-Path -Type Container $_ })]
[string] $TempDir = $env:Temp
)
$placeholder = Get-LargestContiguousBlock -Character 'X' -Text $Template
do {
$replacement = Get-TempDirTemplateReplacement -Length $placeholder.Length
$child = $Template.Replace($placeholder, $replacement)
$path = (Join-Path $TempDir $child)
} while (Test-Path $path)
return (New-Item -ItemType Directory $path).FullName
}
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$VerbosePreference = "Continue"
$rootDir = Split-Path -Parent ($MyInvocation.MyCommand.Definition)
$env:PACKER_CACHE_DIR = Join-Path $rootDir "cache"
$env:PACKER_BUILD_DIR = New-TempDirectory -TempDir (Join-Path $rootDir "builds")
foreach ($arg in $args) {
if (Test-Path -Type Leaf $arg) {
$args[$args.IndexOf($arg)] = (Get-Item $arg).FullName
}
}
Push-Location $env:PACKER_BUILD_DIR
try {
& packer @args
} finally {
Pop-Location
}