dev-expr/build.bash
2024-07-17 14:24:15 +02:00

174 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
# All rights reserved.
RESOURCE_FILE=".build.rc"
BUILD_REGISTER=".build_register"
PROGRAM_NAME=""
PROGRAM_VERSION=""
function usage() {
prog=$(basename "${0}")
msgln "USAGE:"
#msgln " ${prog} <program-name> <program-version> <os>/<platform>..."
msgln " ${prog} <os>/<platform>..."
msgln " ${prog} --dev Build the local exec"
msgln " ${prog} --init Create the resource file in the current directory"
msgln
if [ -r "${RESOURCE_FILE}" ]; then
msgln "Resource file '${RESOURCE_FILE}' content:"
cat >&2 ${RESOURCE_FILE}
else
msgln "Resource file '${RESOURCE_FILE}' not found"
fi
}
function msgln() {
echo >&2 "${1}"
}
function exitUsage() {
echo >&2 "${1}"
usage
exit 1
}
function exitMsg() {
echo >&2 "${1}"
exit 1
}
# CMDLINE: help
if [ "${1}" == "help,," ] || [ "${1,,}" == "--help" ] || [ "${1,,}" == "-h" ]; then
usage
exit
fi
# CMDLINE: init
if [ "${1,,}" == "--init" ]; then
cat >"${RESOURCE_FILE}" <<eot
# Builder resource file
# Created on $(date)
# Program name
PROGRAM_NAME="name"
# Program version
PROGRAM_VERSION="version"
# Preset platform
platform=("linux/amd64" "darwin/arm64")
#--- end of file ---
eot
msgln "Resource file '${RESOURCE_FILE}' create"
${EDITOR-vi} "${RESOURCE_FILE}"
exit
fi
if [ -r "${RESOURCE_FILE}" ]; then
if ! source "${RESOURCE_FILE}"; then
exitMsg "Can't load build resource file '${RESOURCE_FILE}'"
fi
fi
if [ -z "${PROGRAM_NAME}" ]; then
exitUsage "Missing program name"
fi
if [ -z "${PROGRAM_VERSION}" ]; then
exitUsage "Missing program version"
fi
function getBuildCount() {
local reg ver count
if [ -r "${BUILD_REGISTER}" ]; then
reg=$(<"${BUILD_REGISTER}")
else
reg="${PROGRAM_VERSION} 0"
fi
read ver count <<<"${reg}"
if [ "${ver}" != "${PROGRAM_VERSION}" ]; then
count=0
fi
count=$((count+1))
echo >"${BUILD_REGISTER}" "${PROGRAM_VERSION} ${count}"
echo ${count}
}
function build() {
local target=${1} ext cmd
IFS=/ read os cpu <<<"${p}"
#msgln "OS=${os}; CPU=${cpu}"
ext=""
if [ "${os}" == 'windows' ]; then
ext=".exe"
fi
cmd="GOOS='${os}' GOARCH='${cpu}' go build -o '${PROGRAM_NAME}_v${PROGRAM_VERSION}_${os}_${cpu}${ext}'"
eval "${cmd}"
}
function buildLocal() {
local ext cmd
ext=""
if [[ "${OSTYPE}" =~ win.* ]]; then
ext=".exe"
fi
cmd="go build -o '${PROGRAM_NAME}${ext}'"
eval "${cmd}"
}
function exprPath() {
local path
path=$(grep -m1 -F "go-pkg/expr" go.work)
echo ${path}
}
function gitTag() {
local expr_path
expr_path=$(exprPath)
tag=$(git -C "${expr_path}" tag -l --sort=-version:refname "v*"|head -1)
echo ${tag}
}
function createVersionSource() {
local tag
tag=$(gitTag)
cat >version.go <<eot
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// version.go
package main
const (
PROGNAME = "${PROGRAM_NAME}"
VERSION = "v${PROGRAM_VERSION}(build $(getBuildCount)),$(date +"%Y/%m/%d") (celestino.amoroso@portale-stac.it)"
EXPR_VERSION = "${tag}"
)
eot
}
## -- MAIN -- ##
createVersionSource
if [ "${1}" == "--dev" ]; then
buildLocal
exit
fi
if [ ${#} -gt 0 ]; then
for p; do
build "${p}"
done
else
for p in ${platform[@]}; do
build "${p}"
done
fi