Skip to content

Commit

Permalink
WIP: cli fixed, not sure.
Browse files Browse the repository at this point in the history
  • Loading branch information
tavallaie committed May 25, 2024
1 parent 8502bf7 commit e7ee4cf
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 277 deletions.
89 changes: 89 additions & 0 deletions djangowiz/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# djangowiz/app.py

import typer
import os
from typing import Optional, Dict, Any
import yaml
from djangowiz.core.project_generator import ProjectGenerator
from djangowiz.core.model_extractor import ModelExtractor


def create_app():
app = typer.Typer()
generate_app = typer.Typer()
app.add_typer(generate_app, name="generate")

def load_generators(config_file: str) -> Dict[str, Any]:
print(f"Loading configuration from {config_file}")
with open(config_file, "r") as file:
return yaml.safe_load(file)

def get_command_function(generator_name: str, option: str):
def command_function(
app_name: str,
project_name: str,
model_file: str,
template_dir: Optional[str] = typer.Option(
None, help="Custom template directory"
),
config_file: Optional[str] = typer.Option(None, help="Custom config file"),
repo_dir: Optional[str] = typer.Option(None, help="Custom repo directory"),
overwrite: bool = typer.Option(False, help="Overwrite existing files"),
):
model_names = ModelExtractor.extract_model_names(model_file)
project_generator = ProjectGenerator(
app_name, project_name, model_names, template_dir, config_file, repo_dir
)
project_generator.generate([generator_name], option, overwrite)

return command_function

def register_commands(config_file: str):
generators = load_generators(config_file)
# Handle the nested 'generators' key
generator_configs = generators.get("generators", {})

for generator_name, generator_config in generator_configs.items():
for option, config in generator_config.get("options", {}).items():
command_function = get_command_function(generator_name, option)
option_name = f"{generator_name}-{option}"
generate_app.command(name=option_name.replace("_", "-"))(
command_function
)

@app.command()
def generate_files(
app_name: str,
project_name: str,
model_file: str,
template_dir: Optional[str] = typer.Option(None),
config_file: Optional[str] = typer.Option(None),
repo_dir: Optional[str] = typer.Option(None),
single_file: bool = typer.Option(False),
overwrite: bool = typer.Option(False),
):
model_names = ModelExtractor.extract_model_names(model_file)
project_generator = ProjectGenerator(
app_name, project_name, model_names, template_dir, config_file, repo_dir
)
project_generator.generate(
[
"serializers",
"viewsets",
"urls",
"routes",
"dockerfile",
"docker_compose",
"env_files",
],
"single" if single_file else "multi",
overwrite,
)

register_commands(
os.path.join(os.path.dirname(__file__), "repo", "generators.yaml")
)
return app


app = create_app()
146 changes: 66 additions & 80 deletions djangowiz/cli.py
Original file line number Diff line number Diff line change
@@ -1,104 +1,90 @@
# djangowiz/app.py

import typer
from djangowiz.core import ModelExtractor, ProjectGenerator
import os
from typing import Optional, Dict, Any, List
import yaml
from djangowiz.core.project_generator import ProjectGenerator
from djangowiz.core.model_extractor import ModelExtractor

app = typer.Typer()
generate_app = typer.Typer()
app.add_typer(generate_app, name="generate")

generators_config = {}

@app.command()
def generate_files(
app_name: str,
project_name: str,
model_file: str,
single_file: bool = False,
overwrite: bool = False,
template_dir: str = None,
):
model_names = ModelExtractor.extract_model_names(model_file)
generator = ProjectGenerator(app_name, project_name, model_names, template_dir)
generator.generate_all(single_file, overwrite)
print(
f"Files for {len(model_names)} models have been generated in the {app_name} directory."
)

def load_generators(config_file: str) -> Dict[str, Any]:
print(f"Loading configuration from {config_file}")
with open(config_file, "r") as file:
return yaml.safe_load(file)

@app.command()
def generate_core_files(
app_name: str,
project_name: str,
model_file: str,
single_file: bool = False,
overwrite: bool = False,
template_dir: str = None,
):
model_names = ModelExtractor.extract_model_names(model_file)
generator = ProjectGenerator(app_name, project_name, model_names, template_dir)
generator.generate_core_files(single_file, overwrite)
print(
f"Serializers, viewsets, and routes for {len(model_names)} models have been generated in the {app_name} directory."
)

def create_command_function(generator_name: str, options: Dict[str, Any]):
def command_function(
app_name: str,
project_name: str,
model_file: str,
overwrite: bool = typer.Option(False, help="Overwrite existing files"),
option: str = typer.Option(
..., help=f"Specify the generation option ({', '.join(options.keys())})"
),
template_dir: Optional[str] = typer.Option(
None, help="Custom template directory"
),
config_file: Optional[str] = typer.Option(None, help="Custom config file"),
repo_dir: Optional[str] = typer.Option(None, help="Custom repo directory"),
):
model_names = ModelExtractor.extract_model_names(model_file)
project_generator = ProjectGenerator(
app_name, project_name, model_names, template_dir, config_file, repo_dir
)
project_generator.generate([generator_name], option, overwrite)

@app.command()
def generate_serializers(
app_name: str,
project_name: str,
model_file: str,
single_file: bool = False,
overwrite: bool = False,
template_dir: str = None,
):
model_names = ModelExtractor.extract_model_names(model_file)
generator = ProjectGenerator(app_name, project_name, model_names, template_dir)
generator.generate_serializers(single_file, overwrite)
print(
f"Serializers for {len(model_names)} models have been generated in the {app_name} directory."
)
return command_function


@app.command()
def generate_viewsets(
app_name: str,
project_name: str,
model_file: str,
single_file: bool = False,
overwrite: bool = False,
template_dir: str = None,
):
model_names = ModelExtractor.extract_model_names(model_file)
generator = ProjectGenerator(app_name, project_name, model_names, template_dir)
generator.generate_viewsets(single_file, overwrite)
print(
f"Viewsets for {len(model_names)} models have been generated in the {app_name} directory."
)
def register_commands(config_file: str):
global generators_config
generators = load_generators(config_file)
generators_config = generators.get("generators", {})
for generator_name, generator_config in generators_config.items():
options = generator_config.get("options", {})
command_function = create_command_function(generator_name, options)
generate_app.command(name=generator_name)(command_function)


@app.command()
def generate_urls(
def generate_files(
app_name: str,
project_name: str,
model_file: str,
overwrite: bool = False,
template_dir: str = None,
overwrite: bool = typer.Option(False),
option: str = typer.Option(..., help="Specify the generation option"),
template_dir: Optional[str] = typer.Option(None),
config_file: Optional[str] = typer.Option(None),
repo_dir: Optional[str] = typer.Option(None),
):
model_names = ModelExtractor.extract_model_names(model_file)
generator = ProjectGenerator(app_name, project_name, model_names, template_dir)
generator.generate_urls(overwrite)
print(f"URLs have been generated in the {app_name} directory.")

project_generator = ProjectGenerator(
app_name, project_name, model_names, template_dir, config_file, repo_dir
)
project_generator.generate(
[
"serializers",
"viewsets",
"urls",
"routes",
"dockerfile",
"docker_compose",
"env_files",
],
option,
overwrite,
)

@app.command()
def generate_routes(
app_name: str,
project_name: str,
model_file: str,
overwrite: bool = False,
template_dir: str = None,
):
model_names = ModelExtractor.extract_model_names(model_file)
generator = ProjectGenerator(app_name, project_name, model_names, template_dir)
generator.generate_routes(overwrite)
print(f"Routes have been generated in the {app_name} directory.")

register_commands(os.path.join(os.path.dirname(__file__), "repo", "generators.yaml"))

if __name__ == "__main__":
app()
Loading

0 comments on commit e7ee4cf

Please sign in to comment.