Skip to content

Commit 375465a

Browse files
Phase 1: Unified CLI with start, initialise, seed, and run commands
1 parent 32e829b commit 375465a

1 file changed

Lines changed: 65 additions & 34 deletions

File tree

pythoncms/cli.py

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,88 @@
1313
- To get your project version, run ``pythoncms --version``
1414
- Run the sample command as ``pythoncms welcome [OPTIONS] NAME``
1515
"""
16+
import os
17+
import subprocess
18+
import sys
1619
from pathlib import Path
1720

1821
import click
1922
from shopyo.api.file import trycopytree
2023
from shopyo.api.file import trymkfile
2124
from shopyo.api.file import tryrmfile
22-
from shopyo.api.file import tryrmtree
2325

2426
from pythoncms import __version__
2527

2628
path = Path(__file__)
27-
import os
2829

2930

3031
@click.group()
3132
@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"""
3535
pass
3636

3737

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-
5438
@cli.command("start")
5539
@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)
6253
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

Comments
 (0)