|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +""" |
| 20 | +Bootstrap React Plugin CLI Tool. |
| 21 | +
|
| 22 | +This script provides a command-line interface to create new React UI plugin |
| 23 | +directories based on the airflow-core/ui project structure. It sets up all the |
| 24 | +necessary configuration files, dependencies, and basic structure for development |
| 25 | +with the same tooling as used in Airflow's core UI. |
| 26 | +""" |
| 27 | + |
| 28 | +from __future__ import annotations |
| 29 | + |
| 30 | +import argparse |
| 31 | +import re |
| 32 | +import shutil |
| 33 | +import sys |
| 34 | +from pathlib import Path |
| 35 | + |
| 36 | + |
| 37 | +def get_template_dir() -> Path: |
| 38 | + """Get the template directory path.""" |
| 39 | + script_dir = Path(__file__).parent |
| 40 | + template_dir = script_dir / "react_plugin_template" |
| 41 | + |
| 42 | + if not template_dir.exists(): |
| 43 | + print(f"Error: Template directory not found at {template_dir}") |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | + return template_dir |
| 47 | + |
| 48 | + |
| 49 | +def replace_template_variables(content: str, project_name: str) -> str: |
| 50 | + """Replace template variables in file content.""" |
| 51 | + return content.replace("{{PROJECT_NAME}}", project_name) |
| 52 | + |
| 53 | + |
| 54 | +def remove_apache_license_header(content: str, file_extension: str) -> str: |
| 55 | + """Remove Apache license header from file content based on file type.""" |
| 56 | + if file_extension in [".ts", ".tsx", ".js", ".jsx"]: |
| 57 | + license_pattern = r"/\*!\s*\*\s*Licensed to the Apache Software Foundation.*?\*/\s*" |
| 58 | + content = re.sub(license_pattern, "", content, flags=re.DOTALL) |
| 59 | + elif file_extension in [".md"]: |
| 60 | + license_pattern = r"<!--\s*Licensed to the Apache Software Foundation.*?-->\s*" |
| 61 | + content = re.sub(license_pattern, "", content, flags=re.DOTALL) |
| 62 | + elif file_extension in [".html"]: |
| 63 | + license_pattern = r"<!--\s*Licensed to the Apache Software Foundation.*?-->\s*" |
| 64 | + content = re.sub(license_pattern, "", content, flags=re.DOTALL) |
| 65 | + |
| 66 | + return content |
| 67 | + |
| 68 | + |
| 69 | +def copy_template_files(template_dir: Path, project_path: Path, project_name: str) -> None: |
| 70 | + for item in template_dir.rglob("*"): |
| 71 | + if item.is_file(): |
| 72 | + # Calculate relative path from template root |
| 73 | + rel_path = item.relative_to(template_dir) |
| 74 | + target_path = project_path / rel_path |
| 75 | + |
| 76 | + target_path.parent.mkdir(parents=True, exist_ok=True) |
| 77 | + |
| 78 | + with open(item, encoding="utf-8") as f: |
| 79 | + content = f.read() |
| 80 | + |
| 81 | + content = replace_template_variables(content, project_name) |
| 82 | + |
| 83 | + file_extension = item.suffix.lower() |
| 84 | + content = remove_apache_license_header(content, file_extension) |
| 85 | + |
| 86 | + with open(target_path, "w", encoding="utf-8") as f: |
| 87 | + f.write(content) |
| 88 | + |
| 89 | + print(f" Created: {rel_path}") |
| 90 | + |
| 91 | + |
| 92 | +def bootstrap_react_plugin(args) -> None: |
| 93 | + """Bootstrap a new React plugin project.""" |
| 94 | + project_name = args.name |
| 95 | + target_dir = args.dir if args.dir else project_name |
| 96 | + |
| 97 | + project_path = Path(target_dir).resolve() |
| 98 | + template_dir = get_template_dir() |
| 99 | + |
| 100 | + if project_path.exists(): |
| 101 | + print(f"Error: Directory '{project_path}' already exists!") |
| 102 | + sys.exit(1) |
| 103 | + |
| 104 | + if not project_name.replace("-", "").replace("_", "").isalnum(): |
| 105 | + print("Error: Project name should only contain letters, numbers, hyphens, and underscores") |
| 106 | + sys.exit(1) |
| 107 | + |
| 108 | + print(f"Creating React plugin project: {project_name}") |
| 109 | + print(f"Target directory: {project_path}") |
| 110 | + print(f"Template directory: {template_dir}") |
| 111 | + |
| 112 | + project_path.mkdir(parents=True, exist_ok=True) |
| 113 | + |
| 114 | + try: |
| 115 | + # Copy template files |
| 116 | + print("Copying template files...") |
| 117 | + copy_template_files(template_dir, project_path, project_name) |
| 118 | + |
| 119 | + print(f"\n✅ Successfully created {project_name}!") |
| 120 | + print("\nNext steps:") |
| 121 | + print(f" cd {target_dir}") |
| 122 | + print(" pnpm install") |
| 123 | + print(" pnpm dev") |
| 124 | + print("\nHappy coding! 🚀") |
| 125 | + |
| 126 | + except Exception as e: |
| 127 | + print(f"Error creating project: {e}") |
| 128 | + if project_path.exists(): |
| 129 | + shutil.rmtree(project_path) |
| 130 | + sys.exit(1) |
| 131 | + |
| 132 | + |
| 133 | +def main(): |
| 134 | + """Main CLI entry point.""" |
| 135 | + parser = argparse.ArgumentParser( |
| 136 | + description="Bootstrap a new React UI plugin project", |
| 137 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 138 | + epilog=""" |
| 139 | +Examples: |
| 140 | + python bootstrap.py my-plugin |
| 141 | + python bootstrap.py my-plugin --dir /path/to/projects/my-plugin |
| 142 | +
|
| 143 | +This will create a new React project with all the necessary configuration |
| 144 | +files, dependencies, and structure needed for Airflow plugin development. |
| 145 | + """, |
| 146 | + ) |
| 147 | + |
| 148 | + parser.add_argument( |
| 149 | + "name", |
| 150 | + help="Name of the React plugin project (letters, numbers, hyphens, and underscores only)", |
| 151 | + ) |
| 152 | + |
| 153 | + parser.add_argument( |
| 154 | + "--dir", |
| 155 | + "-d", |
| 156 | + help="Target directory for the project (defaults to project name)", |
| 157 | + ) |
| 158 | + |
| 159 | + parser.add_argument( |
| 160 | + "--verbose", |
| 161 | + "-v", |
| 162 | + action="store_true", |
| 163 | + help="Enable verbose output", |
| 164 | + ) |
| 165 | + |
| 166 | + args = parser.parse_args() |
| 167 | + |
| 168 | + try: |
| 169 | + bootstrap_react_plugin(args) |
| 170 | + except KeyboardInterrupt: |
| 171 | + print("\n\nOperation cancelled by user.") |
| 172 | + sys.exit(1) |
| 173 | + except Exception as e: |
| 174 | + print(f"Error: {e}") |
| 175 | + sys.exit(1) |
| 176 | + |
| 177 | + |
| 178 | +if __name__ == "__main__": |
| 179 | + main() |
0 commit comments