forked from goplus/gop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimp.go
135 lines (124 loc) · 3.44 KB
/
imp.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
/*
* 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 (
"go/token"
"go/types"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/goplus/gox/packages"
"github.com/goplus/mod/env"
"github.com/goplus/mod/gopmod"
"github.com/goplus/mod/modfetch"
)
// -----------------------------------------------------------------------------
type Importer struct {
impFrom *packages.Importer
mod *gopmod.Module
gop *env.Gop
fset *token.FileSet
flags GenFlags
}
func NewImporter(mod *gopmod.Module, gop *env.Gop, fset *token.FileSet) *Importer {
const (
defaultFlags = GenFlagPrompt | GenFlagPrintError
)
if mod == nil {
mod = gopmod.Default
}
dir := ""
if hasModfile(mod) {
dir = mod.Root()
}
impFrom := packages.NewImporter(fset, dir)
return &Importer{mod: mod, gop: gop, impFrom: impFrom, fset: fset, flags: defaultFlags}
}
func (p *Importer) Import(pkgPath string) (pkg *types.Package, err error) {
const (
gop = "github.com/goplus/gop"
)
if strings.HasPrefix(pkgPath, gop) {
if suffix := pkgPath[len(gop):]; suffix == "" || suffix[0] == '/' {
gopRoot := p.gop.Root
if suffix == "/cl/internal/gop-in-go/foo" {
if err = p.genGoExtern(gopRoot+suffix, false); err != nil {
return
}
}
return p.impFrom.ImportFrom(pkgPath, gopRoot, 0)
}
}
if mod := p.mod; hasModfile(mod) {
ret, e := mod.Lookup(pkgPath)
if e != nil {
return nil, e
}
switch ret.Type {
case gopmod.PkgtExtern:
isExtern := ret.Real.Version != ""
if isExtern {
if _, err = modfetch.Get(ret.Real.String()); err != nil {
return
}
}
modDir := ret.ModDir
goModfile := filepath.Join(modDir, "go.mod")
if _, e := os.Lstat(goModfile); e != nil { // no go.mod
os.Chmod(modDir, modWritable)
defer os.Chmod(modDir, modReadonly)
os.WriteFile(goModfile, defaultGoMod(ret.ModPath), 0644)
}
return p.impFrom.ImportFrom(pkgPath, ret.ModDir, 0)
case gopmod.PkgtModule, gopmod.PkgtLocal:
if err = p.genGoExtern(ret.Dir, false); err != nil {
return
}
case gopmod.PkgtStandard:
return p.impFrom.ImportFrom(pkgPath, p.gop.Root, 0)
}
}
return p.impFrom.Import(pkgPath)
}
func (p *Importer) genGoExtern(dir string, isExtern bool) (err error) {
genfile := filepath.Join(dir, autoGenFile)
if _, err = os.Lstat(genfile); err != nil { // no gop_autogen.go
if isExtern {
os.Chmod(dir, modWritable)
defer os.Chmod(dir, modReadonly)
}
gen := false
err = genGoIn(dir, &Config{Gop: p.gop, Importer: p, Fset: p.fset}, false, p.flags, &gen)
if err != nil {
return
}
if gen {
cmd := exec.Command("go", "mod", "tidy")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dir
err = cmd.Run()
}
}
return
}
func defaultGoMod(modPath string) []byte {
return []byte(`module ` + modPath + `
go 1.16
`)
}
// -----------------------------------------------------------------------------