Added command decorator.
This commit is contained in:
		
							parent
							
								
									6822bb2352
								
							
						
					
					
						commit
						d912e7d4ce
					
				
					 1 changed files with 86 additions and 53 deletions
				
			
		
							
								
								
									
										139
									
								
								cutekit/cmds.py
									
										
									
									
									
								
							
							
						
						
									
										139
									
								
								cutekit/cmds.py
									
										
									
									
									
								
							|  | @ -1,8 +1,9 @@ | ||||||
|  | from dataclasses import dataclass | ||||||
| import os | import os | ||||||
| import logging | import logging | ||||||
| import sys | import sys | ||||||
| 
 | 
 | ||||||
| from typing import Callable, cast, Optional, NoReturn | from typing import Callable, Unpack, cast, Optional, NoReturn | ||||||
| 
 | 
 | ||||||
| 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 | ||||||
|  | @ -15,25 +16,14 @@ Callback = Callable[[Args], None] | ||||||
| _logger = logging.getLogger(__name__) | _logger = logging.getLogger(__name__) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @dataclass | ||||||
| class Cmd: | class Cmd: | ||||||
|     shortName: Optional[str] |     shortName: Optional[str] = None | ||||||
|     longName: str |     longName: Optional[str] = None | ||||||
|     helpText: str |     helpText: Optional[str] = None | ||||||
|     callback: Callable[[Args], NoReturn] |     callback: Optional[Callable[[Args], None]] = None | ||||||
|     isPlugin: bool = False |     isPlugin: bool = False | ||||||
| 
 | 
 | ||||||
|     def __init__( |  | ||||||
|         self, |  | ||||||
|         shortName: Optional[str], |  | ||||||
|         longName: str, |  | ||||||
|         helpText: str, |  | ||||||
|         callback: Callable[[Args], NoReturn], |  | ||||||
|     ): |  | ||||||
|         self.shortName = shortName |  | ||||||
|         self.longName = longName |  | ||||||
|         self.helpText = helpText |  | ||||||
|         self.callback = callback |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
| cmds: list[Cmd] = [] | cmds: list[Cmd] = [] | ||||||
| 
 | 
 | ||||||
|  | @ -44,6 +34,19 @@ 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 wrapper(callback): | ||||||
|  |         cmds.append(Cmd(**kwargs, callback=callback)) | ||||||
|  | 
 | ||||||
|  |     return wrapper | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @command( | ||||||
|  |     shortName="p", | ||||||
|  |     longName="project", | ||||||
|  |     helpText="Show project information", | ||||||
|  |     isPlugin=False, | ||||||
|  | ) | ||||||
| def runCmd(args: Args): | def runCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
| 
 | 
 | ||||||
|  | @ -64,9 +67,12 @@ def runCmd(args: Args): | ||||||
|     shell.exec(component.outfile(), *args.args) |     shell.exec(component.outfile(), *args.args) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cmds += [Cmd("r", "run", "Run the target", runCmd)] | @command( | ||||||
| 
 |     shortName="t", | ||||||
| 
 |     longName="test", | ||||||
|  |     helpText="Run all test targets", | ||||||
|  |     isPlugin=False, | ||||||
|  | ) | ||||||
| def testCmd(args: Args): | def testCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
| 
 | 
 | ||||||
|  | @ -74,9 +80,12 @@ def testCmd(args: Args): | ||||||
|     builder.testAll(targetSpec) |     builder.testAll(targetSpec) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cmds += [Cmd("t", "test", "Run all test targets", testCmd)] | @command( | ||||||
| 
 |     shortName="d", | ||||||
| 
 |     longName="debug", | ||||||
|  |     helpText="Debug the target", | ||||||
|  |     isPlugin=False, | ||||||
|  | ) | ||||||
| def debugCmd(args: Args): | def debugCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
| 
 | 
 | ||||||
|  | @ -97,9 +106,12 @@ def debugCmd(args: Args): | ||||||
|     shell.exec("lldb", "-o", "run", component.outfile(), *args.args) |     shell.exec("lldb", "-o", "run", component.outfile(), *args.args) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cmds += [Cmd("d", "debug", "Debug the target", debugCmd)] | @command( | ||||||
| 
 |     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() | ||||||
| 
 | 
 | ||||||
|  | @ -113,9 +125,12 @@ def buildCmd(args: Args): | ||||||
|         builder.build(componentSpec, targetSpec, props) |         builder.build(componentSpec, targetSpec, props) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cmds += [Cmd("b", "build", "Build the target", buildCmd)] | @command( | ||||||
| 
 |     shortName="l", | ||||||
| 
 |     longName="list", | ||||||
|  |     helpText="List all targets and components", | ||||||
|  |     isPlugin=False, | ||||||
|  | ) | ||||||
| def listCmd(args: Args): | def listCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
| 
 | 
 | ||||||
|  | @ -139,25 +154,34 @@ def listCmd(args: Args): | ||||||
|     print() |     print() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cmds += [Cmd("l", "list", "List the targets", listCmd)] | @command( | ||||||
| 
 |     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) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cmds += [Cmd("c", "clean", "Clean the build directory", cleanCmd)] | @command( | ||||||
| 
 |     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) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cmds += [Cmd("n", "nuke", "Clean the build directory and cache", nukeCmd)] | @command( | ||||||
| 
 |     shortName="h", | ||||||
| 
 |     longName="help", | ||||||
|  |     helpText="Show this help message", | ||||||
|  |     isPlugin=False, | ||||||
|  | ) | ||||||
| def helpCmd(args: Args): | def helpCmd(args: Args): | ||||||
|     usage() |     usage() | ||||||
| 
 | 
 | ||||||
|  | @ -184,16 +208,22 @@ def helpCmd(args: Args): | ||||||
|     print(f"     - {const.GLOBAL_LOG_FILE}") |     print(f"     - {const.GLOBAL_LOG_FILE}") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cmds += [Cmd("h", "help", "Show this help message", helpCmd)] | @command( | ||||||
| 
 |     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}") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cmds += [Cmd("v", "version", "Show current version", versionCmd)] | @command( | ||||||
| 
 |     shortName="g", | ||||||
| 
 |     longName="graph", | ||||||
|  |     helpText="Show dependency graph", | ||||||
|  |     isPlugin=False, | ||||||
|  | ) | ||||||
| def graphCmd(args: Args): | def graphCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
| 
 | 
 | ||||||
|  | @ -208,9 +238,6 @@ def graphCmd(args: Args): | ||||||
|     graph.view(context, scope=scope, showExe=not onlyLibs, showDisabled=showDisabled) |     graph.view(context, scope=scope, showExe=not onlyLibs, showDisabled=showDisabled) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cmds += [Cmd("g", "graph", "Show dependency graph", graphCmd)] |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| def grabExtern(extern: dict[str, Extern]): | def grabExtern(extern: dict[str, Extern]): | ||||||
|     for extSpec, ext in extern.items(): |     for extSpec, ext in extern.items(): | ||||||
|         extPath = os.path.join(const.EXTERN_DIR, extSpec) |         extPath = os.path.join(const.EXTERN_DIR, extSpec) | ||||||
|  | @ -228,6 +255,12 @@ def grabExtern(extern: dict[str, Extern]): | ||||||
|             grabExtern(context.loadProject(extPath).extern) |             grabExtern(context.loadProject(extPath).extern) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @command( | ||||||
|  |     shortName="i", | ||||||
|  |     longName="install", | ||||||
|  |     helpText="Install all external packages", | ||||||
|  |     isPlugin=False, | ||||||
|  | ) | ||||||
| def installCmd(args: Args): | def installCmd(args: Args): | ||||||
|     project.chdir() |     project.chdir() | ||||||
| 
 | 
 | ||||||
|  | @ -235,9 +268,12 @@ def installCmd(args: Args): | ||||||
|     grabExtern(pj.extern) |     grabExtern(pj.extern) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cmds += [Cmd("i", "install", "Install all the external packages", installCmd)] | @command( | ||||||
| 
 |     shortName="I", | ||||||
| 
 |     longName="init", | ||||||
|  |     helpText="Initialize a new project", | ||||||
|  |     isPlugin=False, | ||||||
|  | ) | ||||||
| def initCmd(args: Args): | def initCmd(args: Args): | ||||||
|     import requests |     import requests | ||||||
| 
 | 
 | ||||||
|  | @ -292,9 +328,6 @@ def initCmd(args: Args): | ||||||
|     ) |     ) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cmds += [Cmd("I", "init", "Initialize a new project", initCmd)] |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| def usage(): | def usage(): | ||||||
|     print(f"Usage: {const.ARGV0} <command> [args...]") |     print(f"Usage: {const.ARGV0} <command> [args...]") | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue