|
| 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 | + |
0 commit comments