rcall.go 1.5 KB
Newer Older
P
Phodal Huang 已提交
1 2 3
package cmd

import (
P
Phodal Huang 已提交
4 5 6
	"encoding/json"
	"fmt"
	"github.com/phodal/coca/cmd/cmd_util"
P
Phodal Huang 已提交
7
	"github.com/phodal/coca/cmd/config"
P
Phodal Huang 已提交
8
	"github.com/phodal/coca/core/context/rcall"
P
Phodal Huang 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
	"github.com/spf13/cobra"
	"log"
	"strings"
)

type ReverseCmdConfig struct {
	DependencePath string
	ClassName      string
	RemovePackage  string
}

var (
	reverseConfig ReverseCmdConfig
)

var reverseCmd = &cobra.Command{
	Use:   "rcall",
P
Phodal Huang 已提交
26
	Short: "reverse call graph visualization",
P
Phodal Huang 已提交
27 28 29 30 31 32 33 34 35 36 37
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		dependence := *&reverseConfig.DependencePath
		className := *&reverseConfig.ClassName
		remove := *&reverseConfig.RemovePackage

		if className == "" {
			log.Fatal("lost ClassName")
		}

		analyser := rcall.NewRCallGraph()
P
Phodal Huang 已提交
38
		file := cmd_util.ReadFile(dependence)
P
Phodal Huang 已提交
39 40 41 42 43 44
		if file == nil {
			log.Fatal("lost file:" + dependence)
		}

		_ = json.Unmarshal(file, &parsedDeps)

P
Phodal Huang 已提交
45
		fmt.Println("start rcall class :", className)
P
Phodal Huang 已提交
46 47 48 49 50 51
		content := analyser.Analysis(className, *&parsedDeps)

		if remove != "" {
			content = strings.ReplaceAll(content, remove, "")
		}

P
Phodal Huang 已提交
52
		cmd_util.WriteToCocaFile("rcall.dot", content)
P
Phodal Huang 已提交
53
		cmd_util.ConvertToSvg("call")
P
Phodal Huang 已提交
54 55 56 57 58 59
	},
}

func init() {
	rootCmd.AddCommand(reverseCmd)

P
Phodal Huang 已提交
60
	reverseCmd.PersistentFlags().StringVarP(&reverseConfig.RemovePackage, "remove", "r", "", "remove package name")
P
Phodal Huang 已提交
61 62 63
	reverseCmd.PersistentFlags().StringVarP(&reverseConfig.ClassName, "className", "c", "", "path")
	reverseCmd.PersistentFlags().StringVarP(&reverseConfig.DependencePath, "dependence", "d", config.CocaConfig.ReporterPath+"/deps.json", "get dependence file")
}