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

feat: add basic count logic

上级 1e9ed420
package cmd package cmd
import ( import (
"coca/src/models"
"coca/src/support"
"encoding/json"
"fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"log"
) )
type CountCmdConfig struct { type CountCmdConfig struct {
Refs bool Refs bool
DependencePath string
} }
var ( var (
countCmdConfig CountCmdConfig countCmdConfig CountCmdConfig
) )
var cparsedDeps []models.JClassNode
var countCmd *cobra.Command = &cobra.Command{ var countCmd *cobra.Command = &cobra.Command{
Use: "count", Use: "count",
Short: "count code", Short: "count code",
Long: ``, Long: ``,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
dependence := countCmdConfig.DependencePath
if dependence == "" {
return
}
file := support.ReadFile(dependence)
if file == nil {
log.Fatal("lost file:" + dependence)
}
_ = json.Unmarshal(file, &cparsedDeps)
var callMap = make(map[string]int)
for _, clz := range cparsedDeps {
for _, call := range clz.MethodCalls {
if callMap[call.Package+"."+call.Class+"."+call.MethodName] == 0 {
callMap[call.Package+"."+call.Class+"."+call.MethodName] = 1
} else {
callMap[call.Package+"."+call.Class+"."+call.MethodName]++
}
}
}
if countCmdConfig.Refs { callMapSort := support.RankByWordCount(callMap)
for _, count := range callMapSort {
fmt.Println(count.Value, count.Key)
} }
}, },
} }
...@@ -27,5 +59,6 @@ var countCmd *cobra.Command = &cobra.Command{ ...@@ -27,5 +59,6 @@ var countCmd *cobra.Command = &cobra.Command{
func init() { func init() {
rootCmd.AddCommand(countCmd) rootCmd.AddCommand(countCmd)
countCmd.PersistentFlags().BoolVarP(&countCmdConfig.Refs, "refs", "s", false, "count refs") countCmd.PersistentFlags().StringVarP(&countCmdConfig.DependencePath, "dependence", "d", "", "get dependence file")
//countCmd.PersistentFlags().BoolVarP(&countCmdConfig.Refs, "refs", "s", false, "count refs")
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册