-
Notifications
You must be signed in to change notification settings - Fork 2
/
run
executable file
·102 lines (93 loc) · 3.15 KB
/
run
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
#!/usr/bin/env bash
usage() {
echo "Usage: ${0} [ options ]"
echo ' -h: Show this help message.'
echo ' -g: Starts or restarts Grafana.'
echo ' -v: Validate the plugin using grafana/plugin-validator.'
echo ' -P: Publish the plugin to Grafana with the latest version number in the'
echo ' changelog. Computes a dry-run first and asks for confirmation.'
echo ' -U: Clears the entire folder contents and runs the create-plugin'
echo ' command. Computes a dry-run first and asks for confirmation.'
exit 1
}
git_status() {
git status | grep -q clean && echo 'No files need to be committed.' || echo -e "The following files need to be committed:\n$(git status -s)"
git status | grep -q 'up to date' && echo 'Local branch is up-to-date.' || echo 'Some commits need to be pushed.'
}
# Print usage for no arguments
[[ "${#}" -eq 0 ]] && usage
grafana_restart=false
validate_plugin=false
force_cmd=false
publishing=false
update_plugin=false
# Parse command line arguments
while getopts ':hgvxPU' opt ; do
case "${opt}" in
'g')
grafana_restart=true ;;
'v')
validate_plugin=true ;;
'x')
force_cmd=true ;;
'P')
publishing=true ;;
'U')
update_plugin=true ;;
'h'|*)
usage ;;
esac
done
"${grafana_restart}" && brew services restart grafana
"${validate_plugin}" && {
plugin_id="$(grep '"id"' < src/plugin.json | sed -E 's/.*"id" *: *"(.*)".*/\1/')"
cp -r dist "${plugin_id}" || {
echo 'Must build plugin first with npm run build'
exit 1
}
zip -qr "${plugin_id}.zip" "${plugin_id}"
npx -y @grafana/plugin-validator@latest -sourceCodeUri file://. "${plugin_id}.zip"
rm -r "${plugin_id}" "${plugin_id}.zip"
}
"${publishing}" && {
version="$(grep -oE "[0-9]+\.[0-9]+\.[0-9]+" CHANGELOG.md | head -n1)"
verjson="$(grep -oE "[0-9]+\.[0-9]+\.[0-9]+" package.json | head -n1)"
if "${force_cmd}" ; then
echo "Publishing with version ${version}."
ext='.bk'
package_json='package.json'
remote_origin='origin'
sed -i"${ext}" -E "s/\"version\": \"[0-9]+\.[0-9]+\.[0-9]+\"/\"version\": \"${version}\"/" "${package_json}"
rm -v "${package_json}${ext}"
git add "${package_json}" && git commit -m "v${version}" && git push
git tag -a "v${version}" -m "${version}" && git push "${remote_origin}" "v${version}"
else
echo "Current version number is ${version}."
git_status
[ "${version}" == "${verjson}" ] && echo 'Unpushed commits will not be pushed.' || echo 'Unpushed commits will be pushed.'
echo "If everything looks good, re-run this command with ${0} -Px"
fi
}
"${update_plugin}" && {
if "${force_cmd}" ; then
shopt -s dotglob nullglob
backup_dir='.backup'
mkdir "${backup_dir}" || exit 1
echo "Moving files to ${backup_dir}..."
mv ./* "./${backup_dir}/"
cd "${backup_dir}" || exit 1
mv .git run ..
cd ..
npx -y @grafana/create-plugin@latest
dir_name="$(ls -d ./[^\.]*/)"
cd "${dir_name}" || exit 1
echo "Moving files from ${dir_name}..."
mv ./* ..
cd ..
echo 'Removing files...'
rm -vr "${dir_name}"
else
git_status
echo "If everything looks good, re-run this command with ${0} -Ux"
fi
}