Repository navigation
Expand file tree
/
Copy pathbootstrap.ps1
More file actions
413 lines (342 loc) · 15.7 KB
/
Copy pathbootstrap.ps1
File metadata and controls
413 lines (342 loc) · 15.7 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#Requires -Version 5.1
<#
.SYNOPSIS
Windows counterpart of bootstrap.sh: creates a standalone CINPO starter project.
.DESCRIPTION
Fetches the CINPO template with git into a temporary sparse checkout, copies
./template/<template-name> into a new project directory, and prepares it as a
standalone starter project.
When run from a CINPO Git checkout, the template is fetched from the current
origin/HEAD commit so the generated project matches that checked-out revision.
Otherwise it defaults to https://github.com/AokiApp/cinpo @ main.
Environment overrides:
CINPO_TEMPLATE_REPO Git repository URL used when auto-detection is unavailable.
CINPO_TEMPLATE_REF Git ref used when auto-detection is unavailable.
.EXAMPLE
powershell -ExecutionPolicy Bypass -File scripts\bootstrap.ps1 my-project
.EXAMPLE
powershell -ExecutionPolicy Bypass -File scripts\bootstrap.ps1 -Template probe C:\src\my-project
#>
[CmdletBinding()]
param(
# PowerShell matches parameter names case-insensitively, so -template and
# -name work without aliases.
[string] $Template = 'basic',
[string] $Name,
[Parameter(Position = 0)]
[string] $Destination
)
$ErrorActionPreference = 'Stop'
$GradleVersion = '9.3.1'
$GradleRepo = 'https://github.com/gradle/gradle'
$DefaultRepo = 'https://github.com/AokiApp/cinpo'
$DefaultRef = 'main'
$TemplateProjectNameLine = "rootProject.name = 'cinpo-template'"
# --------------------------------------------------------------------------
# UI helpers
#
# All decorative output goes to the host, so a caller that pipes or redirects
# this script receives only the machine-readable summary written at the end.
# --------------------------------------------------------------------------
function Write-Banner {
Write-Host @'
し ん ぽ
_____ _____ __ _ _____ ____
/ ____|__ __| \ | | __ \ / __ \
| | | | | | \| | |__) | | | |
| |____ _| |_| |\ | | ___/| |__| |
\_____|_____|_| \__|_| \____/
'@ -ForegroundColor Cyan
Write-Host ' :: project bootstrapper ::' -ForegroundColor DarkGray
}
function Write-Section([string] $Title) {
Write-Host ''
Write-Host "==> $Title"
}
function Write-Info([string] $Key, [string] $Value) {
Write-Host (" {0,-14} {1}" -f $Key, $Value)
}
function Stop-WithError([string] $Message) {
Write-Host "[x] Error: $Message" -ForegroundColor Red
exit 1
}
function Show-Usage {
Write-Host ''
Write-Host 'Usage: scripts\bootstrap.ps1 [-Template <name>] [-Name <name>] <destination>' -ForegroundColor Cyan
Write-Host ''
Write-Host 'Fetches the CINPO template with git into a temporary sparse checkout, copies'
Write-Host './template/<template-name> into a new project directory, and prepares it as a'
Write-Host 'standalone starter project.'
Write-Host ''
Write-Host 'Options:'
Write-Host ' -Template <name> Template to use (default: basic)'
Write-Host ' -Name <name> Project name (default: destination directory basename)'
Write-Host ''
Write-Host 'Environment overrides:'
Write-Host ' CINPO_TEMPLATE_REPO Git repository URL used when auto-detection is unavailable.'
Write-Host ' CINPO_TEMPLATE_REF Git ref used when auto-detection is unavailable.'
Write-Host ''
}
# Runs a step, hiding its output unless it fails. Mirrors run_step in bootstrap.sh.
function Invoke-Step {
param(
[int] $Number,
[int] $Total,
[string] $Label,
[scriptblock] $Action
)
Write-Host ("[..] [{0}/{1}] {2}..." -f $Number, $Total, $Label) -ForegroundColor DarkGray
$logFile = [IO.Path]::GetTempFileName()
try {
& $Action *> $logFile
Write-Host ("[OK] [{0}/{1}] {2}" -f $Number, $Total, $Label) -ForegroundColor Green
} catch {
Write-Host ("[x] [{0}/{1}] {2} (failed)" -f $Number, $Total, $Label) -ForegroundColor Red
$captured = Get-Content -LiteralPath $logFile -Raw -ErrorAction SilentlyContinue
if ($captured) {
Write-Host '--- captured output ---' -ForegroundColor DarkGray
Write-Host $captured
Write-Host '------------------------' -ForegroundColor DarkGray
}
Write-Host "[x] Error: $($_.Exception.Message)" -ForegroundColor Red
exit 1
} finally {
Remove-Item -LiteralPath $logFile -Force -ErrorAction SilentlyContinue
}
}
function Invoke-Git {
param([string[]] $Arguments)
# git writes progress to stderr even on success. Windows PowerShell turns each
# such line into an ErrorRecord, which would terminate the script under
# $ErrorActionPreference = 'Stop', so flatten the streams to plain strings and
# judge success by the exit code alone.
$previousPreference = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
& git @Arguments 2>&1 | ForEach-Object { "$_" }
} finally {
$ErrorActionPreference = $previousPreference
}
if ($LASTEXITCODE -ne 0) {
throw "git $($Arguments -join ' ') failed with exit code $LASTEXITCODE"
}
}
# Writes text as UTF-8 without a BOM and with LF line endings. Gradle wrapper
# properties and settings.gradle are consumed on every platform, so a CRLF or a
# BOM introduced here would follow the generated project to Linux.
function Write-TextFileLf {
param([string] $Path, [string] $Content)
$normalized = $Content -replace "`r`n", "`n"
[IO.File]::WriteAllText($Path, $normalized, (New-Object Text.UTF8Encoding($false)))
}
function Get-CheckoutSource {
param([string] $CandidateDirectory)
if (-not $CandidateDirectory) { return $null }
$previousPreference = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
& git -C $CandidateDirectory rev-parse --show-toplevel 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) { return $null }
$repoUrl = & git -C $CandidateDirectory remote get-url origin 2>&1 | ForEach-Object { "$_" }
if ($LASTEXITCODE -ne 0) { return $null }
$ref = & git -C $CandidateDirectory rev-parse HEAD 2>&1 | ForEach-Object { "$_" }
if ($LASTEXITCODE -ne 0) { return $null }
} finally {
$ErrorActionPreference = $previousPreference
}
if (-not $repoUrl -or -not $ref) { return $null }
return [PSCustomObject]@{ Repo = "$repoUrl".Trim(); Ref = "$ref".Trim() }
}
function Invoke-SparseCheckout {
param(
[string] $RepoUrl,
[string] $Ref,
[string] $CheckoutDirectory,
[string[]] $SparsePaths
)
Invoke-Git @('init', '-q', $CheckoutDirectory)
Invoke-Git @('-C', $CheckoutDirectory, 'remote', 'add', 'origin', $RepoUrl)
Invoke-Git @('-C', $CheckoutDirectory, 'config', 'advice.detachedHead', 'false')
Invoke-Git @('-C', $CheckoutDirectory, 'config', 'core.sparseCheckout', 'true')
# Check out bytes verbatim. With the user's global core.autocrlf=true the POSIX
# gradlew copied into the generated project would otherwise get CRLF endings and
# fail with "bad interpreter: /bin/sh^M" on Linux and in the devcontainer.
Invoke-Git @('-C', $CheckoutDirectory, 'config', 'core.autocrlf', 'false')
Invoke-Git @('-C', $CheckoutDirectory, 'config', 'core.eol', 'lf')
$infoDir = Join-Path $CheckoutDirectory '.git\info'
New-Item -ItemType Directory -Force -Path $infoDir | Out-Null
Write-TextFileLf -Path (Join-Path $infoDir 'sparse-checkout') -Content (($SparsePaths -join "`n") + "`n")
Invoke-Git @('-C', $CheckoutDirectory, 'fetch', '--depth', '1', 'origin', $Ref)
Invoke-Git @('-C', $CheckoutDirectory, 'checkout', '--detach', '-q', 'FETCH_HEAD')
}
# --------------------------------------------------------------------------
# Validate arguments
# --------------------------------------------------------------------------
if (-not $Destination) {
Write-Banner
Show-Usage
exit 1
}
if (-not $Template) {
Stop-WithError 'Missing value for -Template'
}
if ($Template -match '[\\/]' -or $Template -eq '.' -or $Template -eq '..') {
Stop-WithError "Invalid template name: $Template"
}
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
Stop-WithError 'Required command not found: git'
}
# --------------------------------------------------------------------------
# Resolve repo / ref / paths
# --------------------------------------------------------------------------
$templateRepo = $env:CINPO_TEMPLATE_REPO
$templateRef = $env:CINPO_TEMPLATE_REF
if (-not $templateRepo -or -not $templateRef) {
$detected = Get-CheckoutSource -CandidateDirectory $PSScriptRoot
if ($detected) {
if (-not $templateRepo) { $templateRepo = $detected.Repo }
if (-not $templateRef) { $templateRef = $detected.Ref }
}
}
if (-not $templateRepo) { $templateRepo = $DefaultRepo }
if (-not $templateRef) { $templateRef = $DefaultRef }
$destinationDir = [IO.Path]::GetFullPath([IO.Path]::Combine((Get-Location).ProviderPath, $Destination))
if (Test-Path -LiteralPath $destinationDir) {
Stop-WithError "Destination already exists: $destinationDir"
}
$projectName = $Name
if (-not $projectName) {
$projectName = Split-Path -Leaf $destinationDir
}
# --------------------------------------------------------------------------
# Banner + configuration panel
# --------------------------------------------------------------------------
Write-Banner
Write-Section 'Configuration'
Write-Info 'Template' $Template
Write-Info 'Project name' $projectName
Write-Info 'Destination' $destinationDir
Write-Info 'Source' "$templateRepo @ $templateRef"
$workDir = Join-Path ([IO.Path]::GetTempPath()) ("cinpo-bootstrap-" + [guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Force -Path $workDir | Out-Null
try {
# ----------------------------------------------------------------------
# Phase 1 - fetch template
# ----------------------------------------------------------------------
$templateCheckoutDir = Join-Path $workDir 'cinpo-source'
Invoke-Step 1 4 'Fetching template' {
Invoke-SparseCheckout -RepoUrl $templateRepo -Ref $templateRef `
-CheckoutDirectory $templateCheckoutDir -SparsePaths @("template/$Template/")
}
$templateDir = Join-Path $templateCheckoutDir "template\$Template"
if (-not (Test-Path -LiteralPath $templateDir -PathType Container)) {
Write-Host "[x] Error: template not found: $Template" -ForegroundColor Red
Write-Host " Template source: $templateRepo @ $templateRef"
exit 1
}
# ----------------------------------------------------------------------
# Phase 2 - copy files + configure project
# ----------------------------------------------------------------------
Invoke-Step 2 4 'Copying files & configuring project' {
$parent = Split-Path -Parent $destinationDir
if ($parent -and -not (Test-Path -LiteralPath $parent)) {
New-Item -ItemType Directory -Force -Path $parent | Out-Null
}
Copy-Item -LiteralPath $templateDir -Destination $destinationDir -Recurse -Force
foreach ($stale in @('.gradle', 'build', 'vendor')) {
$stalePath = Join-Path $destinationDir $stale
if (Test-Path -LiteralPath $stalePath) {
Remove-Item -LiteralPath $stalePath -Recurse -Force
}
}
$settingsFile = Join-Path $destinationDir 'settings.gradle'
if (-not (Test-Path -LiteralPath $settingsFile -PathType Leaf)) {
throw "settings.gradle not found in extracted template: $settingsFile"
}
$escapedProjectName = $projectName -replace '\\', '\\' -replace "'", "\'"
$replacement = "rootProject.name = '$escapedProjectName'"
$lines = [IO.File]::ReadAllLines($settingsFile)
$replaced = $false
for ($i = 0; $i -lt $lines.Length; $i++) {
if (-not $replaced -and $lines[$i] -ceq $TemplateProjectNameLine) {
$lines[$i] = $replacement
$replaced = $true
}
}
if (-not $replaced) {
throw "Expected template project name declaration not found in $settingsFile"
}
Write-TextFileLf -Path $settingsFile -Content (($lines -join "`n") + "`n")
}
# ----------------------------------------------------------------------
# Phase 3 - Gradle wrapper
# ----------------------------------------------------------------------
$wrapperDir = Join-Path $destinationDir 'gradle\wrapper'
Invoke-Step 3 4 "Fetching Gradle wrapper ($GradleVersion)" {
New-Item -ItemType Directory -Force -Path $wrapperDir | Out-Null
$gradleCheckoutDir = Join-Path $workDir 'gradle-source'
Invoke-SparseCheckout -RepoUrl $GradleRepo -Ref "v$GradleVersion" `
-CheckoutDirectory $gradleCheckoutDir `
-SparsePaths @('gradle/wrapper/gradle-wrapper.jar', 'gradlew', 'gradlew.bat')
$wrapperJar = Join-Path $gradleCheckoutDir 'gradle\wrapper\gradle-wrapper.jar'
if (-not (Test-Path -LiteralPath $wrapperJar -PathType Leaf)) {
throw "gradle-wrapper.jar not found in fetched Gradle source: $GradleRepo @ v$GradleVersion"
}
Copy-Item -LiteralPath $wrapperJar -Destination (Join-Path $wrapperDir 'gradle-wrapper.jar') -Force
Copy-Item -LiteralPath (Join-Path $gradleCheckoutDir 'gradlew') -Destination (Join-Path $destinationDir 'gradlew') -Force
Copy-Item -LiteralPath (Join-Path $gradleCheckoutDir 'gradlew.bat') -Destination (Join-Path $destinationDir 'gradlew.bat') -Force
Write-TextFileLf -Path (Join-Path $wrapperDir 'gradle-wrapper.properties') -Content @"
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-$GradleVersion-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
"@
}
# ----------------------------------------------------------------------
# Phase 4 - git init
# ----------------------------------------------------------------------
Invoke-Step 4 4 'Initializing git repository' {
Invoke-Git @('-C', $destinationDir, 'init', '-q')
# Windows has no POSIX execute bit, so record it in the index directly.
# Without this the generated project's gradlew is not executable on Linux.
Invoke-Git @('-C', $destinationDir, 'add', '-A')
Invoke-Git @('-C', $destinationDir, 'update-index', '--chmod=+x', 'gradlew')
Invoke-Git @('-C', $destinationDir,
'-c', 'user.name=CINPO Template',
'-c', 'user.email=cinpo-template@example.invalid',
'commit', '-q', '-m', 'Initial commit')
}
} finally {
Remove-Item -LiteralPath $workDir -Recurse -Force -ErrorAction SilentlyContinue
}
# --------------------------------------------------------------------------
# Success panel (chrome to the host, summary to stdout)
# --------------------------------------------------------------------------
$box = '+--------------------------------------------------+'
Write-Host ''
Write-Host $box -ForegroundColor Green
Write-Host '| [OK] Project created successfully! |' -ForegroundColor Green
Write-Host ("| {0,-48}|" -f $destinationDir) -ForegroundColor Green
Write-Host $box -ForegroundColor Green
Write-Host ''
Write-Host '[!] Before running, prepare the vendor directory:' -ForegroundColor Yellow
Write-Host " - Download the Java Card SDK Tools and Simulator from Oracle's website."
Write-Host ' - Place the archives (unextracted) into'
Write-Host " $destinationDir\vendor"
Write-Host ''
Write-Host 'Next steps'
Write-Host " 1. cd $destinationDir"
Write-Host ' 2. .\gradlew.bat cinpoRun'
Write-Output @"
Created project from template:
$destinationDir
Template:
$Template
Project name:
$projectName
Template source:
$templateRepo @ $templateRef
"@