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 )
0 commit comments