-
Notifications
You must be signed in to change notification settings - Fork 0
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
13 changed files
with
339 additions
and
277 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,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() |
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,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() |
Oops, something went wrong.