From 2c72c0ec1d126b325ed53e93786f0b8cef6bec42 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Sat, 12 Mar 2022 10:23:39 +0800 Subject: [PATCH] feat(application): use clikt as cli library --- chapi-application/build.gradle.kts | 30 +- .../src/main/kotlin/chapi/app/cli/Chapi.kt | 43 ++ .../src/main/kotlin/chapi/app/cli/Main.kt | 10 - .../src/main/antlr/TypeScriptLexer.tokens | 547 +++++++++--------- 4 files changed, 332 insertions(+), 298 deletions(-) create mode 100644 chapi-application/src/main/kotlin/chapi/app/cli/Chapi.kt delete mode 100644 chapi-application/src/main/kotlin/chapi/app/cli/Main.kt diff --git a/chapi-application/build.gradle.kts b/chapi-application/build.gradle.kts index 9adfba7d..e78316f4 100644 --- a/chapi-application/build.gradle.kts +++ b/chapi-application/build.gradle.kts @@ -6,26 +6,15 @@ plugins { kotlin("plugin.serialization") version "1.6.10" `jacoco-conventions` + + id("com.github.johnrengelman.shadow") version "7.0.0" } repositories { mavenCentral() mavenLocal() - jcenter() -} - -application { - mainClassName = "chapi.app.cli.Main" } -//tasks.withType { -// manifest { -// attributes["Main-Class"] = application.mainClassName -// } -// -// from(configurations.runtimeClasspath.get().map({ if (it.isDirectory) it else zipTree(it) })) -//} - dependencies { // project deps implementation(project(":chapi-domain")) @@ -40,6 +29,8 @@ dependencies { implementation(project(":chapi-ast-csharp")) implementation(project(":chapi-ast-kotlin")) + implementation("com.github.ajalt.clikt:clikt:3.4.0") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2") implementation(kotlin("stdlib-jdk8")) @@ -57,8 +48,17 @@ dependencies { implementation("org.antlr:antlr4-runtime:4.8-1") } -configure { - sourceCompatibility = JavaVersion.VERSION_1_8 + +application { + mainClass.set("chapi.app.cli.ChapiKt") +} + +tasks{ + shadowJar { + manifest { + attributes(Pair("Main-Class", "chapi.app.cli.ChapiKt")) + } + } } tasks.withType { diff --git a/chapi-application/src/main/kotlin/chapi/app/cli/Chapi.kt b/chapi-application/src/main/kotlin/chapi/app/cli/Chapi.kt new file mode 100644 index 00000000..a62a301f --- /dev/null +++ b/chapi-application/src/main/kotlin/chapi/app/cli/Chapi.kt @@ -0,0 +1,43 @@ +package chapi.app.cli + +import chapi.app.analyser.ChapiAnalyser +import chapi.app.analyser.TypeScriptAnalyserApp +import chapi.app.frontend.ComponentHttpCallInfo +import chapi.app.frontend.FrontendApiAnalyser +import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.core.subcommands +import com.github.ajalt.clikt.parameters.options.default +import com.github.ajalt.clikt.parameters.options.option +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json +import java.io.File + +class Ast : CliktCommand() { + val path: String by option(help="the path to analysis").default(".") + + override fun run() { + val results = ChapiAnalyser().analysis(path) + File("chapi.json").writeText(results.toList().toString()) + } +} + +class Chapi: CliktCommand() { + override fun run() = Unit +} + + +class Api : CliktCommand() { + val path: String by option(help="the path to analysis").default(".") + + override fun run() { + val nodes = TypeScriptAnalyserApp().analysisNodeByPath(path) + val componentCalls: Array = FrontendApiAnalyser().analysis(nodes, path) + + File("nodes.json").writeText(Json.encodeToString(nodes)) + File("api.json").writeText(Json.encodeToString(componentCalls)) + } +} + +fun main(args: Array) = Chapi() + .subcommands(Ast(), Api()) + .main(args) diff --git a/chapi-application/src/main/kotlin/chapi/app/cli/Main.kt b/chapi-application/src/main/kotlin/chapi/app/cli/Main.kt deleted file mode 100644 index 73a5282c..00000000 --- a/chapi-application/src/main/kotlin/chapi/app/cli/Main.kt +++ /dev/null @@ -1,10 +0,0 @@ -@file:JvmName("Main") -package chapi.app.cli - -import chapi.app.analyser.ChapiAnalyser -import java.io.File - -fun main() { - val results = ChapiAnalyser().analysis(".") - File("chapi.json").writeText(results.toList().toString()) -} diff --git a/chapi-ast-typescript/src/main/antlr/TypeScriptLexer.tokens b/chapi-ast-typescript/src/main/antlr/TypeScriptLexer.tokens index 583a65fe..04e5065e 100644 --- a/chapi-ast-typescript/src/main/antlr/TypeScriptLexer.tokens +++ b/chapi-ast-typescript/src/main/antlr/TypeScriptLexer.tokens @@ -1,273 +1,274 @@ -MultiLineComment=1 -SingleLineComment=2 -RegularExpressionLiteral=3 -OpenBracket=4 -CloseBracket=5 -OpenParen=6 -CloseParen=7 -OpenBrace=8 -TemplateCloseBrace=9 -CloseBrace=10 -SemiColon=11 -Comma=12 -Assign=13 -QuestionMark=14 -Colon=15 -Ellipsis=16 -Dot=17 -PlusPlus=18 -MinusMinus=19 -Plus=20 -Minus=21 -BitNot=22 -Not=23 -Multiply=24 -Divide=25 -Modulus=26 -Power=27 -NullCoalesce=28 -Hashtag=29 -RightShiftArithmetic=30 -LeftShiftArithmetic=31 -RightShiftLogical=32 -LessThan=33 -MoreThan=34 -LessThanEquals=35 -GreaterThanEquals=36 -Equals_=37 -NotEquals=38 -IdentityEquals=39 -IdentityNotEquals=40 -BitAnd=41 -BitXOr=42 -BitOr=43 -And=44 -Or=45 -MultiplyAssign=46 -DivideAssign=47 -ModulusAssign=48 -PlusAssign=49 -MinusAssign=50 -LeftShiftArithmeticAssign=51 -RightShiftArithmeticAssign=52 -RightShiftLogicalAssign=53 -BitAndAssign=54 -BitXorAssign=55 -BitOrAssign=56 -ARROW=57 -Lodash=58 -Dollar=59 -NullLiteral=60 -BooleanLiteral=61 -DecimalLiteral=62 -HexIntegerLiteral=63 -OctalIntegerLiteral=64 -OctalIntegerLiteral2=65 -BinaryIntegerLiteral=66 -Break=67 -Do=68 -Instanceof=69 -Typeof=70 -Case=71 -Else=72 -New=73 -Var=74 -Catch=75 -Finally=76 -Return=77 -Void=78 -Continue=79 -For=80 -Switch=81 -While=82 -Debugger=83 -Function_=84 -This=85 -With=86 -Default=87 -If=88 -Throw=89 -Delete=90 -In=91 -Try=92 -As=93 -From=94 -ReadOnly=95 -Async=96 -Of=97 -Await=98 -Class=99 -Enum=100 -Extends=101 -Super=102 -Const=103 -Export=104 -Import=105 -Implements=106 -Let=107 -Private=108 -Public=109 -Interface=110 -Package=111 -Protected=112 -Static=113 -Yield=114 -Any=115 -Number=116 -Boolean=117 -String=118 -Symbol=119 -TypeAlias=120 -Get=121 -Set=122 -Constructor=123 -Namespace=124 -Require=125 -Module=126 -Declare=127 -Unknown=128 -Undefined=129 -Abstract=130 -Is=131 -At=132 -Identifier=133 -StringLiteral=134 -BackTick=135 -WhiteSpaces=136 -LineTerminator=137 -HtmlComment=138 -CDataComment=139 -UnexpectedCharacter=140 -TemplateStringStartExpression=141 -TemplateStringAtom=142 -TagOpen=143 -TagClose=144 -TagSlashClose=145 -TagSlash=146 -TagName=147 -AttributeValue=148 -Attribute=149 -'['=4 -']'=5 -'('=6 -')'=7 -'{'=8 -'}'=10 -';'=11 -','=12 -'='=13 -'?'=14 -':'=15 -'...'=16 -'.'=17 -'++'=18 -'--'=19 -'+'=20 -'-'=21 -'~'=22 -'!'=23 -'*'=24 -'/'=25 -'%'=26 -'**'=27 -'??'=28 -'#'=29 -'>>'=30 -'<<'=31 -'>>>'=32 -'<'=33 -'>'=34 -'<='=35 -'>='=36 -'=='=37 -'!='=38 -'==='=39 -'!=='=40 -'&'=41 -'^'=42 -'|'=43 -'&&'=44 -'||'=45 -'*='=46 -'/='=47 -'%='=48 -'+='=49 -'-='=50 -'<<='=51 -'>>='=52 -'>>>='=53 -'&='=54 -'^='=55 -'|='=56 -'=>'=57 -'_'=58 -'$'=59 -'null'=60 -'break'=67 -'do'=68 -'instanceof'=69 -'typeof'=70 -'case'=71 -'else'=72 -'new'=73 -'var'=74 -'catch'=75 -'finally'=76 -'return'=77 -'void'=78 -'continue'=79 -'for'=80 -'switch'=81 -'while'=82 -'debugger'=83 -'function'=84 -'this'=85 -'with'=86 -'default'=87 -'if'=88 -'throw'=89 -'delete'=90 -'in'=91 -'try'=92 -'as'=93 -'from'=94 -'readonly'=95 -'async'=96 -'of'=97 -'await'=98 -'class'=99 -'enum'=100 -'extends'=101 -'super'=102 -'const'=103 -'export'=104 -'import'=105 -'implements'=106 -'let'=107 -'private'=108 -'public'=109 -'interface'=110 -'package'=111 -'protected'=112 -'static'=113 -'yield'=114 -'any'=115 -'number'=116 -'boolean'=117 -'string'=118 -'symbol'=119 -'type'=120 -'get'=121 -'set'=122 -'constructor'=123 -'namespace'=124 -'require'=125 -'module'=126 -'declare'=127 -'unknown'=128 -'undefined'=129 -'abstract'=130 -'is'=131 -'@'=132 -'${'=141 -'/>'=145 +JsxComment=1 +MultiLineComment=2 +SingleLineComment=3 +RegularExpressionLiteral=4 +OpenBracket=5 +CloseBracket=6 +OpenParen=7 +CloseParen=8 +OpenBrace=9 +TemplateCloseBrace=10 +CloseBrace=11 +SemiColon=12 +Comma=13 +Assign=14 +QuestionMark=15 +Colon=16 +Ellipsis=17 +Dot=18 +PlusPlus=19 +MinusMinus=20 +Plus=21 +Minus=22 +BitNot=23 +Not=24 +Multiply=25 +Divide=26 +Modulus=27 +Power=28 +NullCoalesce=29 +Hashtag=30 +RightShiftArithmetic=31 +LeftShiftArithmetic=32 +RightShiftLogical=33 +LessThan=34 +MoreThan=35 +LessThanEquals=36 +GreaterThanEquals=37 +Equals_=38 +NotEquals=39 +IdentityEquals=40 +IdentityNotEquals=41 +BitAnd=42 +BitXOr=43 +BitOr=44 +And=45 +Or=46 +MultiplyAssign=47 +DivideAssign=48 +ModulusAssign=49 +PlusAssign=50 +MinusAssign=51 +LeftShiftArithmeticAssign=52 +RightShiftArithmeticAssign=53 +RightShiftLogicalAssign=54 +BitAndAssign=55 +BitXorAssign=56 +BitOrAssign=57 +ARROW=58 +Lodash=59 +Dollar=60 +NullLiteral=61 +BooleanLiteral=62 +DecimalLiteral=63 +HexIntegerLiteral=64 +OctalIntegerLiteral=65 +OctalIntegerLiteral2=66 +BinaryIntegerLiteral=67 +Break=68 +Do=69 +Instanceof=70 +Typeof=71 +Case=72 +Else=73 +New=74 +Var=75 +Catch=76 +Finally=77 +Return=78 +Void=79 +Continue=80 +For=81 +Switch=82 +While=83 +Debugger=84 +Function_=85 +This=86 +With=87 +Default=88 +If=89 +Throw=90 +Delete=91 +In=92 +Try=93 +As=94 +From=95 +ReadOnly=96 +Async=97 +Of=98 +Await=99 +Class=100 +Enum=101 +Extends=102 +Super=103 +Const=104 +Export=105 +Import=106 +Implements=107 +Let=108 +Private=109 +Public=110 +Interface=111 +Package=112 +Protected=113 +Static=114 +Yield=115 +Any=116 +Number=117 +Boolean=118 +String=119 +Symbol=120 +TypeAlias=121 +Get=122 +Set=123 +Constructor=124 +Namespace=125 +Require=126 +Module=127 +Declare=128 +Unknown=129 +Undefined=130 +Abstract=131 +Is=132 +At=133 +Identifier=134 +StringLiteral=135 +BackTick=136 +WhiteSpaces=137 +LineTerminator=138 +HtmlComment=139 +CDataComment=140 +UnexpectedCharacter=141 +TemplateStringStartExpression=142 +TemplateStringAtom=143 +TagOpen=144 +TagClose=145 +TagSlashClose=146 +TagSlash=147 +TagName=148 +AttributeValue=149 +Attribute=150 +'['=5 +']'=6 +'('=7 +')'=8 +'{'=9 +'}'=11 +';'=12 +','=13 +'='=14 +'?'=15 +':'=16 +'...'=17 +'.'=18 +'++'=19 +'--'=20 +'+'=21 +'-'=22 +'~'=23 +'!'=24 +'*'=25 +'/'=26 +'%'=27 +'**'=28 +'??'=29 +'#'=30 +'>>'=31 +'<<'=32 +'>>>'=33 +'<'=34 +'>'=35 +'<='=36 +'>='=37 +'=='=38 +'!='=39 +'==='=40 +'!=='=41 +'&'=42 +'^'=43 +'|'=44 +'&&'=45 +'||'=46 +'*='=47 +'/='=48 +'%='=49 +'+='=50 +'-='=51 +'<<='=52 +'>>='=53 +'>>>='=54 +'&='=55 +'^='=56 +'|='=57 +'=>'=58 +'_'=59 +'$'=60 +'null'=61 +'break'=68 +'do'=69 +'instanceof'=70 +'typeof'=71 +'case'=72 +'else'=73 +'new'=74 +'var'=75 +'catch'=76 +'finally'=77 +'return'=78 +'void'=79 +'continue'=80 +'for'=81 +'switch'=82 +'while'=83 +'debugger'=84 +'function'=85 +'this'=86 +'with'=87 +'default'=88 +'if'=89 +'throw'=90 +'delete'=91 +'in'=92 +'try'=93 +'as'=94 +'from'=95 +'readonly'=96 +'async'=97 +'of'=98 +'await'=99 +'class'=100 +'enum'=101 +'extends'=102 +'super'=103 +'const'=104 +'export'=105 +'import'=106 +'implements'=107 +'let'=108 +'private'=109 +'public'=110 +'interface'=111 +'package'=112 +'protected'=113 +'static'=114 +'yield'=115 +'any'=116 +'number'=117 +'boolean'=118 +'string'=119 +'symbol'=120 +'type'=121 +'get'=122 +'set'=123 +'constructor'=124 +'namespace'=125 +'require'=126 +'module'=127 +'declare'=128 +'unknown'=129 +'undefined'=130 +'abstract'=131 +'is'=132 +'@'=133 +'${'=142 +'/>'=146