Skip to content

Commit

Permalink
增加打包脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
lwch committed Aug 20, 2021
1 parent c8d77d5 commit 78e7632
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/bin
/test
/test
/build
/release
/tmp
14 changes: 12 additions & 2 deletions build
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#!/bin/sh
CGO_ENABLED=0 go build -o bin/np-svr code/server/*.go
CGO_ENABLED=0 go build -o bin/np-cli code/client/*.go

VERSION="0.0.1"
HASH=`git log -n1 --pretty=format:%h`
REVERSION=`git log --oneline|wc -l|tr -d ' '`
BUILD_TIME=`date +'%Y-%m-%d %H:%M:%S'`
LDFLAGS="-X 'main._GIT_HASH=$HASH'
-X 'main._GIT_REVERSION=$REVERSION'
-X 'main._BUILD_TIME=$BUILD_TIME'
-X 'main._VERSION=$VERSION'"

CGO_ENABLED=0 go build -ldflags "$LDFLAGS" -o bin/np-svr code/server/*.go
CGO_ENABLED=0 go build -ldflags "$LDFLAGS" -o bin/np-cli code/client/*.go
254 changes: 254 additions & 0 deletions build_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package main

import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"

"github.com/lwch/logging"
"github.com/lwch/runtime"
)

const version = "0.0.1"
const buildDir = "tmp"
const releaseDir = "release"

type target struct {
os string
arch string
ext string
packExt string
}

var targets = []target{
// freebsd
{"freebsd", "386", "", ".tar.gz"},
{"freebsd", "amd64", "", ".tar.gz"},
{"freebsd", "arm", "", ".tar.gz"},
{"freebsd", "arm64", "", ".tar.gz"},
// linux
{"linux", "386", "", ".tar.gz"},
{"linux", "amd64", "", ".tar.gz"},
{"linux", "arm", "", ".tar.gz"},
{"linux", "arm64", "", ".tar.gz"},
// netbsd
{"netbsd", "386", "", ".tar.gz"},
{"netbsd", "amd64", "", ".tar.gz"},
{"linux", "arm", "", ".tar.gz"},
{"linux", "arm64", "", ".tar.gz"},
// openbsd
{"openbsd", "386", "", ".tar.gz"},
{"openbsd", "amd64", "", ".tar.gz"},
{"linux", "arm", "", ".tar.gz"},
{"linux", "arm64", "", ".tar.gz"},
// windows
{"windows", "386", ".exe", ".zip"},
{"windows", "amd64", ".exe", ".zip"},
{"windows", "arm", ".exe", ".zip"},
}

func main() {
os.RemoveAll(releaseDir)
runtime.Assert(os.MkdirAll(releaseDir, 0755))
for _, target := range targets {
logging.Info("build target %s/%s...", target.os, target.arch)
build(target)
}
}

func build(t target) {
os.RemoveAll(buildDir)
runtime.Assert(os.MkdirAll(buildDir, 0755))

err := copyFile(path.Join("conf", "client.yaml"), path.Join(buildDir, "client.yaml"))
runtime.Assert(err)
err = copyFile(path.Join("conf", "server.yaml"), path.Join(buildDir, "server.yaml"))
runtime.Assert(err)
if t.os != "windows" && t.os != "android" && t.os != "ios" {
runtime.Assert(os.MkdirAll(path.Join(buildDir, "init.d"), 0755))
err = copyFile(path.Join("init.d", "np-cli"), path.Join(buildDir, "init.d", "np-cli"))
runtime.Assert(err)
err = copyFile(path.Join("init.d", "np-svr"), path.Join(buildDir, "init.d", "np-svr"))
runtime.Assert(err)
}

ldflags := "-X 'main._GIT_HASH=" + gitHash() + "' " +
"-X 'main._GIT_REVERSION=" + gitReversion() + "' " +
"-X 'main._BUILD_TIME=" + buildTime() + "' " +
"-X 'main._VERSION=" + version + "'"

logging.Info("build server...")
cmd := exec.Command("go", "build", "-o", path.Join(buildDir, "np-svr"+t.ext),
"-ldflags", ldflags,
path.Join("code", "server", "main.go"),
path.Join("code", "server", "run.go"))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(),
"CGO_ENABLED=0",
fmt.Sprintf("GOOS=%s", t.os),
fmt.Sprintf("GOARCH=%s", t.arch))
runtime.Assert(cmd.Run())

logging.Info("build client...")
cmd = exec.Command("go", "build", "-o", path.Join(buildDir, "np-cli"+t.ext),
"-ldflags", ldflags,
path.Join("code", "client", "main.go"))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(),
"CGO_ENABLED=0",
fmt.Sprintf("GOOS=%s", t.os),
fmt.Sprintf("GOARCH=%s", t.arch))
runtime.Assert(cmd.Run())

logging.Info("packing...")
pack(buildDir, path.Join(releaseDir, "natpass_"+version+"_"+t.os+"_"+t.arch+t.packExt))
}

func copyFile(src, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()
fi, err := srcFile.Stat()
if err != nil {
return err
}
err = dstFile.Chmod(fi.Mode())
if err != nil {
return err
}
_, err = io.Copy(dstFile, srcFile)
return err
}

func gitHash() string {
var buf bytes.Buffer
cmd := exec.Command("git", "log", "-n1", "--pretty=format:%h")
cmd.Stdout = &buf
cmd.Stderr = os.Stderr
runtime.Assert(cmd.Run())
return buf.String()
}

func gitReversion() string {
var buf bytes.Buffer
cmd := exec.Command("git", "log", "--oneline")
cmd.Stdout = &buf
cmd.Stderr = os.Stderr
runtime.Assert(cmd.Run())
cnt := len(strings.Split(buf.String(), "\n"))
return fmt.Sprintf("%d", cnt)
}

func buildTime() string {
return time.Now().Format(time.RFC3339)
}

func pack(src, dst string) {
switch {
case strings.HasSuffix(dst, ".tar.gz"):
packTarGZ(src, dst)
case strings.HasSuffix(dst, ".zip"):
packZip(src, dst)
}
}

func packTarGZ(src, dst string) {
f, err := os.Create(dst)
runtime.Assert(err)
defer f.Close()
gw := gzip.NewWriter(f)
defer gw.Close()
tw := tar.NewWriter(gw)
defer tw.Close()
src, err = filepath.Abs(src)
runtime.Assert(err)
err = filepath.Walk(src, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if path == src {
return nil
}
dst := strings.TrimPrefix(path, src)
dst = filepath.Join("natpass_"+version, dst)
hdr, err := tar.FileInfoHeader(info, dst)
if err != nil {
return err
}
hdr.Name = dst
err = tw.WriteHeader(hdr)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
f, err = os.Open(path)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(tw, f)
return err
})
runtime.Assert(err)
}

func packZip(src, dst string) {
f, err := os.Create(dst)
runtime.Assert(err)
defer f.Close()
zw := zip.NewWriter(f)
defer zw.Close()
src, err = filepath.Abs(src)
runtime.Assert(err)
err = filepath.Walk(src, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if path == src {
return nil
}
dst := strings.TrimPrefix(path, src)
dst = filepath.Join("natpass_"+version, dst)
hdr, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
hdr.Name = dst
w, err := zw.CreateHeader(hdr)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
f, err = os.Open(path)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(w, f)
return err
})
runtime.Assert(err)
}
21 changes: 21 additions & 0 deletions code/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,34 @@ import (
"github.com/lwch/runtime"
)

var (
_VERSION string = "0.0.0"
_GIT_HASH string
_GIT_REVERSION string
_BUILD_TIME string
)

func showVersion() {
fmt.Printf("version: v%s\ntime: %s\ncommit: %s.%s\n",
_VERSION,
_BUILD_TIME,
_GIT_HASH, _GIT_REVERSION)
os.Exit(0)
}

func main() {
bak := flag.Bool("d", false, "backend running")
pid := flag.String("pid", "", "pid file dir")
user := flag.String("u", "", "daemon user")
conf := flag.String("conf", "", "configure file path")
version := flag.Bool("v", false, "show version info")
flag.Parse()

if *version {
showVersion()
os.Exit(0)
}

if len(*conf) == 0 {
fmt.Println("missing -conf param")
os.Exit(1)
Expand Down
21 changes: 21 additions & 0 deletions code/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,34 @@ import (
"github.com/lwch/runtime"
)

var (
_VERSION string = "0.0.0"
_GIT_HASH string
_GIT_REVERSION string
_BUILD_TIME string
)

func showVersion() {
fmt.Printf("version: v%s\ntime: %s\ncommit: %s.%s\n",
_VERSION,
_BUILD_TIME,
_GIT_HASH, _GIT_REVERSION)
os.Exit(0)
}

func main() {
bak := flag.Bool("d", false, "backend running")
pid := flag.String("pid", "", "pid file dir")
user := flag.String("u", "", "daemon user")
conf := flag.String("conf", "", "configure file path")
version := flag.Bool("v", false, "show version info")
flag.Parse()

if *version {
showVersion()
os.Exit(0)
}

if len(*conf) == 0 {
fmt.Println("missing -conf param")
os.Exit(1)
Expand Down

0 comments on commit 78e7632

Please sign in to comment.