feat: new command line decorator
This commit is contained in:
		
							parent
							
								
									b9fa1422a4
								
							
						
					
					
						commit
						2c9a7c5fc6
					
				
					 1 changed files with 32 additions and 85 deletions
				
			
		
							
								
								
									
										117
									
								
								cutekit/cmds.py
									
										
									
									
									
								
							
							
						
						
									
										117
									
								
								cutekit/cmds.py
									
										
									
									
									
								
							|  | @ -1,15 +1,16 @@ | ||||||
| from dataclasses import dataclass | import inspect | ||||||
| import os |  | ||||||
| import logging | import logging | ||||||
|  | import os | ||||||
| import sys | import sys | ||||||
| 
 | 
 | ||||||
| from typing import Callable, Unpack, cast, Optional, NoReturn | from dataclasses import dataclass | ||||||
|  | from typing import Callable, cast, Optional | ||||||
| 
 | 
 | ||||||
| from cutekit import context, shell, const, vt100, builder, graph, project | from cutekit import context, shell, const, vt100, builder, graph, project | ||||||
| from cutekit.args import Args | from cutekit.args import Args | ||||||
|  | from cutekit.context import contextFor | ||||||
| from cutekit.jexpr import Json | from cutekit.jexpr import Json | ||||||
| from cutekit.model import Extern | from cutekit.model import Extern | ||||||
| from cutekit.context import contextFor |  | ||||||
| 
 | 
 | ||||||
| Callback = Callable[[Args], None] | Callback = Callable[[Args], None] | ||||||
| 
 | 
 | ||||||
|  | @ -18,11 +19,11 @@ _logger = logging.getLogger(__name__) | ||||||
| 
 | 
 | ||||||
| @dataclass | @dataclass | ||||||
| class Cmd: | class Cmd: | ||||||
|     shortName: Optional[str] = None |     shortName: str | ||||||
|     longName: Optional[str] = None |     longName: str | ||||||
|     helpText: Optional[str] = None |     helpText: str | ||||||
|     callback: Optional[Callable[[Args], None]] = None |     isPlugin: bool | ||||||
|     isPlugin: bool = False |     callback: Callback | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cmds: list[Cmd] = [] | cmds: list[Cmd] = [] | ||||||
|  | @ -34,19 +35,20 @@ def append(cmd: Cmd): | ||||||
|     cmds.sort(key=lambda c: c.shortName or c.longName) |     cmds.sort(key=lambda c: c.shortName or c.longName) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def command(**kwargs: Unpack[Cmd]): | def cmd(shortName: str, longName: str, helpText: str): | ||||||
|     def wrapper(callback): |     curframe = inspect.currentframe() | ||||||
|         cmds.append(Cmd(**kwargs, callback=callback)) |     calframe = inspect.getouterframes(curframe, 2) | ||||||
| 
 | 
 | ||||||
|     return wrapper |     def wrap(fn: Callable[[Args], None]): | ||||||
|  |         cmds.append( | ||||||
|  |             Cmd(shortName, longName, helpText, calframe[1].filename != __file__, fn) | ||||||
|  |         ) | ||||||
|  |         return fn | ||||||
|  | 
 | ||||||
|  |     return wrap | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @command( | @cmd("p", "project", "Show project information") | ||||||
|     shortName="p", |  | ||||||
|     longName="project", |  | ||||||
|     helpText="Show project information", |  | ||||||
|     isPlugin=False, |  | ||||||
| ) |  | ||||||
| def runCmd(args: Args): | def runCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
| 
 | 
 | ||||||
|  | @ -67,12 +69,7 @@ def runCmd(args: Args): | ||||||
|     shell.exec(component.outfile(), *args.args) |     shell.exec(component.outfile(), *args.args) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @command( | @cmd("t", "test", "Run all test targets") | ||||||
|     shortName="t", |  | ||||||
|     longName="test", |  | ||||||
|     helpText="Run all test targets", |  | ||||||
|     isPlugin=False, |  | ||||||
| ) |  | ||||||
| def testCmd(args: Args): | def testCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
| 
 | 
 | ||||||
|  | @ -80,12 +77,7 @@ def testCmd(args: Args): | ||||||
|     builder.testAll(targetSpec) |     builder.testAll(targetSpec) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @command( | @cmd("d", "debug", "Debug a component") | ||||||
|     shortName="d", |  | ||||||
|     longName="debug", |  | ||||||
|     helpText="Debug the target", |  | ||||||
|     isPlugin=False, |  | ||||||
| ) |  | ||||||
| def debugCmd(args: Args): | def debugCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
| 
 | 
 | ||||||
|  | @ -106,12 +98,7 @@ def debugCmd(args: Args): | ||||||
|     shell.exec("lldb", "-o", "run", component.outfile(), *args.args) |     shell.exec("lldb", "-o", "run", component.outfile(), *args.args) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @command( | @cmd("b", "build", "Build a component or all components") | ||||||
|     shortName="b", |  | ||||||
|     longName="build", |  | ||||||
|     helpText="Build a component or all components", |  | ||||||
|     isPlugin=False, |  | ||||||
| ) |  | ||||||
| def buildCmd(args: Args): | def buildCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
| 
 | 
 | ||||||
|  | @ -125,12 +112,7 @@ def buildCmd(args: Args): | ||||||
|         builder.build(componentSpec, targetSpec, props) |         builder.build(componentSpec, targetSpec, props) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @command( | @cmd("l", "list", "List all components and targets") | ||||||
|     shortName="l", |  | ||||||
|     longName="list", |  | ||||||
|     helpText="List all targets and components", |  | ||||||
|     isPlugin=False, |  | ||||||
| ) |  | ||||||
| def listCmd(args: Args): | def listCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
| 
 | 
 | ||||||
|  | @ -154,34 +136,19 @@ def listCmd(args: Args): | ||||||
|     print() |     print() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @command( | @cmd("c", "clean", "Clean build files") | ||||||
|     shortName="c", |  | ||||||
|     longName="clean", |  | ||||||
|     helpText="Remove all build files", |  | ||||||
|     isPlugin=False, |  | ||||||
| ) |  | ||||||
| def cleanCmd(args: Args): | def cleanCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
|     shell.rmrf(const.BUILD_DIR) |     shell.rmrf(const.BUILD_DIR) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @command( | @cmd("n", "nuke", "Clean all build files and caches") | ||||||
|     shortName="n", |  | ||||||
|     longName="nuke", |  | ||||||
|     helpText="Clean all build files and caches", |  | ||||||
|     isPlugin=False, |  | ||||||
| ) |  | ||||||
| def nukeCmd(args: Args): | def nukeCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
|     shell.rmrf(const.PROJECT_CK_DIR) |     shell.rmrf(const.PROJECT_CK_DIR) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @command( | @cmd("h", "help", "Show this help message") | ||||||
|     shortName="h", |  | ||||||
|     longName="help", |  | ||||||
|     helpText="Show this help message", |  | ||||||
|     isPlugin=False, |  | ||||||
| ) |  | ||||||
| def helpCmd(args: Args): | def helpCmd(args: Args): | ||||||
|     usage() |     usage() | ||||||
| 
 | 
 | ||||||
|  | @ -208,22 +175,12 @@ def helpCmd(args: Args): | ||||||
|     print(f"     - {const.GLOBAL_LOG_FILE}") |     print(f"     - {const.GLOBAL_LOG_FILE}") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @command( | @cmd("v", "version", "Show current version") | ||||||
|     shortName="v", |  | ||||||
|     longName="version", |  | ||||||
|     helpText="Show current version", |  | ||||||
|     isPlugin=False, |  | ||||||
| ) |  | ||||||
| def versionCmd(args: Args): | def versionCmd(args: Args): | ||||||
|     print(f"CuteKit v{const.VERSION_STR}") |     print(f"CuteKit v{const.VERSION_STR}") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @command( | @cmd("g", "graph", "Show the dependency graph") | ||||||
|     shortName="g", |  | ||||||
|     longName="graph", |  | ||||||
|     helpText="Show dependency graph", |  | ||||||
|     isPlugin=False, |  | ||||||
| ) |  | ||||||
| def graphCmd(args: Args): | def graphCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
| 
 | 
 | ||||||
|  | @ -255,12 +212,7 @@ def grabExtern(extern: dict[str, Extern]): | ||||||
|             grabExtern(context.loadProject(extPath).extern) |             grabExtern(context.loadProject(extPath).extern) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @command( | @cmd("i", "install", "Install required external packages") | ||||||
|     shortName="i", |  | ||||||
|     longName="install", |  | ||||||
|     helpText="Install all external packages", |  | ||||||
|     isPlugin=False, |  | ||||||
| ) |  | ||||||
| def installCmd(args: Args): | def installCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
| 
 | 
 | ||||||
|  | @ -268,12 +220,7 @@ def installCmd(args: Args): | ||||||
|     grabExtern(pj.extern) |     grabExtern(pj.extern) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @command( | @cmd("I", "init", "Initialize a new project") | ||||||
|     shortName="I", |  | ||||||
|     longName="init", |  | ||||||
|     helpText="Initialize a new project", |  | ||||||
|     isPlugin=False, |  | ||||||
| ) |  | ||||||
| def initCmd(args: Args): | def initCmd(args: Args): | ||||||
|     import requests |     import requests | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue