Skip to content

Commit

Permalink
feat: add merge header
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 28, 2019
1 parent 2cd4cdf commit ecd0d82
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 150 deletions.
12 changes: 10 additions & 2 deletions cmd/arch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"github.com/phodal/coca/config"
"github.com/phodal/coca/core/adapter"
"github.com/phodal/coca/core/domain/arch"
"github.com/phodal/coca/core/domain/arch/tequila"
"github.com/spf13/cobra"
"os"
"strings"
)

type ArchCmdConfig struct {
DependencePath string
IsMergePackage bool
}

var (
Expand All @@ -29,7 +31,7 @@ var archCmd = &cobra.Command{

parsedDeps := cmd_util.GetDepsFromJson(archCmdConfig.DependencePath)
archApp := arch.NewArchApp()
dotContent := archApp.Analysis(parsedDeps, identifiersMap)
result := archApp.Analysis(parsedDeps, identifiersMap)

ignores := strings.Split("", ",")
var nodeFilter = func(key string) bool {
Expand All @@ -41,7 +43,12 @@ var archCmd = &cobra.Command{
return false
}

graph := dotContent.ToDot(".", nodeFilter)

if archCmdConfig.IsMergePackage {
result = result.MergeHeaderFile(tequila.MergePackageFunc)
}

graph := result.ToDot(".", nodeFilter)
f, _ := os.Create("coca_reporter/arch.dot")
w := bufio.NewWriter(f)
w.WriteString("di" + graph.String())
Expand All @@ -55,4 +62,5 @@ func init() {
rootCmd.AddCommand(archCmd)

archCmd.PersistentFlags().StringVarP(&archCmdConfig.DependencePath, "dependence", "d", config.CocaConfig.ReporterPath+"/deps.json", "get dependence file")
archCmd.PersistentFlags().BoolVarP(&archCmdConfig.IsMergePackage, "mergePackage", "P", false, "merge package/folder for include dependencies")
}
12 changes: 6 additions & 6 deletions core/domain/arch/arch_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func NewArchApp() ArchApp {
return *&ArchApp{}
}

func (a ArchApp) Analysis(deps []models.JClassNode, identifiersMap map[string]models.JIdentifier) tequila.FullGraph {
fullGraph := tequila.FullGraph{
func (a ArchApp) Analysis(deps []models.JClassNode, identifiersMap map[string]models.JIdentifier) *tequila.FullGraph {
fullGraph := &tequila.FullGraph{
NodeList: make(map[string]string),
RelationList: make(map[string]*tequila.Relation),
}
Expand All @@ -36,12 +36,12 @@ func (a ArchApp) Analysis(deps []models.JClassNode, identifiersMap map[string]mo
fullGraph.RelationList[relation.From+"->"+relation.To] = relation
}

addCallInField(clz, src, fullGraph)
addExtend(clz, src, fullGraph)
addCallInMethod(clz, identifiersMap, src, fullGraph)
addCallInField(clz, src, *fullGraph)
addExtend(clz, src, *fullGraph)
addCallInMethod(clz, identifiersMap, src, *fullGraph)
}

return *&fullGraph
return fullGraph
}

func addCallInField(clz models.JClassNode, src string, fullGraph tequila.FullGraph) {
Expand Down
142 changes: 0 additions & 142 deletions core/domain/arch/tequila/incl_viz.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package tequila

import (
"fmt"
"github.com/awalterschulze/gographviz"
"io/ioutil"
"sort"
"strconv"
"strings"
Expand All @@ -26,24 +24,6 @@ type Fan struct {
FanOut int
}

func (f *FullGraph) FindCrossRef(merge func(string) string) []string {
mergedRelationMap := make(map[string]string)
result := make([]string, 0)
for key := range f.RelationList {
relation := f.RelationList[key]
mergedFrom := merge(relation.From)
mergedTo := merge(relation.To)
if mergedFrom == mergedTo {
continue
}
if _, ok := mergedRelationMap[mergedTo+mergedFrom]; ok {
result = append(result, mergedFrom+" <-> "+mergedTo)
}
mergedRelationMap[mergedFrom+mergedTo] = ""
}
return result
}

func (f *FullGraph) MergeHeaderFile(merge func(string) string) *FullGraph {
result := &FullGraph{
NodeList: make(map[string]string),
Expand Down Expand Up @@ -75,27 +55,6 @@ func (f *FullGraph) MergeHeaderFile(merge func(string) string) *FullGraph {
return result
}

func (f *FullGraph) EntryPoints(merge func(string) string) []string {
mergedGraph := f.MergeHeaderFile(merge)
fromMap := make(map[string]bool)
toMap := make(map[string]bool)
for key := range mergedGraph.RelationList {
relation := mergedGraph.RelationList[key]
if relation.From == "main" {
continue
}
fromMap[relation.From] = true
toMap[relation.To] = true
}
result := make([]string, 0)
for key := range fromMap {
if _, ok := toMap[key]; !ok {
result = append(result, key)
}
}
return result
}

func (f *FullGraph) SortedByFan(merge func(string) string) []*Fan {
mergedGraph := f.MergeHeaderFile(merge)
result := make([]*Fan, len(mergedGraph.NodeList))
Expand All @@ -120,78 +79,6 @@ func (f *FullGraph) SortedByFan(merge func(string) string) []*Fan {

var fullGraph *FullGraph

func parseRelation(edge *gographviz.Edge, nodes map[string]string) {
if _, ok := nodes[edge.Src]; ok {
if _, ok := nodes[edge.Dst]; ok {
dst := nodes[edge.Dst]
src := nodes[edge.Src]
dst = strings.ToLower(dst)
src = strings.ToLower(src)
relation := &Relation{
From: dst,
To: src,
Style: "\"solid\"",
}
fullGraph.RelationList[relation.From+"->"+relation.To] = relation
}
}
}

func filterDirectory(fullMethodName string) bool {
if strings.Contains(fullMethodName, "_test") {
return true
}

if strings.Contains(fullMethodName, "Test") {
return true
}

if strings.Contains(fullMethodName, "/Library/") {
return true
}
return false
}

func parseDotFile(codeDotfile string) {
fbuf, _ := ioutil.ReadFile(codeDotfile)
parseFromBuffer(fbuf)
}
func parseFromBuffer(fbuf []byte) {
g, err := gographviz.Read(fbuf)
if err != nil {
fmt.Println(string(fbuf))
}
nodes := make(map[string]string)
for _, node := range g.Nodes.Nodes {
fullMethodName := strings.Replace(node.Attrs["label"], "\"", "", 2)
if strings.Contains(fullMethodName, " ") {
tmp := strings.Split(fullMethodName, " ")
fullMethodName = tmp[len(tmp)-1]
}
if filterDirectory(fullMethodName) {
continue
}

methodName := formatMethodName(fullMethodName)
fullGraph.NodeList[methodName] = methodName
nodes[node.Name] = methodName
}
for key := range g.Edges.DstToSrcs {
for edgesKey := range g.Edges.DstToSrcs[key] {
for _, edge := range g.Edges.DstToSrcs[key][edgesKey] {
parseRelation(edge, nodes)
}
}
}
}
func formatMethodName(fullMethodName string) string {
methodName := strings.Replace(fullMethodName, "\\l", "", -1)
methodName = strings.Replace(methodName, "src/", "", -1)
methodName = strings.Replace(methodName, "include/", "", -1)
methodName = strings.ToLower(methodName)
return methodName
}

func (fullGraph *FullGraph) ToDot(split string, filter func(string) bool) *gographviz.Graph {
graph := gographviz.NewGraph()
graph.SetName("G")
Expand Down Expand Up @@ -260,32 +147,3 @@ func (fullGraph *FullGraph) ToDot(split string, filter func(string) bool) *gogra
var Foo = func() string {
return ""
}

func (fullGraph *FullGraph) ToDataSet(fileName string, split string, filter func(string) bool) {
nodes := make(map[string]string)

for nodeKey := range fullGraph.NodeList {
if filter(nodeKey) {
continue
}

nodes[nodeKey] = nodeKey
}

relMap := make(map[string][]string)
for key := range fullGraph.RelationList {
relation := fullGraph.RelationList[key]

if nodes[relation.From] == "" && nodes[relation.To] != "" {
if _, ok := relMap[relation.From]; !ok {
relMap[relation.From] = make([]string, 0)
}
relMap[relation.From] = append(relMap[relation.From], relation.To)
}
}

for key := range relMap {
tos := relMap[key]
fmt.Print("['" + strings.Join(tos, "','") + "'],")
}
}

0 comments on commit ecd0d82

Please sign in to comment.