Skip to content

Commit b1174a8

Browse files
committed
Clean up existing commands
1 parent 3f260ac commit b1174a8

File tree

7 files changed

+143
-123
lines changed

7 files changed

+143
-123
lines changed

app/commands/download.py

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -39,80 +39,7 @@
3939
)
4040

4141

42-
def setup_exercise_folder(download_time: datetime, config: ExerciseConfig) -> None:
43-
exercise = config.exercise_name
44-
formatted_exercise = config.formatted_exercise_name
45-
46-
config.downloaded_at = download_time.timestamp()
47-
48-
with open(".gitmastery-exercise.json", "w") as gitmastery_exercise_file:
49-
gitmastery_exercise_file.write(config.to_json())
50-
51-
if config.exercise_repo.repo_type == "local":
52-
info("Creating custom exercise folder")
53-
os.makedirs(config.exercise_repo.repo_name, exist_ok=True)
54-
elif config.exercise_repo.repo_type == "remote":
55-
# We have to assume that Github is checked
56-
info("Retrieving exercise from Github")
57-
58-
username = get_username()
59-
exercise_repo = f"git-mastery/{config.exercise_repo.repo_title}"
60-
if config.exercise_repo.create_fork:
61-
info("Checking if you already have a fork")
62-
fork_name = config.exercise_fork_name(username)
63-
if has_fork(fork_name):
64-
info("You already have a fork, deleting it")
65-
delete_repo(fork_name)
66-
info("Creating fork of exercise repository")
67-
fork(exercise_repo, fork_name)
68-
info("Creating clone of your fork")
69-
clone_with_custom_name(
70-
f"{username}/{fork_name}", config.exercise_repo.repo_name
71-
)
72-
else:
73-
info("Creating clone of repository")
74-
clone_with_custom_name(exercise_repo, config.exercise_repo.repo_name)
75-
76-
os.chdir(config.exercise_repo.repo_name)
77-
download_resources: Optional[Dict[str, str]] = get_variable_from_url(
78-
formatted_exercise, "download.py", "__resources__", {}
79-
)
80-
if download_resources and len(download_resources) > 0:
81-
info("Downloading resources for the exercise...")
82-
83-
if download_resources:
84-
for resource, path in download_resources.items():
85-
os.makedirs(Path(path).parent, exist_ok=True)
86-
is_binary = Path(path).suffix in [".png", ".jpg", ".jpeg", ".gif"]
87-
# Download and load all of these resources
88-
download_file(
89-
get_gitmastery_file_path(f"{formatted_exercise}/res/{resource}"),
90-
path,
91-
is_binary,
92-
)
93-
94-
if config.exercise_repo.init:
95-
init()
96-
initial_commit_message = "Set initial state"
97-
if download_resources:
98-
add_all()
99-
commit(initial_commit_message)
100-
else:
101-
empty_commit(initial_commit_message)
102-
103-
info("Executing download setup")
104-
execute_py_file_function_from_url(
105-
formatted_exercise,
106-
"download.py",
107-
"setup",
108-
{"verbose": get_verbose()},
109-
)
110-
111-
success(f"Completed setting up {click.style(exercise, bold=True, italic=True)}")
112-
info("Start working on it:")
113-
114-
115-
def download_exercise(
42+
def _download_exercise(
11643
exercise: str, formatted_exercise: str, download_time: datetime
11744
) -> None:
11845
info(f"Checking if {exercise} is available")
@@ -197,7 +124,7 @@ def download_exercise(
197124
gitmastery_exercise_file.write(config.to_json())
198125

199126

200-
def download_hands_on(hands_on: str, formatted_hands_on: str) -> None:
127+
def _download_hands_on(hands_on: str, formatted_hands_on: str) -> None:
201128
info(f"Checking if {hands_on} is available")
202129

203130
hands_on_without_prefix = (
@@ -263,6 +190,79 @@ def download_hands_on(hands_on: str, formatted_hands_on: str) -> None:
263190
success(f"Completed setting up {click.style(hands_on, bold=True, italic=True)}")
264191

265192

193+
def setup_exercise_folder(download_time: datetime, config: ExerciseConfig) -> None:
194+
exercise = config.exercise_name
195+
formatted_exercise = config.formatted_exercise_name
196+
197+
config.downloaded_at = download_time.timestamp()
198+
199+
with open(".gitmastery-exercise.json", "w") as gitmastery_exercise_file:
200+
gitmastery_exercise_file.write(config.to_json())
201+
202+
if config.exercise_repo.repo_type == "local":
203+
info("Creating custom exercise folder")
204+
os.makedirs(config.exercise_repo.repo_name, exist_ok=True)
205+
elif config.exercise_repo.repo_type == "remote":
206+
# We have to assume that Github is checked
207+
info("Retrieving exercise from Github")
208+
209+
username = get_username()
210+
exercise_repo = f"git-mastery/{config.exercise_repo.repo_title}"
211+
if config.exercise_repo.create_fork:
212+
info("Checking if you already have a fork")
213+
fork_name = config.exercise_fork_name(username)
214+
if has_fork(fork_name):
215+
info("You already have a fork, deleting it")
216+
delete_repo(fork_name)
217+
info("Creating fork of exercise repository")
218+
fork(exercise_repo, fork_name)
219+
info("Creating clone of your fork")
220+
clone_with_custom_name(
221+
f"{username}/{fork_name}", config.exercise_repo.repo_name
222+
)
223+
else:
224+
info("Creating clone of repository")
225+
clone_with_custom_name(exercise_repo, config.exercise_repo.repo_name)
226+
227+
os.chdir(config.exercise_repo.repo_name)
228+
download_resources: Optional[Dict[str, str]] = get_variable_from_url(
229+
formatted_exercise, "download.py", "__resources__", {}
230+
)
231+
if download_resources and len(download_resources) > 0:
232+
info("Downloading resources for the exercise...")
233+
234+
if download_resources:
235+
for resource, path in download_resources.items():
236+
os.makedirs(Path(path).parent, exist_ok=True)
237+
is_binary = Path(path).suffix in [".png", ".jpg", ".jpeg", ".gif"]
238+
# Download and load all of these resources
239+
download_file(
240+
get_gitmastery_file_path(f"{formatted_exercise}/res/{resource}"),
241+
path,
242+
is_binary,
243+
)
244+
245+
if config.exercise_repo.init:
246+
init()
247+
initial_commit_message = "Set initial state"
248+
if download_resources:
249+
add_all()
250+
commit(initial_commit_message)
251+
else:
252+
empty_commit(initial_commit_message)
253+
254+
info("Executing download setup")
255+
execute_py_file_function_from_url(
256+
formatted_exercise,
257+
"download.py",
258+
"setup",
259+
{"verbose": get_verbose()},
260+
)
261+
262+
success(f"Completed setting up {click.style(exercise, bold=True, italic=True)}")
263+
info("Start working on it:")
264+
265+
266266
# TODO: Think about streamlining the config location
267267
# TODO: Maybe store the random "keys" in config
268268
@click.command()
@@ -276,6 +276,6 @@ def download(exercise: str) -> None:
276276

277277
must_be_in_gitmastery_root()
278278
if is_hands_on:
279-
download_hands_on(exercise, formatted_exercise)
279+
_download_hands_on(exercise, formatted_exercise)
280280
else:
281-
download_exercise(exercise, formatted_exercise, download_time)
281+
_download_exercise(exercise, formatted_exercise, download_time)

app/commands/progress/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
PROGRESS_REPOSITORY_NAME = "git-mastery/progress"
22
STUDENT_PROGRESS_FORK_NAME = "{username}-gitmastery-progress"
3-
LOCAL_FOLDER_NAME = "progress"
3+
PROGRESS_LOCAL_FOLDER_NAME = "progress"

app/commands/progress/reset.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
@click.command()
2727
def reset() -> None:
28-
# TODO: This command should work even if the user does not have syncing on - only check if Github is set up when it is necessary
2928
"""
3029
Resets the progress of the current exercise.
3130
"""
@@ -36,14 +35,18 @@ def reset() -> None:
3635
gitmastery_config = is_in_gitmastery_root()
3736
exercise_config = must_be_in_exercise_root()
3837

38+
is_remote_type = exercise_config.exercise_repo.repo_type == "remote"
39+
has_remote_progress = gitmastery_config.progress_remote
40+
3941
invoke_command(git)
40-
invoke_command(github)
42+
if has_remote_progress or is_remote_type:
43+
invoke_command(github)
4144

4245
exercise_name = exercise_config.exercise_name
4346

4447
os.chdir(exercise_config.path)
4548
info("Resetting the exercise folder")
46-
if exercise_config.exercise_repo.create_fork:
49+
if is_remote_type and exercise_config.exercise_repo.create_fork:
4750
# Remove the fork first
4851
exercise_fork_name = (
4952
f"{username}-gitmastery-{exercise_config.exercise_repo.repo_title}"
@@ -91,8 +94,7 @@ def reset() -> None:
9194
with open("progress.json", "w") as progress_file:
9295
progress_file.write(json.dumps(clean_progress, indent=2))
9396

94-
progress_remote = gitmastery_config.progress_remote
95-
if progress_remote:
97+
if has_remote_progress:
9698
info("Updating your remote progress as well")
9799
add_all()
98100
commit(f"Reset progress for {exercise_name}")

app/commands/progress/show.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def show() -> None:
4949
)
5050

5151
if config.progress_remote:
52-
invoke_command(github)
5352
username = get_username()
5453
dashboard_url = (
5554
f"https://git-mastery.github.io/progress-dashboard/#/dashboard/{username}"

app/commands/progress/sync/off.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from app.commands.check.git import git
88
from app.commands.check.github import github
99
from app.commands.progress.constants import (
10-
LOCAL_FOLDER_NAME,
10+
PROGRESS_LOCAL_FOLDER_NAME,
1111
STUDENT_PROGRESS_FORK_NAME,
1212
)
1313
from app.utils.cli import rmtree
@@ -42,11 +42,11 @@ def off() -> None:
4242
config_file.write(json.dumps(config))
4343

4444
local_progress = []
45-
local_progress_filepath = os.path.join(LOCAL_FOLDER_NAME, "progress.json")
45+
local_progress_filepath = os.path.join(PROGRESS_LOCAL_FOLDER_NAME, "progress.json")
4646
with open(local_progress_filepath, "r") as file:
4747
local_progress = json.load(file)
4848

49-
rmtree(LOCAL_FOLDER_NAME)
49+
rmtree(PROGRESS_LOCAL_FOLDER_NAME)
5050

5151
# Re-create just the progress folder
5252
with open(local_progress_filepath, "a") as progress_file:

0 commit comments

Comments
 (0)