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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
package nodot
import (
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"path/filepath"
"strings"
)
func ApplyNoDot(data []byte) ([]byte, error) {
sections, err := generateNodotSections()
if err != nil {
return nil, err
}
for _, section := range sections {
data = section.createOrUpdateIn(data)
}
return data, nil
}
type nodotSection struct {
name string
pkg string
declarations []string
types []string
}
func (s nodotSection) createOrUpdateIn(data []byte) []byte {
renames := map[string]string{}
contents := string(data)
lines := strings.Split(contents, "\n")
comment := "// Declarations for " + s.name
newLines := []string{}
for _, line := range lines {
if line == comment {
continue
}
words := strings.Split(line, " ")
lastWord := words[len(words)-1]
if s.containsDeclarationOrType(lastWord) {
renames[lastWord] = words[1]
continue
}
newLines = append(newLines, line)
}
if len(newLines[len(newLines)-1]) > 0 {
newLines = append(newLines, "")
}
newLines = append(newLines, comment)
for _, typ := range s.types {
name, ok := renames[s.prefix(typ)]
if !ok {
name = typ
}
newLines = append(newLines, fmt.Sprintf("type %s %s", name, s.prefix(typ)))
}
for _, decl := range s.declarations {
name, ok := renames[s.prefix(decl)]
if !ok {
name = decl
}
newLines = append(newLines, fmt.Sprintf("var %s = %s", name, s.prefix(decl)))
}
newLines = append(newLines, "")
newContents := strings.Join(newLines, "\n")
return []byte(newContents)
}
func (s nodotSection) prefix(declOrType string) string {
return s.pkg + "." + declOrType
}
func (s nodotSection) containsDeclarationOrType(word string) bool {
for _, declaration := range s.declarations {
if s.prefix(declaration) == word {
return true
}
}
for _, typ := range s.types {
if s.prefix(typ) == word {
return true
}
}
return false
}
func generateNodotSections() ([]nodotSection, error) {
sections := []nodotSection{}
declarations, err := getExportedDeclerationsForPackage("github.com/onsi/ginkgo", "ginkgo_dsl.go", "GINKGO_VERSION", "GINKGO_PANIC")
if err != nil {
return nil, err
}
sections = append(sections, nodotSection{
name: "Ginkgo DSL",
pkg: "ginkgo",
declarations: declarations,
types: []string{"Done", "Benchmarker"},
})
declarations, err = getExportedDeclerationsForPackage("github.com/onsi/gomega", "gomega_dsl.go", "GOMEGA_VERSION")
if err != nil {
return nil, err
}
sections = append(sections, nodotSection{
name: "Gomega DSL",
pkg: "gomega",
declarations: declarations,
})
declarations, err = getExportedDeclerationsForPackage("github.com/onsi/gomega", "matchers.go")
if err != nil {
return nil, err
}
sections = append(sections, nodotSection{
name: "Gomega Matchers",
pkg: "gomega",
declarations: declarations,
})
return sections, nil
}
func getExportedDeclerationsForPackage(pkgPath string, filename string, blacklist ...string) ([]string, error) {
pkg, err := build.Import(pkgPath, ".", 0)
if err != nil {
return []string{}, err
}
declarations, err := getExportedDeclarationsForFile(filepath.Join(pkg.Dir, filename))
if err != nil {
return []string{}, err
}
blacklistLookup := map[string]bool{}
for _, declaration := range blacklist {
blacklistLookup[declaration] = true
}
filteredDeclarations := []string{}
for _, declaration := range declarations {
if blacklistLookup[declaration] {
continue
}
filteredDeclarations = append(filteredDeclarations, declaration)
}
return filteredDeclarations, nil
}
func getExportedDeclarationsForFile(path string) ([]string, error) {
fset := token.NewFileSet()
tree, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
return []string{}, err
}
declarations := []string{}
ast.FileExports(tree)
for _, decl := range tree.Decls {
switch x := decl.(type) {
case *ast.GenDecl:
switch s := x.Specs[0].(type) {
case *ast.ValueSpec:
declarations = append(declarations, s.Names[0].Name)
}
case *ast.FuncDecl:
declarations = append(declarations, x.Name.Name)
}
}
return declarations, nil
}
|