Skip to content

Commit

Permalink
gop/x/gopenv
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed May 16, 2022
1 parent f75027a commit 96465d7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 23 deletions.
9 changes: 5 additions & 4 deletions cmd/internal/gopget/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"syscall"

"github.com/goplus/gop/cmd/internal/base"
"github.com/goplus/gop/x/gopenv"
"github.com/goplus/mod/modfetch"
"github.com/goplus/mod/modload"
)
Expand Down Expand Up @@ -59,14 +60,14 @@ func runCmd(cmd *base.Command, args []string) {
}

func get(pkgPath string) {
mod, err := modload.Load(".")
mod, err := modload.Load(".", 0)
hasMod := (err != syscall.ENOENT)
if hasMod {
check(err)
check(mod.UpdateGoMod(true))
check(mod.UpdateGoMod(gopenv.Get(), true))
}
modPath, _ := splitPkgPath(pkgPath)
modVer, isClass, err := modfetch.Get(modPath)
modVer, isClass, err := modfetch.Get(gopenv.Get(), modPath)
check(err)
if hasMod {
if isClass {
Expand All @@ -76,7 +77,7 @@ func get(pkgPath string) {
check(mod.AddRequire(modVer.Path, modVer.Version))
fmt.Fprintf(os.Stderr, "gop get: added %s %s\n", modVer.Path, modVer.Version)
check(mod.Save())
check(mod.UpdateGoMod(false))
check(mod.UpdateGoMod(gopenv.Get(), false))
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/mod/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/goplus/gop/cmd/internal/base"
"github.com/goplus/gop/env"
"github.com/goplus/gop/x/mod/modload"
"github.com/goplus/mod/modload"
)

// gop mod init
Expand Down
7 changes: 4 additions & 3 deletions cmd/internal/mod/tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
"log"

"github.com/goplus/gop/cmd/internal/base"
"github.com/goplus/gop/x/mod/modload"
"github.com/goplus/gop/x/gopenv"
"github.com/goplus/mod/modload"
)

// gop mod tidy
Expand All @@ -34,9 +35,9 @@ func init() {
}

func runTidy(cmd *base.Command, args []string) {
mod, err := modload.Load(".")
mod, err := modload.Load(".", 0)
check(err)
check(mod.Tidy())
check(mod.Tidy(gopenv.Get()))
}

func check(err error) {
Expand Down
26 changes: 26 additions & 0 deletions x/gopenv/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 gopenv

import (
"github.com/goplus/gop/env"
"github.com/goplus/mod/modload"
)

func Get() *modload.GopEnv {
return &modload.GopEnv{Version: env.Version(), Root: env.GOPROOT()}
}
4 changes: 3 additions & 1 deletion x/gopmod/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"errors"
"syscall"

"github.com/goplus/gop/x/gopenv"
"github.com/goplus/mod/modcache"
"github.com/goplus/mod/modfetch"
"github.com/goplus/mod/modfile"
"github.com/goplus/mod/modload"
"golang.org/x/mod/module"
Expand Down Expand Up @@ -81,7 +83,7 @@ func (p *Module) registerMod(modPath string, regcls func(c *Class)) (err error)
if err != syscall.ENOENT {
return
}
mod, _, err = modGet(mod.String())
mod, _, err = modfetch.Get(gopenv.Get(), mod.String())
if err != nil {
return
}
Expand Down
8 changes: 2 additions & 6 deletions x/gopmod/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"syscall"
"time"

"github.com/goplus/gop/env"
"github.com/goplus/gop/token"
"github.com/goplus/gop/x/gopenv"
"github.com/goplus/mod/modcache"
"github.com/goplus/mod/modfetch"
"github.com/goplus/mod/modload"
Expand Down Expand Up @@ -201,17 +201,13 @@ func LoadMod(mod module.Version) (p *Module, err error) {
if err != syscall.ENOENT {
return
}
mod, _, err = modGet(mod.String())
mod, _, err = modfetch.Get(gopenv.Get(), mod.String())
if err != nil {
return
}
return loadModFrom(mod)
}

func modGet(modPath string) (mod module.Version, isClass bool, err error) {
return modfetch.Get(&modload.GopEnv{Version: env.Version(), Root: env.GOPROOT()}, modPath)
}

func loadModFrom(mod module.Version) (p *Module, err error) {
dir, err := modcache.Path(mod)
if err != nil {
Expand Down
10 changes: 2 additions & 8 deletions x/gopproj/gopproj.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ import (
"os"
"path/filepath"

"github.com/goplus/gop/env"
"github.com/goplus/gop/x/gengo"
"github.com/goplus/gop/x/gopenv"
"github.com/goplus/gop/x/gopprojs"
"github.com/goplus/mod/modcache"
"github.com/goplus/mod/modfetch"
"github.com/goplus/mod/modload"
"golang.org/x/mod/module"
)

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -100,7 +98,7 @@ func OpenDir(flags int, dir string) (ctx *Context, proj *Project, err error) {

func OpenPkgPath(flags int, pkgPath string) (ctx *Context, proj *Project, err error) {
modPath, leftPart := splitPkgPath(pkgPath)
modVer, _, err := modGet(modPath)
modVer, _, err := modfetch.Get(gopenv.Get(), modPath)
if err != nil {
return
}
Expand All @@ -111,10 +109,6 @@ func OpenPkgPath(flags int, pkgPath string) (ctx *Context, proj *Project, err er
return OpenDir(flags, dir+leftPart)
}

func modGet(modPath string) (mod module.Version, isClass bool, err error) {
return modfetch.Get(&modload.GopEnv{Version: env.Version(), Root: env.GOPROOT()}, modPath)
}

func splitPkgPath(pkgPath string) (modPath, leftPart string) {
return pkgPath, ""
}
Expand Down

0 comments on commit 96465d7

Please sign in to comment.