From f6a1ba4632ac4c0dbd894c32e0517cf01f48724c Mon Sep 17 00:00:00 2001
From: Akira Ohgaki <akiraohgaki@gmail.com>
Date: Thu, 8 Feb 2018 09:36:15 +0900
Subject: [PATCH] Rewrite packaging script

---
 .travis.yml             |   8 +--
 scripts/build-docker.sh | 119 -----------------------------------
 scripts/build.sh        |  98 -----------------------------
 scripts/package.sh      | 134 ++++++++++++++++++++++++++++++++++++++++
 scripts/scripts.pri     |   3 +-
 5 files changed, 139 insertions(+), 223 deletions(-)
 delete mode 100644 scripts/build-docker.sh
 delete mode 100644 scripts/build.sh
 create mode 100644 scripts/package.sh

diff --git a/.travis.yml b/.travis.yml
index 04f3940..ffd1375 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -10,8 +10,8 @@ branches:
 
 script:
   - mntdir='/mnt/ocs-url'
-  - docker run --rm -v $(pwd):${mntdir} ubuntu:14.04 /bin/bash -c "sh ${mntdir}/scripts/build-docker.sh ubuntu"
-  - docker run --rm -v $(pwd):${mntdir} fedora:20 /bin/bash -c "sh ${mntdir}/scripts/build-docker.sh fedora"
-  - docker run --rm -v $(pwd):${mntdir} opensuse:42.1 /bin/bash -c "sh ${mntdir}/scripts/build-docker.sh opensuse"
-  - docker run --rm -v $(pwd):${mntdir} base/archlinux:latest /bin/bash -c "sh ${mntdir}/scripts/build-docker.sh archlinux"
+  - docker run --rm -v $(pwd):${mntdir} ubuntu:14.04 /bin/bash -c "sh ${mntdir}/scripts/package.sh ci_ubuntu_deb"
+  - docker run --rm -v $(pwd):${mntdir} fedora:20 /bin/bash -c "sh ${mntdir}/scripts/package.sh ci_fedora_rpm"
+  - docker run --rm -v $(pwd):${mntdir} opensuse:42.1 /bin/bash -c "sh ${mntdir}/scripts/package.sh ci_opensuse_rpm"
+  - docker run --rm -v $(pwd):${mntdir} base/archlinux:latest /bin/bash -c "sh ${mntdir}/scripts/package.sh ci_archlinux_pkg"
   - cat transfer.log
diff --git a/scripts/build-docker.sh b/scripts/build-docker.sh
deleted file mode 100644
index 6059549..0000000
--- a/scripts/build-docker.sh
+++ /dev/null
@@ -1,119 +0,0 @@
-#!/bin/bash
-
-PKGNAME='ocs-url'
-
-PKGUSER='pkgbuilder'
-
-BUILDTYPE=''
-if [ "${1}" ]; then
-    BUILDTYPE="${1}"
-fi
-
-PROJDIR="$(cd "$(dirname "${0}")/../" && pwd)"
-
-BUILDSCRIPT="${PROJDIR}/scripts/build.sh"
-
-TRANSFERLOG="${PROJDIR}/transfer.log"
-
-transfer_file() {
-    filepath="${1}"
-    if [ -f "${filepath}" ]; then
-        filename="$(basename "${filepath}")"
-        echo "Uploading ${filename}" >> "${TRANSFERLOG}"
-        curl -fsSL -T "${filepath}" "https://transfer.sh/${filename}" >> "${TRANSFERLOG}"
-        echo '' >> "${TRANSFERLOG}"
-    fi
-}
-
-build_ubuntu() {
-    # docker-image: ubuntu:14.04
-
-    apt update -qq
-    apt -y install curl git
-    apt -y install build-essential qt5-default libqt5svg5-dev qtdeclarative5-dev
-    apt -y install devscripts debhelper fakeroot
-
-    useradd -m ${PKGUSER}
-    chown -R ${PKGUSER}:${PKGUSER} "${PROJDIR}"
-
-    su -c "export HOME=/home/${PKGUSER} && sh "${BUILDSCRIPT}" ${BUILDTYPE}" ${PKGUSER}
-
-    transfer_file "$(find "${PROJDIR}/build_"*${BUILDTYPE} -type f -name "${PKGNAME}*.deb")"
-}
-
-build_fedora() {
-    # docker-image: fedora:20
-
-    yum -y install curl git
-    yum -y install make automake gcc gcc-c++ libtool qt5-qtbase-devel qt5-qtsvg-devel qt5-qtdeclarative-devel
-    yum -y install rpm-build
-
-    useradd -m ${PKGUSER}
-    chown -R ${PKGUSER}:${PKGUSER} "${PROJDIR}"
-
-    su -c "export HOME=/home/${PKGUSER} && sh "${BUILDSCRIPT}" ${BUILDTYPE}" ${PKGUSER}
-
-    transfer_file "$(find "${PROJDIR}/build_"*${BUILDTYPE} -type f -name "${PKGNAME}*.rpm")"
-}
-
-build_opensuse() {
-    # docker-image: opensuse:42.1
-
-    zypper --non-interactive refresh
-    zypper --non-interactive install curl git
-    zypper --non-interactive install make automake gcc gcc-c++ libtool libqt5-qtbase-devel libqt5-qtsvg-devel libqt5-qtdeclarative-devel
-    zypper --non-interactive install rpm-build
-
-    useradd -m ${PKGUSER}
-    chown -R ${PKGUSER}:${PKGUSER} "${PROJDIR}"
-
-    su -c "export HOME=/home/${PKGUSER} && sh "${BUILDSCRIPT}" ${BUILDTYPE}" ${PKGUSER}
-
-    transfer_file "$(find "${PROJDIR}/build_"*${BUILDTYPE} -type f -name "${PKGNAME}*.rpm")"
-}
-
-build_archlinux() {
-    # docker-image: base/archlinux:latest
-
-    pacman -Syu --noconfirm
-    pacman -S --noconfirm curl git
-    pacman -S --noconfirm base-devel qt5-base qt5-svg qt5-declarative qt5-quickcontrols
-
-    useradd -m ${PKGUSER}
-    chown -R ${PKGUSER}:${PKGUSER} "${PROJDIR}"
-
-    su -c "export HOME=/home/${PKGUSER} && sh "${BUILDSCRIPT}" ${BUILDTYPE}" ${PKGUSER}
-
-    transfer_file "$(find "${PROJDIR}/build_"*${BUILDTYPE} -type f -name "${PKGNAME}*.pkg.tar.xz")"
-}
-
-build_appimage() {
-    echo 'Not implemented yet'
-}
-
-build_snap() {
-    echo 'Not implemented yet'
-}
-
-build_flatpak() {
-    echo 'Not implemented yet'
-}
-
-if [ "${BUILDTYPE}" = 'ubuntu' ]; then
-    build_ubuntu
-elif [ "${BUILDTYPE}" = 'fedora' ]; then
-    build_fedora
-elif [ "${BUILDTYPE}" = 'opensuse' ]; then
-    build_opensuse
-elif [ "${BUILDTYPE}" = 'archlinux' ]; then
-    build_archlinux
-elif [ "${BUILDTYPE}" = 'appimage' ]; then
-    build_appimage
-elif [ "${BUILDTYPE}" = 'snap' ]; then
-    build_snap
-elif [ "${BUILDTYPE}" = 'flatpak' ]; then
-    build_flatpak
-else
-    echo "sh $(basename "${0}") [ubuntu|fedora|archlinux|appimage|snap|flatpak]"
-    exit 1
-fi
diff --git a/scripts/build.sh b/scripts/build.sh
deleted file mode 100644
index 3eed0a2..0000000
--- a/scripts/build.sh
+++ /dev/null
@@ -1,98 +0,0 @@
-#!/bin/bash
-
-PKGNAME='ocs-url'
-
-BUILDTYPE=''
-if [ "${1}" ]; then
-    BUILDTYPE="${1}"
-fi
-
-PROJDIR="$(cd "$(dirname "${0}")/../" && pwd)"
-
-BUILDVER="$(cd "${PROJDIR}" && git describe --always)"
-
-BUILDDIR="${PROJDIR}/build_${PKGNAME}_${BUILDVER}_${BUILDTYPE}"
-
-SRCARCHIVE="${BUILDDIR}/${PKGNAME}.tar.gz"
-
-export_srcarchive() {
-    filepath="${1}"
-    $(cd "${PROJDIR}" && git archive --prefix="${PKGNAME}/" --output="${filepath}" HEAD)
-}
-
-build_ubuntu() {
-    cd "${PROJDIR}"
-    mkdir -p "${BUILDDIR}"
-    export_srcarchive "${SRCARCHIVE}"
-
-    tar -xzf "${SRCARCHIVE}" -C "${BUILDDIR}"
-    cp -r "${PROJDIR}/pkg/ubuntu/debian" "${BUILDDIR}/${PKGNAME}"
-    cd "${BUILDDIR}/${PKGNAME}"
-    debuild -uc -us -b
-}
-
-build_fedora() {
-    cd "${PROJDIR}"
-    mkdir -p "${BUILDDIR}"
-    export_srcarchive "${SRCARCHIVE}"
-
-    mkdir "${BUILDDIR}/SOURCES"
-    mkdir "${BUILDDIR}/SPECS"
-    mv "${SRCARCHIVE}" "${BUILDDIR}/SOURCES"
-    cp "${PROJDIR}/pkg/fedora/${PKGNAME}.spec" "${BUILDDIR}/SPECS"
-    rpmbuild --define "_topdir ${BUILDDIR}" -bb "${BUILDDIR}/SPECS/${PKGNAME}.spec"
-}
-
-build_opensuse() {
-    cd "${PROJDIR}"
-    mkdir -p "${BUILDDIR}"
-    export_srcarchive "${SRCARCHIVE}"
-
-    mkdir "${BUILDDIR}/SOURCES"
-    mkdir "${BUILDDIR}/SPECS"
-    mv "${SRCARCHIVE}" "${BUILDDIR}/SOURCES"
-    cp "${PROJDIR}/pkg/opensuse/${PKGNAME}.spec" "${BUILDDIR}/SPECS"
-    rpmbuild --define "_topdir ${BUILDDIR}" -bb "${BUILDDIR}/SPECS/${PKGNAME}.spec"
-}
-
-build_archlinux() {
-    cd "${PROJDIR}"
-    mkdir -p "${BUILDDIR}"
-    export_srcarchive "${SRCARCHIVE}"
-
-    cp "${PROJDIR}/pkg/archlinux/PKGBUILD" "${BUILDDIR}"
-    cd "${BUILDDIR}"
-    updpkgsums
-    makepkg -s
-}
-
-build_appimage() {
-    echo 'Not implemented yet'
-}
-
-build_snap() {
-    echo 'Not implemented yet'
-}
-
-build_flatpak() {
-    echo 'Not implemented yet'
-}
-
-if [ "${BUILDTYPE}" = 'ubuntu' ]; then
-    build_ubuntu
-elif [ "${BUILDTYPE}" = 'fedora' ]; then
-    build_fedora
-elif [ "${BUILDTYPE}" = 'opensuse' ]; then
-    build_opensuse
-elif [ "${BUILDTYPE}" = 'archlinux' ]; then
-    build_archlinux
-elif [ "${BUILDTYPE}" = 'appimage' ]; then
-    build_appimage
-elif [ "${BUILDTYPE}" = 'snap' ]; then
-    build_snap
-elif [ "${BUILDTYPE}" = 'flatpak' ]; then
-    build_flatpak
-else
-    echo "sh $(basename "${0}") [ubuntu|fedora|archlinux|appimage|snap|flatpak]"
-    exit 1
-fi
diff --git a/scripts/package.sh b/scripts/package.sh
new file mode 100644
index 0000000..7b50324
--- /dev/null
+++ b/scripts/package.sh
@@ -0,0 +1,134 @@
+#!/bin/bash
+
+PKGNAME='ocs-url'
+
+USER='pkgbuilder'
+
+SCRIPT="${0}"
+
+FUNCTION=''
+if [ "${1}" ]; then
+    FUNCTION="${1}"
+fi
+
+PROJDIR="$(cd "$(dirname "${0}")/../" && pwd)"
+
+BUILDDIR="${PROJDIR}/build_${PKGNAME}_${FUNCTION}"
+
+export_srcarchive() {
+    filepath="${1}"
+    $(cd "${PROJDIR}" && git archive --prefix="${PKGNAME}/" --output="${filepath}" HEAD)
+}
+
+transfer_file() {
+    filepath="${1}"
+    if [ -f "${filepath}" ]; then
+        filename="$(basename "${filepath}")"
+        logfilepath="${PROJDIR}/transfer.log"
+        echo "Uploading ${filename}" >> "${logfilepath}"
+        curl -fsSL -T "${filepath}" "https://transfer.sh/${filename}" >> "${logfilepath}"
+        echo '' >> "${logfilepath}"
+    fi
+}
+
+build_ubuntu_deb() {
+    cd "${PROJDIR}"
+    mkdir -p "${BUILDDIR}"
+    export_srcarchive "${BUILDDIR}/${PKGNAME}.tar.gz"
+    tar -xzf "${BUILDDIR}/${PKGNAME}.tar.gz" -C "${BUILDDIR}"
+    cp -r "${PROJDIR}/pkg/ubuntu/debian" "${BUILDDIR}/${PKGNAME}"
+    cd "${BUILDDIR}/${PKGNAME}"
+    debuild -uc -us -b
+}
+
+build_fedora_rpm() {
+    cd "${PROJDIR}"
+    mkdir -p "${BUILDDIR}"
+    export_srcarchive "${BUILDDIR}/${PKGNAME}.tar.gz"
+
+    mkdir "${BUILDDIR}/SOURCES"
+    mkdir "${BUILDDIR}/SPECS"
+    mv "${BUILDDIR}/${PKGNAME}.tar.gz" "${BUILDDIR}/SOURCES"
+    cp "${PROJDIR}/pkg/fedora/${PKGNAME}.spec" "${BUILDDIR}/SPECS"
+    rpmbuild --define "_topdir ${BUILDDIR}" -bb "${BUILDDIR}/SPECS/${PKGNAME}.spec"
+}
+
+build_opensuse_rpm() {
+    cd "${PROJDIR}"
+    mkdir -p "${BUILDDIR}"
+    export_srcarchive "${BUILDDIR}/${PKGNAME}.tar.gz"
+
+    mkdir "${BUILDDIR}/SOURCES"
+    mkdir "${BUILDDIR}/SPECS"
+    mv "${BUILDDIR}/${PKGNAME}.tar.gz" "${BUILDDIR}/SOURCES"
+    cp "${PROJDIR}/pkg/opensuse/${PKGNAME}.spec" "${BUILDDIR}/SPECS"
+    rpmbuild --define "_topdir ${BUILDDIR}" -bb "${BUILDDIR}/SPECS/${PKGNAME}.spec"
+}
+
+build_archlinux_pkg() {
+    cd "${PROJDIR}"
+    mkdir -p "${BUILDDIR}"
+    export_srcarchive "${BUILDDIR}/${PKGNAME}.tar.gz"
+
+    cp "${PROJDIR}/pkg/archlinux/PKGBUILD" "${BUILDDIR}"
+    cd "${BUILDDIR}"
+    updpkgsums
+    makepkg -s
+}
+
+ci_ubuntu_deb() { # docker-image: ubuntu:14.04
+    apt update -qq
+    apt -y install curl git
+    apt -y install build-essential qt5-default libqt5svg5-dev qtdeclarative5-dev
+    apt -y install devscripts debhelper fakeroot
+
+    useradd -m ${USER}
+    chown -R ${USER}:${USER} "${PROJDIR}"
+
+    su -c "export HOME=/home/${USER} && sh "${SCRIPT}" build_ubuntu_deb" ${USER}
+
+    transfer_file "$(find "${BUILDDIR}" -type f -name "${PKGNAME}*.deb")"
+}
+
+ci_fedora_rpm() { # docker-image: fedora:20
+    yum -y install curl git
+    yum -y install make automake gcc gcc-c++ libtool qt5-qtbase-devel qt5-qtsvg-devel qt5-qtdeclarative-devel
+    yum -y install rpm-build
+
+    useradd -m ${USER}
+    chown -R ${USER}:${USER} "${PROJDIR}"
+
+    su -c "export HOME=/home/${USER} && sh "${SCRIPT}" build_fedora_rpm" ${USER}
+
+    transfer_file "$(find "${PROJDIR}/build_"*${FUNCTION} -type f -name "${PKGNAME}*.rpm")"
+}
+
+
+ci_opensuse_rpm() { # docker-image: opensuse:42.1
+    zypper --non-interactive refresh
+    zypper --non-interactive install curl git
+    zypper --non-interactive install make automake gcc gcc-c++ libtool libqt5-qtbase-devel libqt5-qtsvg-devel libqt5-qtdeclarative-devel
+    zypper --non-interactive install rpm-build
+
+    useradd -m ${USER}
+    chown -R ${USER}:${USER} "${PROJDIR}"
+
+    su -c "export HOME=/home/${USER} && sh "${SCRIPT}" build_opensuse_rpm" ${USER}
+
+    transfer_file "$(find "${PROJDIR}/build_"*${FUNCTION} -type f -name "${PKGNAME}*.rpm")"
+}
+
+ci_archlinux_pkg() { # docker-image: base/archlinux:latest
+    pacman -Syu --noconfirm
+    pacman -S --noconfirm curl git
+    pacman -S --noconfirm base-devel qt5-base qt5-svg qt5-declarative qt5-quickcontrols
+
+    useradd -m ${USER}
+    chown -R ${USER}:${USER} "${PROJDIR}"
+
+    su -c "export HOME=/home/${USER} && sh "${SCRIPT}" build_archlinux_pkg" ${USER}
+
+    transfer_file "$(find "${PROJDIR}/build_"*${FUNCTION} -type f -name "${PKGNAME}*.pkg.tar.xz")"
+}
+
+${FUNCTION}
diff --git a/scripts/scripts.pri b/scripts/scripts.pri
index cffa148..de6b1aa 100644
--- a/scripts/scripts.pri
+++ b/scripts/scripts.pri
@@ -1,4 +1,3 @@
 DISTFILES += \
-    $${PWD}/build-docker.sh \
-    $${PWD}/build.sh \
+    $${PWD}/package.sh \
     $${PWD}/prepare.sh
-- 
GitLab