-
Notifications
You must be signed in to change notification settings - Fork 7k
Expand file tree
/
Copy pathcommon.ps1
More file actions
181 lines (153 loc) · 5.39 KB
/
common.ps1
File metadata and controls
181 lines (153 loc) · 5.39 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
#!/usr/bin/env pwsh
# Common PowerShell functions analogous to common.sh
function Get-RepoRoot {
try {
$result = git rev-parse --show-toplevel 2>$null
if ($LASTEXITCODE -eq 0) {
return $result
}
} catch {
# Git command failed
}
# Fall back to script location for non-git repos
return (Resolve-Path (Join-Path $PSScriptRoot "../../..")).Path
}
function Get-CurrentBranch {
# First check if SPECIFY_FEATURE environment variable is set
if ($env:SPECIFY_FEATURE) {
return $env:SPECIFY_FEATURE
}
# Then check git if available
try {
$result = git rev-parse --abbrev-ref HEAD 2>$null
if ($LASTEXITCODE -eq 0) {
return $result
}
} catch {
# Git command failed
}
# For non-git repos, try to find the latest feature directory
$repoRoot = Get-RepoRoot
$specsDir = Join-Path $repoRoot "specs"
if (Test-Path $specsDir) {
$latestFeature = ""
$highest = 0
$featureDirs = @()
$featureDirs += Get-ChildItem -Path $specsDir -Directory -ErrorAction SilentlyContinue
$featureDirs += Get-ChildItem -Path (Join-Path $specsDir '*') -Directory -ErrorAction SilentlyContinue | ForEach-Object {
Get-ChildItem -Path $_.FullName -Directory -ErrorAction SilentlyContinue
}
$featureDirs | ForEach-Object {
if ($_.Name -match '^(\d{3})-') {
$num = [int]$matches[1]
if ($num -gt $highest) {
$highest = $num
$latestFeature = $_.Name
}
}
}
if ($latestFeature) {
return $latestFeature
}
}
# Final fallback
return "main"
}
function Test-HasGit {
try {
git rev-parse --show-toplevel 2>$null | Out-Null
return ($LASTEXITCODE -eq 0)
} catch {
return $false
}
}
function Test-FeatureBranch {
param(
[string]$Branch,
[bool]$HasGit = $true
)
# For non-git repos, we can't enforce branch naming but still provide output
if (-not $HasGit) {
Write-Warning "[specify] Warning: Git repository not detected; skipped branch validation"
return $true
}
if ($Branch -notmatch '^[0-9]{3}-') {
Write-Output "ERROR: Not on a feature branch. Current branch: $Branch"
Write-Output "Feature branches should be named like: 001-feature-name"
return $false
}
return $true
}
function Get-FeatureDir {
param([string]$RepoRoot, [string]$Branch)
Join-Path $RepoRoot "specs/$Branch"
}
function Get-FeatureDirByPrefix {
param(
[string]$RepoRoot,
[string]$BranchName
)
$specsDir = Join-Path $RepoRoot 'specs'
if ($BranchName -notmatch '^(\d{3})-') {
return (Join-Path $specsDir $BranchName)
}
$prefix = $matches[1]
$matchesDirs = @()
if (Test-Path $specsDir) {
if ($env:SPECIFY_SPECS_SUBDIR -and (Test-Path (Join-Path $specsDir $env:SPECIFY_SPECS_SUBDIR))) {
$matchesDirs += Get-ChildItem -Path (Join-Path $specsDir $env:SPECIFY_SPECS_SUBDIR "$prefix-*") -Directory -ErrorAction SilentlyContinue
} else {
$matchesDirs += Get-ChildItem -Path (Join-Path $specsDir "$prefix-*") -Directory -ErrorAction SilentlyContinue
$matchesDirs += Get-ChildItem -Path (Join-Path $specsDir '*') -Directory -ErrorAction SilentlyContinue | ForEach-Object {
Get-ChildItem -Path (Join-Path $_.FullName "$prefix-*") -Directory -ErrorAction SilentlyContinue
}
}
}
if ($matchesDirs.Count -eq 0) {
return (Join-Path $specsDir $BranchName)
}
if ($matchesDirs.Count -eq 1) {
return $matchesDirs[0].FullName
}
Write-Error "ERROR: Multiple spec directories found with prefix '$prefix': $($matchesDirs.FullName -join ', ')"
return (Join-Path $specsDir $BranchName)
}
function Get-FeaturePathsEnv {
$repoRoot = Get-RepoRoot
$currentBranch = Get-CurrentBranch
$hasGit = Test-HasGit
$featureDir = Get-FeatureDirByPrefix -RepoRoot $repoRoot -BranchName $currentBranch
[PSCustomObject]@{
REPO_ROOT = $repoRoot
CURRENT_BRANCH = $currentBranch
HAS_GIT = $hasGit
FEATURE_DIR = $featureDir
FEATURE_SPEC = Join-Path $featureDir 'spec.md'
IMPL_PLAN = Join-Path $featureDir 'plan.md'
TASKS = Join-Path $featureDir 'tasks.md'
RESEARCH = Join-Path $featureDir 'research.md'
DATA_MODEL = Join-Path $featureDir 'data-model.md'
QUICKSTART = Join-Path $featureDir 'quickstart.md'
CONTRACTS_DIR = Join-Path $featureDir 'contracts'
}
}
function Test-FileExists {
param([string]$Path, [string]$Description)
if (Test-Path -Path $Path -PathType Leaf) {
Write-Output " ✓ $Description"
return $true
} else {
Write-Output " ✗ $Description"
return $false
}
}
function Test-DirHasFiles {
param([string]$Path, [string]$Description)
if ((Test-Path -Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -ErrorAction SilentlyContinue | Where-Object { -not $_.PSIsContainer } | Select-Object -First 1)) {
Write-Output " ✓ $Description"
return $true
} else {
Write-Output " ✗ $Description"
return $false
}
}