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: AddRequire #50

Merged
merged 3 commits into from
Jan 31, 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
28 changes: 27 additions & 1 deletion modload/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,22 @@ func LoadFromEx(gomod, gopmod string, readFile func(string) ([]byte, error)) (p
return Module{f, opt}, nil
}

func (p Module) AddRequire(path, vers string, hasProj bool) error {
f := p.File
if err := f.AddRequire(path, vers); err != nil {
return err
}
for _, r := range f.Require {
if r.Mod.Path == path {
if !isClass(r) {
addClass(p.Opt, r)
}
break
}
}
return nil
}

func importClassfileFromGoMod(opt *modfile.File, f *gomodfile.File) {
for _, r := range f.Require {
if isClass(r) {
Expand All @@ -210,6 +226,16 @@ func importClassfileFromGoMod(opt *modfile.File, f *gomodfile.File) {
}
}

func addClass(opt *modfile.File, r *gomodfile.Require) {
if line := r.Syntax; line != nil {
line.Suffix = append(line.Suffix, modfile.Comment{
Token: "//gop:class", // without trailing newline
Suffix: true, // an end of line (not whole line) comment
})
opt.ClassMods = append(opt.ClassMods, r.Mod.Path)
}
}

func isClass(r *gomodfile.Require) bool {
if line := r.Syntax; line != nil {
for _, c := range line.Suffix {
Expand Down Expand Up @@ -372,7 +398,7 @@ func getVerb(e modfile.Expr) string {

const (
defaultGoVer = "1.18"
defaultGopVer = "1.1"
defaultGopVer = "1.2"
)

// Default represents the default gop.mod object.
Expand Down
93 changes: 93 additions & 0 deletions modload/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2024 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 modload

import (
"runtime"
"testing"

"github.com/goplus/mod/modfile"
gomodfile "golang.org/x/mod/modfile"
)

func TestEmpty(t *testing.T) {
mod := &Module{File: new(gomodfile.File), Opt: new(modfile.File)}
if mod.HasModfile() {
t.Fatal("mod.HasModfile")
}
if v := mod.Modfile(); v != "" {
t.Fatal("mod.Modfile:", v)
}
if v := mod.Root(); v != "" {
t.Fatal("mod.Root:", v)
}
if v := mod.Path(); v != "" {
t.Fatal("mod.Path:", v)
}
if mod.HasProject() {
t.Fatal("mod.HasProject")
}
if v := len(mod.Opt.ClassMods); v != 0 {
t.Fatal("len(mod.Opt.ClassMods):", v)
}
}

func TestCreate(t *testing.T) {
mod, err := Create("/foo/bar", "github.com/foo/bar", defaultGoVer, defaultGopVer)
if err != nil {
t.Fatal("Create failed:", err)
}
mod.AddRequire("github.com/goplus/yap", "v0.7.2", true)
if b, err := mod.File.Format(); err != nil {
t.Fatal("AddRequire & Format:", err)
} else if v := string(b); v != `module github.com/foo/bar

go 1.18

require github.com/goplus/yap v0.7.2 //gop:class
` {
t.Fatal("AddRequire:", v)
}

if !mod.HasModfile() {
t.Fatal("mod.HasModfile")
}
if runtime.GOOS != "windows" {
if v := mod.Modfile(); v != "/foo/bar/go.mod" {
t.Fatal("mod.Modfile:", v)
}
if v := mod.Root(); v != "/foo/bar" {
t.Fatal("mod.Root:", v)
}
}
if v := mod.Path(); v != "github.com/foo/bar" {
t.Fatal("mod.Path:", v)
}
if hasGopExtended(mod.Opt) {
t.Fatal("hasGopExtended?")
}
if mod.Projects() != nil {
t.Fatal("mod.Projects != nil?")
}
if v := len(mod.Opt.ClassMods); v == 0 {
t.Fatal("len(mod.Opt.ClassMods):", v)
}
mod.AddRequire("github.com/goplus/yap", "v0.7.2", true)
if v := len(mod.Opt.ClassMods); v != 1 {
t.Fatal("len(mod.Opt.ClassMods):", v)
}
}
Loading