-
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.
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #!/bin/env python | ||
|
|
||
| import argparse | ||
| import shutil | ||
| from pathlib import Path | ||
| from jinja2 import Environment, FileSystemLoader | ||
|
|
||
| __version__ = "1.0.0" | ||
|
|
||
| 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("-v", "--version", action="version", version=f"%(prog)s {__version__}") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # 1. Logic for group name and folder naming | ||
| group_name = args.group if args.group else Path.cwd().name | ||
|
|
||
| # Construct created_group name based on presence of application_label | ||
| if args.application_label: | ||
| created_group = f"{args.business_label}-{args.application_label}-{group_name}" | ||
| else: | ||
| created_group = f"{args.business_label}-{group_name}" | ||
|
|
||
| # 2. Setup paths | ||
| template_dir = Path("TEMPLATE") | ||
| target_dir = Path(created_group) | ||
|
|
||
| if not template_dir.exists(): | ||
| print(f"Error: {template_dir} directory not found.") | ||
| return | ||
|
|
||
| target_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # 3. Initialize Jinja2 Environment | ||
| env = Environment(loader=FileSystemLoader(str(template_dir))) | ||
|
|
||
| # 4. Expose all arguments as variables for rendering | ||
| # This dictionary maps exactly to what you can use in your {{ }} tags | ||
| render_vars = { | ||
| "business_label": args.business_label, | ||
| "application_label": args.application_label, | ||
| "group": group_name, | ||
| "created_group": created_group | ||
| } | ||
|
|
||
| print(f"Processing templates for: {created_group}...") | ||
|
|
||
| # 5. File Processing Loop | ||
| for item in template_dir.iterdir(): | ||
| if item.is_dir(): | ||
| continue # Skips subdirectories | ||
|
|
||
| if item.suffix == ".j2": | ||
| # Target logic: Replace 'GROUP' with created_group and strip '.j2' | ||
| new_filename = item.name.replace("GROUP", created_group).replace(".j2", "") | ||
| target_path = target_dir / new_filename | ||
|
|
||
| # Load and render | ||
| 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: | ||
| # Standard copy for non-template files | ||
| shutil.copy2(item, target_dir / item.name) | ||
| print(f" [Copied] {item.name}") | ||
|
|
||
| print(f"\nSuccessfully created group directory: {created_group}") | ||
|
|
||
| if __name__ == "__main__": | ||
| create_sc_group() |