-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathplugin_repackaging.sh
More file actions
194 lines (179 loc) · 5.22 KB
/
plugin_repackaging.sh
File metadata and controls
194 lines (179 loc) · 5.22 KB
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
#!/bin/bash
# author: Junjie.M
DEFAULT_GITHUB_API_URL=https://github.com
DEFAULT_MARKETPLACE_API_URL=https://marketplace.dify.ai
DEFAULT_PIP_MIRROR_URL=https://mirrors.aliyun.com/pypi/simple
GITHUB_API_URL="${GITHUB_API_URL:-$DEFAULT_GITHUB_API_URL}"
MARKETPLACE_API_URL="${MARKETPLACE_API_URL:-$DEFAULT_MARKETPLACE_API_URL}"
PIP_MIRROR_URL="${PIP_MIRROR_URL:-$DEFAULT_PIP_MIRROR_URL}"
CURR_DIR=`dirname $0`
cd $CURR_DIR
CURR_DIR=`pwd`
USER=`whoami`
ARCH_NAME=`uname -m`
OS_TYPE=$(uname)
OS_TYPE=$(echo "$OS_TYPE" | tr '[:upper:]' '[:lower:]')
CMD_NAME="dify-plugin-${OS_TYPE}-amd64"
if [[ "arm64" == "$ARCH_NAME" || "aarch64" == "$ARCH_NAME" ]]; then
CMD_NAME="dify-plugin-${OS_TYPE}-arm64"
fi
PIP_PLATFORM=""
PACKAGE_SUFFIX="offline"
market(){
if [[ -z "$2" || -z "$3" || -z "$4" ]]; then
echo ""
echo "Usage: "$0" market [plugin author] [plugin name] [plugin version]"
echo "Example:"
echo " "$0" market junjiem mcp_sse 0.0.1"
echo " "$0" market langgenius agent 0.0.9"
echo ""
exit 1
fi
echo "From the Dify Marketplace downloading ..."
PLUGIN_AUTHOR=$2
PLUGIN_NAME=$3
PLUGIN_VERSION=$4
PLUGIN_PACKAGE_PATH=${CURR_DIR}/${PLUGIN_AUTHOR}-${PLUGIN_NAME}_${PLUGIN_VERSION}.difypkg
PLUGIN_DOWNLOAD_URL=${MARKETPLACE_API_URL}/api/v1/plugins/${PLUGIN_AUTHOR}/${PLUGIN_NAME}/${PLUGIN_VERSION}/download
echo "Downloading ${PLUGIN_DOWNLOAD_URL} ..."
curl -L -o ${PLUGIN_PACKAGE_PATH} ${PLUGIN_DOWNLOAD_URL}
if [[ $? -ne 0 ]]; then
echo "Download failed, please check the plugin author, name and version."
exit 1
fi
echo "Download success."
repackage ${PLUGIN_PACKAGE_PATH}
}
github(){
if [[ -z "$2" || -z "$3" || -z "$4" ]]; then
echo ""
echo "Usage: "$0" github [Github repo] [Release title] [Assets name (include .difypkg suffix)]"
echo "Example:"
echo " "$0" github junjiem/dify-plugin-tools-dbquery v0.0.2 db_query.difypkg"
echo " "$0" github https://github.com/junjiem/dify-plugin-agent-mcp_sse 0.0.1 agent-mcp_see.difypkg"
echo ""
exit 1
fi
echo "From the Github downloading ..."
GITHUB_REPO=$2
if [[ "${GITHUB_REPO}" != "${GITHUB_API_URL}"* ]]; then
GITHUB_REPO="${GITHUB_API_URL}/${GITHUB_REPO}"
fi
RELEASE_TITLE=$3
ASSETS_NAME=$4
PLUGIN_NAME="${ASSETS_NAME%.difypkg}"
PLUGIN_PACKAGE_PATH=${CURR_DIR}/${PLUGIN_NAME}-${RELEASE_TITLE}.difypkg
PLUGIN_DOWNLOAD_URL=${GITHUB_REPO}/releases/download/${RELEASE_TITLE}/${ASSETS_NAME}
echo "Downloading ${PLUGIN_DOWNLOAD_URL} ..."
curl -L -o ${PLUGIN_PACKAGE_PATH} ${PLUGIN_DOWNLOAD_URL}
if [[ $? -ne 0 ]]; then
echo "Download failed, please check the github repo, release title and assets name."
exit 1
fi
echo "Download success."
repackage ${PLUGIN_PACKAGE_PATH}
}
_local(){
echo $2
if [[ -z "$2" ]]; then
echo ""
echo "Usage: "$0" local [difypkg path]"
echo "Example:"
echo " "$0" local ./db_query.difypkg"
echo " "$0" local /root/dify-plugin/db_query.difypkg"
echo ""
exit 1
fi
PLUGIN_PACKAGE_PATH=`realpath $2`
repackage ${PLUGIN_PACKAGE_PATH}
}
repackage(){
local PACKAGE_PATH=$1
PACKAGE_NAME_WITH_EXTENSION=`basename ${PACKAGE_PATH}`
PACKAGE_NAME="${PACKAGE_NAME_WITH_EXTENSION%.*}"
echo "Unziping ..."
install_unzip
unzip -o ${PACKAGE_PATH} -d ${CURR_DIR}/${PACKAGE_NAME}
if [[ $? -ne 0 ]]; then
echo "Unzip failed."
exit 1
fi
echo "Unzip success."
echo "Repackaging ..."
cd ${CURR_DIR}/${PACKAGE_NAME}
pip download ${PIP_PLATFORM} -r requirements.txt -d ./wheels --index-url ${PIP_MIRROR_URL} --trusted-host mirrors.aliyun.com
if [[ $? -ne 0 ]]; then
echo "Pip download failed."
exit 1
fi
if [[ "linux" == "$OS_TYPE" ]]; then
sed -i '1i\--no-index --find-links=./wheels/' requirements.txt
elif [[ "darwin" == "$OS_TYPE" ]]; then
sed -i ".bak" '1i\
--no-index --find-links=./wheels/
' requirements.txt
rm -f requirements.txt.bak
fi
IGNORE_PATH=.difyignore
if [ ! -f "$IGNORE_PATH" ]; then
IGNORE_PATH=.gitignore
fi
if [ -f "$IGNORE_PATH" ]; then
if [[ "linux" == "$OS_TYPE" ]]; then
sed -i '/^wheels\//d' "${IGNORE_PATH}"
elif [[ "darwin" == "$OS_TYPE" ]]; then
sed -i ".bak" '/^wheels\//d' "${IGNORE_PATH}"
rm -f "${IGNORE_PATH}.bak"
fi
fi
cd ${CURR_DIR}
chmod 755 ${CURR_DIR}/${CMD_NAME}
${CURR_DIR}/${CMD_NAME} plugin package ${CURR_DIR}/${PACKAGE_NAME} -o ${CURR_DIR}/${PACKAGE_NAME}-${PACKAGE_SUFFIX}.difypkg --max-size 5120
if [ $? -ne 0 ]; then
echo "Repackage failed."
exit 1
fi
echo "Repackage success."
}
install_unzip(){
if ! command -v unzip &> /dev/null; then
echo "Installing unzip ..."
yum -y install unzip
if [ $? -ne 0 ]; then
echo "Install unzip failed."
exit 1
fi
fi
}
print_usage() {
echo "usage: $0 [-p platform] [-s package_suffix] {market|github|local}"
echo "-p platform: python packages' platform. Using for crossing repacking.
For example: -p manylinux2014_x86_64 or -p manylinux2014_aarch64"
echo "-s package_suffix: The suffix name of the output offline package.
For example: -s linux-amd64 or -s linux-arm64"
exit 1
}
while getopts "p:s:" opt; do
case "$opt" in
p) PIP_PLATFORM="--platform ${OPTARG} --only-binary=:all:" ;;
s) PACKAGE_SUFFIX="${OPTARG}" ;;
*) print_usage; exit 1 ;;
esac
done
shift $((OPTIND - 1))
echo "$1"
case "$1" in
'market')
market $@
;;
'github')
github $@
;;
'local')
_local $@
;;
*)
print_usage
exit 1
esac
exit 0