diff --git a/install.sh b/install.sh index 4854e9b..ceda468 100755 --- a/install.sh +++ b/install.sh @@ -266,6 +266,45 @@ get_installed_version() { fi } +resolve_install_channel() { + case "${MOLE_VERSION:-}" in + main | latest) + printf 'nightly\n' + return 0 + ;; + dev) + printf 'dev\n' + return 0 + ;; + esac + + if [[ "${MOLE_EDGE_INSTALL:-}" == "true" ]]; then + printf 'nightly\n' + return 0 + fi + + printf 'stable\n' +} + +write_install_channel_metadata() { + local channel="$1" + local metadata_file="$CONFIG_DIR/install_channel" + + local tmp_file + tmp_file=$(mktemp "${CONFIG_DIR}/install_channel.XXXXXX") || return 1 + { + printf 'CHANNEL=%s\n' "$channel" + } > "$tmp_file" || { + rm -f "$tmp_file" 2> /dev/null || true + return 1 + } + + mv -f "$tmp_file" "$metadata_file" || { + rm -f "$tmp_file" 2> /dev/null || true + return 1 + } +} + # CLI parsing (supports main/latest and version tokens). parse_args() { local -a args=("$@") @@ -712,6 +751,12 @@ perform_install() { installed_version="$source_version" fi + local install_channel + install_channel="$(resolve_install_channel)" + if ! write_install_channel_metadata "$install_channel"; then + log_warning "Could not write install channel metadata" + fi + # Edge installs get a suffix to make the version explicit. if [[ "${MOLE_EDGE_INSTALL:-}" == "true" ]]; then installed_version="${installed_version}-edge" @@ -795,6 +840,12 @@ perform_update() { updated_version="$target_version" fi + local install_channel + install_channel="$(resolve_install_channel)" + if ! write_install_channel_metadata "$install_channel"; then + log_warning "Could not write install channel metadata" + fi + echo -e "${GREEN}${ICON_SUCCESS}${NC} Updated to latest version, $updated_version" } diff --git a/mole b/mole index ae61f6c..7d34c75 100755 --- a/mole +++ b/mole @@ -87,6 +87,18 @@ is_homebrew_install() { return 1 } +get_install_channel() { + local channel_file="$SCRIPT_DIR/install_channel" + local channel="stable" + if [[ -f "$channel_file" ]]; then + channel=$(sed -n 's/^CHANNEL=\(.*\)$/\1/p' "$channel_file" | head -1) + fi + case "$channel" in + nightly | dev | stable) printf '%s\n' "$channel" ;; + *) printf 'stable\n' ;; + esac +} + # Background update notice check_for_updates() { local msg_cache="$HOME/.cache/mole/update_message" @@ -205,7 +217,13 @@ show_version() { install_method="Homebrew" fi + local channel + channel=$(get_install_channel) + printf '\nMole version %s\n' "$VERSION" + if [[ "$channel" == "nightly" ]]; then + printf 'Channel: Nightly\n' + fi printf 'macOS: %s\n' "$os_ver" printf 'Architecture: %s\n' "$arch" printf 'Kernel: %s\n' "$kernel" diff --git a/tests/cli.bats b/tests/cli.bats index 92c92ec..83eb922 100644 --- a/tests/cli.bats +++ b/tests/cli.bats @@ -14,6 +14,7 @@ setup_file() { } teardown_file() { + rm -f "$PROJECT_ROOT/install_channel" rm -rf "$HOME" if [[ -n "${ORIGINAL_HOME:-}" ]]; then export HOME="$ORIGINAL_HOME" @@ -47,6 +48,7 @@ SCRIPT setup() { rm -rf "$HOME/.config" mkdir -p "$HOME" + rm -f "$PROJECT_ROOT/install_channel" } @test "mole --help prints command overview" { @@ -63,6 +65,18 @@ setup() { [[ "$output" == *"$expected_version"* ]] } +@test "mole --version shows nightly channel metadata" { + expected_version="$(grep '^VERSION=' "$PROJECT_ROOT/mole" | head -1 | sed 's/VERSION=\"\(.*\)\"/\1/')" + cat > "$PROJECT_ROOT/install_channel" <<'EOF' +CHANNEL=nightly +EOF + + run env HOME="$HOME" "$PROJECT_ROOT/mole" --version + [ "$status" -eq 0 ] + [[ "$output" == *"Mole version $expected_version"* ]] + [[ "$output" == *"Channel: Nightly"* ]] +} + @test "mole unknown command returns error" { run env HOME="$HOME" "$PROJECT_ROOT/mole" unknown-command [ "$status" -ne 0 ]