-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check for dir, dont create unless force
- Loading branch information
Showing
1 changed file
with
17 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,82 +1,84 @@ | ||
| #!/usr/bin/env python | ||
| import argparse | ||
| import shutil | ||
| import sys | ||
| from pathlib import Path | ||
| from jinja2 import Environment, FileSystemLoader | ||
|
|
||
| # Version History: | ||
| # 1.0.0: Initial release | ||
| # 1.0.1: Added shebang, exposed variables to Jinja2 | ||
| # 1.0.2: Enforced lowercase for group and created_group names | ||
| __version__ = "1.0.2" | ||
| # 1.0.3: Added directory existence check and --force flag | ||
| __version__ = "1.0.3" | ||
|
|
||
| def create_sc_group(): | ||
| parser = argparse.ArgumentParser(description="Automate file creation from templates.") | ||
| parser.add_argument("-b", "--business-label", required=True, help="Required business label") | ||
| parser.add_argument("-a", "--application-label", help="Optional application label") | ||
| parser.add_argument("-g", "--group", help="Group name (defaults to current directory name)") | ||
| parser.add_argument("-f", "--force", action="store_true", help="Overwrite existing directory if it exists") | ||
| parser.add_argument("-v", "--version", action="version", version=f"%(prog)s {__version__}") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # 1. Determine the 'group' name and force to lowercase | ||
| # 1. Determine names and force to lowercase | ||
| raw_group = args.group if args.group else Path.cwd().name | ||
| group_name = raw_group.lower() | ||
|
|
||
| # 2. Define the 'created_group' string and force to lowercase | ||
| if args.application_label: | ||
| created_group = f"{args.business_label}-{args.application_label}-{group_name}".lower() | ||
| else: | ||
| created_group = f"{args.business_label}-{group_name}".lower() | ||
|
|
||
| # 3. Setup paths | ||
| template_dir = Path("TEMPLATE") | ||
| # 2. Check for existing directory | ||
| target_dir = Path(created_group) | ||
| if target_dir.exists() and not args.force: | ||
| print(f"Error: Directory '{created_group}' already exists.") | ||
| print("Use --force or -f to recreate the files.") | ||
| sys.exit(1) | ||
|
|
||
| # 3. Setup template path | ||
| template_dir = Path("TEMPLATE") | ||
| if not template_dir.exists(): | ||
| print(f"Error: {template_dir} directory not found.") | ||
| return | ||
| sys.exit(1) | ||
|
|
||
| target_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # 4. Initialize Jinja2 Environment | ||
| env = Environment(loader=FileSystemLoader(str(template_dir))) | ||
|
|
||
| # 5. Expose variables to Jinja2 templates (all relevant strings are now lowercase) | ||
| # 5. Context variables | ||
| render_vars = { | ||
| "business_label": args.business_label, | ||
| "application_label": args.application_label, | ||
| "group": group_name, | ||
| "created_group": created_group | ||
| } | ||
|
|
||
| print(f"Creating files for: {created_group}") | ||
| print(f"{'Recreating' if args.force and target_dir.exists() else 'Creating'} files for: {created_group}") | ||
|
|
||
| # 6. Process files in TEMPLATE/ | ||
| # 6. Process files | ||
| for item in template_dir.iterdir(): | ||
| if item.is_dir(): | ||
| continue # Skips subdirectories | ||
| continue | ||
|
|
||
| if item.suffix == ".j2": | ||
| # Rule: Replace 'GROUP' with created_group and strip '.j2' | ||
| # Note: We replace the literal string 'GROUP' in the filename | ||
| new_filename = item.name.replace("GROUP", created_group).replace(".j2", "") | ||
| target_path = target_dir / new_filename | ||
|
|
||
| # Render the template with variables | ||
| template = env.get_template(item.name) | ||
| output_content = template.render(render_vars) | ||
|
|
||
| target_path.write_text(output_content) | ||
| print(f" [Rendered] {item.name} -> {new_filename}") | ||
|
|
||
| else: | ||
| # Rule: Copy files not ending in .j2 directly | ||
| shutil.copy2(item, target_dir / item.name) | ||
| print(f" [Copied] {item.name}") | ||
|
|
||
| print(f"\nDone. Files located in: ./{created_group}") | ||
|
|
||
| if __name__ == "__main__": | ||
| create_sc_group() | ||
|
|