Skip to content

Commit d85ecc2

Browse files
committed
add git commit flag, refactor write_project_files
1 parent ceccb64 commit d85ecc2

File tree

6 files changed

+53
-30
lines changed

6 files changed

+53
-30
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ python -m pip install ./dist/lpg-*-py3-none-any.whl
2424
### Installed package
2525

2626
```sh
27-
lpg (--title_slug <problem title> | --url <problem url>) [--directory <project directory>] [--lang <language>] [--force] [--git-init]
27+
lpg (--title_slug <problem title> | --url <problem url>) [--directory <project directory>] [--lang <language>] [--force] [--git-init] [--git-commit] [--git-commit-message <message>]
2828
```
2929

3030
### Local package
3131

3232
```sh
33-
python -m lpg (--title_slug <problem title> | --url <problem url>) [--directory <project directory>] [--lang <language>] [--force] [--git-init]
33+
python -m lpg (--title_slug <problem title> | --url <problem url>) [--directory <project directory>] [--lang <language>] [--force] [--git-init] [--git-commit] [--git-commit-message <message>]
3434
```
3535

3636
### Explanation

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
66

77
[project]
88
name = "leetcode-project-generator"
9-
version = "1.2.0"
9+
version = "1.2.1"
1010
authors = [
1111
{ name = "Konrad Guzek", email = "[email protected]" },
1212
]

src/lpg/interfaces/lang/base.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def function_signature_pattern(self) -> Pattern[str]:
1717
"""Returns the function signature regular expression pattern."""
1818

1919
@abstractmethod
20-
def write_project_files(self, template: str) -> None:
21-
"""Writes the project files."""
20+
def prepare_project_files(self, template: str) -> dict[str, str]:
21+
"""Generates a dictionary of filenames to file contents."""
2222

2323
def create_project(self, template: str) -> None:
2424
"""Creates the project template."""
@@ -29,4 +29,6 @@ def create_project(self, template: str) -> None:
2929
)
3030
self.groups = self.match.groupdict()
3131

32-
self.write_project_files(template)
32+
for filename, content in self.prepare_project_files(template).items():
33+
with open(filename, "w", encoding="utf-8") as file:
34+
file.write(content)

src/lpg/interfaces/lang/c.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,7 @@ class CLanguageInterface(BaseLanguageInterface):
2828

2929
function_signature_pattern = FUNCTION_SIGNATURE_PATTERN
3030

31-
def write_project_files(self, template: str):
32-
"""Creates the project template for C."""
33-
34-
with open("solution.c", "w", encoding="utf-8") as file:
35-
file.write(template + "\n")
36-
37-
with open("solution.h", "w", encoding="utf-8") as file:
38-
file.write(HEADER_FILE_TEMPLATE.format(**self.groups))
31+
def prepare_project_files(self, template: str):
3932

4033
params = self.groups["params"].split(", ")
4134
self.groups["param_declarations"] = self.groups["params"].replace(
@@ -44,5 +37,8 @@ def write_project_files(self, template: str):
4437
self.groups["params_call"] = ", ".join(param.split()[-1] for param in params)
4538
formatted = TEST_FILE_TEMPLATE.format(**self.groups)
4639

47-
with open("test.c", "w", encoding="utf-8") as file:
48-
file.write(formatted)
40+
return {
41+
"solution.c": f"{template}\n",
42+
"solution.h": HEADER_FILE_TEMPLATE.format(**self.groups),
43+
"test.c": formatted,
44+
}

src/lpg/interfaces/lang/python3.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ class Python3LanguageInterface(BaseLanguageInterface):
2727

2828
function_signature_pattern = FUNCTION_SIGNATURE_PATTERN
2929

30-
def write_project_files(self, template: str):
31-
"""Creates the project template for Python 3."""
32-
33-
with open("solution.py", "w", encoding="utf-8") as file:
34-
file.write(f"{TYPING_IMPORT_TEMPLATE}\n{template}pass\n")
35-
30+
def prepare_project_files(self, template: str):
3631
params = (
3732
[
3833
param
@@ -49,7 +44,7 @@ def write_project_files(self, template: str):
4944
param.split("=")[0].split(":")[0].strip() for param in params
5045
)
5146

52-
formatted = TEST_FILE_TEMPLATE.format(**self.groups)
53-
54-
with open("test.py", "w", encoding="utf-8") as file:
55-
file.write(f"{TYPING_IMPORT_TEMPLATE}{formatted}")
47+
return {
48+
"solution.py": f"{TYPING_IMPORT_TEMPLATE}\n{template}pass\n",
49+
"test.py": f"{TYPING_IMPORT_TEMPLATE}{TEST_FILE_TEMPLATE.format(**self.groups)}",
50+
}

src/lpg/lpg.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
DEFAULT_PROJECT_LANGUAGE = "c"
1616
DEFAULT_PROJECT_DIRECTORY = R"~/Documents/Coding/{language_name}/leetcode/"
17+
DEFAULT_COMMIT_MESSAGE = "initialise project {problem_name}"
1718

1819

1920
@click.command()
@@ -32,39 +33,61 @@
3233
"-l",
3334
help="The language of the code to generate.",
3435
default=DEFAULT_PROJECT_LANGUAGE,
36+
show_default=True,
3537
)
3638
@click.option(
3739
"--directory",
3840
"-d",
3941
help="The directory for the project to be created in.",
4042
default=DEFAULT_PROJECT_DIRECTORY,
43+
show_default=True,
4144
)
4245
@click.option(
4346
"--force",
4447
"-f",
4548
help="Force-creates the project directory even if it already exists.",
4649
default=False,
4750
is_flag=True,
48-
show_default=True,
4951
)
5052
@click.option(
5153
"--git-init",
5254
"-g",
5355
help="Initialises a git repository in the project directory.",
5456
default=False,
5557
is_flag=True,
56-
show_default=False,
58+
)
59+
@click.option(
60+
"--git-commit",
61+
"-c",
62+
help=(
63+
"Creates an initial commit in the project directory. "
64+
"Can be used without --git-init if you have a parent repository."
65+
),
66+
default=False,
67+
is_flag=True,
68+
)
69+
@click.option(
70+
"--git-commit-message",
71+
"-m",
72+
help=(
73+
"Creates an initial commit in the project directory. "
74+
"Can be used without --git-init if you have a parent repository already."
75+
),
76+
default=DEFAULT_COMMIT_MESSAGE,
77+
show_default=True,
5778
)
5879
@click.help_option("--help", "-h")
5980
@click.argument("url_or_slug", required=False)
60-
def lpg(
81+
def lpg( # pylint: disable=too-many-arguments, too-many-positional-arguments
6182
url_or_slug: str | None = None,
6283
title_slug: str | None = None,
6384
url: str | None = None,
6485
lang: str = DEFAULT_PROJECT_LANGUAGE,
6586
directory: str = DEFAULT_PROJECT_DIRECTORY,
6687
force: bool = True,
6788
git_init: bool = False,
89+
git_commit: bool = False,
90+
git_commit_message: str = DEFAULT_COMMIT_MESSAGE,
6891
):
6992
"""Creates a LeetCode skeleton project from the given problem URL or
7093
title slug in the specified programming language."""
@@ -78,7 +101,14 @@ def lpg(
78101
title_slug, template_data = get_leetcode_template(lang, title_slug, url)
79102
path = create_project(title_slug, directory, template_data, force)
80103
if git_init:
81-
subprocess.run("git init", check=True)
104+
subprocess.run(["git", "init"], check=True)
105+
if git_commit:
106+
subprocess.run(["git", "add", "."], check=True)
107+
subprocess.run(
108+
["git", "commit", "-m", git_commit_message.format(problem_name=title_slug)],
109+
check=False,
110+
)
111+
82112
click.echo(f"Successfully created project at {path}!")
83113

84114

0 commit comments

Comments
 (0)