未验证 提交 2ed5dbf4 编写于 作者: P Phodal Huang

feat: add scan api api

上级 440cf2d6
package api
import (
"encoding/json"
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"os"
......@@ -9,16 +10,24 @@ import (
. "github.com/phodal/coca/adapter/models"
. "github.com/phodal/coca/language/java"
. "github.com/phodal/coca/utils"
)
var nodeInfos []JClassNode
var parsedDeps []JClassNode
type JavaApiApp struct {
}
func (j *JavaApiApp) AnalysisPath(codeDir string, classes []JClassNode) []JClassNode {
nodeInfos = nil
func (j *JavaApiApp) AnalysisPath(codeDir string, depPath string) []JClassNode {
parsedDeps = nil
file := ReadFile(depPath)
if file == nil {
return nil
}
_ = json.Unmarshal(file, &parsedDeps)
files := (*JavaApiApp)(nil).JavaFiles(codeDir)
for index := range files {
file := files[index]
......@@ -30,18 +39,15 @@ func (j *JavaApiApp) AnalysisPath(codeDir string, classes []JClassNode) []JClass
context := parser.CompilationUnit()
listener := NewJavaApiListener()
listener.appendClasses(classes)
listener.appendClasses(parsedDeps)
antlr.NewParseTreeWalker().Walk(listener, context)
apis := listener.getApis()
fmt.Println(apis)
//nodeInfo = listener.getNodeInfo()
//nodeInfo.Path = file
//nodeInfos = append(nodeInfos, *nodeInfo)
}
return nodeInfos
return parsedDeps
}
func (j *JavaApiApp) JavaFiles(codeDir string) []string {
......
......@@ -161,6 +161,7 @@ func buildRestApi(ctx *MethodDeclarationContext) {
func (s *JavaApiListener) appendClasses(classes []models.JClassNode) {
clz = classes
//fmt.Println(clz)
}
func (s *JavaApiListener) getApis() []RestApi {
......
package call
import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
. "github.com/phodal/coca/adapter/models"
. "github.com/phodal/coca/language/java"
......@@ -93,8 +92,6 @@ func (s *JavaCallListener) EnterLocalVariableDeclaration(ctx *LocalVariableDecla
typ := ctx.GetChild(0).(antlr.ParseTree).GetText()
variableName := ctx.GetChild(1).GetChild(0).GetChild(0).(antlr.ParseTree).GetText()
localVars[variableName] = typ
fmt.Println(typ, variableName)
}
func (s *JavaCallListener) EnterMethodDeclaration(ctx *MethodDeclarationContext) {
......
......@@ -24,7 +24,6 @@ func (j *SqlIdentifierApp) AnalysisPath(codeDir string) {
parsedXml := ParseXml(xmlFile)
for _, attr := range parsedXml.Attrs {
if strings.Contains(attr.Name.Local, "namespace") {
fmt.Println(attr.Value)
}
......
......@@ -4,13 +4,12 @@ import (
"encoding/json"
"github.com/spf13/cobra"
. "github.com/phodal/coca/adapter/api"
. "github.com/phodal/coca/adapter/call"
. "github.com/phodal/coca/adapter/identifier"
. "github.com/phodal/coca/utils"
)
var collCmd *cobra.Command = &cobra.Command{
var analysisCmd *cobra.Command = &cobra.Command{
Use: "analysis",
Short: "analysis package",
Long: ``,
......@@ -31,15 +30,13 @@ var collCmd *cobra.Command = &cobra.Command{
callNodes := callApp.AnalysisPath(importPath, classes)
cModel, _ := json.MarshalIndent(callNodes, "", "\t")
apiApp := new(JavaApiApp)
apiApp.AnalysisPath(importPath, callNodes)
WriteToFile("deps.json", string(cModel))
}
},
}
func init() {
rootCmd.PersistentFlags().StringP("path", "p", "Code Path", "example -p src/main")
rootCmd.AddCommand(analysisCmd)
analysisCmd.PersistentFlags().StringP("path", "p", "Code Path", "example -p src/main")
}
package cmd
import (
. "github.com/phodal/coca/adapter/api"
"github.com/spf13/cobra"
)
var apiCmd *cobra.Command = &cobra.Command{
Use: "api",
Short: "scan api",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
path := cmd.Flag("path").Value.String()
dependence := cmd.Flag("dependence").Value.String()
if path != "" {
app := new(JavaApiApp)
app.AnalysisPath(path, dependence)
}
},
}
func init() {
rootCmd.AddCommand(apiCmd)
rootCmd.PersistentFlags().StringP("path", "p", "", "path")
rootCmd.PersistentFlags().StringP("dependence", "d", "", "get dependence file")
}
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
......@@ -16,14 +15,12 @@ var (
}
)
// Execute executes the root command.
func Execute() error {
fmt.Println(rootCmd.Flag("config").Value.String())
return rootCmd.Execute()
}
func init() {
rootCmd.AddCommand(collCmd)
rootCmd.AddCommand(analysisCmd)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
}
......
......@@ -18,8 +18,3 @@ var sqlCmd *cobra.Command = &cobra.Command{
},
}
func init() {
rootCmd.AddCommand(sqlCmd)
sqlCmd.PersistentFlags().StringP("path", "p", "", "path")
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册