simple script

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2024-03-12 18:27:40 +01:00
commit 95f8964cbf
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632

View file

@ -1,19 +1,82 @@
#!/bin/bash
SED_PARAM=""
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) SED_PARAM=" -i ";;
Darwin*) SED_PARAM=" -i '' ";;
*) exit 1
esac
exec_with_dry_run () {
if [[ ${1} -eq ${FLAGS_TRUE} ]]; then
echo "# ${2}"
else
eval "${2}" 2> /tmp/error || \
{
echo -e "failed with following error";
output=$(cat /tmp/error | sed -e "s/^/ error -> /g");
echo -e "${output}";
return 1;
}
fi
return 0
}
edit_file () {
dryrun=${1}
cmd=${2}
file=${3}
exec_with_dry_run "${1}" "${2}" || return 1
exec_with_dry_run "${1}" "git add ${3}" || return 1
return 0
}
main () {
[[ -f ./libs/shflags ]] && . ./libs/shflags || eval "$(curl --silent https://gitlab.g3e.fr/h6n/tools/-/raw/main/libs/shflags)"
DEFINE_string 'tag' 'v0.0.0' 'region cible' 't'
DEFINE_string 'name' 'totor' 'region cible' 'n'
DEFINE_boolean 'dryrun' false 'enable dry-run mode' 'd'
DEFINE_string 'tag' 'v0.0.0' 'Version' 't'
DEFINE_string 'name' 'totor' 'Nom de la version' 'n'
DEFINE_boolean 'create_branch' false 'Creation de la branch' 'c'
DEFINE_boolean 'dryrun' false 'Enable dry-run mode' 'd'
DEFINE_string 'release_path' './docs/release' 'Release path' 'p'
DEFINE_string 'regex_version' 'v[0-9]*.[0-9]*.[0-9]*' 'Version Regex' 'r'
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
echo "${FLAGS_tag} - ${FLAGS_name} . ${FLAGS_dryrun}"
NAME=${FLAGS_name}
VERSION=${FLAGS_tag}
# Creation de la branch
# . -c -t (version) -n (name)
if [[ ${FLAGS_create_branch} -eq ${FLAGS_TRUE} ]]; then
echo "Create Release ${NAME} - ${VERSION}"
exec_with_dry_run "${FLAGS_dryrun}" "git checkout -b release-${VERSION}" || exit 1
exec_with_dry_run "${FLAGS_dryrun}" "sed -e 's/NAME VERSION/${NAME} ${VERSION}/' ${FLAGS_release_path}/.template.md > ${FLAGS_release_path}/next_release.md" || return 1
exit 0
fi
NAME=$(cat ${FLAGS_release_path}/next_release.md | head -n 1 | cut -d\ -f 2)
VERSION=$(cat ${FLAGS_release_path}/next_release.md | head -n 1 | cut -d\ -f 3)
LOWER_NAME=$(echo ${NAME} | tr '[:upper:]' '[:lower:]')
# Release standard
BRANCH=$(git branch --show-current)
if [[ "${BRANCH}" == "release-${VERSION}" ]]; then
echo "Release ${NAME} - ${VERSION}"
echo "Edit README file"
edit_file "${FLAGS_dryrun}" "sed ${SED_PARAM} 's/^${FLAGS_regex_version}: [A-Z|a-z| ]*$/${VERSION}: ${NAME}/' ./README.md" "./README.md" || return 1
edit_file "${FLAGS_dryrun}" "mv '${FLAGS_release_path}/next_release.md' '${FLAGS_release_path}/${VERSION}_${LOWER_NAME}.md'" "${FLAGS_release_path}/${VERSION}_${LOWER_NAME}.md" || return 1
exec_with_dry_run "${FLAGS_dryrun}" "git commit -s -S -m 'release-${VERSION}'" || return 1
exec_with_dry_run "${FLAGS_dryrun}" "git tag '${VERSION}' -s -m 'Release ${VERSION} ${NAME}'" || return 1
fi
}
[[ "${BASH_SOURCE[0]}" == "${0}" ]] && main "$@"
[[ "${BASH_SOURCE[0]}" == "" ]] && main "$@"
[[ "${BASH_SOURCE[0]}" == "${0}" ]] && (main "$@" || exit 1)
[[ "${BASH_SOURCE[0]}" == "" ]] && (main "$@" || exit 1)