-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathnormalize_path.sh
More file actions
55 lines (46 loc) · 1.52 KB
/
normalize_path.sh
File metadata and controls
55 lines (46 loc) · 1.52 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
#!/bin/bash
# Call: normalize_path_with_symlinks [target_dir] [dir_prefix]
#
# Normalizes the PATH environment variable by replacing each directory that
# begins with dir_prefix with a symbolic link in target_dir. For example, if
# PATH is "/usr/bin:/bin", target_dir is /tmp, and dir_prefix is /usr, then
# PATH will become "/tmp/0:/bin", where /tmp/0 links to /usr/bin.
#
# This is useful for ensuring that PATH is consistent across CI runs and helps
# with reusing the same cache across them. Many of our go tests read the PATH
# variable, and if it changes between runs, the cache gets invalidated.
normalize_path_with_symlinks() {
local target_dir="${1:-}"
local dir_prefix="${2:-}"
if [[ -z "$target_dir" || -z "$dir_prefix" ]]; then
echo "Usage: normalize_path_with_symlinks <target_dir> <dir_prefix>"
return 1
fi
local old_path="$PATH"
local -a new_parts=()
local i=0
IFS=':' read -ra _parts <<<"$old_path"
for dir in "${_parts[@]}"; do
# Skip empty components that can arise from "::"
[[ -z $dir ]] && continue
# Skip directories that don't start with $dir_prefix
if [[ "$dir" != "$dir_prefix"* ]]; then
new_parts+=("$dir")
continue
fi
local link="$target_dir/$i"
# Replace any pre-existing file or link at $target_dir/$i
if [[ -e $link || -L $link ]]; then
rm -rf -- "$link"
fi
# without MSYS ln will deepcopy the directory on Windows
MSYS=winsymlinks:nativestrict ln -s -- "$dir" "$link"
new_parts+=("$link")
i=$((i + 1))
done
export PATH
PATH="$(
IFS=':'
echo "${new_parts[*]}"
)"
}