OSDN Git Service

[update] : Added --clean
[alterlinux/wfa.git] / wfa
diff --git a/wfa b/wfa
index b50707c..e9c6ce3 100755 (executable)
--- a/wfa
+++ b/wfa
@@ -1,4 +1,34 @@
 #!/usr/bin/env bash
+#
+# 1. Author info
+#
+# Yamada Hayao
+# Twitter: @Hayao0819
+# Email  : hayao@fascode.net
+#
+# (c) 2019-2020 Fascode Network.
+#
+# 2. Overview
+# 
+# Wfa is a multilingual AUR helper written in bash that is being developed to replace yaourt
+#
+# 3. License
+# 
+#        DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
+#                    Version 2, December 2004 
+#
+# Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> 
+#
+# Everyone is permitted to copy and distribute verbatim or modified 
+# copies of this license document, and changing it is allowed as long 
+# as the name is changed. 
+#
+#            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
+#   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 
+#
+#  0. You just DO WHAT THE FUCK YOU WANT TO.
+#
+#
 
 set -eu
 
@@ -11,7 +41,7 @@ declare -A ja_JP=(
     ["only one operation may be used at a time"]="一度に使用できるオペレーションは1つだけです"
     ["Failed to set the argument of %s"]="%sの引数の設定に失敗しました"
     ["Setting that command is not currently supported"]="そのコマンドの設定は現在サポートされていません"
-    ["Install dependent packages..."]="依存パッケージをインストールします..."
+    ["Searching in AUR ..."]="AUR内を検索しています..."
     ["No package with an exact name match was found"]="完全に一致する名前のパッケージが見つかりませんでした"
     ["Select a package %s with an exact name match"]="名前が完全に一致するパッケージ %s を選択します"
     ["Download PKGBUILD of %s"]="%s のPKGBUILDをダウンロード"
@@ -19,7 +49,16 @@ declare -A ja_JP=(
     ["PKGBUILD has already been downloaded"]="PKGBUILDは既にダウンロードされています"
     ["Do you want to overwrite and download? [n] :"]="上書きダウンロードしますか? [n] :"
     ["Unpacking the tarball of PKGBUILD ..."]="PKGBUILDを展開しています ..."
+    [".SRCINFO was not found.\nGenerating it using makepkg"]=".SCRINFOが見つかりませんでした。makepkgを使用して生成しています"
+    ["Conflict(s) was found"]="衝突が見つかりました"
+    ["Install dependent packages..."]="依存パッケージをインストールします..."
+    ["Found %s"]="%s を見つけました"
+    ["Packages to cleanBuild? [n] :"]="パッケージをクリーンビルドしますか? [n] :"
+    ["Could not find all required packages:\n  %s"]="必要なすべてのパッケージが見つかりませんでした     %s"
+    ["This is a feature that has not been implemented yet"]="まだ実装されていない機能です"
 
+    ["(Orphaned)"]="(メンテナ不在)"
+    ["[Installed]"]="[インストール済み]"
     ["This is a feature that has not been implemented yet"]="まだ実装されていない機能です"
 )
 ######################################################################################
@@ -32,6 +71,7 @@ wfa_command="wfa"
 
 #-- options (int) --#
 option_y_count=0
+sync_clean_count=0
 
 #-- options (str) --#
 aururl="https://aur.archlinux.org/"
@@ -111,7 +151,7 @@ msg() {
     local _output="stdout"
 
     _help() {
-        echo "usage ${0} [option] [type] [message]"
+        echo "usage msg [option] [type] [message]"
         echo
         echo "Display a message with a colored app name and message type label"
         echo
@@ -460,6 +500,57 @@ msg_error() {
     fi
 }
 
+text() {
+    local OPTIND OPTARG arg _textcolor _decotypes=() _message
+    while getopts "c:b" arg; do
+        case ${arg} in
+            c)
+                case "${OPTARG}" in
+                    "black")
+                        _textcolor="30"
+                        ;;
+                    "red")
+                        _textcolor="31"
+                        ;;
+                    "green")
+                        _textcolor="32"
+                        ;;
+                    "yellow")
+                        _textcolor="33"
+                        ;;
+                    "blue")
+                        _textcolor="34"
+                        ;;
+                    "magenta")
+                        _textcolor="35"
+                        ;;
+                    "cyan")
+                        _textcolor="36"
+                        ;;
+                    "white")
+                        _textcolor="37"
+                        ;;
+                    *)
+                        return 1
+                        ;;
+                esac
+                ;;
+            b)
+                _decotypes+=(1)
+                ;;
+        esac
+    done
+    shift $((OPTIND - 1))
+
+    _message="${@}"
+    _message="$(translate "${@}")"
+    if [[ "${nocolor}" = true ]]; then
+        echo -ne "${@}"
+    else
+        echo -ne "\e[$([[ -v _textcolor ]] && echo -n ";${_textcolor}"; [[ -v _decotypes ]] && echo -n ";${_decotypes}")m${_message}\e[m"
+    fi
+}
+
 ######################################################################################
 # ここから実際の処理開始
 # ここから下のメッセージは翻訳可能です
@@ -639,6 +730,7 @@ check_installed_package() {
     fi
 }
 
+# ~/.cacheに相当するディレクトリを返します
 get_cache_dir() {
     local _user_config_dir _cache_dir
     if [[ -v XDG_CONFIG_HOME ]]; then
@@ -687,6 +779,7 @@ install_aur_package() {
     mkdir -p "${wfa_cache_dir}/build/${_package}"
 
     # AurJsonから値を取得
+    msg_info "Searching in AUR ..."
     local _aur_json=$(curl -sL "https://aur.archlinux.org/rpc/?v=5&type=search&by=name&arg=${_package}" | jq -r)
     if (( "$(echo "${_aur_json}" | jq -r ".resultcount")" == 0 )); then
         msg_error "Could not find all required packages:\n     ${_package}"
@@ -733,13 +826,13 @@ install_aur_package() {
     fi
 
     # PKGBUILDを展開
-    msg_info "Unpacking the tarball of PKGBUILD ..."  #ここまで翻訳
+    msg_info "Unpacking the tarball of PKGBUILD ..."
     tar -xv -f "${_pkgbuild_archive_path}" -C "${wfa_cache_dir}/build/" > /dev/null 2>&1
 
     # .SRCINFOを解析
     local _build_dir="${wfa_cache_dir}/build/${_package}"
     if [[ ! -f "${_build_dir}/.SRCINFO" ]]; then
-        msg_warn ".SRCINFO was not found.\nGenerate it using makepkg."
+        msg_warn ".SRCINFO was not found.\nGenerating it using makepkg"
         (
             cd "${_build_dir}"
             "${makepkg_command}" --printsrcinfo > "${_build_dir}/.SRCINFO"
@@ -749,9 +842,9 @@ install_aur_package() {
     local _makedepends="$(get_srcinfo_data "${_build_dir}/.SRCINFO" ".makedepends[]?")"
     local _depends="$(get_srcinfo_data "${_build_dir}/.SRCINFO" ".depends[]?")"
     local _conflicts="$(get_srcinfo_data "${_build_dir}/.SRCINFO" ".conflicts[]?")"
-    msg_debug "makedepends: ${_makedepends}"
-    msg_debug "depends: ${_depends}"
-    msg_debug "conflicts: ${_conflicts}"
+    msg_debug "makedepends: %s" "${_makedepends}"
+    msg_debug "depends: %s" "${_depends}"
+    msg_debug "conflicts: %s " "${_conflicts}"
 
 
     # 衝突を確認
@@ -767,7 +860,7 @@ install_aur_package() {
         _conflicts_found=true
     fi
     if [[ "${_conflicts_found}" = true ]]; then
-        msg_error "A conflict was found."
+        msg_error "Conflict(s) was found"
         exit 1
     fi
 
@@ -785,7 +878,7 @@ install_aur_package() {
     # ビルド準備
     # srcdirの確認
     if [[ -d "${_build_dir}/src" ]]; then
-        msg_info "Found ${_build_dir}/src"
+        msg_info "Found %s" "${_build_dir}/src"
         msg_info -n "Packages to cleanBuild? [n] :"
         local _yes_or_no
         unset _yes_or_no
@@ -840,32 +933,59 @@ install_aur_package() {
 # AURのパッケージを検索
 search_aur_package() {
     local _package="${1}"
+    #msg_info "Searching in AUR ..."
     local _aur_json=$(curl -sL "https://aur.archlinux.org/rpc/?v=5&type=search&by=name-desc&arg=${_package}" | jq -r )
     local _found_result_count="$(echo "${_aur_json}" | jq -r ".resultcount")"
     if (( "${_found_result_count}" == 0 )); then
-        msg_error "Could not find all required packages:\n     ${_package}"
+        msg_error "Could not find all required packages:\n     %s" "${_package}"
         exit 1
     fi
     _aur_json=$(echo "${_aur_json}" | jq -r ".results[]")
 
-    #echo ${_aur_json} 
-
     local _found_pkgname=($(echo "${_aur_json}" | jq -r ".Name" ))
 
-
-
-
-    local  _found_package __pkgver __popularity __vote __pkgdesc
+    local  _found_package __pkgver __popularity __vote __pkgdesc __orphaned __installed __output_text=()
     for _found_package in ${_found_pkgname[@]}; do
+        __orphaned=false
+        __installed=false
         _found_json="$(echo ${_aur_json} | jq "select(.Name == \"${_found_package}\")")"
+
+        #echo "${_found_json}"
         __pkgver="$(echo "${_found_json}" | jq -r ".Version" )"
         __popularity="$(echo "${_found_json}" | jq -r ".Popularity" )"
         __vote="$(echo "${_found_json}" | jq -r ".NumVotes" )"
         __pkgdesc="$(echo "${_found_json}" | jq -r ".Description" )"
-    
-        echo "aur/${_found_package} ${__pkgver} (+${__vote} ${__popularity})"
+
+        # 人気度の少数2位以下を四捨五入
+        # 参考: http://www.rivhiro-weather.com/knowledge/?p=536
+        msg_debug "Row popularity: %s" "${__popularity}"
+        __popularity="$(printf "%g\n" "${__popularity}" | awk '{printf("%4.2f", $1)}')"
+
+        # 孤児判定
+        #参考: https://www.366service.com/jp/qa/7c95f46e5236039134ff5b862ae2cd13
+        if ! echo "${_found_json}" | jq -r --exit-status ".Maintainer" 1> /dev/null 2> /dev/null ; then
+            __orphaned=true
+        fi
+
+        # インストール済み判定
+        if check_installed_package "${_found_package}"; then
+            __installed=true
+        fi
+
+        __output_text+=(
+            "$(text -c blue -b "aur")/$(text -b "${_found_package}") $(text -c cyan "${__pkgver}") (+$(text -b "${__vote} ${__popularity}"))"
+        )
+        
+        if [[ "${__orphaned}" = true ]]; then
+            __output_text+=("$(text -bc red "(Orphaned)")")
+        fi
+
+        if [[ "${__installed}" = true ]]; then
+            __output_text+=("$(text -bc cyan "[Installed]")")
+        fi
+        echo "${__output_text[*]}"
         echo "    ${__pkgdesc}"
-        unset __pkgver __popularity __vote __pkgdesc _found_json
+        unset __pkgver __popularity __vote __pkgdesc _found_json __orphaned __installed __output_text
     done
 }
 
@@ -901,16 +1021,28 @@ install_package() {
 }
 
 upgrade_aur_package() {
+    #ここまで翻訳
     msg_error "This is a feature that has not been implemented yet"
 }
 
 operation_sync(){
     local _package
+    if (( "${sync_clean_count}" >= 1 )); then
+        remove "$(get_cache_dir)/wfa"
+        run_pacman ${pacman_args} $(
+            local _count
+            for _count in $(seq 1 ${sync_clean_count}); do
+                echo -n "-c "
+            done
+        )
+    fi
+
+
     if [[ "${sync_search}" = true ]]; then
         for _package in ${specified_packages[@]}; do
             search_aur_package "${_package}"
         done
-        "${pacman_command}" ${pacman_args} ${specified_packages[@]}
+        "${pacman_command}" ${pacman_args} ${specified_packages[@]} || :
     else
         if [[ "${sync_upgrade}" = true ]]; then
             upgrade_aur_package
@@ -933,8 +1065,8 @@ operation_sync(){
 
 # Parse options
 ARGUMENT="${@}"
-_opt_short="QRShVdb:aysu"
-_opt_long="query,remove,sync,help,version,debug,dbpath:,aururl,aur,noconfirm,config:,makepkg:,mflags:,pacman:,git:,gitflags:,gpg:,gpgflags:,makepkgconf:,nomakepkgconf,nodeps,refresh,bash-debug,msg-debug,sysupgrade"
+_opt_short="QRShVdb:aysuc"
+_opt_long="query,remove,sync,help,version,debug,dbpath:,aururl,aur,noconfirm,config:,makepkg:,mflags:,pacman:,git:,gitflags:,gpg:,gpgflags:,makepkgconf:,nomakepkgconf,nodeps,refresh,bash-debug,msg-debug,sysupgrade,color:,nocolor.clean"
 
 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- ${ARGUMENT})
 [[ ${?} != 0 ]] && exit 1
@@ -1007,6 +1139,10 @@ while :; do
             sync_upgrade=true
             shift 1
             ;;
+        -c | --clean)
+            sync_clean_count=$(( sync_clean_count + 1 ))
+            shift 1
+            ;;
         --aururl)
             aururl="${2}"
             shift 2
@@ -1018,7 +1154,7 @@ while :; do
             ;;
         --config)
             pacman_config="${2}"
-            add_args pacman "--config \"${2}\""
+            add_args pacman "--config ${2}"
             shift 2
             ;;
         --makepkg)
@@ -1063,6 +1199,27 @@ while :; do
             msgdebug=true
             shift 1
             ;;
+        --color)
+            case "${2}" in
+                "never")
+                    nocolor=true
+                    add_args pacman "--color never"
+                    ;;
+                "always")
+                    nocolor=false
+                    add_args pacman "--color always"
+                    ;;
+                "auto")
+                    msg_error "auto is not currently supported."
+                    add_args pacman "--color auto"
+                    ;;
+            esac
+            ;;
+        --nocolor)
+            nocolor=true
+            add_args pacman "--color never"
+            shift 1
+            ;;
         -h | --help)
             usage
             shift 1