filebrowser/build/main.go

95 lines
2.0 KiB
Go
Raw Normal View History

2016-08-09 21:49:03 +00:00
package main
import (
2016-08-10 08:54:40 +00:00
"io/ioutil"
2016-08-09 21:49:03 +00:00
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
2016-08-10 13:26:39 +00:00
if len(os.Getenv("TRAVIS")) > 0 || len(os.Getenv("CI")) > 0 {
return
}
2016-08-09 21:49:03 +00:00
goPath := os.Getenv("GOPATH")
hugoPath := filepath.Join(goPath, "src/github.com/spf13/hugo")
if found, err := exists(hugoPath); !found || err != nil {
log.Fatalf("Aborting. Can't find Hugo source on %s.", hugoPath)
}
// NOTE: I assume that 'go get -u' was run before of this and that
// every package and dependency is up to date.
// Get new tags from remote
run("git", []string{"fetch", "--tags"}, hugoPath)
// Get the revision for the latest tag
commit := run("git", []string{"rev-list", "--tags", "--max-count=1"}, hugoPath)
// Get the latest tag
tag := run("git", []string{"describe", "--tags", commit}, hugoPath)
// Checkout the latest tag
run("git", []string{"checkout", tag}, hugoPath)
// Build hugo binary
pluginPath := filepath.Join(goPath, "src/github.com/hacdias/caddy-hugo")
run("go", []string{"build", "-o", "assets/hugo", "github.com/spf13/hugo"}, pluginPath)
2016-08-10 08:54:40 +00:00
updateVersion(pluginPath, tag)
2016-08-09 21:49:03 +00:00
}
func run(command string, args []string, path string) string {
cmd := exec.Command(command, args...)
cmd.Dir = path
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
return strings.TrimSpace(string(out))
}
// exists returns whether the given file or directory exists or not
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
2016-08-10 08:54:40 +00:00
func updateVersion(path string, version string) {
path = filepath.Join(path, "installer.go")
input, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalln(err)
}
lines := strings.Split(string(input), "\n")
for i, line := range lines {
if strings.Contains(line, "const version") {
lines[i] = "const version = \"" + version + "\""
}
}
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(path, []byte(output), 0644)
if err != nil {
log.Fatalln(err)
}
}