analysis.go 1.2 KB
Newer Older
P
Phodal HUANG 已提交
1 2 3
package cmd

import (
P
Phodal HUANG 已提交
4
	"encoding/json"
P
Phodal HUANG 已提交
5
	"github.com/spf13/cobra"
P
Phodal Huang 已提交
6 7 8 9

	. "github.com/phodal/coca/core/adapter/call"
	. "github.com/phodal/coca/core/adapter/identifier"
	. "github.com/phodal/coca/core/support"
P
Phodal HUANG 已提交
10 11
)

P
Phodal Huang 已提交
12 13 14 15 16 17 18 19 20
type AnalysisCmdConfig struct {
	Path string
}

var (
	analysisCmdConfig AnalysisCmdConfig
)

var analysisCmd = &cobra.Command{
P
Phodal HUANG 已提交
21 22 23 24
	Use:   "analysis",
	Short: "analysis package",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
P
Phodal Huang 已提交
25
		importPath := *&analysisCmdConfig.Path
P
Phodal HUANG 已提交
26

P
Phodal Huang 已提交
27
		if importPath != "" {
P
Phodal Huang 已提交
28
			identifierApp := NewJavaIdentifierApp()
P
Phodal Huang 已提交
29
			iNodes := identifierApp.AnalysisPath(importPath)
P
Phodal HUANG 已提交
30

P
Phodal Huang 已提交
31 32
			identModel, _ := json.MarshalIndent(iNodes, "", "\t")
			WriteToCocaFile("identify.json", string(identModel))
P
Phodal Huang 已提交
33

P
Phodal Huang 已提交
34 35 36 37 38 39 40 41 42 43 44
			var classes []string = nil

			for _, node := range iNodes {
				classes = append(classes, node.Package+"."+node.ClassName)
			}

			callApp := new(JavaCallApp)

			callNodes := callApp.AnalysisPath(importPath, classes, iNodes)
			cModel, _ := json.MarshalIndent(callNodes, "", "\t")
			WriteToCocaFile("deps.json", string(cModel))
P
Phodal HUANG 已提交
45 46 47 48 49
		}
	},
}

func init() {
P
Phodal Huang 已提交
50 51
	rootCmd.AddCommand(analysisCmd)

P
Phodal Huang 已提交
52
	analysisCmd.PersistentFlags().StringVarP(&analysisCmdConfig.Path, "path", "p", ".", "example -p core/main")
P
Phodal HUANG 已提交
53
}