-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
187 lines (173 loc) · 4.33 KB
/
files.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
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
// Copyright (c) 2022, The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/go/packages"
)
// LoadedPackageNames are single prefix names of packages that were
// loaded in the list of files to process
var LoadedPackageNames = map[string]bool{}
func IsGoFile(f fs.DirEntry) bool {
name := f.Name()
return !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go") && !f.IsDir()
}
func IsHLSLFile(f fs.DirEntry) bool {
name := f.Name()
return !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".hlsl") && !f.IsDir()
}
func IsSPVFile(f fs.DirEntry) bool {
name := f.Name()
return !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".spv") && !f.IsDir()
}
func AddFile(fn string, fls []string, procd map[string]bool) []string {
if _, has := procd[fn]; has {
return fls
}
fls = append(fls, fn)
procd[fn] = true
dir, _ := filepath.Split(fn)
if dir != "" {
dir = dir[:len(dir)-1]
pd, sd := filepath.Split(dir)
if pd != "" {
dir = sd
}
if !(dir == "math32") {
if _, has := LoadedPackageNames[dir]; !has {
LoadedPackageNames[dir] = true
// fmt.Printf("package: %s\n", dir)
}
}
}
return fls
}
// FilesFromPaths processes all paths and returns a full unique list of files
// for subsequent processing.
func FilesFromPaths(paths []string) []string {
fls := make([]string, 0, len(paths))
procd := make(map[string]bool)
for _, path := range paths {
switch info, err := os.Stat(path); {
case err != nil:
var pkgs []*packages.Package
dir, fl := filepath.Split(path)
if dir != "" && fl != "" && strings.HasSuffix(fl, ".go") {
pkgs, err = packages.Load(&packages.Config{Mode: packages.NeedName | packages.NeedFiles}, dir)
} else {
fl = ""
pkgs, err = packages.Load(&packages.Config{Mode: packages.NeedName | packages.NeedFiles}, path)
}
if err != nil {
fmt.Println(err)
continue
}
pkg := pkgs[0]
gofls := pkg.GoFiles
if len(gofls) == 0 {
fmt.Printf("WARNING: no go files found in path: %s\n", path)
}
if fl != "" {
for _, gf := range gofls {
if strings.HasSuffix(gf, fl) {
fls = AddFile(gf, fls, procd)
// fmt.Printf("added file: %s from package: %s\n", gf, path)
break
}
}
} else {
for _, gf := range gofls {
fls = AddFile(gf, fls, procd)
// fmt.Printf("added file: %s from package: %s\n", gf, path)
}
}
case !info.IsDir():
path := path
fls = AddFile(path, fls, procd)
default:
// Directories are walked, ignoring non-Go, non-HLSL files.
err := filepath.WalkDir(path, func(path string, f fs.DirEntry, err error) error {
if err != nil || !(IsGoFile(f) || IsHLSLFile(f)) {
return err
}
_, err = f.Info()
if err != nil {
return nil
}
fls = AddFile(path, fls, procd)
return nil
})
if err != nil {
log.Println(err)
}
}
}
return fls
}
func CopyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}
func CopySlrand() error {
hdr := "slrand.hlsl"
tofn := filepath.Join(*outDir, hdr)
pnm := "github.com/emer/gosl/v2/slrand"
pkgs, err := packages.Load(&packages.Config{Mode: packages.NeedName | packages.NeedFiles}, pnm)
if err != nil {
fmt.Println(err)
return err
}
if len(pkgs) != 1 {
err = fmt.Errorf("%s package not found", pnm)
fmt.Println(err)
return err
}
pkg := pkgs[0]
var fn string
if len(pkg.GoFiles) > 0 {
fn = pkg.GoFiles[0]
} else if len(pkg.OtherFiles) > 0 {
fn = pkg.GoFiles[0]
} else {
err = fmt.Errorf("No files found in package: %s", pnm)
fmt.Println(err)
return err
}
dir, _ := filepath.Split(fn)
// dir = filepath.Join(dir, "slrand")
fmfn := filepath.Join(dir, hdr)
CopyFile(fmfn, tofn)
return nil
}
// RemoveGenFiles removes .go, .hlsl, .spv files in shader generated dir
func RemoveGenFiles(dir string) {
err := filepath.WalkDir(dir, func(path string, f fs.DirEntry, err error) error {
if err != nil {
return err
}
if IsGoFile(f) || IsHLSLFile(f) || IsSPVFile(f) {
os.Remove(path)
}
return nil
})
if err != nil {
log.Println(err)
}
}