Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modload.LoadFromEx #45

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions modfile/regtest_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
/*
* Copyright (c) 2021 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 modfile_test

import (
"os"
"testing"

"github.com/goplus/mod/gopmod"
"github.com/goplus/mod/modload"
)

func TestGopMod(t *testing.T) {
Expand All @@ -24,3 +42,30 @@ func TestGopMod(t *testing.T) {
t.Fatal("PkgType foo:", pt)
}
}

func TestLoadFromEx(t *testing.T) {
const gomodText = `
module github.com/goplus/community

go 1.18

require (
github.com/goplus/yap v0.5.0 //gop:class
github.com/qiniu/a v0.1.0
github.com/qiniu/x v1.13.2 // gop:class
)
`
const gomod = "go.mod"
mod, err := modload.LoadFromEx(gomod, "gop.mod", func(s string) ([]byte, error) {
if s == gomod {
return []byte(gomodText), nil
}
return nil, os.ErrNotExist
})
if err != nil {
t.Fatal("LoadFromEx:", err)
}
if n := len(mod.Opt.Import); n != 2 {
t.Fatal("len(mod.Opt.Import):", n)
}
}
37 changes: 32 additions & 5 deletions modload/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,21 @@ func Load(dir string) (p Module, err error) {

// LoadFrom loads a module from specified go.mod file and an optional gop.mod file.
func LoadFrom(gomod, gopmod string) (p Module, err error) {
data, err := os.ReadFile(gomod)
return LoadFromEx(gomod, gopmod, os.ReadFile)
}

// LoadFromEx loads a module from specified go.mod file and an optional gop.mod file.
// It can specify a customized `readFile` to read file content.
func LoadFromEx(gomod, gopmod string, readFile func(string) ([]byte, error)) (p Module, err error) {
data, err := readFile(gomod)
if err != nil {
err = errors.NewWith(err, `os.ReadFile(gomod)`, -2, "os.ReadFile", gomod)
err = errors.NewWith(err, `readFile(gomod)`, -2, "readFile", gomod)
return
}

var fixed bool
fix := fixVersion(&fixed)
f, err := gomodfile.Parse(gomod, data, fix)
f, err := gomodfile.ParseLax(gomod, data, fix)
if err != nil {
err = errors.NewWith(err, `gomodfile.Parse(gomod, data, fix)`, -2, "gomodfile.Parse", gomod, data, fix)
return
Expand All @@ -185,19 +191,40 @@ func LoadFrom(gomod, gopmod string) (p Module, err error) {
}

var opt *modfile.File
data, err = os.ReadFile(gopmod)
data, err = readFile(gopmod)
if err != nil {
opt = newGopMod(gopmod, defaultGopVer)
} else {
opt, err = modfile.Parse(gopmod, data, fix)
opt, err = modfile.ParseLax(gopmod, data, fix)
if err != nil {
err = errors.NewWith(err, `modfile.Parse(gopmod, data, fix)`, -2, "modfile.Parse", gopmod, data, fix)
return
}
}
importClassfileFromGoMod(opt, f)
return Module{f, opt}, nil
}

func importClassfileFromGoMod(opt *modfile.File, f *gomodfile.File) {
for _, r := range f.Require {
if isClass(r) {
opt.AddImport(r.Mod.Path)
}
}
}

func isClass(r *gomodfile.Require) bool {
if line := r.Syntax; line != nil {
for _, c := range line.Suffix {
text := strings.TrimLeft(c.Token[2:], " \t")
if strings.HasPrefix(text, "gop:class") {
return true
}
}
}
return false
}

// -----------------------------------------------------------------------------

func (p Module) Projects() []*modfile.Project {
Expand Down