Use go module (#303)

* Migrate to gomodule

* Fix install

* Update makefile
This commit is contained in:
Sung Won Cho 2019-11-11 15:28:47 +08:00 committed by GitHub
commit 2124e28a9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 399 additions and 720 deletions

109
scripts/cli/build.sh Executable file
View file

@ -0,0 +1,109 @@
#!/usr/bin/env bash
#
# build.sh compiles dnote binary for target platforms. It is resonsible for creating
# distributable files that can be released by a human or a script.
#
# It can either cross-compile for different platforms using xgo, simply target a specific
# platform. Set GOOS and GOARCH environment variables to disable xgo and instead
# compile for a specific platform.
#
# use:
# ./scripts/build.sh 0.4.8
# GOOS=linux GOARCH=amd64 ./scripts/build.sh 0.4.8
set -ex
dir=$(dirname "${BASH_SOURCE[0]}")
version=$1
projectDir="$dir/../.."
basedir="$projectDir/pkg/cli"
outputDir="$projectDir/build/cli"
command_exists () {
command -v "$1" >/dev/null 2>&1;
}
if ! command_exists shasum; then
echo "please install shasum"
exit 1
fi
if [ $# -eq 0 ]; then
echo "no version specified."
exit 1
fi
if [[ $1 == v* ]]; then
echo "do not prefix version with v"
exit 1
fi
goVersion=1.12.x
get_binary_name() {
platform=$1
if [ "$platform" == "windows" ]; then
echo "dnote.exe"
else
echo "dnote"
fi
}
build() {
platform=$1
arch=$2
# native indicates if the compilation is to take place natively on the host platform
# if not true, use xgo with Docker to cross-compile
native=$3
# build binary
destDir="$outputDir/$platform-$arch"
ldflags="-X main.apiEndpoint=https://api.dnote.io -X main.versionTag=$version"
tags="fts5"
mkdir -p "$destDir"
if [ "$native" == true ]; then
GOOS="$platform" GOARCH="$arch" \
go build \
-ldflags "$ldflags" \
--tags "$tags" \
-o="$destDir/cli-$platform-$arch" \
"$basedir"
else
xgo \
-go "$goVersion" \
--targets="$platform/$arch" \
-ldflags "$ldflags" \
--tags "$tags" \
--dest="$destDir" \
"$basedir"
fi
binaryName=$(get_binary_name "$platform")
mv "$destDir/cli-${platform}-"* "$destDir/$binaryName"
# build tarball
tarballName="dnote_${version}_${platform}_${arch}.tar.gz"
tarballPath="$outputDir/$tarballName"
cp "$projectDir/licenses/GPLv3.txt" "$destDir"
cp "$basedir/README.md" "$destDir"
tar -C "$destDir" -zcvf "$tarballPath" "."
rm -rf "$destDir"
# calculate checksum
pushd "$outputDir"
shasum -a 256 "$tarballName" >> "$outputDir/dnote_${version}_checksums.txt"
popd
}
if [ -z "$GOOS" ] && [ -z "$GOARCH" ]; then
# fetch tool
go get -u github.com/karalabe/xgo
build linux amd64
build darwin amd64
build windows amd64
else
build "$GOOS" "$GOARCH" true
fi

11
scripts/cli/dev.sh Executable file
View file

@ -0,0 +1,11 @@
#!/usr/bin/env bash
# dev.sh builds a new binary and replaces the old one in the PATH with it
set -eux
dir=$(dirname "${BASH_SOURCE[0]}")
sudo rm -rf "$(which dnote)" "$GOPATH/bin/cli"
# change tags to darwin if on macos
go install -ldflags "-X main.apiEndpoint=http://127.0.0.1:5000" --tags "linux fts5" "$dir/../../pkg/cli"
sudo ln -s "$GOPATH/bin/cli" /usr/local/bin/dnote

5
scripts/cli/dump_schema.sh Executable file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
# dump_schema.sh dumps the current system's dnote schema
set -eux
sqlite3 ~/.dnote/dnote.db .schema

14
scripts/cli/test.sh Executable file
View file

@ -0,0 +1,14 @@
#!/usr/bin/env bash
# test.sh runs test files sequentially
# https://stackoverflow.com/questions/23715302/go-how-to-run-tests-for-multiple-packages
set -eux
dir=$(dirname "${BASH_SOURCE[0]}")
pushd "$dir/../../pkg/cli"
# clear tmp dir in case not properly torn down
rm -rf "./tmp"
go test -a ./... \
-p 1\
--tags "fts5"
popd