From: hayao Date: Sun, 4 Oct 2020 02:39:22 +0000 (+0900) Subject: [update] : Enhanced clean.sh X-Git-Tag: rc3-alpha1~152 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=de3dc8d08b34f488f1a0d895ff4da4d81982eb0a;p=alterlinux%2Falterlinux.git [update] : Enhanced clean.sh --- diff --git a/allarch.sh b/allarch.sh index 2b70c6df..31e60a44 100755 --- a/allarch.sh +++ b/allarch.sh @@ -1392,13 +1392,7 @@ elif [[ "${channel_name}" = "rebuild" ]]; then msg_error "The previous build information is not in the working directory." "1" fi elif [[ "${channel_name}" = "clean" ]]; then - umount_chroot - remove "${script_path}/menuconfig/build" - remove "${script_path}/system/cpp-src/mkalteriso/build" - remove "${script_path}/menuconfig-script/kernel_choice" - remove "${work_dir%/}"/* - remove "${work_dir}" - remove "${rebuildfile}" + "${script_path}/tools/clean.sh" -w $(realpath "${work_dir}") $([[ "${debug}" = true ]] && echo -n "-d") exit 0 fi diff --git a/build.sh b/build.sh index 92c5fa4c..2b4c4121 100755 --- a/build.sh +++ b/build.sh @@ -1474,13 +1474,7 @@ elif [[ "${channel_name}" = "rebuild" ]]; then msg_error "The previous build information is not in the working directory." "1" fi elif [[ "${channel_name}" = "clean" ]]; then - umount_chroot - remove "${script_path}/menuconfig/build" - remove "${script_path}/system/cpp-src/mkalteriso/build" - remove "${script_path}/menuconfig-script/kernel_choice" - remove "${work_dir%/}"/* - remove "${work_dir}" - remove "${rebuildfile}" + "${script_path}/tools/clean.sh" -w $(realpath "${work_dir}") $([[ "${debug}" = true ]] && echo -n "-d") exit 0 fi diff --git a/tools/clean.sh b/tools/clean.sh index dedcfc7a..d57d14e5 100755 --- a/tools/clean.sh +++ b/tools/clean.sh @@ -1,9 +1,127 @@ #!/usr/bin/env bash script_path="$( cd -P "$( dirname "$(readlink -f "$0")" )" && cd .. && pwd )" +work_dir="${script_path}/work" +debug=false -cd "${script_path}" -sudo make clean -sudo rm -f "${script_path}/system/mkalteriso" +# Show an INFO message +# $1: message string +msg_info() { + local _msg_opts="-a clean.sh" + if [[ "${1}" = "-n" ]]; then + _msg_opts="${_msg_opts} -o -n" + shift 1 + fi + "${script_path}/tools/msg.sh" ${_msg_opts} info "${1}" +} + +# Show an Warning message +# $1: message string +msg_warn() { + local _msg_opts="-a clean.sh" + if [[ "${1}" = "-n" ]]; then + _msg_opts="${_msg_opts} -o -n" + shift 1 + fi + "${script_path}/tools/msg.sh" ${_msg_opts} warn "${1}" +} + +# Show an debug message +# $1: message string +msg_debug() { + if [[ "${debug}" = true ]]; then + local _msg_opts="-a clean.sh" + if [[ "${1}" = "-n" ]]; then + _msg_opts="${_msg_opts} -o -n" + shift 1 + fi + "${script_path}/tools/msg.sh" ${_msg_opts} debug "${1}" + fi +} + +# Show an ERROR message then exit with status +# $1: message string +# $2: exit code number (with 0 does not exit) +msg_error() { + local _msg_opts="-a clean.sh" + if [[ "${1}" = "-n" ]]; then + _msg_opts="${_msg_opts} -o -n" + shift 1 + fi + "${script_path}/tools/msg.sh" ${_msg_opts} error "${1}" + if [[ -n "${2:-}" ]]; then + exit ${2} + fi +} + +# rm helper +# Delete the file if it exists. +# For directories, rm -rf is used. +# If the file does not exist, skip it. +# remove ... +remove() { + local _list=($(echo "$@")) _file + for _file in "${_list[@]}"; do + if [[ -f ${_file} ]]; then + msg_info "Removeing ${_file}" + rm -f "${_file}" + elif [[ -d ${_file} ]]; then + msg_info "Removeing ${_file}" + rm -rf "${_file}" + fi + done +} + +# Unmount chroot dir +umount_chroot () { + local _mount + for _mount in $(mount | getclm 3 | grep $(realpath ${work_dir}) | tac); do + msg_info "Unmounting ${_mount}" + umount -lf "${_mount}" 2> /dev/null + done +} + +# Usage: getclm +# 標準入力から値を受けとり、引数で指定された列を抽出します。 +getclm() { + echo "$(cat -)" | cut -d " " -f "${1}" +} + +_help() { + echo "usage ${0} [option]" + echo + echo "Outputs colored messages" + echo + echo " General options:" + echo " -d Show debug message" + echo " -w [dir] Specify the work dir" + echo " -h This help message" +} + +while getopts "dw:h" arg; do + case ${arg} in + d) debug=true ;; + w) work_dir="${OPTARG}" ;; + h) + _help + exit 0 + ;; + *) + _help + exit 1 + ;; + esac +done + +shift $((OPTIND - 1)) + +msg_debug "ほげえ" + +umount_chroot +remove "${script_path}/menuconfig/build/"** +remove "${script_path}/system/cpp-src/mkalteriso/build"/** +remove "${script_path}/menuconfig-script/kernel_choice" +remove "${work_dir%/}"/** +remove "${work_dir}" +remove "${script_path}/system/mkalteriso" -cd - > /dev/null