2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-08-18 12:57:10 -07:00
|
|
|
|
|
|
|
// +build ignore
|
|
|
|
|
2014-08-18 12:38:22 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2014-12-22 02:52:09 -07:00
|
|
|
"crypto/md5"
|
2014-08-18 12:38:22 -07:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-10-13 11:13:42 -07:00
|
|
|
"time"
|
2014-08-18 12:38:22 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
versionRe = regexp.MustCompile(`-[0-9]{1,3}-g[0-9a-f]{5,10}`)
|
|
|
|
goarch string
|
|
|
|
goos string
|
|
|
|
noupgrade bool
|
2014-10-13 11:10:32 -07:00
|
|
|
version string
|
2014-11-29 14:17:54 -07:00
|
|
|
race bool
|
2014-08-18 12:38:22 -07:00
|
|
|
)
|
|
|
|
|
2014-08-19 02:04:20 -07:00
|
|
|
const minGoVersion = 1.3
|
|
|
|
|
2014-08-18 12:38:22 -07:00
|
|
|
func main() {
|
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
|
|
|
if os.Getenv("GOPATH") == "" {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
gopath := filepath.Clean(filepath.Join(cwd, "../../../../"))
|
|
|
|
log.Println("GOPATH is", gopath)
|
|
|
|
os.Setenv("GOPATH", gopath)
|
|
|
|
}
|
|
|
|
os.Setenv("PATH", fmt.Sprintf("%s%cbin%c%s", os.Getenv("GOPATH"), os.PathSeparator, os.PathListSeparator, os.Getenv("PATH")))
|
|
|
|
|
|
|
|
flag.StringVar(&goarch, "goarch", runtime.GOARCH, "GOARCH")
|
|
|
|
flag.StringVar(&goos, "goos", runtime.GOOS, "GOOS")
|
2014-11-29 14:17:54 -07:00
|
|
|
flag.BoolVar(&noupgrade, "no-upgrade", noupgrade, "Disable upgrade functionality")
|
2014-10-13 11:10:32 -07:00
|
|
|
flag.StringVar(&version, "version", getVersion(), "Set compiled in version string")
|
2014-11-29 14:17:54 -07:00
|
|
|
flag.BoolVar(&race, "race", race, "Use race detector")
|
2014-08-18 12:38:22 -07:00
|
|
|
flag.Parse()
|
|
|
|
|
2014-08-25 05:29:07 -07:00
|
|
|
switch goarch {
|
2015-01-09 02:21:30 -07:00
|
|
|
case "386", "amd64", "arm":
|
2014-08-25 05:29:07 -07:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
log.Printf("Unknown goarch %q; proceed with caution!", goarch)
|
|
|
|
}
|
|
|
|
|
2014-08-19 02:04:20 -07:00
|
|
|
checkRequiredGoVersion()
|
|
|
|
|
2014-08-18 12:38:22 -07:00
|
|
|
if flag.NArg() == 0 {
|
2014-11-17 08:02:41 -07:00
|
|
|
var tags []string
|
|
|
|
if noupgrade {
|
|
|
|
tags = []string{"noupgrade"}
|
|
|
|
}
|
|
|
|
install("./cmd/...", tags)
|
2015-04-28 13:32:10 -07:00
|
|
|
|
|
|
|
vet("./cmd/syncthing")
|
|
|
|
vet("./internal/...")
|
|
|
|
lint("./cmd/syncthing")
|
|
|
|
lint("./internal/...")
|
2014-08-18 12:38:22 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:07:13 -07:00
|
|
|
for _, cmd := range flag.Args() {
|
|
|
|
switch cmd {
|
|
|
|
case "setup":
|
|
|
|
setup()
|
2014-08-18 12:38:22 -07:00
|
|
|
|
2014-09-28 04:07:13 -07:00
|
|
|
case "install":
|
|
|
|
pkg := "./cmd/..."
|
2014-11-17 08:01:16 -07:00
|
|
|
var tags []string
|
|
|
|
if noupgrade {
|
|
|
|
tags = []string{"noupgrade"}
|
|
|
|
}
|
|
|
|
install(pkg, tags)
|
2014-08-18 12:38:22 -07:00
|
|
|
|
2014-09-28 04:07:13 -07:00
|
|
|
case "build":
|
|
|
|
pkg := "./cmd/syncthing"
|
|
|
|
var tags []string
|
|
|
|
if noupgrade {
|
|
|
|
tags = []string{"noupgrade"}
|
|
|
|
}
|
|
|
|
build(pkg, tags)
|
2014-08-18 12:38:22 -07:00
|
|
|
|
2014-09-28 04:07:13 -07:00
|
|
|
case "test":
|
2015-04-28 13:32:10 -07:00
|
|
|
test("./...")
|
2014-08-18 12:38:22 -07:00
|
|
|
|
2014-09-28 04:07:13 -07:00
|
|
|
case "assets":
|
|
|
|
assets()
|
2014-08-18 12:38:22 -07:00
|
|
|
|
2014-09-28 04:07:13 -07:00
|
|
|
case "xdr":
|
|
|
|
xdr()
|
2014-08-18 12:38:22 -07:00
|
|
|
|
2014-09-28 04:07:13 -07:00
|
|
|
case "translate":
|
|
|
|
translate()
|
2014-08-18 12:38:22 -07:00
|
|
|
|
2014-09-28 04:07:13 -07:00
|
|
|
case "transifex":
|
|
|
|
transifex()
|
2014-08-18 12:38:22 -07:00
|
|
|
|
2014-09-28 04:07:13 -07:00
|
|
|
case "deps":
|
|
|
|
deps()
|
2014-08-18 12:38:22 -07:00
|
|
|
|
2014-09-28 04:07:13 -07:00
|
|
|
case "tar":
|
|
|
|
buildTar()
|
2014-08-18 12:38:22 -07:00
|
|
|
|
2014-09-28 04:07:13 -07:00
|
|
|
case "zip":
|
|
|
|
buildZip()
|
2014-08-18 12:38:22 -07:00
|
|
|
|
2015-05-11 09:39:53 -07:00
|
|
|
case "deb":
|
|
|
|
buildDeb()
|
|
|
|
|
2014-09-28 04:07:13 -07:00
|
|
|
case "clean":
|
|
|
|
clean()
|
2014-08-18 12:38:22 -07:00
|
|
|
|
2015-04-28 13:32:10 -07:00
|
|
|
case "vet":
|
|
|
|
vet("./cmd/syncthing")
|
|
|
|
vet("./internal/...")
|
|
|
|
|
|
|
|
case "lint":
|
|
|
|
lint("./cmd/syncthing")
|
|
|
|
lint("./internal/...")
|
|
|
|
|
2014-09-28 04:07:13 -07:00
|
|
|
default:
|
|
|
|
log.Fatalf("Unknown command %q", cmd)
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
2014-08-19 02:04:20 -07:00
|
|
|
func checkRequiredGoVersion() {
|
|
|
|
ver := run("go", "version")
|
|
|
|
re := regexp.MustCompile(`go version go(\d+\.\d+)`)
|
|
|
|
if m := re.FindSubmatch(ver); len(m) == 2 {
|
|
|
|
vs := string(m[1])
|
|
|
|
// This is a standard go build. Verify that it's new enough.
|
|
|
|
f, err := strconv.ParseFloat(vs, 64)
|
|
|
|
if err != nil {
|
2014-12-01 02:24:13 -07:00
|
|
|
log.Printf("*** Couldn't parse Go version out of %q.\n*** This isn't known to work, proceed on your own risk.", vs)
|
2014-08-19 02:04:20 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if f < minGoVersion {
|
|
|
|
log.Fatalf("*** Go version %.01f is less than required %.01f.\n*** This is known not to work, not proceeding.", f, minGoVersion)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("*** Unknown Go version %q.\n*** This isn't known to work, proceed on your own risk.", ver)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 12:38:22 -07:00
|
|
|
func setup() {
|
2014-11-29 16:17:00 -07:00
|
|
|
runPrint("go", "get", "-v", "golang.org/x/tools/cmd/cover")
|
|
|
|
runPrint("go", "get", "-v", "golang.org/x/tools/cmd/vet")
|
|
|
|
runPrint("go", "get", "-v", "golang.org/x/net/html")
|
2014-08-18 12:38:22 -07:00
|
|
|
runPrint("go", "get", "-v", "github.com/tools/godep")
|
|
|
|
}
|
|
|
|
|
|
|
|
func test(pkg string) {
|
2014-09-28 04:07:13 -07:00
|
|
|
setBuildEnv()
|
2014-11-30 13:21:37 -07:00
|
|
|
runPrint("go", "test", "-short", "-timeout", "60s", pkg)
|
2014-08-18 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
2014-11-17 08:01:16 -07:00
|
|
|
func install(pkg string, tags []string) {
|
2014-08-18 12:38:22 -07:00
|
|
|
os.Setenv("GOBIN", "./bin")
|
2014-11-17 08:01:16 -07:00
|
|
|
args := []string{"install", "-v", "-ldflags", ldflags()}
|
|
|
|
if len(tags) > 0 {
|
|
|
|
args = append(args, "-tags", strings.Join(tags, ","))
|
|
|
|
}
|
2014-11-29 14:17:54 -07:00
|
|
|
if race {
|
|
|
|
args = append(args, "-race")
|
|
|
|
}
|
2014-11-17 08:01:16 -07:00
|
|
|
args = append(args, pkg)
|
2014-08-23 01:52:12 -07:00
|
|
|
setBuildEnv()
|
2014-11-17 08:01:16 -07:00
|
|
|
runPrint("go", args...)
|
2014-08-18 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func build(pkg string, tags []string) {
|
2014-12-22 02:52:09 -07:00
|
|
|
binary := "syncthing"
|
|
|
|
if goos == "windows" {
|
|
|
|
binary += ".exe"
|
|
|
|
}
|
|
|
|
|
|
|
|
rmr(binary, binary+".md5")
|
2014-09-28 04:07:13 -07:00
|
|
|
args := []string{"build", "-ldflags", ldflags()}
|
2014-08-18 12:38:22 -07:00
|
|
|
if len(tags) > 0 {
|
|
|
|
args = append(args, "-tags", strings.Join(tags, ","))
|
|
|
|
}
|
2014-11-29 14:17:54 -07:00
|
|
|
if race {
|
|
|
|
args = append(args, "-race")
|
|
|
|
}
|
2014-08-18 12:38:22 -07:00
|
|
|
args = append(args, pkg)
|
2014-08-18 12:53:08 -07:00
|
|
|
setBuildEnv()
|
2014-09-28 04:07:13 -07:00
|
|
|
runPrint("go", args...)
|
2014-12-22 02:52:09 -07:00
|
|
|
|
|
|
|
// Create an md5 checksum of the binary, to be included in the archive for
|
|
|
|
// automatic upgrades.
|
|
|
|
err := md5File(binary)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-08-18 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildTar() {
|
|
|
|
name := archiveName()
|
|
|
|
var tags []string
|
|
|
|
if noupgrade {
|
|
|
|
tags = []string{"noupgrade"}
|
|
|
|
name += "-noupgrade"
|
|
|
|
}
|
|
|
|
build("./cmd/syncthing", tags)
|
|
|
|
filename := name + ".tar.gz"
|
2014-11-24 05:48:52 -07:00
|
|
|
files := []archiveFile{
|
2015-01-07 08:15:50 -07:00
|
|
|
{src: "README.md", dst: name + "/README.txt"},
|
|
|
|
{src: "LICENSE", dst: name + "/LICENSE.txt"},
|
|
|
|
{src: "AUTHORS", dst: name + "/AUTHORS.txt"},
|
|
|
|
{src: "syncthing", dst: name + "/syncthing"},
|
|
|
|
{src: "syncthing.md5", dst: name + "/syncthing.md5"},
|
2014-11-24 05:48:52 -07:00
|
|
|
}
|
2015-01-07 08:15:50 -07:00
|
|
|
|
2014-11-24 05:48:52 -07:00
|
|
|
for _, file := range listFiles("etc") {
|
2015-01-07 08:15:50 -07:00
|
|
|
files = append(files, archiveFile{src: file, dst: name + "/" + file})
|
|
|
|
}
|
|
|
|
for _, file := range listFiles("extra") {
|
|
|
|
files = append(files, archiveFile{src: file, dst: name + "/" + filepath.Base(file)})
|
2014-11-24 05:48:52 -07:00
|
|
|
}
|
2015-01-07 08:15:50 -07:00
|
|
|
|
2014-11-24 05:48:52 -07:00
|
|
|
tarGz(filename, files)
|
2014-08-18 12:38:22 -07:00
|
|
|
log.Println(filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildZip() {
|
|
|
|
name := archiveName()
|
|
|
|
var tags []string
|
|
|
|
if noupgrade {
|
|
|
|
tags = []string{"noupgrade"}
|
|
|
|
name += "-noupgrade"
|
|
|
|
}
|
|
|
|
build("./cmd/syncthing", tags)
|
|
|
|
filename := name + ".zip"
|
2014-11-24 05:48:52 -07:00
|
|
|
files := []archiveFile{
|
2015-01-07 08:15:50 -07:00
|
|
|
{src: "README.md", dst: name + "/README.txt"},
|
|
|
|
{src: "LICENSE", dst: name + "/LICENSE.txt"},
|
|
|
|
{src: "AUTHORS", dst: name + "/AUTHORS.txt"},
|
|
|
|
{src: "syncthing.exe", dst: name + "/syncthing.exe"},
|
|
|
|
{src: "syncthing.exe.md5", dst: name + "/syncthing.exe.md5"},
|
2014-11-24 05:48:52 -07:00
|
|
|
}
|
2015-01-07 08:15:50 -07:00
|
|
|
|
|
|
|
for _, file := range listFiles("extra") {
|
|
|
|
files = append(files, archiveFile{src: file, dst: name + "/" + filepath.Base(file)})
|
|
|
|
}
|
|
|
|
|
2014-11-24 05:48:52 -07:00
|
|
|
zipFile(filename, files)
|
2014-08-18 12:38:22 -07:00
|
|
|
log.Println(filename)
|
|
|
|
}
|
|
|
|
|
2015-05-11 09:39:53 -07:00
|
|
|
func buildDeb() {
|
|
|
|
os.RemoveAll("deb")
|
|
|
|
|
|
|
|
build("./cmd/syncthing", []string{"noupgrade"})
|
|
|
|
|
|
|
|
files := []archiveFile{
|
|
|
|
{src: "README.md", dst: "deb/usr/share/doc/syncthing/README.txt", perm: 0644},
|
|
|
|
{src: "LICENSE", dst: "deb/usr/share/doc/syncthing/LICENSE.txt", perm: 0644},
|
|
|
|
{src: "AUTHORS", dst: "deb/usr/share/doc/syncthing/AUTHORS.txt", perm: 0644},
|
|
|
|
{src: "syncthing", dst: "deb/usr/bin/syncthing", perm: 0755},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range listFiles("extra") {
|
|
|
|
files = append(files, archiveFile{src: file, dst: "deb/usr/share/doc/syncthing/" + filepath.Base(file), perm: 0644})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, af := range files {
|
|
|
|
if err := copyFile(af.src, af.dst, af.perm); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 10:04:39 -07:00
|
|
|
debarch := goarch
|
|
|
|
if debarch == "386" {
|
|
|
|
debarch = "i386"
|
|
|
|
}
|
|
|
|
|
2015-05-11 09:39:53 -07:00
|
|
|
control := `Package: syncthing
|
|
|
|
Architecture: {{goarch}}
|
|
|
|
Depends: libc6
|
|
|
|
Version: {{version}}
|
|
|
|
Maintainer: Jakob Borg <jakob@nym.se>
|
|
|
|
Description: Open Source Continuous File Synchronization
|
|
|
|
Syncthing does bidirectional synchronization of files between two or
|
|
|
|
more computers.
|
|
|
|
`
|
|
|
|
changelog := `syncthing ({{version}}); urgency=medium
|
|
|
|
|
|
|
|
* Packaging of {{version}}.
|
|
|
|
|
|
|
|
-- Jakob Borg <jakob@nym.se> {{date}}
|
|
|
|
`
|
|
|
|
|
2015-05-11 10:04:39 -07:00
|
|
|
control = strings.Replace(control, "{{debarch}}", goarch, -1)
|
2015-05-11 09:39:53 -07:00
|
|
|
control = strings.Replace(control, "{{version}}", version[1:], -1)
|
2015-05-11 10:04:39 -07:00
|
|
|
changelog = strings.Replace(changelog, "{{debarch}}", goarch, -1)
|
2015-05-11 09:39:53 -07:00
|
|
|
changelog = strings.Replace(changelog, "{{version}}", version[1:], -1)
|
|
|
|
changelog = strings.Replace(changelog, "{{date}}", time.Now().Format(time.RFC1123), -1)
|
|
|
|
|
|
|
|
os.MkdirAll("deb/DEBIAN", 0755)
|
|
|
|
ioutil.WriteFile("deb/DEBIAN/control", []byte(control), 0644)
|
|
|
|
ioutil.WriteFile("deb/DEBIAN/compat", []byte("9\n"), 0644)
|
|
|
|
ioutil.WriteFile("deb/DEBIAN/changelog", []byte(changelog), 0644)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyFile(src, dst string, perm os.FileMode) error {
|
|
|
|
dstDir := filepath.Dir(dst)
|
|
|
|
os.MkdirAll(dstDir, 0755) // ignore error
|
|
|
|
srcFd, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcFd.Close()
|
|
|
|
dstFd, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, perm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dstFd.Close()
|
|
|
|
_, err = io.Copy(dstFd, srcFd)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-24 05:48:52 -07:00
|
|
|
func listFiles(dir string) []string {
|
|
|
|
var res []string
|
|
|
|
filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fi.Mode().IsRegular() {
|
|
|
|
res = append(res, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2014-08-18 12:53:08 -07:00
|
|
|
func setBuildEnv() {
|
|
|
|
os.Setenv("GOOS", goos)
|
2015-01-09 02:21:30 -07:00
|
|
|
os.Setenv("GOARCH", goarch)
|
2014-09-28 04:07:13 -07:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Warning: can't determine current dir:", err)
|
|
|
|
log.Println("Build might not work as expected")
|
|
|
|
}
|
|
|
|
os.Setenv("GOPATH", fmt.Sprintf("%s%c%s", filepath.Join(wd, "Godeps", "_workspace"), os.PathListSeparator, os.Getenv("GOPATH")))
|
|
|
|
log.Println("GOPATH=" + os.Getenv("GOPATH"))
|
2014-08-18 12:53:08 -07:00
|
|
|
}
|
|
|
|
|
2014-08-18 12:38:22 -07:00
|
|
|
func assets() {
|
2014-09-28 04:07:13 -07:00
|
|
|
setBuildEnv()
|
|
|
|
runPipe("internal/auto/gui.files.go", "go", "run", "cmd/genassets/main.go", "gui")
|
2014-08-18 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func xdr() {
|
2015-01-17 18:12:06 -07:00
|
|
|
runPrint("go", "generate", "./internal/discover", "./internal/db")
|
2014-08-18 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func translate() {
|
2014-11-26 05:46:34 -07:00
|
|
|
os.Chdir("gui/assets/lang")
|
|
|
|
runPipe("lang-en-new.json", "go", "run", "../../../cmd/translate/main.go", "lang-en.json", "../../index.html")
|
2014-08-18 12:38:22 -07:00
|
|
|
os.Remove("lang-en.json")
|
|
|
|
err := os.Rename("lang-en-new.json", "lang-en.json")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-11-26 05:46:34 -07:00
|
|
|
os.Chdir("../../..")
|
2014-08-18 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func transifex() {
|
2014-11-26 05:46:34 -07:00
|
|
|
os.Chdir("gui/assets/lang")
|
|
|
|
runPrint("go", "run", "../../../cmd/transifexdl/main.go")
|
|
|
|
os.Chdir("../../..")
|
2014-08-18 12:38:22 -07:00
|
|
|
assets()
|
|
|
|
}
|
|
|
|
|
|
|
|
func deps() {
|
|
|
|
rmr("Godeps")
|
|
|
|
runPrint("godep", "save", "./cmd/...")
|
|
|
|
}
|
|
|
|
|
|
|
|
func clean() {
|
|
|
|
rmr("bin", "Godeps/_workspace/pkg", "Godeps/_workspace/bin")
|
|
|
|
rmr(filepath.Join(os.Getenv("GOPATH"), fmt.Sprintf("pkg/%s_%s/github.com/syncthing", goos, goarch)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func ldflags() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("-w")
|
2014-10-13 11:10:32 -07:00
|
|
|
b.WriteString(fmt.Sprintf(" -X main.Version %s", version))
|
2014-08-18 12:38:22 -07:00
|
|
|
b.WriteString(fmt.Sprintf(" -X main.BuildStamp %d", buildStamp()))
|
|
|
|
b.WriteString(fmt.Sprintf(" -X main.BuildUser %s", buildUser()))
|
|
|
|
b.WriteString(fmt.Sprintf(" -X main.BuildHost %s", buildHost()))
|
|
|
|
b.WriteString(fmt.Sprintf(" -X main.BuildEnv %s", buildEnvironment()))
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func rmr(paths ...string) {
|
|
|
|
for _, path := range paths {
|
|
|
|
log.Println("rm -r", path)
|
|
|
|
os.RemoveAll(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 03:17:18 -07:00
|
|
|
func getReleaseVersion() (string, error) {
|
|
|
|
fd, err := os.Open("RELEASE")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
bs, err := ioutil.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(bytes.TrimSpace(bs)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGitVersion() (string, error) {
|
2014-10-15 09:15:21 -07:00
|
|
|
v, err := runError("git", "describe", "--always", "--dirty")
|
2014-10-13 11:13:42 -07:00
|
|
|
if err != nil {
|
2015-03-12 03:17:18 -07:00
|
|
|
return "", err
|
2014-10-13 11:13:42 -07:00
|
|
|
}
|
2014-08-18 12:38:22 -07:00
|
|
|
v = versionRe.ReplaceAllFunc(v, func(s []byte) []byte {
|
|
|
|
s[0] = '+'
|
|
|
|
return s
|
|
|
|
})
|
2015-03-12 03:17:18 -07:00
|
|
|
return string(v), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getVersion() string {
|
|
|
|
// First try for a RELEASE file,
|
|
|
|
if ver, err := getReleaseVersion(); err == nil {
|
|
|
|
return ver
|
|
|
|
}
|
2015-03-13 02:10:13 -07:00
|
|
|
// ... then see if we have a Git tag.
|
2015-03-12 03:17:18 -07:00
|
|
|
if ver, err := getGitVersion(); err == nil {
|
|
|
|
return ver
|
|
|
|
}
|
|
|
|
// This seems to be a dev build.
|
|
|
|
return "unknown-dev"
|
2014-08-18 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildStamp() int64 {
|
2014-10-15 09:15:21 -07:00
|
|
|
bs, err := runError("git", "show", "-s", "--format=%ct")
|
2014-10-13 11:13:42 -07:00
|
|
|
if err != nil {
|
|
|
|
return time.Now().Unix()
|
|
|
|
}
|
2014-08-18 12:38:22 -07:00
|
|
|
s, _ := strconv.ParseInt(string(bs), 10, 64)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildUser() string {
|
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return "unknown-user"
|
|
|
|
}
|
|
|
|
return strings.Replace(u.Username, " ", "-", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildHost() string {
|
|
|
|
h, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return "unknown-host"
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildEnvironment() string {
|
|
|
|
if v := os.Getenv("ENVIRONMENT"); len(v) > 0 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return "default"
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildArch() string {
|
|
|
|
os := goos
|
|
|
|
if os == "darwin" {
|
|
|
|
os = "macosx"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s-%s", os, goarch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func archiveName() string {
|
2014-10-13 11:10:32 -07:00
|
|
|
return fmt.Sprintf("syncthing-%s-%s", buildArch(), version)
|
2014-08-18 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func run(cmd string, args ...string) []byte {
|
2014-10-15 09:15:21 -07:00
|
|
|
bs, err := runError(cmd, args...)
|
2014-08-18 12:38:22 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Println(cmd, strings.Join(args, " "))
|
|
|
|
log.Println(string(bs))
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return bytes.TrimSpace(bs)
|
|
|
|
}
|
|
|
|
|
2014-10-15 09:15:21 -07:00
|
|
|
func runError(cmd string, args ...string) ([]byte, error) {
|
|
|
|
ecmd := exec.Command(cmd, args...)
|
|
|
|
bs, err := ecmd.CombinedOutput()
|
2015-04-28 13:32:10 -07:00
|
|
|
return bytes.TrimSpace(bs), err
|
2014-10-15 09:15:21 -07:00
|
|
|
}
|
|
|
|
|
2014-08-18 12:38:22 -07:00
|
|
|
func runPrint(cmd string, args ...string) {
|
|
|
|
log.Println(cmd, strings.Join(args, " "))
|
|
|
|
ecmd := exec.Command(cmd, args...)
|
|
|
|
ecmd.Stdout = os.Stdout
|
|
|
|
ecmd.Stderr = os.Stderr
|
|
|
|
err := ecmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runPipe(file, cmd string, args ...string) {
|
|
|
|
log.Println(cmd, strings.Join(args, " "), ">", file)
|
|
|
|
fd, err := os.Create(file)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
ecmd := exec.Command(cmd, args...)
|
|
|
|
ecmd.Stdout = fd
|
|
|
|
ecmd.Stderr = os.Stderr
|
|
|
|
err = ecmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-08-23 13:19:29 -07:00
|
|
|
fd.Close()
|
2014-08-18 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type archiveFile struct {
|
2015-05-11 09:39:53 -07:00
|
|
|
src string
|
|
|
|
dst string
|
|
|
|
perm os.FileMode
|
2014-08-18 12:38:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func tarGz(out string, files []archiveFile) {
|
|
|
|
fd, err := os.Create(out)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gw := gzip.NewWriter(fd)
|
|
|
|
tw := tar.NewWriter(gw)
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
sf, err := os.Open(f.src)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := sf.Stat()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
h := &tar.Header{
|
|
|
|
Name: f.dst,
|
|
|
|
Size: info.Size(),
|
|
|
|
Mode: int64(info.Mode()),
|
|
|
|
ModTime: info.ModTime(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tw.WriteHeader(h)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = io.Copy(tw, sf)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
sf.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tw.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = gw.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func zipFile(out string, files []archiveFile) {
|
|
|
|
fd, err := os.Create(out)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
zw := zip.NewWriter(fd)
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
sf, err := os.Open(f.src)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := sf.Stat()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fh, err := zip.FileInfoHeader(info)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fh.Name = f.dst
|
|
|
|
fh.Method = zip.Deflate
|
|
|
|
|
|
|
|
if strings.HasSuffix(f.dst, ".txt") {
|
|
|
|
// Text file. Read it and convert line endings.
|
|
|
|
bs, err := ioutil.ReadAll(sf)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
bs = bytes.Replace(bs, []byte{'\n'}, []byte{'\n', '\r'}, -1)
|
|
|
|
fh.UncompressedSize = uint32(len(bs))
|
|
|
|
fh.UncompressedSize64 = uint64(len(bs))
|
|
|
|
|
|
|
|
of, err := zw.CreateHeader(fh)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
of.Write(bs)
|
|
|
|
} else {
|
|
|
|
// Binary file. Copy verbatim.
|
|
|
|
of, err := zw.CreateHeader(fh)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = io.Copy(of, sf)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = zw.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2014-12-22 02:52:09 -07:00
|
|
|
|
|
|
|
func md5File(file string) error {
|
|
|
|
fd, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
h := md5.New()
|
|
|
|
_, err = io.Copy(h, fd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := os.Create(file + ".md5")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = fmt.Fprintf(out, "%x\n", h.Sum(nil))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.Close()
|
|
|
|
}
|
2015-04-28 13:32:10 -07:00
|
|
|
|
|
|
|
func vet(pkg string) {
|
|
|
|
bs, err := runError("go", "vet", pkg)
|
|
|
|
if err != nil && err.Error() == "exit status 3" || bytes.Contains(bs, []byte("no such tool \"vet\"")) {
|
|
|
|
// Go said there is no go vet
|
|
|
|
log.Println(`- No go vet, no vetting. Try "go get -u golang.org/x/tools/cmd/vet".`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
falseAlarmComposites := regexp.MustCompile("composite literal uses unkeyed fields")
|
|
|
|
exitStatus := regexp.MustCompile("exit status 1")
|
|
|
|
for _, line := range bytes.Split(bs, []byte("\n")) {
|
|
|
|
if falseAlarmComposites.Match(line) || exitStatus.Match(line) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Printf("%s", line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lint(pkg string) {
|
|
|
|
bs, err := runError("golint", pkg)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(`- No golint, not linting. Try "go get -u github.com/golang/lint/golint".`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
analCommentPolicy := regexp.MustCompile(`exported (function|method|const|type|var) [^\s]+ should have comment`)
|
|
|
|
for _, line := range bytes.Split(bs, []byte("\n")) {
|
|
|
|
if analCommentPolicy.Match(line) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Printf("%s", line)
|
|
|
|
}
|
|
|
|
}
|