1+ # type: ignore
12import shutil
3+ import subprocess
24
35import toml
4- from colorama import Fore
5- from colorama import init as init_colorama
66from invoke import Context , task
7+ from rich .console import Console
78
8- import monkey_patch_invoke as _ # noqa
9+ import monkey_patch_invoke as _ # noqa: F401
10+
11+ console = Console ()
912
1013
1114def get_pep8_compliant_name (project_name : str ) -> str :
@@ -19,6 +22,14 @@ def get_project_path():
1922 return project_name
2023
2124
25+ def execute_command (command : str ) -> dict [str , str ]:
26+ with subprocess .Popen (command , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE ) as process :
27+ stdout , stderr = process .communicate ()
28+ if process .returncode != 0 :
29+ raise Exception (stderr .decode ())
30+ return {'command' : command , 'result' : stdout .decode ()}
31+
32+
2233@task
2334def run (context : Context ):
2435 context .run (f'python { get_project_path ()} /main.py' , pty = True )
@@ -30,20 +41,23 @@ def test(context: Context):
3041
3142
3243@task
33- def format_code (context : Context ) -> None :
34- init_colorama ()
35-
36- print (f'{ Fore .MAGENTA } ==========Remove unused imports with `autoflake`=========={ Fore .RESET } ' )
37- context .run (f'pautoflake { get_project_path ()} ' , pty = True )
38-
39- print (f'{ Fore .MAGENTA } ==========Sort imports with `isort`=========={ Fore .RESET } ' )
40- context .run (f'isort { get_project_path ()} ' , pty = True )
44+ def format_code (context : Context , verbose : bool = False ) -> None :
45+ commands = [
46+ f'pautoflake { get_project_path ()} ' ,
47+ f'ruff --fix { get_project_path ()} ' ,
48+ f'yapf --in-place --recursive --parallel { get_project_path ()} ' ,
49+ ]
50+ results : list [dict [str , str ]] = []
51+ with console .status ('[bold green] Formatting code...' ):
52+ for command in commands :
53+ results .append (execute_command (command ))
4154
42- print (f'{ Fore .MAGENTA } ==========Unifying quotes with `unify`=========={ Fore .RESET } ' )
43- context .run (f'unify --in-place -r { get_project_path ()} ' )
55+ if verbose :
56+ for result in results :
57+ console .print (f'$ { result ["command" ]} ' )
58+ console .print (result ['result' ])
4459
45- print (f'{ Fore .MAGENTA } ==========Format code with `yapf`=========={ Fore .RESET } ' )
46- context .run (f'yapf --in-place --recursive --parallel { get_project_path ()} ' , pty = True )
60+ console .print ('[bold green]Success[/bold green]' )
4761
4862
4963@task
@@ -54,32 +68,18 @@ def check(context: Context):
5468
5569@task
5670def check_code_style (context : Context ):
57- init_colorama ()
71+ commands = [
72+ f'pautoflake { get_project_path ()} --check' ,
73+ f'ruff { get_project_path ()} ' ,
74+ f'yapf --diff --recursive --parallel { get_project_path ()} ' ,
75+ ]
5876
59- print (f'{ Fore .MAGENTA } ==========Check Code Styles with `autoflake`=========={ Fore .GREEN } ' )
60- context .run (f'pautoflake { get_project_path ()} --check' , pty = True )
61-
62- print (f'{ Fore .MAGENTA } ==========Check Code Styles with `isort`=========={ Fore .GREEN } ' )
63- context .run (f'isort { get_project_path ()} --check --diff' , pty = True )
64- print (f'{ Fore .GREEN } isort: Success{ Fore .RESET } ' )
65-
66- print (f'{ Fore .MAGENTA } ==========Check Code Styles with `pylint`=========={ Fore .GREEN } ' )
67- context .run (f'pylint { get_project_path ()} ' , pty = True )
68-
69- print (f'{ Fore .MAGENTA } ==========Check Code Styles with `yapf`=========={ Fore .RESET } ' )
70- context .run (f'yapf --diff --recursive --parallel { get_project_path ()} ' , pty = True )
71- print (f'{ Fore .GREEN } yapf: Success{ Fore .RESET } ' )
77+ for command in commands :
78+ context .run (command , pty = True )
7279
7380
7481@task
7582def check_types (context : Context ):
76- """Check types with `pyright` and `mypy`."""
77- init_colorama ()
78-
79- print (f'{ Fore .CYAN } ==========Check typings with `pyright`=========={ Fore .RESET } ' )
80- context .run (f'pyright { get_project_path ()} ' , pty = True )
81-
82- print (f'\n { Fore .CYAN } ==========Check typings with `mypy`=========={ Fore .RESET } ' )
8383 context .run (f'mypy { get_project_path ()} ' , pty = True )
8484
8585
0 commit comments