Skip to content

Commit

Permalink
Merge pull request #22 from swarmd-io/feat-add-install-script
Browse files Browse the repository at this point in the history
feat: add install script
  • Loading branch information
Miaxos authored Dec 6, 2023
2 parents 738b8b1 + a7aada7 commit 41f7c78
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ doc = false
[features]
default = []

[build-dependencies]
chrono = "0.4"

[dependencies]
anyhow.workspace = true
async-trait.workspace = true
async-stream.workspace = true
base64.workspace = true
clap = { workspace = true, features = ["derive"] }
chrono = "0.4"
config.workspace = true
derivative.workspace = true
derive_builder.workspace = true
Expand Down
6 changes: 1 addition & 5 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ fn main() {
let git_hash = String::from_utf8(output.stdout).unwrap();
println!("cargo:rustc-env=GIT_HASH={}", git_hash);

let date = Command::new("date")
.args(["-u", "+%Y-%m-%dT%H:%M:%SZ"])
.output()
.unwrap();
let date = String::from_utf8(date.stdout).unwrap();
let date = chrono::Utc::now().to_rfc3339();
println!("cargo:rustc-env=BUILD_DATE={}", date);
}
11 changes: 11 additions & 0 deletions cli/src/infrastructure/updater/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ pub async fn get_last_release() -> anyhow::Result<Option<Release>> {
}

pub async fn check_if_update_available() -> anyhow::Result<Option<String>> {
let build_date = env!("BUILD_DATE");
let now = chrono::Utc::now();
let date = chrono::DateTime::parse_from_rfc3339(build_date)?;
let dur = now.signed_duration_since(date);

// We only check after 3 days
// TODO: do only one check a day and store the data to reuse until tomorrow.
if dur < chrono::Duration::days(3) {
return Ok(None);
}

if let Some(release) = get_last_release().await? {
let current_version = self_update::cargo_crate_version!();
if current_version == release.version {
Expand Down
77 changes: 77 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/sh
#
# Adapted from Deno

set -e

if [ "$OS" = "Windows_NT" ]; then
if ! command -v unzip >/dev/null; then
echo "Error: unzip is required to install Swarmd for windows." 1>&2
exit 1
fi
fi

# TODO: We should implement a way to differentiate between windws x86 and
# windows arm.
if [ "$OS" = "Windows_NT" ]; then
target="x86_64-pc-windows-msvc"
ending="zip"
else
ending="tar.gz"
case $(uname -sm) in
"Darwin x86_64") target="x86_64-apple-darwin" ;;
"Darwin arm64") target="aarch64-apple-darwin" ;;
"Linux aarch64")
# TODO: We'll just disable dev feature with a feature flag and make it
# available.
echo "Error: Official Swarmd builds for Linux aarch64 are not available due to an issue with Deno. (see: https://github.com/denoland/deno/issues/1846 )" 1>&2
exit 1
;;
*) target="x86_64-unknown-linux-gnu" ;;
esac
fi

if [ $# -eq 0 ]; then
swarmd_uri="https://github.com/swarmd-io/swarmd/releases/latest/download/swarmd-${target}.${ending}"
else
swarmd_uri="https://github.com/swarmd-io/swarmd/releases/download/${1}/swarmd-${target}.${ending}"
fi

swarmd_install="${SWARMD_INSTALL:-$HOME/.swarmd}"
bin_dir="$swarmd_install/bin"
exe="$bin_dir/swarmd"

if [ ! -d "$bin_dir" ]; then
mkdir -p "$bin_dir"
fi

curl --fail --location --progress-bar --output "$exe.$ending" "$swarmd_uri"
if [ "$OS" = "Windows_NT" ]; then
unzip -d "$bin_dir" -o "$exe.$ending"
else
tar -xf "$exe.$ending" --strip-components=1 --directory "$bin_dir"
fi

chmod +x "$exe"
rm "$exe.$ending"

echo "Swarmd was installed successfully to $exe"
echo
echo
if command -v swarmd >/dev/null; then
echo "Run 'swarmd --help' to get started"
else
case $SHELL in
/bin/zsh) shell_profile=".zshrc" ;;
*) shell_profile=".bashrc" ;;
esac
echo "Manually add the directory to your \$HOME/$shell_profile (or similar)"
echo " export SWARMD_INSTALL=\"$swarmd_install\""
echo " export PATH=\"\$SWARMD_INSTALL/bin:\$PATH\""
echo "Run '$exe --help' to get started"
fi
echo
echo
echo
echo
echo "Join our Discord!! https://discord.gg/QpHDyE3WnW"

0 comments on commit 41f7c78

Please sign in to comment.