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

feat: add basic count logic

上级 1e9ed420
package cmd
import (
"coca/src/models"
"coca/src/support"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"log"
)
type CountCmdConfig struct {
Refs bool
DependencePath string
}
var (
countCmdConfig CountCmdConfig
)
var cparsedDeps []models.JClassNode
var countCmd *cobra.Command = &cobra.Command{
Use: "count",
Short: "count code",
Long: ``,
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{
func init() {
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.
先完成此消息的编辑!
想要评论请 注册