From d1f799991d966b8299f198221d2f946a37d81c79 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sat, 9 Sep 2017 17:26:11 +0200 Subject: [PATCH] Refactor/clean a bit ynh-build --- ynh-build | 184 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 117 insertions(+), 67 deletions(-) diff --git a/ynh-build b/ynh-build index da5a45e..60b202c 100755 --- a/ynh-build +++ b/ynh-build @@ -1,102 +1,152 @@ #!/bin/bash +readonly THISSCRIPT=$0 +readonly PROJECT=$1 +readonly BRANCH=$2 +readonly DISTRIB=$3 +readonly VERSION=$4 +readonly TAG="debian/$VERSION" + source /home/vinaigrette/config/config # ##### # # Usage # # ##### # -usage() { +function usage() +{ cat << EOF + Usage: - `basename $0` + $THISSCRIPT Arguments: moulinette, yunohost, yunohost-admin or SSOwat testing or stable jessie or stretch x.y.z (ex: 2.6.1) + EOF } -if [[ $1 == "-h" ]]; then - usage - exit 0 -fi - # ################# # # Check user inputs # # ################# # -PROJECT=$1 -BRANCH=$2 -DISTRIB=$3 -VERSION=$4 +function validate_arguments() +{ -## Project -if [[ ! $PROJECT =~ ^yunohost|yunohost-admin|moulinette|SSOwat$ ]]; then - echo "Invalid project $PROJECT" - usage - exit 1 -fi + ## Project + if [[ ! $PROJECT =~ ^yunohost|yunohost-admin|moulinette|SSOwat$ ]]; then + echo "Invalid project $PROJECT" + usage + exit 1 + fi -## Branch -if [[ ! $BRANCH =~ ^testing|stable$ ]]; then - echo "Invalid branch $BRANCH" - usage - exit 2 -fi + ## Branch + if [[ ! $BRANCH =~ ^testing|stable|unstable$ ]]; then + echo "Invalid branch $BRANCH" + usage + exit 2 + fi -# Distribution -if [[ ! $DISTRIB =~ ^jessie|stretch$ ]]; then - echo "Invalid distribution $DISTRIB" - usage - exit 3 -fi + # Distribution + if [[ ! $DISTRIB =~ ^jessie|stretch$ ]]; then + echo "Invalid distribution $DISTRIB" + usage + exit 3 + fi -# Version -if [ -z "$VERSION" ]; then - echo "Invalid version $VERSION" - usage - exit 4 -fi + # Version + if [ -z "$VERSION" ]; then + echo "Invalid version $VERSION" + usage + exit 4 + fi +} -# Sum up configuration -echo "## #################################################### ##" -echo "## Building $PROJECT $BRANCH release - $VERSION version" -echo "## #################################################### ##" +function checkout_tag() +{ + # Update project's repository + echo "" + echo "> Updating $GIT_REPOS/$PROJECT repository ... " + echo "" -# Update project's repository -echo "" -echo "## Updating $GIT_REPOS/$PROJECT repository ..." -cd $GIT_REPOS/$PROJECT -git fetch --quiet -git fetch --tags --quiet -git checkout $BRANCH --quiet -git pull origin $BRANCH --quiet + cd $GIT_REPOS/$PROJECT + git fetch --quiet + git fetch --tags --quiet + git checkout $DISTRIB-$BRANCH --quiet + #git checkout $BRANCH --quiet + #git pull origin $BRANCH --quiet -# Check if tag really exists -if git rev-parse "debian/$VERSION" >/dev/null 2>&1; then - TAG="debian/$VERSION" -else - echo "Invalid version $VERSION (there's no debian/$VERSION tag in the git repo !)" - usage - exit -fi + LASTVERSIONCHANGELOG=$(dpkg-parsechangelog -S Version 2>/dev/null) + + if [[ "$VERSION" != "$LASTVERSIONCHANGELOG" ]] + then + echo "Version $VERSION is not the last version in changelog" + exit + fi + + # Check if tag really exists + if ! git rev-parse "$TAG" >/dev/null 2>&1; + then + echo "Invalid version $VERSION (there's no tag $TAG in the git repo !)" + exit + fi + # Get commit for the tag and for HEAD + TAGCOMMIT=$(git rev-parse "$TAG") + HEADCOMMIT=$(git rev-parse "HEAD") + + if [[ "$TAGCOMMIT" != "$HEADCOMMIT" ]] + then + echo "Tag $TAG is not the HEAD of the branch :/" + exit + fi -# Create temporary folder -TMP_FOLDER=$(mktemp -d) +} -# Extract git archive a desired tag -echo "" -echo "## Exporting in $TMP_FOLDER ..." -git archive $TAG --format=tar | tar -x -C $TMP_FOLDER +function build() +{ + # Create temporary folder + TMP_FOLDER=$(mktemp -d) -# Build Debian package -echo "" -echo "## Building Debian package ..." -echo "" -cd $TMP_FOLDER -$BUILD_DEB -d $BRANCH -c $DISTRIB . + # Extract git archive a desired tag + echo "" + echo "> Exporting in $TMP_FOLDER ... " + echo "" + git archive $TAG --format=tar | tar -x -C $TMP_FOLDER + + # Build Debian package + echo "###############################" + echo "# Building Debian package ... #" + echo "###############################" + cd $TMP_FOLDER + $BUILD_DEB -d $BRANCH -c $DISTRIB . +} + + +function main() +{ + + # If used asked for help, show usage + if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then + usage + exit 0 + fi + + validate_arguments + + # Sum up configuration + echo "########################################################" + echo "# Building $PROJECT $BRANCH release - $VERSION version " + echo "########################################################" + + checkout_tag + build + +} + + +main