github.com/goplus/gop@v1.2.6/tidy.go (about) 1 /* 2 * Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package gop 18 19 import ( 20 "os" 21 "os/exec" 22 23 "github.com/goplus/mod/env" 24 "github.com/goplus/mod/gopmod" 25 "github.com/qiniu/x/errors" 26 ) 27 28 func Tidy(dir string, gop *env.Gop) (err error) { 29 modObj, err := gopmod.Load(dir) 30 if err != nil { 31 return errors.NewWith(err, `gopmod.Load(dir, mod.GopModOnly)`, -2, "gopmod.Load", dir) 32 } 33 34 modRoot := modObj.Root() 35 /* 36 depMods, err := GenDepMods(modObj, modRoot, true) 37 if err != nil { 38 return errors.NewWith(err, `GenDepMods(modObj, modRoot, true)`, -2, "gop.GenDepMods", modObj, modRoot, true) 39 } 40 41 old := modObj.DepMods() 42 for modPath := range old { 43 if _, ok := depMods[modPath]; !ok { // removed 44 modObj.DropRequire(modPath) 45 } 46 } 47 for modPath := range depMods { 48 if _, ok := old[modPath]; !ok { // added 49 if newMod, e := modfetch.Get(modPath); e != nil { 50 return errors.NewWith(e, `modfetch.Get(modPath)`, -1, "modfetch.Get", modPath) 51 } else { 52 modObj.AddRequire(newMod.Path, newMod.Version) 53 } 54 } 55 } 56 57 modObj.Cleanup() 58 err = modObj.Save() 59 if err != nil { 60 return errors.NewWith(err, `modObj.Save()`, -2, "(*gopmod.Module).Save") 61 } 62 */ 63 conf := &Config{Gop: gop} 64 err = genGoDir(modRoot, conf, true, true, 0) 65 if err != nil { 66 return errors.NewWith(err, `genGoDir(modRoot, conf, true, true)`, -2, "gop.genGoDir", modRoot, conf, true, true) 67 } 68 69 cmd := exec.Command("go", "mod", "tidy") 70 cmd.Stdout = os.Stdout 71 cmd.Stderr = os.Stderr 72 cmd.Dir = modRoot 73 err = cmd.Run() 74 if err != nil { 75 err = errors.NewWith(err, `cmd.Run()`, -2, "(*exec.Cmd).Run") 76 } 77 return 78 }