aboutsummaryrefslogtreecommitdiff
path: root/lib/actions/fetch.bash
blob: 8582865aab298a12a695471f3214be44256c916e (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
#!/usr/bin/env bash

RSTAR_DEPS_BIN+=(
	awk
	curl
	git
	tar
)

action() {
	# Ensure the directory to download to exists
	mkdir -p "$BASEDIR/src"

	# Download all core components
	for component in moarvm nqp rakudo
	do
		download_core "$component"
	done

	mkdir -p "$BASEDIR/src/rakudo-star-modules"

	# Download all modules available over http
	list_modules "http" | while read -r name _ url prefix
	do
		download_module_http "$name" "$url" "$prefix"
	done

	# Download all modules available over git
	list_modules "git" | while read -r name _ url ref
	do
		download_module_git "$name" "$url" "$ref"
	done
}

download_core() {
	local version
	local source
	local destination

	version="$(config_etc_kv "dist_$1.txt" "version")"
	source="$(config_etc_kv "dist_$1.txt" "url" | sed "s/%s/$version/g")"
	destination="$BASEDIR/src/$1-$version"

	if [[ -d $destination ]]
	then
		warn "Skipping sources for $1, destination already exists: $destination"
		return 0
	fi

	mkdir -p -- "$destination"

	tarball="$(fetch_http "$source")" \
		&& tar xzf "$tarball" -C "$destination" --strip-components=1 \
		&& return

	crit "Failed to download $destination"
	rm -fr -- "$destination"
}

download_module_git() {
	local name=$1
	local url=$2
	local ref=$3
	local destination="$BASEDIR/src/rakudo-star-modules/$name"

	if [[ -d "$destination" ]]
	then
		warn "Skipping sources for $name, destination already exists: $destination"
		return 0
	fi

	notice "Cloning $url@$ref to $destination"
	git clone -b "$ref" "$url" --depth=1 --single-branch "$destination" \
		> /dev/null 2>&1

	rm -fr -- "$destination/.git"
}

download_module_http() {
	local name=$1
	local url=$2
	local prefix=$3
	local destination="$BASEDIR/src/rakudo-star-modules/$name"
	local tarball
	local extracted

	if [[ -d "$destination" ]]
	then
		warn "Skipping sources for $name, destination already exists: $destination"
		return 0
	fi

	tarball="$(fetch_http "$url")"
	extracted="$(tmpdir)"

	notice "Extracting $tarball into $extracted"
	tar xzf "$tarball" -C "$extracted"

	notice "Moving $extracted/$prefix to $destination"
	mv -- "$extracted/$prefix" "$destination"
}

list_modules() {
	awk '/^[^#]/ && $2 == "'"$1"'" { print }' "$BASEDIR/etc/modules.txt"
}