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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package packagestest
import (
"archive/zip"
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"golang.org/x/tools/go/packages"
)
// Modules is the exporter that produces module layouts.
// Each "repository" is put in it's own module, and the module file generated
// will have replace directives for all other modules.
// Given the two files
// golang.org/repoa#a/a.go
// golang.org/repob#b/b.go
// You would get the directory layout
// /sometemporarydirectory
// ├── repoa
// │ ├── a
// │ │ └── a.go
// │ └── go.mod
// └── repob
// ├── b
// │ └── b.go
// └── go.mod
// and the working directory would be
// /sometemporarydirectory/repoa
var Modules = modules{}
type modules struct{}
func (modules) Name() string {
return "Modules"
}
func (modules) Filename(exported *Exported, module, fragment string) string {
if module == exported.primary {
return filepath.Join(primaryDir(exported), fragment)
}
return filepath.Join(moduleDir(exported, module), fragment)
}
func (modules) Finalize(exported *Exported) error {
// Write out the primary module. This module can use symlinks and
// other weird stuff, and will be the working dir for the go command.
// It depends on all the other modules.
primaryDir := primaryDir(exported)
exported.Config.Dir = primaryDir
exported.written[exported.primary]["go.mod"] = filepath.Join(primaryDir, "go.mod")
primaryGomod := "module " + exported.primary + "\nrequire (\n"
for other := range exported.written {
if other == exported.primary {
continue
}
primaryGomod += fmt.Sprintf("\t%v %v\n", other, moduleVersion(other))
}
primaryGomod += ")\n"
if err := ioutil.WriteFile(filepath.Join(primaryDir, "go.mod"), []byte(primaryGomod), 0644); err != nil {
return err
}
// Create the mod cache so we can rename it later, even if we don't need it.
if err := os.MkdirAll(modCache(exported), 0755); err != nil {
return err
}
// Write out the go.mod files for the other modules.
for module, files := range exported.written {
if module == exported.primary {
continue
}
dir := moduleDir(exported, module)
modfile := filepath.Join(dir, "go.mod")
if err := ioutil.WriteFile(modfile, []byte("module "+module+"\n"), 0644); err != nil {
return err
}
files["go.mod"] = modfile
}
// Zip up all the secondary modules into the proxy dir.
proxyDir := filepath.Join(exported.temp, "modproxy")
for module, files := range exported.written {
if module == exported.primary {
continue
}
dir := filepath.Join(proxyDir, module, "@v")
if err := writeModuleProxy(dir, module, files); err != nil {
return fmt.Errorf("creating module proxy dir for %v: %v", module, err)
}
}
// Discard the original mod cache dir, which contained the files written
// for us by Export.
if err := os.Rename(modCache(exported), modCache(exported)+".orig"); err != nil {
return err
}
exported.Config.Env = append(exported.Config.Env,
"GO111MODULE=on",
"GOPATH="+filepath.Join(exported.temp, "modcache"),
"GOPROXY=file://"+filepath.ToSlash(proxyDir))
// Run go mod download to recreate the mod cache dir with all the extra
// stuff in cache. All the files created by Export should be recreated.
if err := invokeGo(exported.Config, "mod", "download"); err != nil {
return err
}
return nil
}
// writeModuleProxy creates a directory in the proxy dir for a module.
func writeModuleProxy(dir, module string, files map[string]string) error {
ver := moduleVersion(module)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
// list file. Just the single version.
if err := ioutil.WriteFile(filepath.Join(dir, "list"), []byte(ver+"\n"), 0644); err != nil {
return err
}
// go.mod, copied from the file written in Finalize.
modContents, err := ioutil.ReadFile(files["go.mod"])
if err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(dir, ver+".mod"), modContents, 0644); err != nil {
return err
}
// info file, just the bare bones.
infoContents := []byte(fmt.Sprintf(`{"Version": "%v", "Time":"2017-12-14T13:08:43Z"}`, ver))
if err := ioutil.WriteFile(filepath.Join(dir, ver+".info"), infoContents, 0644); err != nil {
return err
}
// zip of all the source files.
f, err := os.OpenFile(filepath.Join(dir, ver+".zip"), os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
z := zip.NewWriter(f)
for name, path := range files {
zf, err := z.Create(module + "@" + ver + "/" + name)
if err != nil {
return err
}
contents, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if _, err := zf.Write(contents); err != nil {
return err
}
}
if err := z.Close(); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return nil
}
func invokeGo(cfg *packages.Config, args ...string) error {
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
cmd := exec.Command("go", args...)
cmd.Env = append(append([]string{}, cfg.Env...), "PWD="+cfg.Dir)
cmd.Dir = cfg.Dir
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("go %v: %s: %s", args, err, stderr)
}
return nil
}
func modCache(exported *Exported) string {
return filepath.Join(exported.temp, "modcache/pkg/mod")
}
func primaryDir(exported *Exported) string {
return filepath.Join(exported.temp, "primarymod", path.Base(exported.primary))
}
func moduleDir(exported *Exported, module string) string {
return filepath.Join(modCache(exported), path.Dir(module), path.Base(module)+"@"+moduleVersion(module))
}
var versionSuffixRE = regexp.MustCompile(`v\d+`)
func moduleVersion(module string) string {
if versionSuffixRE.MatchString(path.Base(module)) {
return path.Base(module) + ".0.0"
}
return "v1.0.0"
}
|