Skip to content

Commit f12e247

Browse files
committed
Added path related variables
1 parent 0ee6624 commit f12e247

File tree

4 files changed

+67
-23
lines changed

4 files changed

+67
-23
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ inquirerpy = "^0.3.4"
4040
yaspin = "^3.3.0"
4141
click = "^8.3.0"
4242
psutil = "^7.1.3"
43+
importlib-resources = "^6.5.2"
4344

4445
[tool.poetry.group.dev.dependencies]
4546
black = "^25.9.0"

src/cli/builder.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from InquirerPy import inquirer # type: ignore
88
from InquirerPy.validator import EmptyInputValidator # type: ignore
99
from click import Command, Option
10+
from importlib_resources import files # type: ignore
1011
from yaspin import yaspin # type: ignore
1112

1213
from ..core.copy_files import copy_files # type: ignore
@@ -16,6 +17,8 @@
1617
from .custom_group import CustomGroup
1718
from .menu import Menus
1819

20+
dicts = dict[str, Any]
21+
1922

2023
class Builder(CustomGroup):
2124

@@ -31,9 +34,9 @@ def create(self) -> Command:
3134
def callback(network: bool = False) -> None:
3235
clear(0)
3336

34-
services: set[dict[str, Any]] = set([])
37+
services: set[dicts] = set([])
3538
networks: set[str] = set([])
36-
envs: set[dict[str, Any]] = set([])
39+
envs: set[dicts] = set([])
3740

3841
if not network:
3942
menu = Menus()
@@ -96,18 +99,18 @@ def callback(
9699
) -> None:
97100
clear(0)
98101

99-
path: Path = Path()
102+
path: Path = self.cwd.joinpath("data.json")
100103

101104
if not path.exists():
102105
print("Missing JSON file for services. Use 'create' first.")
103106
return
104107

105-
data: dict[str, Any] = read_json(path) or {}
106-
compose: dict[str, Any] = data.get("compose", {}) or {}
108+
data: dicts = read_json(path) or {}
109+
compose: dicts = data.get("compose", {}) or {}
107110

108-
services: set[dict[str, Any]] = set(compose.get("services", []))
111+
services: set[dicts] = set(compose.get("services", []))
109112
networks: set[str] = set(compose.get("networks", []))
110-
envs: set[dict[str, Any]] = set(data.get("envs", []))
113+
envs: set[dicts] = set(data.get("envs", []))
111114

112115
if networks:
113116
pass
@@ -126,21 +129,21 @@ def callback(
126129
)
127130

128131
def build(self) -> Command:
129-
help = "Build the files for the containerization in case create failed."
132+
help = "Build the files for the containerization."
130133
options: list[Option] = []
131134

132135
def callback(
133136
service: str | None = None, add: bool = False, remove: bool = False
134137
) -> None:
135138
clear(0)
136139

137-
path: Path = Path()
140+
path: Path = self.cwd.joinpath("data.json")
138141

139142
if not path.exists():
140143
print("Missing JSON file for services. Use 'create' first.")
141144
return
142145

143-
data: dict[str, Any] = read_json(Path()) or {}
146+
data: dicts = read_json(path) or {}
144147

145148
if not data:
146149
print("JSON file is empty. Use 'create' first.")
@@ -159,7 +162,7 @@ def callback(
159162

160163
def __get_data(
161164
self, menu: Menus, get_service: bool = True, get_env: bool = True
162-
) -> tuple[dict[str, Any], dict[str, Any]]:
165+
) -> tuple[dicts, dicts]:
163166
clear(0.5)
164167

165168
name = self.__get_name(message="Enter the name of the service: ")
@@ -187,14 +190,26 @@ def __get_name(self, message: str) -> str:
187190
return name
188191

189192
@yaspin(text="Creating files...", color="cyan")
190-
def __save_files(self, data: dict[str, Any], build: bool = False) -> None:
193+
def __save_files(self, data: dicts, build: bool = False) -> None:
194+
tmps_path = files("minecraft-docker-cli.assets.templates")
195+
composer_template = tmps_path.joinpath("docker-compose.yml.j2")
196+
env_template = tmps_path.joinpath(".env.j2")
197+
191198
if not build:
192-
write_json(Path(), data)
199+
write_json(self.cwd.joinpath("data.json"), data)
193200

194-
composer: dict[str, Any] = data.get("composer") or {}
195-
template_to_file(Path(), composer, Path())
201+
composer: dicts = data.get("composer") or {}
202+
template_to_file(
203+
composer_template, composer, self.cwd.joinpath("docker-compose.yml")
204+
)
196205

197-
envs: list[dict[str, Any]] = data.get("envs") or []
206+
services: list[dicts] = composer.get("services", []) or []
207+
names: list[str] = [service.get("name") for service in services] # type: ignore
208+
copy_files(self.cwd, names)
209+
210+
envs: list[dicts] = data.get("envs") or []
198211
for env in envs:
199212
relative_path = f"servers/{env.get("CONTAINER_NAME")}/.env" # type: ignore
200-
template_to_file(Path(), env, Path())
213+
template_to_file(
214+
env_template, env, self.cwd.joinpath(relative_path)
215+
)

src/cli/custom_group.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from __future__ import annotations
22

33
import inspect
4+
from pathlib import Path
45

56
from click import Command, Group
67

78

89
class CustomGroup(Group):
910

11+
cwd: Path = Path.cwd()
12+
1013
def __init__(self) -> None:
1114
super().__init__()
1215
self.__register_commands()

src/core/copy_files.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
from __future__ import annotations
22

33
from pathlib import Path
4-
from shutil import copyfile
4+
from typing import Iterable
55

6+
from importlib_resources import files # type: ignore
67

7-
def copy_files(path: Path, services: list[str]) -> None:
8+
9+
def copy_files(path: Path, services: list[str] | Iterable[str]) -> None:
10+
# Resolve resource traversables
11+
docker_pkg = files("minecraft-docker-cli.assets.docker")
12+
dockerfile_res = docker_pkg.joinpath("Dockerfile")
13+
dockerignore_res = docker_pkg.joinpath(".dockerignore")
14+
runsh_res = files("minecraft-docker-cli.assets.scripts").joinpath("run.sh")
15+
readme_res = files("minecraft-docker-cli.assets").joinpath("README.md")
16+
17+
# Ensure base path exists
18+
if not path.exists():
19+
raise ValueError("Path doesnt exist")
20+
21+
# Read bytes from resources once
22+
dockerfile_bytes = dockerfile_res.read_bytes()
23+
dockerignore_bytes = dockerignore_res.read_bytes()
24+
runsh_bytes = runsh_res.read_bytes()
25+
readme_bytes = readme_res.read_bytes()
26+
27+
# Write files for each service
828
for service in services:
9-
copyfile(Path(), path.joinpath(f"servers/{service}")) # Dockerfile
10-
copyfile(Path(), path.joinpath(f"servers/{service}")) # .dockerignore
11-
copyfile(Path(), path.joinpath(f"servers/{service}")) # run.sh
12-
copyfile(Path(), path) # README.md
29+
dest_dir = path.joinpath("servers", service)
30+
dest_dir.mkdir(parents=True, exist_ok=True)
31+
32+
(dest_dir / "Dockerfile").write_bytes(dockerfile_bytes)
33+
(dest_dir / ".dockerignore").write_bytes(dockerignore_bytes)
34+
(dest_dir / "run.sh").write_bytes(runsh_bytes)
35+
36+
# Write top-level README into the given path
37+
(path / "README.md").write_bytes(readme_bytes)

0 commit comments

Comments
 (0)