-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-interactive.ps1
More file actions
127 lines (112 loc) · 5.6 KB
/
Copy pathtest-interactive.ps1
File metadata and controls
127 lines (112 loc) · 5.6 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<#
.SYNOPSIS
Interactive smoke test for java-service-archetype.
.DESCRIPTION
Installs the archetype to the local Maven repo, then runs mvn archetype:generate
in interactive mode by redirecting answers from a temp file into stdin.
The generated project is written to a temp directory and removed on exit.
.PARAMETER Verify
Also run `mvn verify -f parent/pom.xml` on the generated project (slow).
.EXAMPLE
.\test-interactive.ps1
.\test-interactive.ps1 -Verify
#>
param([switch]$Verify)
$ErrorActionPreference = 'Stop'
# Run native commands (Maven, the JVM) through this helper. They write warnings
# and progress to stderr; under $ErrorActionPreference='Stop', PowerShell turns
# that stderr into a TERMINATING error the moment the caller redirects streams
# (e.g. `.\test-interactive.ps1 *> run.log`). 'SilentlyContinue' makes the native
# stderr non-fatal AND keeps it out of a redirected log, while the real Maven
# errors (written to stdout) and the exit-code gate below still surface failures.
function Invoke-Native {
param(
[Parameter(Mandatory)][string]$What,
[Parameter(Mandatory)][scriptblock]$Command
)
$prevEap = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'
try { & $Command } finally { $ErrorActionPreference = $prevEap }
if ($LASTEXITCODE -ne 0) { throw "$What failed with exit code $LASTEXITCODE" }
}
$ArchetypeGroupId = "io.github.jeffjensen"
$ArchetypeArtifactId = "java-service-archetype"
# $ArchetypeVersion is read from the project pom.xml at runtime (after install),
# so this script needs no edits when the project version changes.
$ScriptDir = $PSScriptRoot
$OutDir = Join-Path $env:TEMP "archetype-test-$(Get-Random)"
$AnswersFile = Join-Path $env:TEMP "archetype-answers-$(Get-Random).txt"
try {
# -- 1. Install -------------------------------------------------------------
Write-Host "==> Installing archetype to local repo..."
Invoke-Native "mvn install" { & "$ScriptDir\mvnw.cmd" -f "$ScriptDir\pom.xml" install -q }
# -- Read the archetype version straight from the project POM, so this script
# needs no maintenance when the project version changes ------------------
# Use cmd /c with a single command string: PowerShell 5.1 mangles arguments
# like "-Dexpression=project.version" when passing them to the mvnw.cmd batch
# file, so let cmd.exe parse the line (same pattern as the generate step).
$ArchetypeVersion = cmd /c "`"$ScriptDir\mvnw.cmd`" -f `"$ScriptDir\pom.xml`" help:evaluate -Dexpression=project.version -q -DforceStdout --no-transfer-progress 2>NUL"
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace("$ArchetypeVersion")) {
throw "Could not determine archetype version from $ScriptDir\pom.xml"
}
$ArchetypeVersion = "$ArchetypeVersion".Trim()
Write-Host "==> Using archetype version: $ArchetypeVersion"
# -- 2. Write answers to a temp file ---------------------------------------
# Answers fed to each Maven prompt, in the exact order maven-archetype-plugin
# 3.4.x prompts. Required properties WITHOUT a non-empty default are prompted
# first, in archetype-metadata.xml order, THEN groupId / artifactId / package.
# Properties with a non-empty default are NOT prompted, so they get no answer
# line here: version (1.0.0-SNAPSHOT), serviceAreas (",") and presentationTypes
# (rest). The "," default makes serviceAreas resolve to a single "service" module.
#
# appName -> My Service
# integrations -> database:main
# groupId -> com.example
# artifactId -> my-service
# package -> (Enter: accept default derived from groupId)
# confirm -> Y
$answers = @(
"My Service",
"database:main",
"com.example",
"my-service",
"",
"Y"
)
# Write WITHOUT a BOM. Windows PowerShell 5.1 'Set-Content -Encoding UTF8'
# prepends a UTF-8 BOM, which would corrupt the first answer (groupId).
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
[System.IO.File]::WriteAllText($AnswersFile, ($answers -join "`n") + "`n", $utf8NoBom)
# -- 3. Generate ------------------------------------------------------------
New-Item -ItemType Directory -Path $OutDir | Out-Null
Write-Host "==> Generating project in $OutDir..."
Invoke-Native "archetype:generate" {
cmd /c "cd /d `"$OutDir`" && mvn archetype:generate --no-transfer-progress -DarchetypeGroupId=$ArchetypeGroupId -DarchetypeArtifactId=$ArchetypeArtifactId -DarchetypeVersion=$ArchetypeVersion < `"$AnswersFile`""
}
# -- 4. Show generated layout -----------------------------------------------
Write-Host ""
Write-Host "==> Generated project layout:"
$projectDir = Join-Path $OutDir "my-service"
Get-ChildItem -Path $projectDir -Recurse -File |
Sort-Object FullName |
ForEach-Object { " " + $_.FullName.Replace($OutDir + "\", "") }
# -- 5. Optionally verify the generated project compiles and tests pass -----
if ($Verify) {
Write-Host ""
Write-Host "==> Running mvn verify on generated project..."
Invoke-Native "mvn verify" {
cmd /c "cd /d `"$projectDir`" && mvn verify -f parent/pom.xml --no-transfer-progress"
}
Write-Host "==> Build: PASSED"
}
Write-Host ""
Write-Host "==> SUCCESS"
}
finally {
if (Test-Path $OutDir -ErrorAction SilentlyContinue) {
Remove-Item $OutDir -Recurse -Force -ErrorAction SilentlyContinue
}
if (Test-Path $AnswersFile -ErrorAction SilentlyContinue) {
Remove-Item $AnswersFile -Force -ErrorAction SilentlyContinue
}
}