-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ps1
More file actions
executable file
·221 lines (186 loc) · 6.24 KB
/
setup.ps1
File metadata and controls
executable file
·221 lines (186 loc) · 6.24 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
<#
PowerShell version of the project template setup script.
Run this in the root of your new project folder.
#>
$ErrorActionPreference = "Stop"
# Current directory is the project root
$ROOT = Get-Location
$FOLDER_NAME = Split-Path -Leaf $ROOT
$PROJECT_NAME = $FOLDER_NAME -replace '\.', '_'
# Configurable list of optional folders to ask about removing
$FOLDERS_TO_ASK = @("data", "scratch")
$GITHUB_ISSUE_TEMPLATE_DIR = ".github/ISSUE_TEMPLATE"
$GITHUB_WORKFLOWS_DIR = ".github/workflows"
$GITHUB_FILE_PUBLISH = ".github/_publish.yml"
$GITHUB_FILE_CONFIG = ".github/ISSUE_TEMPLATE/config.yml"
$GITHUB_FILES = @(
"$GITHUB_WORKFLOWS_DIR/build.yml"
"$GITHUB_WORKFLOWS_DIR/docs.yml"
"$GITHUB_WORKFLOWS_DIR/draft.yml"
"$GITHUB_WORKFLOWS_DIR/format.yml"
"$GITHUB_WORKFLOWS_DIR/test.yml"
"$GITHUB_ISSUE_TEMPLATE_DIR/01-bug.yml"
"$GITHUB_ISSUE_TEMPLATE_DIR/02-feature-request.yml"
"$GITHUB_ISSUE_TEMPLATE_DIR/03-docs-problem.yml"
"$GITHUB_ISSUE_TEMPLATE_DIR/04-build-problem.yml"
)
function Replace-InFile {
param(
[string]$File,
[string]$Pattern,
[string]$Replacement
)
if (-not (Test-Path $File)) {
Write-Host "Warning: $File not found, skipping."
return
}
(Get-Content $File -Raw) -replace $Pattern, $Replacement | Set-Content $File
}
function Ask-YesNo {
param([string]$Prompt)
Write-Host "\n"
while ($true) {
$answer = Read-Host "$Prompt [y/n]"
if ($answer -match '^[Yy]') { return $true }
if ($answer -match '^[Nn]') { return $false }
Write-Host "Please answer y or n."
}
}
function Remove-VcsDirs {
Write-Host "Removing any existing .git or .jj directories..."
Remove-Item -Path .git, .jj -Recurse -Force -ErrorAction SilentlyContinue
}
function Remove-Target {
Write-Host "Removing target/ directory..."
Remove-Item -Path target -Recurse -Force -ErrorAction SilentlyContinue
}
function Maybe-RemoveBacon {
if (Test-Path "bacon.toml") {
if (Ask-YesNo "Do you want to remove bacon.toml?") {
Remove-Item "bacon.toml" -Force
Write-Host "Removed bacon.toml"
} else {
Write-Host "Kept bacon.toml"
}
}
}
function Update-MakefileToml {
if (Test-Path "Makefile.toml") {
Write-Host "Updating Makefile.toml (using FOLDER_NAME: $PROJECT_NAME)"
Replace-InFile "Makefile.toml" 'env\.PROJECT_NAME = "rust_template"' "env.PROJECT_NAME = `"$PROJECT_NAME`""
}
}
function Update-CargoToml {
if (Test-Path "Cargo.toml") {
Write-Host "Updating Cargo.toml (using PROJECT_NAME: $PROJECT_NAME)"
Replace-InFile "Cargo.toml" 'name\s*=\s*"rust_template"' "name = `"$PROJECT_NAME`""
}
}
function Maybe-RemoveOptionalFolders {
foreach ($folder in $FOLDERS_TO_ASK) {
if (Test-Path $folder -PathType Container) {
if (Ask-YesNo "Do you want to remove the $folder/ folder?") {
Remove-Item $folder -Recurse -Force
Write-Host "Removed $folder/"
} else {
Write-Host "Kept $folder/ and added to .gitignore"
Add-Content -Path ./.gitignore -Value "/$folder/"
}
}
}
}
function Update-GithubPublish {
$file = $GITHUB_FILE_PUBLISH
if (Test-Path $file) {
Write-Host "Updating $file (PROJECT_NAME → $PROJECT_NAME)"
Replace-InFile $file "PROJECT_NAME: rust_template" "PROJECT_NAME: $PROJECT_NAME"
}
}
function Update-IssueTemplateWorkflows {
foreach ($file in $GITHUB_FILES) {
if (Test-Path $file) {
Write-Host "Updating $file (PROJECT_NAME → $PROJECT_NAME)"
Replace-InFile $file "PROJECT_NAME: rust_template" "PROJECT_NAME: $PROJECT_NAME"
}
}
}
function Update-ConfigYml {
$file = $GITHUB_FILE_CONFIG
if (Test-Path $file) {
Write-Host "Updating $file (repo URL → $FOLDER_NAME)"
Replace-InFile $file "url: https://github.com/MrDwarf7/REPO_NAME/discussions" "url: https://github.com/MrDwarf7/$FOLDER_NAME/discussions"
}
}
function Setup-UsingJJ {
$cmd_bin = "jj"
Write-Host "jj command found."
if (Ask-YesNo "Do you want to initialize with jj (recommended for existing remote)?") {
Write-Host "Choose initialization method:"
Write-Host "\t 1) jj git init"
Write-Host "\t 2) jj git init --colocate (shares .git directory with Git tools)"
while ($true) {
$choice = Read-Host "Enter choice (1 or 2)"
if ($choice -eq "1") {
jj git init
break
} elseif ($choice -eq "2") {
jj git init --colocate
break
} else {
Write-Host "Invalid choice, please enter 1 or 2."
}
}
return
}
}
function Setup-Repository {
Write-Host ""
$cmd_bin = "git"
Add-Content -Path ./.gitignore -Value "./.extras/"
if (Get-Command jj -ErrorAction SilentlyContinue) {
$cmd_bin = "jj"
}
# Remote exists → offer jj colocation option if available
if ("$cmd_bin" -eq "jj") {
Setup-UsingJJ
Invoke-Expression "$cmd_bin file untrack .extras"
} else {
# No remote yet → default to plain git init
Write-Host "No existing remote. Initializing a fresh Git repository."
Invoke-Expression "$cmd_bin init"
Write-Host "Initialized empty Git repository. You can create the GitHub repo later and add it as remote."
}
return
}
funciton Maybe-RemoveSetupScript {
if (Test-Path "setup.ps1") {
if (Ask-YesNo "Do you want to remove setup.ps1?") {
Remove-Item "setup.ps1" -Force
Write-Host "Removed setup.ps1"
} else {
Write-Host "Kept setup.ps1"
}
}
return
}
function Main {
Write-Host "Starting project template setup for folder: $FOLDER_NAME"
Write-Host "Derived PROJECT_NAME (for Cargo/binary): $PROJECT_NAME"
Write-Host ""
# Remove the other operating system's setup script
Remove-Item -Path setup.sh -Force -ErrorAction SilentlyContinue
Remove-VcsDirs
Remove-Target
Maybe-RemoveBacon
Update-MakefileToml
Update-CargoToml
Maybe-RemoveOptionalFolders
Update-GithubPublish
Update-IssueTemplateWorkflows
Update-ConfigYml
Setup-Repository
Write-Host ""
Write-Host "Setup complete!"
Maybe-RemoveSetupScript
}
Main