-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.sh
More file actions
143 lines (116 loc) · 3.41 KB
/
util.sh
File metadata and controls
143 lines (116 loc) · 3.41 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
#!/bin/sh
# Utility functions for handling codes.
# Usage: source util.sh
# Commands: run_in_dir, git_all, clone_gitlab, clone_github
#
# Author: Christopher Albert
# Date: 2024-03-01
# License: MIT
alias cdcode='cd $CODE'
alias vscode='code $CODE'
set_branch() {
if [ -n "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" ]; then
export CODE_BRANCH=$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
elif [ -n "$CI_COMMIT_REF_NAME" ]; then
export CODE_BRANCH=$CI_COMMIT_REF_NAME
else
pushd $CODE
export CODE_BRANCH=$(git branch --show-current)
popd
fi
echo "Activating $CODE on branch $CODE_BRANCH"
}
add_to_path() {
if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
PATH="$1${PATH:+":$PATH"}"
fi
}
add_to_library_path() {
if [ -d "$1" ] && [[ ":$LD_LIBRARY_PATH:" != *":$1:"* ]]; then
LD_LIBRARY_PATH="$1${LD_LIBRARY_PATH:+":$LD_LIBRARY_PATH"}"
fi
if is_a_mac; then
if [ -d "$1" ] && [[ ":$DYLD_LIBRARY_PATH:" != *":$1:"* ]]; then
DYLD_LIBRARY_PATH="$1${DYLD_LIBRARY_PATH:+":$DYLD_LIBRARY_PATH"}"
fi
fi
}
is_a_mac() {
[[ "$(uname)" == "Darwin" ]]
}
# Run a command in a directory.
# Usage: run_in_dir <dir> <command>
run_in_dir() (
local dir=$1
shift
local cmd="$@"
cd "$dir" || return
eval "$cmd"
)
# Run a git command in all in the current directory and in top-level git subdirectories.
# Usage: git_all commit | push | pull | fetch | status | branch ...
git_all() (
local cmd=("$@")
echo "$(pwd)"
(git "${cmd[@]}") &
jobs=($!)
for dir in $(ls -d */); do
if [ -d "$dir/.git" ]; then
echo "$dir"
(
cd "$dir" || exit 1
git "${cmd[@]}"
) &
jobs+=($!) # Capture the PID of the background process
fi
done
# Wait for all background processes to complete
for job in "${jobs[@]}"; do
wait $job
done
)
# Clone a repository from https://gitlab.tugraz.at/plasma/codes/ .
# Usage: clone_gitlab <repo>
clone_gitlab() (
local repo=$1
if [ -n "$GIT_HTTPS" ]; then
URL=https://user:[email protected]/plasma/codes/$repo
else
[email protected]:plasma/codes/$repo.git
fi
git clone $URL
cd $repo
set_branch
git checkout $CODE_BRANCH 2>/dev/null || true
)
# Clone a repository from https://github.com/itpplasma/ .
# Usage: clone_github <repo>
clone_github() (
local repo=$1
if [ -n "$GIT_HTTPS" ]; then
URL=https://oauth2:[email protected]/itpplasma/$repo
else
[email protected]:itpplasma/$repo.git
fi
git clone $URL
cd $repo
set_branch
git checkout $CODE_BRANCH 2>/dev/null || true
)
# Upload a package to registry
# Usage: upload_package <package> <version>
upload_package() (
local PACKAGENAME=$1
local VERSION=$2
curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file \
${PACKAGENAME}-${VERSION}.tar.gz \
${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${PACKAGENAME}/${VERSION}/${PACKAGENAME}-${VERSION}.tar.gz
)
# Install a package from registry
# Usage: install_package <package> <version>
install_package() (
local PACKAGENAME=$1
local VERSION=$2
curl --header "JOB-TOKEN: $CI_JOB_TOKEN" -L \
${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${PACKAGENAME}/${VERSION}/${PACKAGENAME}-${VERSION}.tar.gz -o - | tar xz
)