-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdepgraph.go
More file actions
170 lines (140 loc) 路 3.58 KB
/
Copy pathdepgraph.go
File metadata and controls
170 lines (140 loc) 路 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"regexp"
"runtime"
"sync"
"github.com/henryhale/depgraph/cmd"
"github.com/henryhale/depgraph/internal/graph"
"github.com/henryhale/depgraph/internal/lang"
"github.com/henryhale/depgraph/internal/output"
"github.com/henryhale/depgraph/internal/util"
"golang.org/x/sync/errgroup"
)
// command name
const Name string = "depgraph"
// version number
var version = "(untracked)"
func main() {
// setup a logger
log.SetPrefix(Name + ": ")
log.SetFlags(0)
// parse command line flags
config := cmd.ParseConfig()
// help
if *config.ShowHelp {
fmt.Print("Usage: ", Name, " [options]\n\n")
fmt.Println("Options:")
flag.PrintDefaults()
os.Exit(0)
}
// version
if *config.ShowVersion {
fmt.Println(Name, "version", version)
os.Exit(0)
}
// select language
if len(*config.Lang) == 0 {
log.Fatal("programming language not specified")
}
pl, found := lang.Get(*config.Lang)
if !found {
log.Fatal("'" + *config.Lang + "' language is not yet supported")
}
// check output format
if !output.FormatSupported(config.OutputFormat) {
log.Fatal("'" + *config.OutputFormat + "' output format is not supported")
}
// read target directory
// - filter out ignored file paths
// - verify file extensions
files, err := util.TraverseDirectory(config.Dir, &pl.Extensions, &config.IgnoredPaths)
if err != nil {
log.Fatal(err)
}
// build deps map - analyze each file concurrently
deps := make(graph.DependencyGraph)
var mu sync.Mutex // protect deps map
external := make(map[string][]string)
allImports := make(map[string][]string)
var impMu sync.Mutex // protect allImports map
g, _ := errgroup.WithContext(context.Background())
g.SetLimit(runtime.NumCPU()) // limit concurrency
for _, filePath := range *files {
filePath := filePath // capture loop variable
g.Go(func() error {
result := lang.SourceFile{
Imports: make(map[string][]string),
Exports: []string{},
Local: true,
}
extractorOptions := new(lang.ExtractorOptions)
extractorOptions.Replacers = &config.ReplacePaths
extractorOptions.Result = &result
extractorOptions.File = &filePath
fileContent, err := os.ReadFile(filePath)
if err != nil {
return err
}
sourceCode := util.Preprocess(string(fileContent), pl.Comments)
for _, rule := range pl.Rules {
re := regexp.MustCompile(rule.RegExp)
matches := re.FindAllStringSubmatch(sourceCode, -1)
if matches == nil {
continue
}
extractorOptions.Rule = &rule
for _, match := range matches {
extractorOptions.Match = &match
pl.Extract(extractorOptions)
}
}
mu.Lock()
deps[filePath] = result
mu.Unlock()
// collect all imports
impMu.Lock()
for importpath, items := range result.Imports {
if existing, ok := allImports[importpath]; ok {
allImports[importpath] = append(existing, items...)
} else {
allImports[importpath] = items
}
}
impMu.Unlock()
return nil
})
}
if err := g.Wait(); err != nil {
log.Fatal(err)
}
// build externals
for path, items := range allImports {
if _, exists := deps[path]; !exists {
external[path] = items
}
}
// add externals
for path, exports := range external {
deps[path] = lang.SourceFile{
Imports: make(map[string][]string),
Exports: exports,
Local: false,
}
}
// produce formatted output
output := output.Format(config.OutputFormat, &deps)
// done!
if *config.OutputFile == "stdout" {
fmt.Println(output)
} else {
err := os.WriteFile(*config.OutputFile, []byte(output), 0644)
if err != nil {
log.Fatal(err)
}
}
}