-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
145 lines (137 loc) · 3.44 KB
/
main.go
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
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/build"
"go/format"
"go/parser"
"go/token"
"log"
"os"
"path"
"sort"
"strings"
"time"
)
func combinedImports(dirs []string) (results []string) {
all := make(map[string]struct{})
for _, dir := range dirs {
stat, err := os.Stat(dir)
if os.IsNotExist(err) || !stat.IsDir() {
log.Fatal("Specified directory does not exist:", dir)
}
imported, err := build.Default.ImportDir(dir, build.ImportComment)
if err != nil {
log.Fatal(err)
}
for _, imp := range imported.Imports {
all[imp] = struct{}{}
}
}
for imp := range all {
results = append(results, imp)
}
sort.Strings(results)
return results
}
type Config struct {
Dirs []string
Header string
}
func parseConfig() (result Config) {
var dirs string
flag.StringVar(&result.Header, "header", "",
"A file header to go in the package-level comment of the output file. (Maybe a version number?)")
flag.StringVar(&dirs, "dirs", "",
strings.Join([]string{
"The colon-separated list of directories containing go files to merge into a single file.",
"The package name of the first directory listed will be used as the package name of the output file.",
"It should go without saying that each of the packages should be independent of all the others.",
"It should also go without saying that there must be no name collisions between any of the packages.",
"All source code comments will be absent in the output file.",
"Go test files (those ending in '_test.go') are NOT included in output file.",
}, " "),
)
flag.Parse()
if len(dirs) == 0 {
log.Fatal("Usage: mergepkg -dirs '<colon-separated-listing>'")
}
result.Dirs = strings.Split(dirs, ":")
result.Header = strings.ReplaceAll(strings.TrimSpace(result.Header), "\n", "\n// ")
return result
}
func main() {
config := parseConfig()
output := new(bytes.Buffer)
emit := func(args ...any) { _, _ = fmt.Fprintln(output, args...) }
imports := combinedImports(config.Dirs)
for d, dir := range config.Dirs {
stat, err := os.Stat(dir)
if os.IsNotExist(err) || !stat.IsDir() {
log.Fatal("Specified directory does not exist:", dir)
}
listing, err := os.ReadDir(dir)
if err != nil {
log.Fatal("Cannot read specified directory.")
}
if d == 0 {
imported, err := build.Default.ImportDir(dir, build.ImportComment)
if err != nil {
log.Fatal(err)
}
emit("// Package", imported.Name, "info:", config.Header)
emit("// AUTO-GENERATED:", time.Now())
emit("package", imported.Name)
emit()
if len(imports) > 0 {
emit("import (")
for _, pkg := range imports {
emit(fmt.Sprintf("%#v", pkg))
}
emit(")")
emit()
}
}
for _, file := range listing {
if file.IsDir() {
continue
}
name := file.Name()
if strings.HasSuffix(name, "_test.go") {
continue
}
if !strings.HasSuffix(name, ".go") {
continue
}
emit()
emit("// FILE:", name)
emit()
filepath := path.Join(dir, name)
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filepath, nil, 0)
if err != nil {
log.Println(err)
continue
}
for _, declaration := range f.Decls {
g, ok := declaration.(*ast.GenDecl)
if ok && g.Tok == token.IMPORT {
continue
}
err = format.Node(output, fset, declaration)
emit()
if err != nil {
log.Println(err)
break
}
}
}
}
out, err := format.Source(output.Bytes())
if err != nil {
log.Fatal(err)
}
_, _ = os.Stdout.Write(out)
}