1
+ # type: ignore
1
2
import shutil
3
+ import subprocess
2
4
3
5
import toml
4
- from colorama import Fore
5
- from colorama import init as init_colorama
6
6
from invoke import Context , task
7
+ from rich .console import Console
7
8
8
- import monkey_patch_invoke as _ # noqa
9
+ import monkey_patch_invoke as _ # noqa: F401
10
+
11
+ console = Console ()
9
12
10
13
11
14
def get_pep8_compliant_name (project_name : str ) -> str :
@@ -19,6 +22,14 @@ def get_project_path():
19
22
return project_name
20
23
21
24
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
+
22
33
@task
23
34
def run (context : Context ):
24
35
context .run (f'python { get_project_path ()} /main.py' , pty = True )
@@ -30,20 +41,23 @@ def test(context: Context):
30
41
31
42
32
43
@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 ))
41
54
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' ])
44
59
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]' )
47
61
48
62
49
63
@task
@@ -54,32 +68,18 @@ def check(context: Context):
54
68
55
69
@task
56
70
def 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
+ ]
58
76
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 )
72
79
73
80
74
81
@task
75
82
def 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 } ' )
83
83
context .run (f'mypy { get_project_path ()} ' , pty = True )
84
84
85
85
0 commit comments