|
13 | 13 | - To get your project version, run ``pythoncms --version`` |
14 | 14 | - Run the sample command as ``pythoncms welcome [OPTIONS] NAME`` |
15 | 15 | """ |
| 16 | +import os |
| 17 | +import subprocess |
| 18 | +import sys |
16 | 19 | from pathlib import Path |
17 | 20 |
|
18 | 21 | import click |
19 | 22 | from shopyo.api.file import trycopytree |
20 | 23 | from shopyo.api.file import trymkfile |
21 | 24 | from shopyo.api.file import tryrmfile |
22 | | -from shopyo.api.file import tryrmtree |
23 | 25 |
|
24 | 26 | from pythoncms import __version__ |
25 | 27 |
|
26 | 28 | path = Path(__file__) |
27 | | -import os |
28 | 29 |
|
29 | 30 |
|
30 | 31 | @click.group() |
31 | 32 | @click.version_option(__version__) |
32 | | -@click.pass_context |
33 | | -def cli(ctx): |
34 | | - """CLI entry point""" |
| 33 | +def cli(): |
| 34 | + """pythoncms CLI - The fastest way to build CMS in Python""" |
35 | 35 | pass |
36 | 36 |
|
37 | 37 |
|
38 | | -# @cli.command("welcome") |
39 | | -# @click.argument("name") |
40 | | -# @click.option("--verbose", "-v", is_flag=True, default=False) |
41 | | -# def welcome(name, verbose): |
42 | | -# """Sample command to welcome users. |
43 | | - |
44 | | -# NAME will be printed along with the welcome message |
45 | | -# """ |
46 | | -# click.secho(f"Hi {name}. Welcome to pythoncms", fg="cyan") |
47 | | - |
48 | | -# if verbose: |
49 | | -# click.echo("See you soon") |
50 | | - |
51 | | -reqs = f"""pythoncms=={__version__}""" |
52 | | - |
53 | | - |
54 | 38 | @cli.command("start") |
55 | 39 | @click.argument("name") |
56 | | -def start(name): |
57 | | - """ |
58 | | - New project |
59 | | - """ |
60 | | - dest = os.getcwd() + f"/{name}" |
61 | | - |
| 40 | +@click.option("--run", is_flag=True, help="Initialize and run the server immediately") |
| 41 | +def start(name, run): |
| 42 | + """Create a new pythoncms project""" |
| 43 | + dest = os.path.join(os.getcwd(), name) |
| 44 | + |
| 45 | + if os.path.exists(dest): |
| 46 | + click.echo(f"Error: Directory {name} already exists.") |
| 47 | + return |
| 48 | + |
| 49 | + # Copy the project structure |
| 50 | + # We copy the parent of this file (pythoncms/ directory) |
| 51 | + # but we need to exclude things like __pycache__ and cli.py from the target if needed |
| 52 | + # actually the original logic was trycopytree(str(path.parent.absolute()), dest) |
62 | 53 | trycopytree(str(path.parent.absolute()), dest) |
63 | | - tryrmfile(dest + "/cli.py") |
64 | | - trymkfile(dest + "/requirements.txt", reqs) |
65 | | - try: |
66 | | - os.rename('.env_demo', '.env') |
67 | | - except Exception as e: |
68 | | - print(e) |
69 | | - click.echo(f"🍭 Pythoncms project {name} is ready to go!") |
| 54 | + |
| 55 | + # Cleanup target |
| 56 | + tryrmfile(os.path.join(dest, "cli.py")) |
| 57 | + |
| 58 | + # Create requirements.txt |
| 59 | + reqs = f"pythoncms=={__version__}" |
| 60 | + trymkfile(os.path.join(dest, "requirements.txt"), reqs) |
| 61 | + |
| 62 | + # Setup .env |
| 63 | + env_demo = os.path.join(dest, ".env_demo") |
| 64 | + env_real = os.path.join(dest, ".env") |
| 65 | + if os.path.exists(env_demo): |
| 66 | + os.rename(env_demo, env_real) |
| 67 | + |
| 68 | + click.echo(f"🍭 Pythoncms project {name} is ready!") |
| 69 | + |
| 70 | + if run: |
| 71 | + os.chdir(dest) |
| 72 | + click.echo("Running initialization...") |
| 73 | + subprocess.run(["shopyo", "initialise"], check=True) |
| 74 | + click.echo("Seeding database...") |
| 75 | + subprocess.run(["flask", "shopyo-seed"], check=True) |
| 76 | + click.echo("Starting server...") |
| 77 | + subprocess.run(["flask", "--debug", "run"], check=True) |
| 78 | + |
| 79 | + |
| 80 | +@cli.command("initialise") |
| 81 | +def initialise(): |
| 82 | + """Initialise the project database and assets""" |
| 83 | + subprocess.run(["shopyo", "initialise"], check=True) |
| 84 | + |
| 85 | + |
| 86 | +@cli.command("seed") |
| 87 | +def seed(): |
| 88 | + """Seed the database with default data""" |
| 89 | + subprocess.run(["flask", "shopyo-seed"], check=True) |
| 90 | + |
| 91 | + |
| 92 | +@cli.command("run") |
| 93 | +@click.option("--debug", is_flag=True, default=True) |
| 94 | +def run(debug): |
| 95 | + """Run the development server""" |
| 96 | + args = ["flask"] |
| 97 | + if debug: |
| 98 | + args.append("--debug") |
| 99 | + args.append("run") |
| 100 | + subprocess.run(args, check=True) |
0 commit comments