summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: d7ac5507c1020282d2eab4bb58ac871292d2a7e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Maintainer: zmr233 <zmr_233@outlook.com>
# Contributor and Ahthor: qier22 <qier222@outlook.com>

# Refer to https://aur.archlinux.org/packages/yesplaymusic-git
# 参考了如上AUR包的PKGBUILD文件以使用系统electron

_pkgname=yesplaymusic
pkgname=yesplaymusic-plus
pkgver=0.4.9
pkgrel=7
pkgdesc="A third-party music player for Netease Music"
arch=('x86_64')
url="https://github.com/qier222/YesPlayMusic"
license=('MIT')
provides=("${_pkgname}=${pkgver}")

# _electronversion=33 无法确保系统中有对应版本的electron,故使用系统中的electron
_nodeversion=16

conflicts=(
    "my-yesplaymusic-bin"
    "r3play-bin"
    "r3playx-bin"
    "r3playx-git"
    "yesplaymusic"
    "yesplaymusic-appimage"
    "yesplaymusic-bin"
    "yesplaymusic-electron"
    "yesplaymusic-git"
    "yesplaymusic-new"
    "yesplaymusicosd-origin-electron-git"
    "yesplaymusicosd-origin-git"
)

depends=(
    "electron"
)

makedepends=(
    'jq'
    'npm'
    'nvm'
    'git'
    'curl'
    'yarn'
    'gcc'
    'python-setuptools'
)

source=("$pkgname-$pkgver.tar.gz::https://github.com/qier222/YesPlayMusic/archive/refs/tags/v$pkgver.tar.gz"
        "$pkgname.desktop"
        "$pkgname.sh")

sha256sums=('9b240d6b605d547c9dbc7d6222a0d2f28bfb0586201e01181e1d0e25bb83885a'
            '8139caec51a8b831d7e41fccb4e89daecbe1119cc6a4be9fd9f4ed2848690a69'
            '6947c3866ce502e80f4e4cda811a8c22e4b902f69fca4c02495fe1eccda4a63b'
)

_ensure_local_nvm() {
    local NVM_DIR="${srcdir}/.nvm"
    source /usr/share/nvm/init-nvm.sh || [[ $? != 1 ]]
    nvm install "${_nodeversion}"
    nvm use "${_nodeversion}"
}

# 获取国家代码
_get_country_code() {
    local response
    # 尝试不使用代理
    response=$(curl -s --noproxy ip-api.com --max-time 2 http://ip-api.com/json)
    if [[ $? -eq 0 && -n "$response" ]]; then
        echo "$response" | jq -r .countryCode
        return 0
    fi

    # 尝试使用代理
    response=$(curl -s --max-time 2 http://ip-api.com/json)
    if [[ $? -eq 0 && -n "$response" ]]; then
        echo "$response" | jq -r .countryCode
        return 0
    fi

    # 尝试备用服务不使用代理
    response=$(curl -s --noproxy ipinfo.io --max-time 2 http://ipinfo.io/json)
    if [[ $? -eq 0 && -n "$response" ]]; then
        echo "$response" | jq -r .country
        return 0
    fi

    # 尝试备用服务使用代理
    response=$(curl -s --max-time 2 http://ipinfo.io/json)
    if [[ $? -eq 0 && -n "$response" ]]; then
        echo "$response" | jq -r .country
        return 0
    fi

    # 如果所有方法都失败,返回空并发出警告
    echo "Failed to retrieve country code from all services." >&2
    return 1
}

prepare() {
    export SYSTEM_ELECTRON_VERSION="$(electron -v | sed 's/v//g')"
    # 检查最低electron版本
    if [[ $(vercmp "$SYSTEM_ELECTRON_VERSION" "13.0.0") -lt 0 ]]; then
        echo "Error: Electron version 13.0.0 or higher is required."
        exit 1
    fi
}

build() {
    # 设置启动脚本
    #  不再特殊指定版本 => 使用系统中的electron
    sed -e "
        s/@electronversion@//g 
        s/@appname@/${_pkgname}/g
        s/@runname@/app.asar/g
        s/@cfgdirname@/${_pkgname}/g
        s/@options@//g
    " -i "${srcdir}/${pkgname}.sh"

    # 设置构建node版本
    _ensure_local_nvm
    cd "$srcdir/YesPlayMusic-$pkgver"

    # 设置系统electron版本
    
    HOME="${srcdir}/.electron-gyp"
    mkdir -p "${srcdir}/.electron-gyp"

    # 获取国家代码
    countryCode=$(_get_country_code)

    if [[ "$countryCode" == "CN" ]]; then
        echo "Using mirror in China"
        {
            echo -e '\n'
            echo 'registry "https://repo.nju.edu.cn/repository/npm/"'
            echo 'electron_mirror "https://registry.npmmirror.com/-/binary/electron/"'
            echo 'electron_builder_binaries_mirror "https://registry.npmmirror.com/-/binary/electron-builder-binaries/"'
            echo "cacheFolder \"${srcdir}/.yarn/cache\""
            echo "pluginsFolder \"${srcdir}/.yarn/plugins\""
            echo "globalFolder \"${srcdir}/.yarn/global\""
            echo 'useHardlinks true'
            #echo 'buildFromSource true'
            echo 'linkWorkspacePackages true'
            echo 'fetchRetries 3'
            echo 'fetchRetryTimeout 10000'
        } >> .yarnrc
        find ./ -type f -name "yarn.lock" -exec sed -i "s/registry.yarnpkg.com/registry.npmmirror.com/g;s/registry.npmjs.org/registry.npmmirror.com/g" {} +
    elif [[ -z "$countryCode" ]]; then
        echo "Empty countryCode from ip-api.com and ipinfo.io"
    elif [[ $? -ne 0 ]]; then
        echo "Failed to retrieve country code from all services. Proceeding without proxy settings."
    else 
        echo "No proxy"
    fi

    # 设置较高并发数
    yarn config set network-concurrency 16

    sed -i "s/\"electron\": \"[^\"]*\"/\"electron\": \"${SYSTEM_ELECTRON_VERSION}\"/g" package.json

    # 添加 homepage 字段到 package.json
    sed -i 's/"name": "yesplaymusic"/"name": "yesplaymusic", "homepage": "https:\/\/github.com\/qier222\/YesPlayMusic"/' package.json
    
    # 更换版本号
    sed -i "s/\"version\": \".*\"/\"version\": \"$pkgver\"/" package.json
    
    # 设置网易云API
    cp .env.example .env

    NODE_ENV=development    yarn install --cache-folder "${srcdir}/.yarn_cache" --ignore-optional
    NODE_ENV=development    yarn add --ignore-platform --ignore-optional \
                                @unblockneteasemusic/rust-napi-linux-x64-gnu@0.4.0 \
                                @unblockneteasemusic/rust-napi-linux-arm64-gnu@0.4.0 \
                                @unblockneteasemusic/rust-napi-linux-arm-gnueabihf@0.4.0 \
                                utf-8-validate
    NODE_ENV=production     yarn run electron:build-linux --dir
}

package() {
  cd "$srcdir/YesPlayMusic-$pkgver"

  # 安装主程序
  install -Dm644 "dist_electron/linux-"*/resources/app.asar -t "${pkgdir}/usr/lib/${_pkgname}"

  # 安装启动脚本
  install -Dm755 "$srcdir/$pkgname.sh" "$pkgdir/usr/bin/$_pkgname"

  # 安装桌面条目和图标
  install -Dm644 "$srcdir/$pkgname.desktop" "$pkgdir/usr/share/applications/$_pkgname.desktop"
  for size in 16 32 48 64 128 256 512; do
    install -Dm644 "build/icons/${size}x${size}.png" "$pkgdir/usr/share/icons/hicolor/${size}x${size}/apps/$_pkgname.png"
  done

  # 安装许可证
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$_pkgname/LICENSE"
}