-
Notifications
You must be signed in to change notification settings - Fork 140
/
install-packages.sh
executable file
·125 lines (106 loc) · 3.56 KB
/
install-packages.sh
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
#!/usr/bin/env bash
script_dir="$(dirname "$0")"
target="$1"
output_dir="$2"
option_no_update="${no_update:+1}"
source "${script_dir}"/bootstrap.sh
require 'styles'
require 'resolver'
if [[ -z "$target" ]] || [[ -z "$output_dir" ]]; then
echo "Usage: $(basename "$0") :<configuration>|<package-name> <output-directory>"
exit 1
fi
set -e
[[ -d "${output_dir}" ]] || mkdir -p "${output_dir}"
files_updated=0
install_package() {
local user_name="$(resolve_user_name "$1")"
local package_name="$(resolve_package_name "$1")"
local package_dir="${root_dir:-.}/package/${user_name}/${package_name}"
local package="$(resolve_package "$1")"
local branch="$(resolve_branch "$1")"
local branch_label="${branch:+@${branch}}"
local recipe="$(resolve_recipe "$1")"
local recipe_options=($(resolve_recipe_options "$1"))
fetch_or_update_package
if [[ -n "${recipe}" ]]; then
require 'recipe'
install_recipe "${package_dir}/${recipe}.recipe.yaml"
elif [[ -f "${package_dir}/recipe.yaml" ]]; then
require 'recipe'
install_recipe "${package_dir}/recipe.yaml"
else
install_files_from_package "${package_dir}"
fi
}
fetch_or_update_package() {
if ! [[ -d "${package_dir}" ]]; then
echo $(info 'Downloading package:') $(highlight "${package}") $(print_option "${branch_label}")
local fetch_options=()
if [[ -n "${branch}" ]]; then
fetch_options+=(--branch "${branch}")
fi
"${script_dir}"/fetch-package.sh "${package}" "${package_dir}" "${fetch_options[@]}"
else
if [[ -z "${option_no_update}" ]]; then
echo $(info 'Updating package:') $(highlight "${package}")
else
echo $(info 'Found package:') $(highlight "${package}")
fi
"${script_dir}"/update-package.sh "${package_dir}" "${branch}"
fi
}
install_files_from_package() {
local package_dir="$1"
local IFS=$'\r\n'
local data_files=(
$(
cd "${package_dir}"
ls *.yaml 2> /dev/null \
| grep -v -e '\.custom\.yaml$' -e '\.recipe\.yaml$' -e '^recipe\.yaml$'
ls *.txt 2> /dev/null
ls *.gram 2> /dev/null
ls opencc/*.* 2> /dev/null \
| grep -e '\.json$' -e '\.ocd$' -e '\.txt$'
)
)
install_files "${data_files[@]}"
}
install_files() {
if [[ "$#" -eq 0 ]]; then
return
fi
local source_path
local target_path
for file in "$@"; do
source_path="${package_dir}/${file}"
target_path="${output_dir}/${file}"
if ! [ -e "${target_path}" ]; then
create_containing_directory "${target_path}"
echo $(info 'Installing:') $(print_item "${file}")
elif ! diff -q "${source_path}" "${target_path}" &> /dev/null; then
echo $(info 'Updating:') $(print_item "${file}")
else
continue
fi
cp "${source_path}" "${target_path}"
((++files_updated))
done
}
create_containing_directory() {
local target_dir="$(dirname "$1")"
if ! [ -d "${target_dir}" ]; then
echo $(info 'Creating directory:') $(print_item "${target_dir}")
mkdir -p "${target_dir}"
fi
}
load_package_list_from_target "${target}"
for package in "${package_list[@]}"; do
install_package "${package}"
done
if [[ "${files_updated}" -eq 0 ]]; then
echo $(print_result 'No files updated.')
else
echo $(print_result "Updated ~ ${files_updated} files " \
"from ${#package_list[@]} packages in") "'${output_dir}'"
fi