Skip to content

Commit e098fe0

Browse files
authored
feat: add batch rename scripts for project initialization (#5)
1 parent 2c696c1 commit e098fe0

File tree

3 files changed

+286
-0
lines changed

3 files changed

+286
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818

1919
Use this repository as a GitHub template to quickly start a new Rust project.
2020

21+
## Getting Started
22+
23+
1. Create a new repository using this template
24+
2. Clone your repository and run the rename script:
25+
- **Linux/macOS:** `./rename-project.sh`
26+
- **Windows:** `.\rename-project.ps1`
27+
3. Follow the prompts, review changes, and commit
28+
4. Start building your project!
29+
2130
## Minimum Rust version policy
2231

2332
This crate is built against the latest stable release, and its minimum supported rustc version is 1.85.0.

rename-project.ps1

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Copyright 2025 FastLabs Developers
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
$ErrorActionPreference = "Stop"
16+
17+
Write-Host "========================================" -ForegroundColor Blue
18+
Write-Host " Template Project Batch Renamer " -ForegroundColor Blue
19+
Write-Host "========================================" -ForegroundColor Blue
20+
Write-Host ""
21+
22+
if (-not (Test-Path "Cargo.toml") -or -not (Test-Path "template" -PathType Container)) {
23+
Write-Host "ERROR: This script must be run from the project root directory" -ForegroundColor Red
24+
exit 1
25+
}
26+
27+
Write-Host "Please provide the following information:" -ForegroundColor Yellow
28+
Write-Host ""
29+
30+
$ProjectName = Read-Host "New project name (e.g., my-awesome-project)"
31+
$GitHubUser = Read-Host "GitHub username/org (e.g., myname)"
32+
33+
if ([string]::IsNullOrWhiteSpace($ProjectName)) {
34+
Write-Host "ERROR: Project name is required" -ForegroundColor Red
35+
exit 1
36+
}
37+
38+
if ([string]::IsNullOrWhiteSpace($GitHubUser)) {
39+
Write-Host "ERROR: GitHub username is required" -ForegroundColor Red
40+
exit 1
41+
}
42+
43+
# Validate project name format (Rust package naming convention)
44+
if ($ProjectName -notmatch '^[a-z][a-z0-9_-]*$') {
45+
Write-Host "ERROR: Project name must start with a lowercase letter and contain only lowercase letters, numbers, hyphens, and underscores" -ForegroundColor Red
46+
exit 1
47+
}
48+
49+
Write-Host ""
50+
Write-Host "Summary:" -ForegroundColor Blue
51+
Write-Host " Project name: $ProjectName" -ForegroundColor Green
52+
Write-Host " GitHub repo: $GitHubUser/$ProjectName" -ForegroundColor Green
53+
Write-Host " Crates.io URL: https://crates.io/crates/$ProjectName" -ForegroundColor Green
54+
Write-Host ""
55+
56+
$Confirm = Read-Host "Continue with renaming? (y/N)"
57+
$ConfirmLower = $Confirm.ToLower()
58+
if ($ConfirmLower -ne "y" -and $ConfirmLower -ne "yes") {
59+
Write-Host "Cancelled." -ForegroundColor Yellow
60+
exit 0
61+
}
62+
63+
Write-Host ""
64+
Write-Host "Starting batch rename..." -ForegroundColor Blue
65+
Write-Host ""
66+
67+
function Update-FileContent {
68+
param(
69+
[string]$FilePath,
70+
[string]$OldValue,
71+
[string]$NewValue
72+
)
73+
74+
$content = Get-Content $FilePath -Raw
75+
$content = $content -replace [regex]::Escape($OldValue), $NewValue
76+
Set-Content $FilePath -Value $content -NoNewline
77+
}
78+
79+
# 1. Update root Cargo.toml
80+
Write-Host "[OK] Updating Cargo.toml..." -ForegroundColor Green
81+
Update-FileContent "Cargo.toml" "https://github.com/fast/template" "https://github.com/$GitHubUser/$ProjectName"
82+
Update-FileContent "Cargo.toml" '"template"' "`"$ProjectName`""
83+
84+
# 2. Update template/Cargo.toml
85+
Write-Host "[OK] Updating template/Cargo.toml..." -ForegroundColor Green
86+
Update-FileContent "template/Cargo.toml" 'name = "template"' "name = `"$ProjectName`""
87+
88+
# 3. Update README.md
89+
Write-Host "[OK] Updating README.md..." -ForegroundColor Green
90+
Update-FileContent "README.md" "crates.io/crates/template" "crates.io/crates/$ProjectName"
91+
Update-FileContent "README.md" "img.shields.io/crates/v/template.svg" "img.shields.io/crates/v/$ProjectName.svg"
92+
Update-FileContent "README.md" "img.shields.io/crates/l/template" "img.shields.io/crates/l/$ProjectName"
93+
Update-FileContent "README.md" "github.com/fast/template" "github.com/$GitHubUser/$ProjectName"
94+
Update-FileContent "README.md" "docs.rs/template" "docs.rs/$ProjectName"
95+
96+
# 4. Update .github/semantic.yml
97+
Write-Host "[OK] Updating .github/semantic.yml..." -ForegroundColor Green
98+
Update-FileContent ".github/semantic.yml" "github.com/fast/template" "github.com/$GitHubUser/$ProjectName"
99+
100+
# 5. Rename template directory
101+
Write-Host "[OK] Renaming template/ directory to $ProjectName/..." -ForegroundColor Green
102+
if (Test-Path "template" -PathType Container) {
103+
Rename-Item "template" $ProjectName
104+
}
105+
106+
# 6. Update Cargo.lock
107+
Write-Host "[OK] Updating Cargo.lock..." -ForegroundColor Green
108+
Update-FileContent "Cargo.lock" 'name = "template"' "name = `"$ProjectName`""
109+
110+
Write-Host ""
111+
Write-Host "========================================" -ForegroundColor Green
112+
Write-Host " SUCCESS: Renaming completed! " -ForegroundColor Green
113+
Write-Host "========================================" -ForegroundColor Green
114+
Write-Host ""
115+
Write-Host "Next steps:" -ForegroundColor Blue
116+
Write-Host ""
117+
Write-Host " 1. Review the changes:"
118+
Write-Host " git diff" -ForegroundColor Yellow
119+
Write-Host ""
120+
Write-Host " 2. Delete the rename scripts (no longer needed):"
121+
Write-Host " Remove-Item rename-project.sh, rename-project.ps1" -ForegroundColor Yellow
122+
Write-Host ""
123+
Write-Host " 3. Update the project description in README.md"
124+
Write-Host ""
125+
Write-Host " 4. Commit your changes:"
126+
Write-Host " git add ." -ForegroundColor Yellow
127+
Write-Host " git commit -m `"chore: initialize project as $ProjectName`"" -ForegroundColor Yellow
128+
Write-Host ""
129+
Write-Host " 5. Push to GitHub:"
130+
Write-Host " git push" -ForegroundColor Yellow
131+
Write-Host ""
132+
Write-Host "Happy coding!" -ForegroundColor Green
133+

rename-project.sh

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2025 FastLabs Developers
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -euo pipefail
17+
18+
# Colors for output
19+
RED='\033[0;31m'
20+
GREEN='\033[0;32m'
21+
YELLOW='\033[1;33m'
22+
BLUE='\033[0;34m'
23+
NC='\033[0m' # No Color
24+
25+
echo -e "${BLUE}========================================${NC}"
26+
echo -e "${BLUE} Template Project Batch Renamer ${NC}"
27+
echo -e "${BLUE}========================================${NC}"
28+
echo ""
29+
30+
if [[ ! -f "Cargo.toml" ]] || [[ ! -d "template" ]]; then
31+
echo -e "${RED}ERROR: This script must be run from the project root directory${NC}"
32+
exit 1
33+
fi
34+
35+
echo -e "${YELLOW}Please provide the following information:${NC}"
36+
echo ""
37+
38+
read -p "New project name (e.g., my-awesome-project): " PROJECT_NAME
39+
read -p "GitHub username/org (e.g., myname): " GITHUB_USER
40+
41+
if [[ -z "$PROJECT_NAME" ]]; then
42+
echo -e "${RED}ERROR: Project name is required${NC}"
43+
exit 1
44+
fi
45+
46+
if [[ -z "$GITHUB_USER" ]]; then
47+
echo -e "${RED}ERROR: GitHub username is required${NC}"
48+
exit 1
49+
fi
50+
51+
# Validate project name format (Rust package naming convention)
52+
if [[ ! "$PROJECT_NAME" =~ ^[a-z][a-z0-9_-]*$ ]]; then
53+
echo -e "${RED}ERROR: Project name must start with a lowercase letter and contain only lowercase letters, numbers, hyphens, and underscores${NC}"
54+
exit 1
55+
fi
56+
57+
echo ""
58+
echo -e "${BLUE}Summary:${NC}"
59+
echo -e " Project name: ${GREEN}$PROJECT_NAME${NC}"
60+
echo -e " GitHub repo: ${GREEN}$GITHUB_USER/$PROJECT_NAME${NC}"
61+
echo -e " Crates.io URL: ${GREEN}https://crates.io/crates/$PROJECT_NAME${NC}"
62+
echo ""
63+
read -p "Continue with renaming? (y/N): " CONFIRM
64+
65+
CONFIRM_LOWER=$(echo "$CONFIRM" | tr '[:upper:]' '[:lower:]')
66+
67+
if [[ "$CONFIRM_LOWER" != "y" ]] && [[ "$CONFIRM_LOWER" != "yes" ]]; then
68+
echo -e "${YELLOW}Cancelled.${NC}"
69+
exit 0
70+
fi
71+
72+
echo ""
73+
echo -e "${BLUE}Starting batch rename...${NC}"
74+
echo ""
75+
76+
update_file() {
77+
local file=$1
78+
local old=$2
79+
local new=$3
80+
81+
if [[ "$OSTYPE" == "darwin"* ]]; then
82+
# macOS
83+
sed -i '' "s|$old|$new|g" "$file"
84+
else
85+
# Linux
86+
sed -i "s|$old|$new|g" "$file"
87+
fi
88+
}
89+
90+
# 1. Update root Cargo.toml
91+
echo -e "${GREEN}[OK]${NC} Updating Cargo.toml..."
92+
update_file "Cargo.toml" "https://github.com/fast/template" "https://github.com/$GITHUB_USER/$PROJECT_NAME"
93+
update_file "Cargo.toml" '"template"' "\"$PROJECT_NAME\""
94+
95+
# 2. Update template/Cargo.toml
96+
echo -e "${GREEN}[OK]${NC} Updating template/Cargo.toml..."
97+
update_file "template/Cargo.toml" 'name = "template"' "name = \"$PROJECT_NAME\""
98+
99+
# 3. Update README.md
100+
echo -e "${GREEN}[OK]${NC} Updating README.md..."
101+
update_file "README.md" "crates.io/crates/template" "crates.io/crates/$PROJECT_NAME"
102+
update_file "README.md" "img.shields.io/crates/v/template.svg" "img.shields.io/crates/v/$PROJECT_NAME.svg"
103+
update_file "README.md" "img.shields.io/crates/l/template" "img.shields.io/crates/l/$PROJECT_NAME"
104+
update_file "README.md" "github.com/fast/template" "github.com/$GITHUB_USER/$PROJECT_NAME"
105+
update_file "README.md" "docs.rs/template" "docs.rs/$PROJECT_NAME"
106+
107+
# 4. Update .github/semantic.yml
108+
echo -e "${GREEN}[OK]${NC} Updating .github/semantic.yml..."
109+
update_file ".github/semantic.yml" "github.com/fast/template" "github.com/$GITHUB_USER/$PROJECT_NAME"
110+
111+
# 5. Rename template directory
112+
echo -e "${GREEN}[OK]${NC} Renaming template/ directory to $PROJECT_NAME/..."
113+
if [[ -d "template" ]]; then
114+
mv template "$PROJECT_NAME"
115+
fi
116+
117+
# 6. Update Cargo.lock
118+
echo -e "${GREEN}[OK]${NC} Updating Cargo.lock..."
119+
update_file "Cargo.lock" 'name = "template"' "name = \"$PROJECT_NAME\""
120+
121+
echo ""
122+
echo -e "${GREEN}========================================${NC}"
123+
echo -e "${GREEN} SUCCESS: Renaming completed! ${NC}"
124+
echo -e "${GREEN}========================================${NC}"
125+
echo ""
126+
echo -e "${BLUE}Next steps:${NC}"
127+
echo ""
128+
echo -e " 1. Review the changes:"
129+
echo -e " ${YELLOW}git diff${NC}"
130+
echo ""
131+
echo -e " 2. Delete the rename scripts (no longer needed):"
132+
echo -e " ${YELLOW}rm rename-project.sh rename-project.ps1${NC}"
133+
echo ""
134+
echo -e " 3. Update the project description in README.md"
135+
echo ""
136+
echo -e " 4. Commit your changes:"
137+
echo -e " ${YELLOW}git add .${NC}"
138+
echo -e " ${YELLOW}git commit -m \"chore: initialize project as $PROJECT_NAME\"${NC}"
139+
echo ""
140+
echo -e " 5. Push to GitHub:"
141+
echo -e " ${YELLOW}git push${NC}"
142+
echo ""
143+
echo -e "${GREEN}Happy coding!${NC}"
144+

0 commit comments

Comments
 (0)