analysis.go 3.8 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"
P
Phodal Huang 已提交
8 9 10
	"github.com/phodal/coca/pkg/application/analysis/javaapp"
	"github.com/phodal/coca/pkg/application/analysis/pyapp"
	"github.com/phodal/coca/pkg/application/analysis/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) {
P
Phodal Huang 已提交
32 33
		var outputName string
		var ds []core_domain.CodeDataStruct
34 35
		switch analysisCmdConfig.Lang {
		case "go":
P
Phodal Huang 已提交
36 37
			ds = AnalysisGo()
			outputName = "godeps.json"
38
		case "py", "python":
P
Phodal Huang 已提交
39 40
			ds = AnalysisPython()
			outputName = "pydeps.json"
41
		case "ts", "typescript":
P
Phodal Huang 已提交
42 43
			ds = AnalysisTypeScript()
			outputName = "tsdeps.json"
44
		default:
P
Phodal Huang 已提交
45 46
			ds = AnalysisJava()
			outputName = "deps.json"
47
		}
P
Phodal Huang 已提交
48 49 50

		cModel, _ := json.MarshalIndent(ds, "", "\t")
		cmd_util.WriteToCocaFile(outputName, string(cModel))
51 52
	},
}
P
Phodal HUANG 已提交
53

P
Phodal Huang 已提交
54
func AnalysisTypeScript() []core_domain.CodeDataStruct {
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	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...)
	}

P
Phodal Huang 已提交
73
	return ds
74 75
}

P
Phodal Huang 已提交
76
func AnalysisPython() []core_domain.CodeDataStruct {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	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...)
	}

P
Phodal Huang 已提交
94
	return ds
95 96
}

P
Phodal Huang 已提交
97
func AnalysisGo() []core_domain.CodeDataStruct {
98
	importPath := analysisCmdConfig.Path
P
Phodal Huang 已提交
99

100 101 102 103 104 105
	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 已提交
106

107 108
		results = append(results, result)
	}
P
Phodal Huang 已提交
109

110 111 112 113
	var ds []core_domain.CodeDataStruct
	for _, result := range results {
		ds = append(ds, result.DataStructures...)
	}
P
Phodal Huang 已提交
114

P
Phodal Huang 已提交
115
	return ds
116
}
P
Phodal Huang 已提交
117

P
Phodal Huang 已提交
118
func AnalysisJava() []core_domain.CodeDataStruct {
119
	importPath := analysisCmdConfig.Path
P
Phodal Huang 已提交
120
	identifierApp := javaapp.NewJavaIdentifierApp()
121 122 123 124 125 126 127 128 129 130 131
	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)
	}

P
Phodal Huang 已提交
132
	callApp := javaapp.NewJavaFullApp()
133 134

	callNodes := callApp.AnalysisPath(importPath, classes, iNodes)
P
Phodal Huang 已提交
135
	return callNodes
P
Phodal HUANG 已提交
136 137 138
}

func init() {
P
Phodal Huang 已提交
139 140
	rootCmd.AddCommand(analysisCmd)

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