|
2 | 2 |
|
3 | 3 | import os |
4 | 4 | import sys |
| 5 | +from typing import List, Tuple |
| 6 | + |
| 7 | +from tabulate import tabulate |
5 | 8 |
|
6 | 9 | from ask_lang import cfg |
7 | 10 | from ask_lang.transpiler import transpiler |
8 | | -from ask_lang.utilities import utils |
| 11 | +from ask_lang.utilities import files, serve_run, printing, askfile |
| 12 | + |
| 13 | + |
| 14 | +def parse_sys_args(sys_args: List[str]) -> Tuple[str, bool]: |
| 15 | + flags = ['-d', '--dev', '-xd', '--extra-dev', '-v', '--version', '-h', '--help'] |
| 16 | + |
| 17 | + file_name = '' |
| 18 | + no_valid_flags = True |
| 19 | + |
| 20 | + for param in sys_args[1:]: |
| 21 | + if param in flags: |
| 22 | + no_valid_flags = False |
| 23 | + |
| 24 | + if param in ['-d', '--dev']: |
| 25 | + cfg.is_dev = True |
| 26 | + if param in ['-xd', '--extra-d']: |
| 27 | + cfg.is_extra_dev = True |
| 28 | + printing.style_print('Extra Dev Mode Activated!', 'red', ['bold']) |
| 29 | + elif param in ['-v', '--version']: |
| 30 | + printing.style_print('- Version:', color='blue', end=' ') |
| 31 | + print(cfg.project_information["version"]) |
| 32 | + elif param in ['-h', '--help']: |
| 33 | + print('Usage: ask_lang [OPTIONS] [FILE]...', end='\n\n') |
| 34 | + print(tabulate( |
| 35 | + [ |
| 36 | + ['-h', '--help', 'Show this message.'], |
| 37 | + ['-v', '--version', 'Show version information.'], |
| 38 | + ['-d', '--d', 'Turn on developer/debug mode.'], |
| 39 | + ], |
| 40 | + headers=['Option', 'Long Format', 'Description'] |
| 41 | + )) |
| 42 | + print() |
| 43 | + print('Other configurations can be added to a file called `Askfile.toml`.') |
| 44 | + print('Go to: https://docs.ask-lang.org for more information', end='\n\n') |
| 45 | + else: |
| 46 | + file_name = param |
| 47 | + |
| 48 | + if cfg.is_dev and file_name == '': |
| 49 | + no_valid_flags = True |
| 50 | + |
| 51 | + return file_name, no_valid_flags |
| 52 | + |
| 53 | + |
| 54 | +def repl(first_time: bool = False): |
| 55 | + cfg.is_repl = True |
| 56 | + |
| 57 | + if first_time: |
| 58 | + printing.style_print('Type "q" to quit.', color='gray') |
| 59 | + |
| 60 | + line = input('Ask >>> ') |
| 61 | + |
| 62 | + # Quit/Exit |
| 63 | + if line == 'q': |
| 64 | + files.maybe_delete_app(True) |
| 65 | + return |
| 66 | + |
| 67 | + transpiler.transpile([line]) |
| 68 | + serve_run.run_server() |
| 69 | + |
| 70 | + repl() |
9 | 71 |
|
10 | 72 |
|
11 | 73 | def main(): |
12 | | - utils.initial_print() |
| 74 | + printing.initial_print() |
13 | 75 |
|
14 | 76 | if len(sys.argv) > 1: |
15 | | - param_file_name, no_valid_flags = utils.parse_sys_args(sys.argv) |
| 77 | + param_file_name, no_valid_flags = parse_sys_args(sys.argv) |
| 78 | + |
| 79 | + if not param_file_name and True in [x in sys.argv for x in ['-d', '--dev', '-xd', '--extra-dev']]: |
| 80 | + repl(True) |
16 | 81 |
|
17 | 82 | cfg.source_file_name = param_file_name |
18 | 83 | if os.path.isfile(f'{os.getcwd()}/{cfg.source_file_name}'): |
19 | 84 | # Transpiles. |
20 | 85 | with open(cfg.source_file_name) as f: |
21 | 86 | source_lines = f.readlines() |
22 | 87 |
|
| 88 | + if not source_lines: |
| 89 | + printing.style_print('\t- The file is empty!', color='red') |
| 90 | + exit(1) |
| 91 | + |
23 | 92 | transpiler.transpile(source_lines) |
24 | 93 |
|
25 | | - # Starts server |
26 | | - utils.run_server() |
| 94 | + if askfile.get(['system', 'server'], True): |
| 95 | + # Starts server |
| 96 | + serve_run.run_server() |
| 97 | + else: |
| 98 | + printing.style_print('\nAuto start server is turned OFF.', styles=['bold']) |
| 99 | + print('\t - The transpiled code can be found in:', end=' ') |
| 100 | + printing.style_print(askfile.get(['system', 'output_path'], 'app.py'), color='blue', end='') |
| 101 | + print('.') |
27 | 102 | else: |
28 | 103 | if no_valid_flags: |
29 | | - utils.style_print('- The file could not be found!', color='red') |
| 104 | + printing.style_print('- The file could not be found!', color='red') |
30 | 105 | else: |
31 | | - # TODO: Launch Repl. |
32 | | - utils.style_print('- Please provide a script file!', color='red') |
| 106 | + repl(True) |
33 | 107 |
|
34 | 108 |
|
35 | 109 | if __name__ == '__main__': |
36 | 110 | main() |
37 | | - |
|
0 commit comments