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

import (
P
Phodal HUANG 已提交
4
	"encoding/json"
5
	"fmt"
P
Phodal Huang 已提交
6
	"github.com/phodal/coca/cmd/cmd_util"
7
	"github.com/phodal/coca/pkg/adapter/cocafile"
8
	"github.com/phodal/coca/pkg/application/analysis"
9
	"github.com/phodal/coca/pkg/application/pyapp"
10
	"github.com/phodal/coca/pkg/application/tsapp"
11 12
	"github.com/phodal/coca/pkg/domain/core_domain"
	"github.com/phodal/coca/pkg/infrastructure/ast/cocago"
13
	"github.com/spf13/cobra"
14
	"io/ioutil"
P
Phodal HUANG 已提交
15 16
)

P
Phodal Huang 已提交
17
type AnalysisCmdConfig struct {
18 19
	Path        string
	ForceUpdate bool
20
	Lang        string
P
Phodal Huang 已提交
21 22 23 24 25 26 27
}

var (
	analysisCmdConfig AnalysisCmdConfig
)

var analysisCmd = &cobra.Command{
P
Phodal HUANG 已提交
28
	Use:   "analysis",
P
Phodal Huang 已提交
29
	Short: "analysis code",
P
Phodal HUANG 已提交
30 31
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
32 33
		switch analysisCmdConfig.Lang {
		case "go":
34
			analysisGo()
35
		case "py", "python":
36
			analysisPython()
37 38
		case "ts", "typescript":
			analysisTypeScript()
39
		default:
40 41 42 43
			analysisJava()
		}
	},
}
P
Phodal HUANG 已提交
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
func analysisTypeScript() {
	importPath := analysisCmdConfig.Path

	var results []core_domain.CodeFile
	files := cocafile.GetFilesWithFilter(importPath, cocafile.TypeScriptFileFilter)
	fmt.Println(files)
	for _, file := range files {
		fmt.Fprintf(output, "Process TypeScript file: %s\n", file)
		app := new(tsapp.TypeScriptApiApp)
		content, _ := ioutil.ReadFile(file)
		result := app.Analysis(string(content), "")
		results = append(results, result)
	}

	var ds []core_domain.CodeDataStruct
	for _, result := range results {
		ds = append(ds, result.DataStructures...)
	}

	cModel, _ := json.MarshalIndent(ds, "", "\t")
	cmd_util.WriteToCocaFile("tsdeps.json", string(cModel))
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
func analysisPython() {
	importPath := analysisCmdConfig.Path

	var results []core_domain.CodeFile
	files := cocafile.GetFilesWithFilter(importPath, cocafile.PythonFileFilter)
	for _, file := range files {
		fmt.Fprintf(output, "Process Python file: %s\n", file)
		app := new(pyapp.PythonApiApp)
		content, _ := ioutil.ReadFile(file)
		result := app.Analysis(string(content), "")
		results = append(results, result)
	}

	var ds []core_domain.CodeDataStruct
	for _, result := range results {
		ds = append(ds, result.DataStructures...)
	}

	cModel, _ := json.MarshalIndent(ds, "", "\t")
	cmd_util.WriteToCocaFile("pydeps.json", string(cModel))
}

90 91
func analysisGo() {
	importPath := analysisCmdConfig.Path
P
Phodal Huang 已提交
92

93 94 95 96 97 98
	var results []core_domain.CodeFile
	files := cocafile.GetFilesWithFilter(importPath, cocafile.GoFileFilter)
	for _, file := range files {
		parser := cocago.NewCocagoParser()
		parser.SetOutput(output)
		result := parser.ProcessFile(file)
P
Phodal Huang 已提交
99

100 101
		results = append(results, result)
	}
P
Phodal Huang 已提交
102

103 104 105 106
	var ds []core_domain.CodeDataStruct
	for _, result := range results {
		ds = append(ds, result.DataStructures...)
	}
P
Phodal Huang 已提交
107

108 109 110
	cModel, _ := json.MarshalIndent(ds, "", "\t")
	cmd_util.WriteToCocaFile("godeps.json", string(cModel))
}
P
Phodal Huang 已提交
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
func analysisJava() {
	importPath := analysisCmdConfig.Path
	identifierApp := analysis.NewJavaIdentifierApp()
	iNodes := identifierApp.AnalysisPath(importPath)

	identModel, _ := json.MarshalIndent(iNodes, "", "\t")
	cmd_util.WriteToCocaFile("identify.json", string(identModel))

	var classes []string = nil

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

	callApp := analysis.NewJavaFullApp()

	callNodes := callApp.AnalysisPath(importPath, classes, iNodes)
	cModel, _ := json.MarshalIndent(callNodes, "", "\t")
	cmd_util.WriteToCocaFile("deps.json", string(cModel))
P
Phodal HUANG 已提交
131 132 133
}

func init() {
P
Phodal Huang 已提交
134 135
	rootCmd.AddCommand(analysisCmd)

P
Phodal Huang 已提交
136
	analysisCmd.PersistentFlags().StringVarP(&analysisCmdConfig.Path, "path", "p", ".", "example -p core/main")
137
	analysisCmd.PersistentFlags().StringVarP(&analysisCmdConfig.Lang, "lang", "l", "java", "coca analysis -l java")
138
	analysisCmd.PersistentFlags().BoolVarP(&analysisCmdConfig.ForceUpdate, "force", "f", false, "force update -f")
P
Phodal HUANG 已提交
139
}