-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrabby.sh
executable file
·156 lines (135 loc) · 3.5 KB
/
krabby.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#! /usr/bin/env bash
# Copyright 2024 Dotanuki Labs
# SPDX-License-Identifier: MIT
set -e
dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$dir"
readonly callinectes="ghcr.io/dotanuki-labs/callinectes:latest@sha256:911f5a7d1d5b9ae4f47fc4c53411297a5bcc824f5c6f04b060642a68df35441e"
readonly task="$1"
readonly output_dir="artifacts"
usage() {
echo
echo "Available tasks:"
echo
echo "setup # Installs required Cargo extensions"
echo "lint # Check code formatting and smells"
echo "tests # Run tests for Rust modules and integration tests"
echo "assemble # Builds binaries according to the environment (local or CI)"
echo "security # Run security checks and generates supply-chain artifacts"
echo "sbom # Generate a CycloneDX SBOM from Rust dependencies"
echo "prepare-release # Prepares metadata for releasing artifacts"
echo
}
setup_rust_toolchain() {
echo "🔥 Installing and activating Rust toolchain"
rustup show active-toolchain
echo
}
check_code_smells() {
echo
echo "🔥 Checking code smells for Rust code"
echo
docker run --rm -v "${PWD}:/usr/src" "$callinectes" fmt clippy
}
run_cargo_tests() {
echo
echo "🔥 Running unit + integration tests for Rust code"
echo
cargo test
echo
}
build_binaries() {
echo
echo "🔥 Building project according to environment"
echo
local gha_runner="${RUNNER_OS:-local}"
local platform
echo "Detected environment → $gha_runner"
case "$gha_runner" in
"local")
cargo build --release
exit 0
;;
"macOS")
platform="apple-darwin"
;;
"Linux")
platform="unknown-linux-gnu"
;;
*)
echo "Error: unsupported environment → $gha_runner"
echo
exit 1
;;
esac
rm -rf "$output_dir" && mkdir -p "$output_dir"
for arch in x86_64 aarch64; do
local target="$arch-$platform"
rustup target add "$target"
cargo build --release --target "$target"
local binary="target/$target/release/gwv"
cp "$binary" "$output_dir"/gwv-"$target"
chmod +x "$output_dir"/gwv-"$target"
done
}
check_supply_chain() {
echo
echo "🔥 Checking dependencies and supply-chain"
echo
docker run --rm -v "${PWD}:/usr/src" "$callinectes" msrv deny machete
}
generate_cyclonedx_sbom() {
echo
echo "🔥 Generating cyclonedx SBOM"
echo
docker run --rm -v "${PWD}:/usr/src" "$callinectes" cyclonedx
}
compute_checksums() {
readonly checksums="checksums.txt"
cd "$output_dir"
touch "$checksums"
find . -name 'gwv-*' -exec sha256sum {} \; |
sed "s/\.\///g" |
sed "s/gwv-binaries-macOS\///g" |
sed "s/gwv-binaries-Linux\///g" >"$checksums"
}
export_release_version() {
version=$(grep 'version' Cargo.toml | head -1 | sed "s/version[[:space:]]=[[:space:]]//g" | tr -d '"')
echo "version=$version" >>"$GITHUB_OUTPUT"
}
prepare_github_release() {
compute_checksums
export_release_version
}
if [[ -z "$task" ]]; then
usage
exit 0
fi
case "$task" in
"setup")
setup_rust_toolchain
;;
"lint")
check_code_smells
;;
"tests")
run_cargo_tests
;;
"assemble")
build_binaries
;;
"security")
check_supply_chain
;;
"sbom")
generate_cyclonedx_sbom
;;
"prepare-release")
prepare_github_release
;;
*)
echo "Error: unsupported task → $task"
usage
exit 1
;;
esac