forked from goplus/gop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
222 lines (201 loc) · 5.37 KB
/
load.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gop
import (
"fmt"
"go/token"
"go/types"
"io/fs"
"path/filepath"
"strings"
"syscall"
"github.com/goplus/gop/ast"
"github.com/goplus/gop/cl"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/x/gopenv"
"github.com/goplus/gox"
"github.com/goplus/mod/env"
"github.com/goplus/mod/gopmod"
"github.com/qiniu/x/errors"
)
type Config struct {
Gop *env.Gop
Fset *token.FileSet
Filter func(fs.FileInfo) bool
Importer types.Importer
DontUpdateGoMod bool
DontCheckModChanged bool
}
// -----------------------------------------------------------------------------
func NotFound(err error) bool {
return errors.Err(err) == syscall.ENOENT
}
func loadMod(dir string, gop *env.Gop, conf *Config) (mod *gopmod.Module, err error) {
mod, err = gopmod.Load(dir, 0)
if err != nil && !NotFound(err) {
err = errors.NewWith(err, `gopmod.Load(dir, 0`, -2, "gopmod.Load", dir, 0)
return
}
if mod != nil {
err = mod.RegisterClasses()
if err != nil {
err = errors.NewWith(err, `mod.RegisterClasses()`, -2, "(*gopmod.Module).RegisterClasses", mod)
return
}
if !conf.DontUpdateGoMod {
err = mod.UpdateGoMod(gop, !conf.DontCheckModChanged)
if err != nil {
err = errors.NewWith(err, `mod.UpdateGoMod(gop, !conf.DontCheckModChanged)`, -2, "(*gopmod.Module).UpdateGoMod", mod, gop, !conf.DontCheckModChanged)
}
}
return
}
return new(gopmod.Module), nil
}
func lookupPub(mod *gopmod.Module) func(pkgPath string) (pubfile string, err error) {
return func(pkgPath string) (pubfile string, err error) {
if mod.File == nil { // no go.mod/gop.mod file
return "", syscall.ENOENT
}
pkg, err := mod.Lookup(pkgPath)
if err == nil {
pubfile = filepath.Join(pkg.Dir, "c2go.a.pub")
}
return
}
}
// -----------------------------------------------------------------------------
func LoadDir(dir string, conf *Config, genTestPkg bool, promptGenGo ...bool) (out, test *gox.Package, err error) {
if conf == nil {
conf = new(Config)
}
fset := conf.Fset
if fset == nil {
fset = token.NewFileSet()
}
gop := conf.Gop
if gop == nil {
gop = gopenv.Get()
}
mod, err := loadMod(dir, gop, conf)
if err != nil {
return
}
pkgs, err := parser.ParseDirEx(fset, dir, parser.Config{
IsClass: mod.IsClass,
Filter: conf.Filter,
Mode: parser.ParseComments,
})
if err != nil {
return
}
if len(pkgs) == 0 {
return nil, nil, syscall.ENOENT
}
if promptGenGo != nil && promptGenGo[0] {
fmt.Printf("GenGo %v ...\n", dir)
}
imp := conf.Importer
if imp == nil {
imp = NewImporter(mod, gop, fset)
}
var pkgTest *ast.Package
var clConf = &cl.Config{
WorkingDir: dir,
Fset: fset,
Importer: imp,
LookupClass: mod.LookupClass,
LookupPub: lookupPub(mod),
}
for name, pkg := range pkgs {
if strings.HasSuffix(name, "_test") {
if pkgTest != nil {
return nil, nil, errMultiTestPackges
}
pkgTest = pkg
continue
}
if out != nil {
return nil, nil, errMultiPackges
}
if len(pkg.Files) == 0 { // no Go+ source files
break
}
out, err = cl.NewPackage("", pkg, clConf)
if err != nil {
return
}
}
if out == nil {
return nil, nil, syscall.ENOENT
}
if pkgTest != nil && genTestPkg {
test, err = cl.NewPackage("", pkgTest, clConf)
}
return
}
// -----------------------------------------------------------------------------
func LoadFiles(files []string, conf *Config) (out *gox.Package, err error) {
if conf == nil {
conf = new(Config)
}
fset := conf.Fset
if fset == nil {
fset = token.NewFileSet()
}
gop := conf.Gop
if gop == nil {
gop = gopenv.Get()
}
mod, err := loadMod("", gop, conf)
if err != nil {
err = errors.NewWith(err, `loadMod("", gop, conf)`, -2, "gop.loadMod", "", gop, conf)
return
}
pkgs, err := parser.ParseFiles(fset, files, parser.ParseComments)
if err != nil {
err = errors.NewWith(err, `parser.ParseFiles(fset, files, parser.ParseComments)`, -2, "parser.ParseFiles", fset, files, parser.ParseComments)
return
}
if len(pkgs) != 1 {
err = errors.NewWith(errMultiPackges, `len(pkgs) != 1`, -1, "!=", len(pkgs), 1)
return
}
for _, pkg := range pkgs {
imp := conf.Importer
if imp == nil {
imp = NewImporter(mod, gop, fset)
}
clConf := &cl.Config{
Fset: fset,
Importer: imp,
LookupClass: mod.LookupClass,
LookupPub: lookupPub(mod),
}
out, err = cl.NewPackage("", pkg, clConf)
if err != nil {
err = errors.NewWith(err, `cl.NewPackage("", pkg, clConf)`, -2, "cl.NewPackage", "", pkg, clConf)
}
break
}
return
}
// -----------------------------------------------------------------------------
var (
errMultiPackges = errors.New("multiple packages")
errMultiTestPackges = errors.New("multiple test packages")
)
// -----------------------------------------------------------------------------