Pull request: all: add a new Makefile and scripts, remove goreleaaser

Merge in DNS/adguard-home from 2276-releases to master

Updates #2276.

Squashed commit of the following:

commit 84961947c51477aae53606ec6e2e0cce0bdfc139
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Dec 30 14:36:13 2020 +0300

    all: fix github build

commit 54af2adbf2f433e80393fb142e66ba6b3a78b13e
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Dec 30 14:34:02 2020 +0300

    all: remove old Dockerfile, improve build scripts

commit 99bb2f2ba1458d32074ac0911b5c02ce6669e43e
Merge: 2292b677a 5e20ac7ed
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Dec 30 13:47:19 2020 +0300

    Merge branch 'master' into WIP-2276-releases

commit 2292b677a20ce8e93d9e6e2bb042cd468606fec3
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Dec 30 13:30:10 2020 +0300

    all: improve docker build

commit 0bcc97c41f105ee4a4363f20fa4775c7643bf0cc
Merge: c7d3f12ef aef4659e9
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Dec 29 17:47:45 2020 +0300

    Merge branch 'master' into WIP-2276-releases

commit c7d3f12ef2b63ddfa2acf46e3129fcbc56fb0a90
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Dec 29 16:28:25 2020 +0300

    all: improve build scripts

commit 55de1e5d7ef0fbdbd1a76cfb71362d16ca0a1966
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Dec 29 15:36:47 2020 +0300

    all: fix Makefile

commit d11b1fe28d0fde1efeaf6160a614951b19d0ef94
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Dec 29 14:16:19 2020 +0300

    scripts: fix build-release

commit ecc0577e2451afa86c37da7283a63a9d26fb37ba
Merge: dde64ed8e 483f02c92
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Dec 29 13:59:32 2020 +0300

    Merge branch 'master' into WIP-2276-releases

commit dde64ed8e456f73559f21c2ca549dc3b46724add
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Dec 25 18:04:46 2020 +0300

    all: imp docs, other improvements

commit be8574408db79901bb15c1d31916db3ca352a35f
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Dec 25 14:48:30 2020 +0300

    all: imp docker build

commit fc1876f34b93d667bf166226f4bc666d394f10c7
Merge: fa5a304c8 955b735c8
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Dec 25 13:54:29 2020 +0300

    Merge branch 'master' into WIP-2276-releases

commit fa5a304c83d86145796a2de4141de6d18f7c56bf
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Dec 24 19:10:51 2020 +0300

    all: improve scripts

commit 3f32e3fd5e658d058d5c5172519384efc6cfef83
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Dec 24 18:50:01 2020 +0300

    all: improve scripts

commit 2d38b81421acab4b90a7a19da7598c75063e8e93
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Dec 24 18:25:21 2020 +0300

    all: fix shell for windows, improve go-lint.sh

commit d695285cd6dc476c0d972cfe0c49bbeea5f5a049
Merge: 313b020e9 9fb6bf82c
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Dec 24 18:14:38 2020 +0300

    Merge branch 'master' into WIP-2276-releases

commit 313b020e9dfcdab736670cee72b2171eac8c32b7
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Dec 24 18:13:31 2020 +0300

    Makefile: use npm ci again

commit 5acee9d6a6c8cd2a7dd04b173a73929650882bad
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Dec 24 17:57:54 2020 +0300

    all: try fixing windows build

commit c63a2a54641ac8cd032a3306bb35e49b9ae74728
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Dec 24 17:39:30 2020 +0300

    all: imp scripts, try another goproxy and direct

commit 423229e8b63ee73caeee8e84c23f67d145aff9df
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Dec 24 17:25:29 2020 +0300

    all: imp HACKING.md, try a new proxy

... and 1 more commit
This commit is contained in:
Ainar Garipov
2020-12-30 18:26:25 +03:00
parent 5e20ac7ed5
commit 0e84962fde
28 changed files with 1065 additions and 663 deletions

49
scripts/make/Dockerfile Normal file
View File

@@ -0,0 +1,49 @@
# A docker file for scripts/make/build-docker.sh.
FROM alpine:3.12
ARG BUILD_DATE
ARG VERSION
ARG VCS_REF
LABEL maintainer="AdGuard Team <devteam@adguard.com>" \
org.opencontainers.image.created=$BUILD_DATE \
org.opencontainers.image.url="https://adguard.com/adguard-home.html" \
org.opencontainers.image.source="https://github.com/AdguardTeam/AdGuardHome" \
org.opencontainers.image.version=$VERSION \
org.opencontainers.image.revision=$VCS_REF \
org.opencontainers.image.vendor="AdGuard" \
org.opencontainers.image.title="AdGuard Home" \
org.opencontainers.image.description="Network-wide ads & trackers blocking DNS server" \
org.opencontainers.image.licenses="GPL-3.0"
# Update certificates.
RUN apk --no-cache --update add ca-certificates libcap && \
rm -rf /var/cache/apk/* && \
mkdir -p /opt/adguardhome/conf /opt/adguardhome/work && \
chown -R nobody: /opt/adguardhome
ARG DIST_DIR
ARG TARGETARCH
ARG TARGETOS
ARG TARGETVARIANT
COPY --chown=nobody:nogroup\
./${DIST_DIR}/docker/AdGuardHome_${TARGETOS}_${TARGETARCH}_${TARGETVARIANT}\
/opt/adguardhome/AdGuardHome
RUN setcap 'cap_net_bind_service=+eip' /opt/adguardhome/AdGuardHome
EXPOSE 53/tcp 53/udp 67/udp 68/udp 80/tcp 443/tcp 853/tcp 3000/tcp
VOLUME ["/opt/adguardhome/conf", "/opt/adguardhome/work"]
WORKDIR /opt/adguardhome/work
ENTRYPOINT ["/opt/adguardhome/AdGuardHome"]
CMD [ \
"--no-check-update", \
"-c", "/opt/adguardhome/conf/AdGuardHome.yaml", \
"-h", "0.0.0.0", \
"-w", "/opt/adguardhome/work", \
]

View File

@@ -0,0 +1,101 @@
#!/bin/sh
verbose="${VERBOSE:-0}"
if [ "$verbose" -gt '0' ]
then
set -x
debug_flags='-D'
else
set +x
debug_flags=''
fi
set -e -f -u
# Require these to be set. The channel value is validated later.
readonly channel="$CHANNEL"
readonly commit="$COMMIT"
readonly dist_dir="$DIST_DIR"
readonly version="$VERSION"
# Allow users to use sudo.
readonly sudo_cmd="${SUDO:-}"
readonly docker_platforms="\
linux/386,\
linux/amd64,\
linux/arm/v6,\
linux/arm/v7,\
linux/arm64,\
linux/ppc64le"
readonly build_date="$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
# Set DOCKER_IMAGE_NAME to 'adguard/adguard-home' if you want (and are
# allowed) to push to DockerHub.
readonly docker_image_name="${DOCKER_IMAGE_NAME:-adguardhome-dev}"
# Set DOCKER_OUTPUT to 'type=image,name=adguard/adguard-home,push=true'
# if you want (and are allowed) to push to DockerHub.
readonly docker_output="${DOCKER_OUTPUT:-type=image,name=${docker_image_name},push=false}"
case "$channel"
in
('release')
readonly docker_image_full_name="${docker_image_name}:${version}"
readonly docker_tags="--tag ${docker_image_name}:latest"
;;
('beta')
readonly docker_image_full_name="${docker_image_name}:${version}"
readonly docker_tags="--tag ${docker_image_name}:beta"
;;
('edge')
# Don't set the version tag when pushing to the edge channel.
readonly docker_image_full_name="${docker_image_name}:edge"
readonly docker_tags=''
;;
('development')
readonly docker_image_full_name="${docker_image_name}"
readonly docker_tags=''
;;
(*)
echo "invalid channel '$channel', supported values are\
'development', 'edge', 'beta', and 'release'" 1>&2
exit 1
;;
esac
# Copy the binaries into a new directory under new names, so that it's
# eaiser to COPY them later. DO NOT remove the trailing underscores.
# See scripts/make/Dockerfile.
readonly dist_docker="${dist_dir}/docker"
mkdir -p "$dist_docker"
cp "${dist_dir}/AdGuardHome_linux_386/AdGuardHome/AdGuardHome"\
"${dist_docker}/AdGuardHome_linux_386_"
cp "${dist_dir}/AdGuardHome_linux_amd64/AdGuardHome/AdGuardHome"\
"${dist_docker}/AdGuardHome_linux_amd64_"
cp "${dist_dir}/AdGuardHome_linux_arm64/AdGuardHome/AdGuardHome"\
"${dist_docker}/AdGuardHome_linux_arm64_"
cp "${dist_dir}/AdGuardHome_linux_arm_6/AdGuardHome/AdGuardHome"\
"${dist_docker}/AdGuardHome_linux_arm_v6"
cp "${dist_dir}/AdGuardHome_linux_arm_7/AdGuardHome/AdGuardHome"\
"${dist_docker}/AdGuardHome_linux_arm_v7"
cp "${dist_dir}/AdGuardHome_linux_ppc64le/AdGuardHome/AdGuardHome"\
"${dist_docker}/AdGuardHome_linux_ppc64le_"
# Don't use quotes with $docker_tags and $debug_flags because we want
# word splitting and or an empty space if tags are empty.
$sudo_cmd docker\
$debug_flags\
buildx build\
--build-arg BUILD_DATE="$build_date"\
--build-arg DIST_DIR="$dist_dir"\
--build-arg VCS_REF="$commit"\
--build-arg VERSION="$version"\
--output "$docker_output"\
--platform "$docker_platforms"\
$docker_tags\
-t "$docker_image_full_name"\
-f ./scripts/make/Dockerfile\
.

View File

@@ -0,0 +1,325 @@
#!/bin/sh
# AdGuard Home Release Script
#
# The commentary in this file is written with the assumption that the
# reader only has superficial knowledge of the POSIX shell language and
# alike. Experienced readers may find it overly verbose.
# The default verbosity level is 0. Show every command that is run if
# the caller requested verbosity level greater than 0. Show the
# environment if the callre requested verbosity level greater than 1.
# Otherwise, print nothing.
#
# The level of verbosity for the build script is the same minus one
# level. See below in build().
readonly verbose="${VERBOSE:-0}"
if [ "$verbose" -gt '1' ]
then
env
set -x
elif [ "$verbose" -gt '0' ]
then
set -x
fi
# By default, sign the packages, but allow users to skip that step.
readonly sign="${SIGN:-1}"
# Exit the script if a pipeline fails (-e), prevent accidental filename
# expansion (-f), and consider undefined variables as errors (-u).
set -e -f -u
# Function log is an echo wrapper that writes to stderr if the caller
# requested verbosity level greater than 0. Otherwise, it does nothing.
log() {
if [ "$verbose" -gt '0' ]
then
# Don't use quotes to get word splitting.
echo $@ 1>&2
fi
}
log 'starting to build AdGuard Home release'
# Require the channel to be set. Additional validation is performed
# later by go-build.sh.
readonly channel="$CHANNEL"
# Check VERSION against the default value from the Makefile. If it is
# that, use the version calculation script.
if [ "${VERSION:-}" = 'v0.0.0' -o "${VERSION:-}" = '' ]
then
readonly version="$(sh ./scripts/make/version.sh)"
else
readonly version="$VERSION"
fi
log "channel '$channel'"
log "version '$version'"
# Require the gpg key and passphrase to be set if the signing is
# required.
if [ "$sign" = '1' ]
then
readonly gpg_key_passphrase="$GPG_KEY_PASSPHRASE"
readonly gpg_key="$GPG_KEY"
fi
# The default distribution files directory is dist.
readonly dist="${DIST_DIR:-dist}"
# Give users the ability to override the go command from environment.
# For example, to build two releases with two different Go versions and
# test the difference.
readonly go="${GO:-go}"
log "checking tools"
# Make sure we fail gracefully if one of the tools we need is missing.
for tool in gpg gzip sed sha256sum snapcraft tar zip
do
which "$tool" >/dev/null ||\
{ log "pieces don't fit, '$tool' not found"; exit 1; }
done
# Data section. Arrange data into space-separated tables for read -r to
# read. Use 0 for missing values.
readonly arms='5
6
7'
readonly mipses='softfloat'
# os arch arm mips snap
readonly platforms="\
darwin 386 0 0 0
darwin amd64 0 0 0
freebsd 386 0 0 0
freebsd amd64 0 0 0
freebsd arm 5 0 0
freebsd arm 6 0 0
freebsd arm 7 0 0
linux 386 0 0 i386
linux amd64 0 0 amd64
linux arm 5 0 0
linux arm 6 0 0
linux arm 7 0 armhf
linux arm64 0 0 arm64
linux mips 0 softfloat 0
linux mips64 0 softfloat 0
linux mips64le 0 softfloat 0
linux mipsle 0 softfloat 0
linux ppc64le 0 0 0
windows 386 0 0 0
windows amd64 0 0 0"
# Function build builds the release for one platform. It builds
# a binary, an archive and, if needed, a snap package.
build() {
# Get the arguments. Here and below, use the "build_" prefix
# for all variables local to function build.
build_dir="${dist}/${1}/AdGuardHome"\
build_ar="$2"\
build_os="$3"\
build_arch="$4"\
build_arm="$5"\
build_mips="$6"\
build_snap="$7"\
;
# Use the ".exe" filename extension if we build a Windows
# release.
if [ "$build_os" = 'windows' ]
then
build_output="./${build_dir}/AdGuardHome.exe"
else
build_output="./${build_dir}/AdGuardHome"
fi
mkdir -p "./${build_dir}"
# Build the binary.
#
# Set GOARM and GOMIPS to an empty string if $build_arm and
# $build_mips are zero by removing the zero as if it's a prefix.
#
# Don't use quotes with $build_par because we want an empty
# space if parallelism wasn't set.
env\
GOARCH="$build_arch"\
GOARM="${build_arm#0}"\
GOMIPS="${build_mips#0}"\
GOOS="$os"\
VERBOSE="$(( verbose - 1 ))"\
OUT="$build_output"\
sh ./scripts/make/go-build.sh\
;
log "$build_output"
if [ "$sign" = '1' ]
then
gpg\
--default-key "$gpg_key"\
--detach-sig\
--passphrase "$gpg_key_passphrase"\
--pinentry-mode loopback\
-q\
"$build_output"\
;
fi
# Prepare the build directory for archiving.
cp ./CHANGELOG.md ./LICENSE.txt ./README.md "$build_dir"
# Make archives. Windows and macOS prefer ZIP archives; the
# rest, gzipped tarballs.
case "$build_os"
in
('darwin'|'windows')
build_archive="./${dist}/${build_ar}.zip"
zip -9 -q "$build_archive" "$build_dir"
;;
(*)
build_archive="./${dist}/${build_ar}.tar.gz"
tar -C "./${dist}/${1}" -c -f - "./AdGuardHome"\
| gzip -9 - >"$build_archive"
;;
esac
log "$build_archive"
if [ "$build_snap" = '0' ]
then
return
fi
# Prepare snap build.
build_snap_output="./${dist}/AdGuardHome_${build_snap}.snap"
build_snap_dir="${build_snap_output}.dir"
# Create the meta subdirectory and copy files there.
mkdir -p "${build_snap_dir}/meta"
cp "$build_output"\
'./scripts/snap/local/adguard-home-web.sh'\
"$build_snap_dir"
cp -r './scripts/snap/gui'\
"${build_snap_dir}/meta/"
# Create a snap.yaml file, setting the values.
sed -e 's/%VERSION%/'"$version"'/'\
-e 's/%ARCH%/'"$build_snap"'/'\
./scripts/snap/snap.tmpl.yaml\
>"${build_snap_dir}/meta/snap.yaml"
# TODO(a.garipov): The snapcraft tool will *always* write
# everything, including errors, to stdout. And there doesn't
# seem to be a way to change that. So, save the combined
# output, but only show it when snapcraft actually fails.
set +e
build_snapcraft_output="$(
snapcraft pack "$build_snap_dir"\
--output "$build_snap_output" 2>&1
)"
build_snapcraft_exit_code="$?"
set -e
if [ "$build_snapcraft_exit_code" != '0' ]
then
log "$build_snapcraft_output"
exit "$build_snapcraft_exit_code"
fi
log "$build_snap_output"
}
log "starting builds"
# Go over all platforms defined in the space-separated table above,
# tweak the values where necessary, and feed to build.
echo "$platforms" | while read -r os arch arm mips snap
do
case "$arch"
in
(arm)
dir="AdGuardHome_${os}_${arch}_${arm}"
ar="AdGuardHome_${os}_${arch}v${arm}"
;;
(mips*)
dir="AdGuardHome_${os}_${arch}_${mips}"
ar="$dir"
;;
(*)
dir="AdGuardHome_${os}_${arch}"
ar="$dir"
;;
esac
build "$dir" "$ar" "$os" "$arch" "$arm" "$mips" "$snap"
done
log "calculating checksums"
# Calculate the checksums of the files in a subshell with file expansion
# enabled (+f) so that we don't need to use find or basename.
(
set +f
cd "./${dist}"
# Don't use quotes to get word splitting.
sha256sum $(ls *.tar.gz *.zip) > ./checksums.txt
)
log "writing versions"
echo "$version" > "./${dist}/version.txt"
# Create the verison.json file.
readonly version_download_url="https://static.adguard.com/adguardhome/${channel}"
readonly version_json="./${dist}/version.json"
# Point users to the master branch if the channel is edge.
if [ "$channel" = 'edge' ]
then
readonly version_history_url='https://github.com/AdguardTeam/AdGuardHome/commits/master'
else
readonly version_history_url='https://github.com/AdguardTeam/AdGuardHome/releases'
fi
rm -f "$version_json"
echo "{
\"version\": \"${version}\",
\"announcement\": \"AdGuard Home ${version} is now available!\",
\"announcement_url\": \"${version_history_url}\",
\"selfupdate_min_version\": \"0.0\",
" >> "$version_json"
(
# Use +f here so that ls works and we don't need to use find.
set +f
# Don't use quotes to get word splitting.
for f in $(ls "./${dist}/"*.tar.gz "./${dist}/"*.zip)
do
platform="$f"
# Remove the prefix.
platform="${platform#./${dist}/AdGuardHome_}"
# Remove the filename extensions.
platform="${platform%.zip}"
platform="${platform%.tar.gz}"
# Use the filename's base path.
filename="${f#./${dist}/}"
echo " \"download_${platform}\": \"${version_download_url}/${filename}\"," >> "$version_json"
done
)
echo '}' >> "$version_json"
log "finished"

33
scripts/make/clean.sh Normal file
View File

@@ -0,0 +1,33 @@
#!/bin/sh
verbose="${VERBOSE:-0}"
if [ "$verbose" -gt '0' ]
then
set -x
fi
set -e -f -u
dist_dir="$DIST_DIR"
go="${GO:-go}"
# Set the GOPATH explicitly in case make clean is called from under sudo
# after a Docker build.
env PATH="$("$go" env GOPATH)/bin":"$PATH" packr clean
rm -f\
./AdGuardHome\
./AdGuardHome.exe\
./coverage.txt\
;
rm -f -r\
./bin/\
./build/\
./build2/\
./client/node_modules/\
./client2/node_modules/\
./data/\
"./${dist_dir}/"\
;

98
scripts/make/go-build.sh Normal file
View File

@@ -0,0 +1,98 @@
#!/bin/sh
# AdGuard Home Build Script
#
# The commentary in this file is written with the assumption that the
# reader only has superficial knowledge of the POSIX shell language and
# alike. Experienced readers may find it overly verbose.
# The default verbosity level is 0. Show every command that is run and
# every package that is processed if the caller requested verbosity
# level greater than 0. Also show subcommands if the requested
# verbosity level is greater than 1. Otherwise, do nothing.
verbose="${VERBOSE:-0}"
if [ "$verbose" -gt '1' ]
then
env
set -x
readonly v_flags='-v'
readonly x_flags='-x'
elif [ "$verbose" -gt '0' ]
then
set -x
readonly v_flags='-v'
readonly x_flags=''
else
set +x
readonly v_flags=''
readonly x_flags=''
fi
# Exit the script if a pipeline fails (-e), prevent accidental filename
# expansion (-f), and consider undefined variables as errors (-u).
set -e -f -u
# Allow users to set the Go version.
go="${GO:-go}"
# Require the channel to be set and validate the value.
channel="$CHANNEL"
case "$channel"
in
('development'|'edge'|'beta'|'release')
# All is well, go on.
;;
(*)
echo "invalid channel '$channel', supported values are\
'development', 'edge', 'beta', and 'release'" 1>&2
exit 1
;;
esac
# Require the version to be set.
#
# TODO(a.garipov): Additional validation?
version="$VERSION"
# Set the linker flags accordingly: set the channel and the versio as
# well as goarm and gomips variable values, if the variables are set and
# are not empty.
ldflags="-s -w -X main.version=${version}"
ldflags="${ldflags} -X main.channel=${channel}"
if [ "${GOARM:-}" != '' ]
then
ldflags="${ldflags} -X main.goarm=${GOARM}"
elif [ "${GOMIPS:-}" != '' ]
then
ldflags="${ldflags} -X main.gomips=${GOMIPS}"
fi
# Allow users to limit the build's parallelism.
readonly parallelism="${PARALLELISM:-}"
if [ "$parallelism" != '' ]
then
readonly par_flags="-p ${parallelism}"
else
readonly par_flags=''
fi
# Allow users to specify a different output name.
readonly out="${OUT:-}"
if [ "$out" != '' ]
then
readonly out_flags="-o ${out}"
else
readonly out_flags=''
fi
# Don't use cgo. Use modules.
export CGO_ENABLED='0' GO111MODULE='on'
readonly build_flags="${BUILD_FLAGS:-$out_flags $par_flags\
$v_flags $x_flags}"
# Don't use quotes with flag variables to get word splitting.
"$go" generate $v_flags $x_flags ./...
# Don't use quotes with flag variables to get word splitting.
"$go" build --ldflags "$ldflags" $build_flags

31
scripts/make/go-deps.sh Normal file
View File

@@ -0,0 +1,31 @@
#!/bin/sh
verbose="${VERBOSE:-0}"
if [ "$verbose" -gt '1' ]
then
env
set -x
readonly v_flags='-v'
readonly x_flags='-x'
elif [ "$verbose" -gt '0' ]
then
set -x
readonly v_flags='-v'
readonly x_flags=''
else
set +x
readonly v_flags=''
readonly x_flags=''
fi
set -e -f -u
go="${GO:-go}"
# Don't use quotes with flag variables because we want an empty space if
# those aren't set.
"$go" mod download $x_flags
env GOBIN="${PWD}/bin" "$go" install $v_flags $x_flags\
github.com/gobuffalo/packr/packr

126
scripts/make/go-lint.sh Normal file
View File

@@ -0,0 +1,126 @@
#!/bin/sh
verbose="${VERBOSE:-0}"
# Verbosity levels:
# 0 = Don't print anything except for errors.
# 1 = Print commands, but not nested commands.
# 2 = Print everything.
if [ "$verbose" -gt '0' ]
then
set -x
fi
# Set $EXIT_ON_ERROR to zero to see all errors.
if [ "${EXIT_ON_ERROR:-1}" = '0' ]
then
set +e
else
set -e
fi
# We don't need glob expansions and we want to see errors about unset
# variables.
set -f -u
not_found_msg='
looks like a binary not found error.
make sure you have installed the linter binaries using:
$ make go-tools
'
not_found() {
if [ "$?" = '127' ]
then
# Code 127 is the exit status a shell uses when
# a command or a file is not found, according to the
# Bash Hackers wiki.
#
# See https://wiki.bash-hackers.org/dict/terms/exit_status.
echo "$not_found_msg" 1>&2
fi
}
trap not_found EXIT
# blocklist_imports is a simple check against unwanted packages.
# Currently it only looks for package log which is replaced by our own
# package github.com/AdguardTeam/golibs/log.
blocklist_imports() {
git grep -F -e '"log"' -- '*.go' || exit 0;
}
# underscores is a simple check against Go filenames with underscores.
underscores() {
git ls-files '*_*.go' | { grep -F -e '_darwin.go' \
-e '_freebsd.go' -e '_linux.go' -e '_others.go' \
-e '_test.go' -e '_unix.go' -e '_windows.go' \
-v || exit 0; }
}
# exit_on_output exits with a nonzero exit code if there is anything in
# the command's combined output.
exit_on_output() {
test "$VERBOSE" -lt '2' && set +x
cmd="$1"
shift
exitcode='0'
output="$("$cmd" "$@" 2>&1)"
if [ "$output" != '' ]
then
if [ "$*" != '' ]
then
echo "combined output of '$cmd $@':"
else
echo "combined output of '$cmd':"
fi
echo "$output"
exitcode='1'
fi
test "$VERBOSE" -gt '0' && set -x
return "$exitcode"
}
exit_on_output blocklist_imports
exit_on_output underscores
exit_on_output gofumpt --extra -l -s .
golint --set_exit_status ./...
"$GO" vet ./...
gocyclo --over 20 .
gosec --quiet .
ineffassign .
unparam ./...
git ls-files -- '*.go' '*.md' '*.yaml' '*.yml' 'Makefile'\
| xargs misspell --error
looppointer ./...
nilness ./...
# TODO(a.garipov): Enable shadow after fixing all of the shadowing.
# shadow --strict ./...
# TODO(a.garipov): Enable errcheck fully after handling all errors,
# including the deferred ones, properly. Also, perhaps, enable --blank.
# errcheck ./...
exit_on_output sh -c '
errcheck --asserts ./... |\
{ grep -e "defer" -e "_test\.go:" -v || exit 0; }
'
staticcheck ./...

41
scripts/make/go-test.sh Normal file
View File

@@ -0,0 +1,41 @@
#!/bin/sh
verbose="${VERBOSE:-0}"
# Verbosity levels:
# 0 = Don't print anything except for errors.
# 1 = Print commands, but not nested commands.
# 2 = Print everything.
if [ "$verbose" -gt '1' ]
then
set -x
v_flags='-v'
x_flags='-x'
elif [ "$verbose" -gt '0' ]
then
set -x
v_flags='-v'
x_flags=''
else
set +x
v_flags=''
x_flags=''
fi
set -e -f -u
race="${RACE:-1}"
if [ "$race" = '0' ]
then
race_flags=''
else
race_flags='--race'
fi
go="${GO:-go}"
cover_flags='--coverprofile ./coverage.txt'
count_flags='--count 1'
# Don't use quotes with flag variables because we want an empty space if
# those aren't set.
"$go" test $race_flags $count_flags $cover_flags $x_flags $v_flags ./...

27
scripts/make/go-tools.sh Normal file
View File

@@ -0,0 +1,27 @@
#!/bin/sh
verbose="${VERBOSE:-0}"
if [ "$verbose" -gt '0' ]
then
set -x
fi
set -e -f -u
# TODO(a.garipov): Add goconst?
env GOBIN="${PWD}/bin" "$GO" install --modfile=./internal/tools/go.mod\
github.com/fzipp/gocyclo/cmd/gocyclo\
github.com/golangci/misspell/cmd/misspell\
github.com/gordonklaus/ineffassign\
github.com/kisielk/errcheck\
github.com/kyoh86/looppointer/cmd/looppointer\
github.com/securego/gosec/v2/cmd/gosec\
golang.org/x/lint/golint\
golang.org/x/tools/go/analysis/passes/nilness/cmd/nilness\
golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow\
honnef.co/go/tools/cmd/staticcheck\
mvdan.cc/gofumpt\
mvdan.cc/unparam\
;

57
scripts/make/version.sh Normal file
View File

@@ -0,0 +1,57 @@
#!/bin/sh
readonly verbose="${VERBOSE:-0}"
if [ "$verbose" -gt '1' ]
then
set -x
fi
set -e -f -u
readonly awk_program='/^v[0-9]+\.[0-9]+\.[0-9]+.*$/ {
if (!$4) {
# The last tag is a full release version, so bump the
# minor one to get the next one.
$2++;
}
print($1 "." $2 "." $3);
next;
}
{
printf("invalid version: \"%s\"\n", $0);
exit 1;
}'
readonly last_tag="$(git describe --abbrev=0)"
readonly current_desc="$(git describe)"
readonly channel="$CHANNEL"
case "$channel"
in
('development')
echo 'v0.0.0'
;;
('edge')
next=$(echo $last_tag | awk -F '[.+-]' "$awk_program")
echo "${next}-SNAPSHOT-$(git rev-parse --short HEAD)"
;;
('beta'|'release')
if [ "$current_desc" != "$last_tag" ]
then
echo 'need a tag' 1>&2
exit 1
fi
echo "$last_tag"
;;
(*)
echo "invalid channel '$channel', supported values are\
'development', 'edge', 'beta', and 'release'" 1>&2
exit 1
;;
esac