|
1 | 1 | import sys
|
| 2 | +import typing as t |
2 | 3 |
|
3 |
| -from ellar.common.commands import command |
| 4 | +import django |
| 5 | +import ellar_cli.click as click |
4 | 6 |
|
5 |
| -HELP_MESSAGE = """ |
6 |
| -Ellar will always intercept and command with '--help'. |
7 | 7 |
|
8 |
| -So if you want to get help on any django command, |
9 |
| -simply wrap the command and --help in quotes |
| 8 | +class _CommandItem(t.NamedTuple): |
| 9 | + name: str |
| 10 | + description: str |
10 | 11 |
|
11 |
| -For example: ellar django 'migrate --help' |
12 |
| -""" |
13 | 12 |
|
| 13 | +_django_support_commands: t.List[_CommandItem] = [ |
| 14 | + _CommandItem( |
| 15 | + name="migrate", |
| 16 | + description="Synchronizes the database state with the current set of models and migrations", |
| 17 | + ), |
| 18 | + _CommandItem( |
| 19 | + name="makemigrations", |
| 20 | + description="Creates new migrations based on the changes detected to your models.", |
| 21 | + ), |
| 22 | + _CommandItem( |
| 23 | + name="check", |
| 24 | + description="inspects the entire Django project for common problems", |
| 25 | + ), |
| 26 | + _CommandItem( |
| 27 | + name="createcachetable", |
| 28 | + description="Creates the cache tables for use with the database cache backend", |
| 29 | + ), |
| 30 | + _CommandItem( |
| 31 | + name="dbshell", |
| 32 | + description="Runs the command-line client for the database engine", |
| 33 | + ), |
| 34 | + _CommandItem( |
| 35 | + name="diffsettings", |
| 36 | + description="Displays differences between the current settings and default settings", |
| 37 | + ), |
| 38 | + _CommandItem( |
| 39 | + name="dumpdata", |
| 40 | + description="Outputs to standard output all data in the database", |
| 41 | + ), |
| 42 | + _CommandItem( |
| 43 | + name="flush", |
| 44 | + description="Removes all data from the database and re-executes any post-synchronization handlers", |
| 45 | + ), |
| 46 | + _CommandItem( |
| 47 | + name="inspectdb", description="Introspects the database tables in the database" |
| 48 | + ), |
| 49 | + _CommandItem( |
| 50 | + name="loaddata", |
| 51 | + description="Searches for and loads the contents into the database.", |
| 52 | + ), |
| 53 | + _CommandItem( |
| 54 | + name="optimizemigration", |
| 55 | + description="Optimizes the operations for the named migration and overrides the existing file", |
| 56 | + ), |
| 57 | + _CommandItem( |
| 58 | + name="showmigrations", description="Shows all migrations in a project." |
| 59 | + ), |
| 60 | + _CommandItem( |
| 61 | + name="sqlflush", |
| 62 | + description="Prints the SQL statements that would be executed for the flush command", |
| 63 | + ), |
| 64 | + _CommandItem( |
| 65 | + name="sqlmigrate", description="Prints the SQL for the named migration." |
| 66 | + ), |
| 67 | + _CommandItem( |
| 68 | + name="sqlsequencereset", |
| 69 | + description="Prints the SQL statements for resetting sequences for the given app name(s)", |
| 70 | + ), |
| 71 | + _CommandItem( |
| 72 | + name="squashmigrations", description="Squashes the migrations for app_label" |
| 73 | + ), |
| 74 | + _CommandItem( |
| 75 | + name="startapp", description="Creates a Django app directory structure" |
| 76 | + ), |
| 77 | + _CommandItem( |
| 78 | + name="changepassword", |
| 79 | + description="This command is only available if Django’s authentication system", |
| 80 | + ), |
| 81 | + _CommandItem( |
| 82 | + name="createsuperuser", |
| 83 | + description="Creates a superuser account (a user who has all permissions)", |
| 84 | + ), |
| 85 | + _CommandItem( |
| 86 | + name="collectstatic", description="Expose static files to STATIC_ROOT folder" |
| 87 | + ), |
| 88 | + _CommandItem(name="findstatic", description="Search for a static file location"), |
| 89 | + _CommandItem( |
| 90 | + name="clearsessions", |
| 91 | + description="Can be run as a cron job or directly to clean out expired sessions.", |
| 92 | + ), |
| 93 | +] |
14 | 94 |
|
15 |
| -@command( |
| 95 | + |
| 96 | +def version_callback(ctx: click.Context, _: t.Any, value: bool) -> None: |
| 97 | + if value: |
| 98 | + click.echo(f"Django Version: {django.__version__}") |
| 99 | + raise click.Exit(0) |
| 100 | + |
| 101 | + |
| 102 | +@click.group( |
16 | 103 | name="django",
|
17 |
| - context_settings={"ignore_unknown_options": True, "allow_extra_args": True}, |
18 |
| - help=HELP_MESSAGE, |
| 104 | + help="- Ellar Django Commands", |
19 | 105 | )
|
20 |
| -def django_command_wrapper() -> None: |
21 |
| - from django.core.management import execute_from_command_line |
| 106 | +@click.option( |
| 107 | + "-v", |
| 108 | + "--version", |
| 109 | + callback=version_callback, |
| 110 | + help="Show the version and exit.", |
| 111 | + is_flag=True, |
| 112 | + expose_value=False, |
| 113 | + is_eager=True, |
| 114 | +) |
| 115 | +@click.pass_context |
| 116 | +def django_command(ctx: click.Context) -> None: |
| 117 | + pass |
| 118 | + |
| 119 | + |
| 120 | +def _add_django_command(command_item: _CommandItem) -> None: |
| 121 | + def help_callback(ctx: click.Context, _: t.Any, value: bool) -> None: |
| 122 | + from django.core.management import execute_from_command_line |
| 123 | + |
| 124 | + if value: |
| 125 | + args = ["manage.py", command_item.name, "--help"] |
| 126 | + |
| 127 | + execute_from_command_line(args) |
| 128 | + raise click.Exit(0) |
| 129 | + |
| 130 | + @django_command.command( |
| 131 | + name=command_item.name, |
| 132 | + help=command_item.description, |
| 133 | + context_settings={"ignore_unknown_options": True, "allow_extra_args": True}, |
| 134 | + add_help_option=False, |
| 135 | + ) |
| 136 | + @click.option( |
| 137 | + "-h", |
| 138 | + "--help", |
| 139 | + callback=help_callback, |
| 140 | + help="Show the version and exit.", |
| 141 | + is_flag=True, |
| 142 | + expose_value=False, |
| 143 | + is_eager=True, |
| 144 | + ) |
| 145 | + @click.with_app_context |
| 146 | + def _command() -> None: |
| 147 | + from django.core.management import execute_from_command_line |
| 148 | + |
| 149 | + args = ["manage.py", command_item.name] |
| 150 | + |
| 151 | + for item in sys.argv[3:]: |
| 152 | + args.extend(item.split(" ")) |
| 153 | + execute_from_command_line(args) |
| 154 | + |
22 | 155 |
|
23 |
| - args = [] |
24 |
| - for item in sys.argv[1:]: |
25 |
| - args.extend(item.split(" ")) |
26 |
| - execute_from_command_line(args) |
| 156 | +list(map(_add_django_command, _django_support_commands)) |
0 commit comments