Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/codegen/cli/commands/init/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
from codegen.cli.commands.init.render import get_success_message
from codegen.cli.git.url import get_git_organization_and_repo
from codegen.cli.rich.codeblocks import format_command
from codegen.cli.utils.constants import ProgrammingLanguage
from codegen.cli.workspace.initialize_workspace import initialize_codegen


@click.command(name="init")
@click.option("--repo-name", type=str, help="The name of the repository")
@click.option("--organization-name", type=str, help="The name of the organization")
@click.option("--fetch-docs", is_flag=True, help="Fetch docs and examples (requires auth)")
def init_command(repo_name: str | None = None, organization_name: str | None = None, fetch_docs: bool = False):
@click.option("--language", type=click.Choice(["python", "typescript"], case_sensitive=False), help="Override automatic language detection")
def init_command(repo_name: str | None = None, organization_name: str | None = None, fetch_docs: bool = False, language: str | None = None):
"""Initialize or update the Codegen folder."""
# Print a message if not in a git repo
try:
Expand All @@ -31,6 +33,11 @@ def init_command(repo_name: str | None = None, organization_name: str | None = N
rich.print(format_command("codegen init"))
sys.exit(1)

# Convert language string to enum if provided
programming_language = None
if language:
programming_language = ProgrammingLanguage[language.upper()]

# Only create session if we need to fetch docs
session = CodegenSession() if fetch_docs else None
codegen_dir = Path.cwd() / CODEGEN_DIR if not session else session.codegen_dir
Expand All @@ -46,11 +53,13 @@ def init_command(repo_name: str | None = None, organization_name: str | None = N
cwd_org, cwd_repo = get_git_organization_and_repo(session.git_repo)
session.config.organization_name = session.config.organization_name or cwd_org
session.config.repo_name = session.config.repo_name or cwd_repo
if programming_language:
session.config.programming_language = programming_language
session.write_config()

action = "Updating" if is_update else "Initializing"
rich.print("") # Add a newline before the spinner
codegen_dir, docs_dir, examples_dir = initialize_codegen(action, session=session, fetch_docs=fetch_docs)
rich.print("")
codegen_dir, docs_dir, examples_dir = initialize_codegen(action, session=session, fetch_docs=fetch_docs, programming_language=programming_language)

# Print success message
rich.print(f"✅ {action} complete\n")
Expand Down
13 changes: 9 additions & 4 deletions src/codegen/cli/workspace/initialize_workspace.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import shutil
from contextlib import nullcontext
from pathlib import Path
from typing import Optional

import requests

Check failure on line 6 in src/codegen/cli/workspace/initialize_workspace.py

View workflow job for this annotation

GitHub Actions / mypy

error: Library stubs not installed for "requests" [import-untyped]
import rich
import toml

Check failure on line 8 in src/codegen/cli/workspace/initialize_workspace.py

View workflow job for this annotation

GitHub Actions / mypy

error: Library stubs not installed for "toml" [import-untyped]
from rich.status import Status

from codegen.cli.api.client import RestAPI
Expand All @@ -13,23 +14,23 @@
from codegen.cli.git.repo import get_git_repo
from codegen.cli.git.url import get_git_organization_and_repo
from codegen.cli.rich.spinners import create_spinner
from codegen.cli.utils.constants import ProgrammingLanguage
from codegen.cli.utils.notebooks import create_notebook
from codegen.cli.workspace.docs_workspace import populate_api_docs
from codegen.cli.workspace.examples_workspace import populate_examples
from codegen.cli.workspace.venv_manager import VenvManager


def initialize_codegen(
status: Status | str = "Initializing",
session: CodegenSession | None = None,
fetch_docs: bool = False,
status: Status | str = "Initializing", session: CodegenSession | None = None, fetch_docs: bool = False, programming_language: Optional[ProgrammingLanguage] = None
) -> tuple[Path, Path, Path]:
"""Initialize or update the codegen directory structure and content.

Args:
status: Either a Status object to update, or a string action being performed ("Initializing" or "Updating")
session: Optional CodegenSession for fetching docs and examples
fetch_docs: Whether to fetch docs and examples (requires auth)
programming_language: Optional override for the programming language

Returns:
Tuple of (codegen_folder, docs_folder, examples_folder)
Expand Down Expand Up @@ -111,7 +112,11 @@
populate_examples(session, EXAMPLES_FOLDER, response.examples, status_obj)

# Set programming language
session.config.programming_language = str(response.language)
if programming_language:
session.config.programming_language = programming_language
else:
session.config.programming_language = str(response.language)

session.write_config()

return CODEGEN_FOLDER, DOCS_FOLDER, EXAMPLES_FOLDER
Expand Down
Loading