feat: [dep] make gradle works

上级 743147e5
......@@ -64,22 +64,23 @@ func BuildDeps(val xmlparse.XmlNode) []domain.JDependency {
dependency.Scope = textNode.Val.(string)
}
}
}
deps = append(deps, *dependency)
deps = append(deps, *dependency)
}
return deps
}
func AnalysisGradle(str string) {
func AnalysisGradle(str string) []domain.JDependency {
parser := ProcessGroovyString(str)
context := parser.CompilationUnit()
listener := groovy.NewGroovyIdentListener()
antlr.NewParseTreeWalker().Walk(listener, context)
return listener.GetDepsInfo()
}
func ProcessGroovyString(code string) *GroovyParser {
......
......@@ -14,9 +14,10 @@ func Test_ShouldReturnGradleDep(t *testing.T) {
developmentOnly 'org.springframework.boot:spring-boot-devtools'
}`
AnalysisGradle(pluginsStr)
results := AnalysisGradle(pluginsStr)
g.Expect(true).To(Equal(true))
g.Expect(len(results)).To(Equal(2))
g.Expect(results[0].ArtifactId).To(Equal("spring-boot-starter-web"))
}
func Test_ShouldReturnCorrectMavenDeps(t *testing.T) {
......
package groovy
import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/domain"
parser "github.com/phodal/coca/languages/groovy"
"reflect"
"strings"
)
var nodeDeps []domain.JDependency
type GroovyIdentifierListener struct {
parser.BaseGroovyParserListener
}
func NewGroovyIdentListener() *GroovyIdentifierListener {
nodeDeps = nil
return &GroovyIdentifierListener{}
}
......@@ -20,7 +24,6 @@ func NewGroovyIdentListener() *GroovyIdentifierListener {
// 2. use regex replace it
// 3. remove the features
func (s *GroovyIdentifierListener) EnterScriptStatement(ctx *parser.ScriptStatementContext) {
fmt.Println("EnterScriptStatement")
if reflect.TypeOf(ctx.GetChild(0)).String() == "*parser.ExpressionStmtAltContext" {
cmdExpr := ctx.GetChild(0).(*parser.ExpressionStmtAltContext).StatementExpression().GetChild(0).(*parser.CommandExpressionContext).Expression()
if cmdExpr != nil {
......@@ -33,9 +36,15 @@ func (s *GroovyIdentifierListener) EnterScriptStatement(ctx *parser.ScriptStatem
}
}
func buildGroovyMap(pathExprCtx *parser.PathExpressionContext) {
func (s *GroovyIdentifierListener) GetDepsInfo() []domain.JDependency {
return nodeDeps
}
func buildGroovyMap(pathExprCtx *parser.PathExpressionContext) []domain.JDependency {
if reflect.TypeOf(pathExprCtx.GetChild(0)).String() == "*parser.IdentifierPrmrAltContext" {
fmt.Println(pathExprCtx.GetChild(0).(antlr.ParseTree).GetText())
if pathExprCtx.GetChild(0).(antlr.ParseTree).GetText() != "dependencies" {
return nil
}
}
pathChild := pathExprCtx.GetChild(1)
if pathChild != nil {
......@@ -45,19 +54,22 @@ func buildGroovyMap(pathExprCtx *parser.PathExpressionContext) {
expressionContext := pathElement.ClosureOrLambdaExpression().(*parser.ClosureOrLambdaExpressionContext)
if reflect.TypeOf(expressionContext.GetChild(0)).String() == "*parser.ClosureContext" {
closureContext := expressionContext.GetChild(0).(*parser.ClosureContext)
buildBlockStatements(closureContext)
nodeDeps = buildBlockStatements(closureContext)
return nodeDeps
}
}
}
return nil
}
func buildBlockStatements(closureContext *parser.ClosureContext) {
func buildBlockStatements(closureContext *parser.ClosureContext) []domain.JDependency {
var results []domain.JDependency
statementsContext := closureContext.BlockStatementsOpt().(*parser.BlockStatementsOptContext).BlockStatements().(*parser.BlockStatementsContext)
for _, blockStatement := range statementsContext.AllBlockStatement() {
child := blockStatement.GetChild(0).GetChild(0).GetChild(0).(*parser.CommandExpressionContext)
declare := child.GetChild(0).(antlr.ParseTree).GetText()
var deps []string = nil
var result *domain.JDependency = nil
for _, arg := range child.GetChild(1).(antlr.ParseTree).(*parser.ArgumentListContext).AllArgumentListElement() {
if reflect.TypeOf(arg.(*parser.ArgumentListElementContext).GetChild(0)).String() == "*parser.ExpressionListElementContext" {
listElementContext := arg.(*parser.ArgumentListElementContext).GetChild(0).(*parser.ExpressionListElementContext)
......@@ -68,11 +80,22 @@ func buildBlockStatements(closureContext *parser.ClosureContext) {
GetChild(0).
(*parser.LiteralPrmrAltContext)
result := literalPrmrAltContext.Literal().GetChild(0).(*parser.StringLiteralContext).StringLiteral().GetText()
deps = append(deps, result)
resultStr := literalPrmrAltContext.Literal().GetChild(0).(*parser.StringLiteralContext).StringLiteral().GetText()
result = ConvertToJDep(resultStr)
}
}
fmt.Println(declare, deps)
if result != nil {
result.Scope = declare
results = append(results, *result)
}
}
return results
}
func ConvertToJDep(result string) *domain.JDependency {
withQuote := strings.ReplaceAll(result, "'", "")
split := strings.Split(withQuote, ":")
return domain.NewJDependency(split[0], split[1])
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册