github.com/puellanivis/breton@v0.2.16/masher/mash.sh (about) 1 #!/bin/bash 2 3 ARCH="amd64" 4 ARCH_DIR="x86_64" 5 6 while [[ $# -gt 0 ]]; do 7 key="$1" 8 val="${key#*=}" 9 10 case "$key" in 11 --cache=*) 12 export GOCACHE="$val" 13 ;; 14 --buildver=*) 15 BUILDVER="$val" 16 ;; 17 --timestamp=*) 18 TIMESTAMP="$val" 19 ;; 20 --id=*) 21 ID="$val" 22 ;; 23 --private=*) 24 export GOPRIVATE="$val" 25 ;; 26 27 --arm64) 28 ARCH="arm64" 29 ARCH_DIR="arm64" 30 ;; 31 --linux) 32 LINUX="true" 33 ;; 34 --darwin) 35 DARWIN="true" 36 ;; 37 --openbsd) 38 OPENBSD="true" 39 ;; 40 --windows) 41 WINDOWS="true" 42 ;; 43 44 --allprotos) 45 ALLPROTOS="true" 46 ;; 47 --proto) 48 NOTESTING="true" 49 NOBUILD="true" 50 ;; 51 52 --test) 53 TESTING="true" 54 ;; 55 --notest) 56 NOTESTING="true" 57 ;; 58 --nobuild) 59 NOBUILD="true" 60 ;; 61 62 --deb) 63 DEB="true" 64 ;; 65 --nodeb) 66 DEB="false" 67 ;; 68 69 --shell) 70 ESCAPE="true" 71 ;; 72 73 --) 74 shift 75 break 76 ;; 77 --*) 78 echo "$0: unknown flag $key" 1>&2 79 exit 1 80 ;; 81 *) 82 break 83 ;; 84 esac 85 shift 86 done 87 88 if [[ -d ./vendor ]]; then 89 export GOFLAGS="-mod=vendor" 90 VENDOR="true" 91 fi 92 93 if [[ $ESCAPE == "true" ]]; then 94 exec /bin/bash "$@" 95 exit 1 96 fi 97 98 case "${LINUX}${DARWIN}${OPENBSD}${WINDOWS}" in 99 "") 100 if [[ -n $DEB ]]; then 101 LINUX="true" 102 else 103 NOCOMPILE="true" 104 fi 105 ;; 106 truetrue*) 107 TESTING="true" 108 esac 109 110 if which go > /dev/null 2>&1 ; then 111 GO_VERSION="$(go version)" 112 GO_VERSION=${GO_VERSION#go version go} 113 GO_VERSION=${GO_VERSION%% *} 114 [[ -z $NOCOMPILE ]] && echo "Building with Go Version: $GO_VERSION" 115 fi 116 117 if ! ls *.go > /dev/null 2>&1 ; then 118 NOGOFILES=true 119 fi 120 121 PROTOC_FLAGS="--go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative" 122 123 if [[ -n $ALLPROTOS ]]; then 124 if [[ -n $NOGOFILES ]]; then 125 echo "Building all subdir protos" 126 protos=$( find . -type f -name "*.proto" -exec dirname \{\} \; | sort -u ) 127 else 128 echo "Building go dependency protos" 129 protos=$( go list -f "{{range .Deps}}{{println}}{{end}}" | grep "/proto$" | sort -u ) 130 fi 131 132 for proto in $protos; do 133 echo "Building proto: $proto" 134 protoc ${PROTOC_FLAGS} "${proto#./}/"*.proto || exit 1 135 done 136 137 elif [[ -d "proto" ]]; then 138 echo "Building local proto" 139 protoc ${PROTOC_FLASG} proto/*.proto 140 fi 141 142 143 if [[ -n $NOGOFILES ]]; then 144 if [[ -x test.sh ]] ; then 145 # if there is a test.sh file, then execute this instead of 146 # erroring out that there are no go files. 147 exec ./test.sh 148 fi 149 150 echo "No go files found, not building" 151 exit 0 152 fi 153 154 if [[ ("$TESTING" == "true") && ("$NOTESTING" != "true") ]]; then 155 echo "Testing..." 156 go test || exit 1 157 fi 158 159 PACKAGE=`go list -f {{.Name}}` 160 if [[ $PACKAGE != "main" ]]; then 161 echo "This is not a package main go project." 162 163 if [[ $PROD == "true" ]]; then 164 echo "Building is being aborted." 165 exit 1 166 fi 167 168 echo "Building is being skipped." 169 exit 0 170 fi 171 172 if [[ $VENDOR != "true" && $NOCOMPILE != "true" ]]; then 173 echo "Getting dependencies..." 174 if [[ -r go.mod ]]; then 175 echo "Using go modules..." 176 177 elif [[ -r Gopkg.toml ]]; then 178 DEP_UP="" 179 if [[ -r Gopkg.lock ]]; then 180 DEP_UP="-update" 181 fi 182 183 dep ensure $DEP_UP 184 185 else 186 go get -v -d || exit 1 187 fi 188 fi 189 190 if [[ "$NOBUILD" == "true" ]]; then 191 exit 0 192 fi 193 194 BUILDSTAMP="$TIMESTAMP" 195 if [[ -n $ID ]]; then 196 [[ -n $BUILDSTAMP ]] && BUILDSTAMP="${BUILDSTAMP}~" 197 BUILDSTAMP="${BUILDSTAMP}${ID}" 198 fi 199 200 PROJECT="${PWD##*/}" 201 if [[ -n $BUILDSTAMP ]]; then 202 LDFLAGS="-ldflags=-X main.VersionBuild=$BUILDSTAMP -X main.Buildstamp=$BUILDSTAMP" 203 if [[ -n $BUILDVER ]]; then 204 LDFLAGS="$LDFLAGS -X main.Version=$BUILDVER" 205 fi 206 207 LIB_UTIL_DEP=$( go list -f "{{range .Deps}}{{println}}{{end}}" | grep -e "\<breton/lib/util\>" | head -n1 ) 208 if [[ -n $LIB_UTIL_DEP ]]; then 209 LDFLAGS="$LDFLAGS -X ${LIB_UTIL_DEP}.BUILD=$BUILDSTAMP" 210 fi 211 fi 212 213 touch /tmp/stuff > /dev/null 2>&1 || mount -t tmpfs -o rw,nodev,nosuid,size=1G /dev/null /tmp 214 215 if [[ -r ./.gitignore ]]; then 216 grep -qFx -e "bin" ./.gitignore || echo "bin" >> ./.gitignore 217 fi 218 219 if [[ $LINUX == "true" ]]; then 220 OUT="bin/linux.${ARCH_DIR}" 221 echo "Compiling ${OUT}/${PROJECT}" 222 [ -d "$OUT" ] || mkdir -p $OUT || exit 1 223 GOOS=linux GOARCH=$ARCH go build -o "${OUT}/${PROJECT}" "${LDFLAGS}" || exit 1 224 225 [[ ( ${DEB} != "false" ) && ( -d debian ) ]] && DEB="true" 226 fi 227 228 if [[ $DARWIN == "true" ]]; then 229 OUT="bin/darwin.${ARCH_DIR}" 230 echo "Compiling ${OUT}/${PROJECT}" 231 [ -d "$OUT" ] || mkdir -p $OUT || exit 1 232 GOOS=darwin GOARCH=$ARCH go build -o "${OUT}/${PROJECT}" "${LDFLAGS}" || exit 1 233 fi 234 235 if [[ $OPENBSD == "true" ]]; then 236 OUT="bin/openbsd.${ARCH_DIR}" 237 echo "Compiling ${OUT}/${PROJECT}" 238 [ -d "$OUT" ] || mkdir -p $OUT || exit 1 239 GOOS=openbsd GOARCH=$ARCH go build -o "${OUT}/${PROJECT}" "${LDFLAGS}" || exit 1 240 fi 241 242 if [[ $WINDOWS == "true" ]]; then 243 OUT="bin/windows.${ARCH_DIR}" 244 echo "Compiling ${OUT}/${PROJECT}.exe" 245 [ -d "$OUT" ] || mkdir -p $OUT || exit 1 246 GOOS=windows GOARCH=$ARCH go build -o "${OUT}/${PROJECT}.exe" "${LDFLAGS}" || exit 1 247 fi 248 249 if [[ ( $DEB == "true" ) && ( -x bin/linux.${ARCH_DIR}/${PROJECT} ) ]]; then 250 [ -d debian ] || mkdir debian 251 252 echo "Building debian package..." 253 254 VERSION="${BUILDSTAMP}" 255 BIN_VERSION="$( ./bin/linux.${ARCH_DIR}/${PROJECT} --version )" 256 [[ -n $BIN_VERSION ]] && echo "BIN_VERSION=\"$BIN_VERSION\"" 257 case "${BIN_VERSION}" in 258 *\ v*) 259 VERSION="$( echo "${BIN_VERSION}" | cut -d" " -f2 )" 260 VERSION="${VERSION#v}" 261 ;; 262 esac 263 echo "VERSION=${VERSION}" 264 265 if [[ -r ./.gitignore ]]; then 266 grep -qFx -e "build" ./.gitignore || echo "build" >> ./.gitignore 267 grep -qFx -e "debian/changelog.gz" -e "*.gz" ./.gitignore || echo "debian/changelog.gz" >> ./.gitignore 268 grep -qFx -e "*.deb" ./.gitignore || echo "*.deb" >> ./.gitignore 269 fi 270 271 WHOAMI="nobody <nobody@example.com>" 272 [[ -r debian/whoami ]] && WHOAMI="$(cat debian/whoami)" 273 274 ORIGIN_URL="$( git remote get-url origin )" 275 276 277 if [[ ! -r debian/control ]]; then 278 cat <<EOF > debian/control 279 Source: @@PROJECT@@ 280 Section: unknown 281 Priority: optional 282 Maintainer: $WHOAMI 283 Homepage: $ORIGIN_URL 284 Package: @@PROJECT@@ 285 Architecture: @@ARCH@@ 286 Version: @@VERSION@@ 287 Description: TODO 288 EOF 289 fi 290 291 install -d build/DEBIAN 292 for f in conffiles control; do 293 if [[ -r "debian/$f" ]]; then 294 install -m644 debian/$f build/DEBIAN/ 295 sed -i "s/@@PROJECT@@/${PROJECT}/" build/DEBIAN/$f 296 sed -i "s/@@VERSION@@/${VERSION}/" build/DEBIAN/$f 297 sed -i "s/@@ARCH@@/${ARCH}/" build/DEBIAN/$f 298 fi 299 done 300 for f in postinst postrm prerm; do 301 if [[ -r debian/$f ]]; then 302 install -m755 debian/$f build/DEBIAN/ 303 fi 304 done 305 306 DEB_PACKAGE="$( grep "^Package: " build/DEBIAN/control | cut -d" " -f2 )" 307 308 # install copyright and changelog documentation 309 install -d "build/usr/share/doc/${DEB_PACKAGE}" 310 311 if [[ ! -r debian/copyright ]]; then 312 if [[ -r LICENSE ]]; then 313 LICENSE="LICENSE" 314 elif [[ -r LICENSE.md ]]; then 315 LICENSE="LICENSE.md" 316 fi 317 318 YEARS="$( date +%Y )" 319 ORIGIN_URL="$( git remote get-url origin )" 320 321 if [[ -n $LICENSE ]]; then ( 322 cat <<EOF 323 Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ 324 Upstream-Name: $PROJECT 325 Upstream-Contact: $WHOAMI 326 Source: $ORIGIN_URL 327 328 Files: * 329 Copyright: $YEAR $WHOAMI 330 License: LICENSE 331 332 License: LICENSE 333 EOF 334 awk '!NF{$0="."}1' "$LICENSE" 335 ) > debian/copyright; fi 336 fi 337 338 if [[ -r debian/copyright ]]; then 339 install -m644 debian/copyright "build/usr/share/doc/${DEB_PACKAGE}/" 340 fi 341 342 if [[ -r CHANGELOG ]]; then 343 # if CHANGELOG is newer than changelog.gz, then compress it and write it to changelog.gz 344 if [[ CHANGELOG -nt debian/changelog.gz ]]; then 345 gzip -9 -c CHANGELOG > debian/changelog.gz 346 fi 347 else 348 349 # if changelog.gz does not exist, then make it with a bare-minimum changelog gzip file. 350 cat <<EOF | gzip -9 > debian/changelog.gz 351 ${DEB_PACKAGE} (${VERSION}) unstable; urgency=low 352 353 * No information. 354 355 -- ${WHOAMI} $(date -R) 356 EOF 357 fi 358 359 install -m644 debian/changelog.gz "build/usr/share/doc/${DEB_PACKAGE}/changelog.Debian.gz" 360 361 # install binary 362 install -d build/usr/bin 363 install -m755 "bin/linux.${ARCH_DIR}/${PROJECT}" "build/usr/bin/${PROJECT}" 364 365 if [[ ( -n ${DEB_PACKAGE} ) && ( -n ${VERSION} ) && ( -n ${ARCH} ) ]]; then 366 DEB_FILE="${DEB_PACKAGE}_${VERSION}_${ARCH}.deb" 367 368 # Final packaging 369 rm -f "${DEB_FILE}" 370 fakeroot dpkg-deb --build build . 371 lintian -X binaries "${DEB_FILE}" 372 fi 373 fi 374 375 echo "Complete"