Skip to content

Commit

Permalink
Specify language on 'codegen init' in CLI (#289)
Browse files Browse the repository at this point in the history
Co-authored-by: codegen-team <[email protected]>
  • Loading branch information
vishalshenoy and codegen-team authored Feb 5, 2025
1 parent 86fda48 commit 0c0d6c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
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,6 +1,7 @@
import shutil
from contextlib import nullcontext
from pathlib import Path
from typing import Optional

import requests
import rich
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 @@ def initialize_codegen(
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

0 comments on commit 0c0d6c9

Please sign in to comment.