Skip to content

Commit

Permalink
fix: cli invocation for Robyn (#723)
Browse files Browse the repository at this point in the history
* fix: cli invocation for Robyn

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix clippy

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
sansyrox and pre-commit-ci[bot] authored Dec 25, 2023
1 parent ca4a4a3 commit 3bcd7a5
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 118 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ nox = "2023.4.22"
websocket-client = "1.5.0"

[tool.poetry.scripts]
robyn = "robyn.cli:run"
test_server = { callable = "integration_tests.base_routes:main" }

[tool.ruff]
Expand Down
119 changes: 2 additions & 117 deletions robyn/__main__.py
Original file line number Diff line number Diff line change
@@ -1,119 +1,4 @@
import os
import sys
from typing import Optional
import webbrowser
from InquirerPy import prompt
from InquirerPy.base.control import Choice
from .argument_parser import Config
from .reloader import setup_reloader
from robyn.robyn import get_version
from pathlib import Path
import shutil
import subprocess


SCAFFOLD_DIR = Path(__file__).parent / "scaffold"
CURRENT_WORKING_DIR = Path.cwd()


def create_robyn_app():
questions = [
{
"type": "input",
"message": "Directory Path:",
"name": "directory",
},
{
"type": "list",
"message": "Need Docker? (Y/N)",
"choices": [
Choice("Y", name="Y"),
Choice("N", name="N"),
],
"default": Choice("N", name="N"),
"name": "docker",
},
{
"type": "list",
"message": "Please select project type (Mongo/Postgres/Sqlalchemy/Prisma): ",
"choices": [
Choice("no-db", name="No DB"),
Choice("sqlite", name="Sqlite"),
Choice("postgres", name="Postgres"),
Choice("mongo", name="MongoDB"),
Choice("sqlalchemy", name="SqlAlchemy"),
Choice("prisma", name="Prisma"),
],
"default": Choice("no-db", name="No DB"),
"name": "project_type",
},
]
result = prompt(questions=questions)
project_dir_path = Path(result["directory"]).resolve()
docker = result["docker"]
project_type = result["project_type"]

final_project_dir_path = (CURRENT_WORKING_DIR / project_dir_path).resolve()

print(f"Creating a new Robyn project '{final_project_dir_path}'...")

# Create a new directory for the project
os.makedirs(final_project_dir_path, exist_ok=True)

selected_project_template = (SCAFFOLD_DIR / Path(project_type)).resolve()
shutil.copytree(str(selected_project_template), str(final_project_dir_path), dirs_exist_ok=True)

# If docker is not needed, delete the docker file
if docker == "N":
os.remove(f"{final_project_dir_path}/Dockerfile")

print(f"New Robyn project created in '{final_project_dir_path}' ")


def docs():
print("Opening Robyn documentation... | Offline docs coming soon!")
webbrowser.open("https://robyn.tech")


def start_dev_server(file_path: Optional[str] = None):
if file_path is None:
return

directory_path = Path.cwd()
absolute_file_path = (directory_path / file_path).resolve()

if config.dev and not os.environ.get("IS_RELOADER_RUNNING", False):
setup_reloader(str(directory_path), str(absolute_file_path))
return


def start_app_normally(config: Config):
# Parsing the known and unknown arguments
known_arguments, unknown_args = config.parser.parse_known_args()

# Convert known arguments to a list of strings suitable for subprocess.run
known_args_list = [f"{key}={value}" for key, value in vars(known_arguments).items() if value is not None]

# Combine the python executable, unknown arguments, and known arguments
command = [sys.executable, *unknown_args, *known_args_list]

# Run the subprocess
subprocess.run(command, start_new_session=False)

from robyn.cli import run

if __name__ == "__main__":
config = Config()
if config.create:
create_robyn_app()

elif config.version:
print(get_version())

elif config.docs:
docs()

elif config.dev:
print("Starting dev server...")
start_dev_server(config.file_path)
else:
start_app_normally(config)
run()
119 changes: 119 additions & 0 deletions robyn/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import os
import sys
from typing import Optional
import webbrowser
from InquirerPy import prompt
from InquirerPy.base.control import Choice
from .argument_parser import Config
from .reloader import setup_reloader
from robyn.robyn import get_version
from pathlib import Path
import shutil
import subprocess


SCAFFOLD_DIR = Path(__file__).parent / "scaffold"
CURRENT_WORKING_DIR = Path.cwd()


def create_robyn_app():
questions = [
{
"type": "input",
"message": "Directory Path:",
"name": "directory",
},
{
"type": "list",
"message": "Need Docker? (Y/N)",
"choices": [
Choice("Y", name="Y"),
Choice("N", name="N"),
],
"default": Choice("N", name="N"),
"name": "docker",
},
{
"type": "list",
"message": "Please select project type (Mongo/Postgres/Sqlalchemy/Prisma): ",
"choices": [
Choice("no-db", name="No DB"),
Choice("sqlite", name="Sqlite"),
Choice("postgres", name="Postgres"),
Choice("mongo", name="MongoDB"),
Choice("sqlalchemy", name="SqlAlchemy"),
Choice("prisma", name="Prisma"),
],
"default": Choice("no-db", name="No DB"),
"name": "project_type",
},
]
result = prompt(questions=questions)
project_dir_path = Path(result["directory"]).resolve()
docker = result["docker"]
project_type = result["project_type"]

final_project_dir_path = (CURRENT_WORKING_DIR / project_dir_path).resolve()

print(f"Creating a new Robyn project '{final_project_dir_path}'...")

# Create a new directory for the project
os.makedirs(final_project_dir_path, exist_ok=True)

selected_project_template = (SCAFFOLD_DIR / Path(project_type)).resolve()
shutil.copytree(str(selected_project_template), str(final_project_dir_path), dirs_exist_ok=True)

# If docker is not needed, delete the docker file
if docker == "N":
os.remove(f"{final_project_dir_path}/Dockerfile")

print(f"New Robyn project created in '{final_project_dir_path}' ")


def docs():
print("Opening Robyn documentation... | Offline docs coming soon!")
webbrowser.open("https://robyn.tech")


def start_dev_server(config: Config, file_path: Optional[str] = None):
if file_path is None:
return

directory_path = Path.cwd()
absolute_file_path = (directory_path / file_path).resolve()

if config.dev and not os.environ.get("IS_RELOADER_RUNNING", False):
setup_reloader(str(directory_path), str(absolute_file_path))
return


def start_app_normally(config: Config):
# Parsing the known and unknown arguments
known_arguments, unknown_args = config.parser.parse_known_args()

# Convert known arguments to a list of strings suitable for subprocess.run
known_args_list = [f"{key}={value}" for key, value in vars(known_arguments).items() if value is not None]

# Combine the python executable, unknown arguments, and known arguments
command = [sys.executable, *unknown_args, *known_args_list]

# Run the subprocess
subprocess.run(command, start_new_session=False)


def run():
config = Config()
if config.create:
create_robyn_app()

elif config.version:
print(get_version())

elif config.docs:
docs()

elif config.dev:
print("Starting dev server...")
start_dev_server(config, config.file_path)
else:
start_app_normally(config)
2 changes: 1 addition & 1 deletion src/types/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Headers {

if new_value.is_err() {
let value = value.to_string();
self.headers.entry(key).or_insert_with(Vec::new).push(value);
self.headers.entry(key).or_default().push(value);
} else {
let value: Vec<String> = new_value.unwrap().iter().map(|x| x.to_string()).collect();
self.headers.entry(key).or_default().extend(value);
Expand Down

0 comments on commit 3bcd7a5

Please sign in to comment.