|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2025 FastLabs Developers |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import os |
| 18 | +import sys |
| 19 | + |
| 20 | +def main(): |
| 21 | + print("Welcome to the project bootstrap script!") |
| 22 | + |
| 23 | + # 1. Get user input |
| 24 | + try: |
| 25 | + project_name = input("Enter your project name (e.g., my-awesome-project): ").strip() |
| 26 | + if not project_name: |
| 27 | + print("Error: Project name cannot be empty.") |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | + github_username = input("Enter your GitHub username (e.g., torvalds): ").strip() |
| 31 | + if not github_username: |
| 32 | + print("Error: GitHub username cannot be empty.") |
| 33 | + sys.exit(1) |
| 34 | + except KeyboardInterrupt: |
| 35 | + print("\nOperation cancelled.") |
| 36 | + sys.exit(0) |
| 37 | + |
| 38 | + print(f"\nBootstrapping project '{project_name}' for user '{github_username}'...\n") |
| 39 | + |
| 40 | + # 2. Update README.md |
| 41 | + # Replaces: |
| 42 | + # - fast/template -> username/project_name |
| 43 | + # - ${projectName} -> project_name |
| 44 | + readme_path = "README.md" |
| 45 | + if os.path.exists(readme_path): |
| 46 | + with open(readme_path, "r", encoding="utf-8") as f: |
| 47 | + content = f.read() |
| 48 | + |
| 49 | + new_content = content.replace("fast/template", f"{github_username}/{project_name}") |
| 50 | + new_content = new_content.replace("${projectName}", project_name) |
| 51 | + |
| 52 | + if content != new_content: |
| 53 | + with open(readme_path, "w", encoding="utf-8") as f: |
| 54 | + f.write(new_content) |
| 55 | + print(f"✅ Updated {readme_path}") |
| 56 | + else: |
| 57 | + print(f"ℹ️ No changes needed in {readme_path}") |
| 58 | + else: |
| 59 | + print(f"⚠️ Warning: {readme_path} not found.") |
| 60 | + |
| 61 | + # 3. Update Cargo.toml (Workspace Root) |
| 62 | + # Replaces: |
| 63 | + # - fast/template -> username/project_name |
| 64 | + # - "template" (in members) -> "project_name" |
| 65 | + root_cargo_path = "Cargo.toml" |
| 66 | + if os.path.exists(root_cargo_path): |
| 67 | + with open(root_cargo_path, "r", encoding="utf-8") as f: |
| 68 | + content = f.read() |
| 69 | + |
| 70 | + new_content = content.replace("fast/template", f"{github_username}/{project_name}") |
| 71 | + # Identify workspace member "template" specifically to avoid false positives |
| 72 | + new_content = new_content.replace('"template"', f'"{project_name}"') |
| 73 | + |
| 74 | + if content != new_content: |
| 75 | + with open(root_cargo_path, "w", encoding="utf-8") as f: |
| 76 | + f.write(new_content) |
| 77 | + print(f"✅ Updated {root_cargo_path}") |
| 78 | + else: |
| 79 | + print(f"ℹ️ No changes needed in {root_cargo_path}") |
| 80 | + else: |
| 81 | + print(f"⚠️ Warning: {root_cargo_path} not found.") |
| 82 | + |
| 83 | + # 4. Update template/Cargo.toml (Package Name) |
| 84 | + # Replaces: |
| 85 | + # - name = "template" -> name = "project_name" |
| 86 | + # Note: We edit the file inside the directory *before* renaming the directory |
| 87 | + template_cargo_path = "template/Cargo.toml" |
| 88 | + if os.path.exists(template_cargo_path): |
| 89 | + with open(template_cargo_path, "r", encoding="utf-8") as f: |
| 90 | + content = f.read() |
| 91 | + |
| 92 | + new_content = content.replace('name = "template"', f'name = "{project_name}"') |
| 93 | + |
| 94 | + if content != new_content: |
| 95 | + with open(template_cargo_path, "w", encoding="utf-8") as f: |
| 96 | + f.write(new_content) |
| 97 | + print(f"✅ Updated {template_cargo_path}") |
| 98 | + else: |
| 99 | + print(f"ℹ️ No changes needed in {template_cargo_path}") |
| 100 | + else: |
| 101 | + # If the directory was already renamed in a previous run, we might want to check the new name |
| 102 | + # but for a simple bootstrap script, assuming standard state is fine. |
| 103 | + print(f"⚠️ Warning: {template_cargo_path} not found (Did you already run this script?)") |
| 104 | + |
| 105 | + # 5. Rename template directory |
| 106 | + if os.path.exists("template"): |
| 107 | + os.rename("template", project_name) |
| 108 | + print(f"✅ Renamed directory 'template' to '{project_name}'") |
| 109 | + else: |
| 110 | + if os.path.exists(project_name): |
| 111 | + print(f"ℹ️ Directory '{project_name}' already exists.") |
| 112 | + else: |
| 113 | + print("⚠️ Warning: Directory 'template' not found.") |
| 114 | + |
| 115 | + print("\n🎉 Bootstrap complete!") |
| 116 | + print(f"You can now delete this script: rm {os.path.basename(__file__)}") |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + main() |
0 commit comments