1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-04 12:06:45 +00:00

mo update

This commit is contained in:
Tw93
2025-10-05 20:22:39 +08:00
parent 2145e3736e
commit 98506183a4
3 changed files with 83 additions and 27 deletions

View File

@@ -192,7 +192,7 @@ mo analyze
### 如何更新和卸载 Mole
**更新:** 一键安装用 `mo update` · Homebrew `brew upgrade mole`(检测不到新版本?先运行 `brew update`
**更新:** 直接运行 `mo update`Homebrew 安装会自动执行 `brew update && brew upgrade mole`
**卸载:** 任何安装方式都用 `mo remove`(会自动识别安装方式,清理所有相关文件)

View File

@@ -171,6 +171,10 @@ check_requirements() {
# Check if already installed via Homebrew
if command -v brew >/dev/null 2>&1 && brew list mole >/dev/null 2>&1; then
if [[ "$ACTION" == "update" ]]; then
return 0
fi
echo -e "${YELLOW}Mole is installed via Homebrew${NC}"
echo ""
echo "Choose one:"
@@ -435,6 +439,36 @@ perform_install() {
perform_update() {
check_requirements
if command -v brew >/dev/null 2>&1 && brew list mole >/dev/null 2>&1; then
echo -e "${BLUE}${NC} Updating Homebrew..."
# Update Homebrew with real-time output
brew update 2>&1 | grep -v "^==>" | grep -v "^Already up-to-date" || true
echo -e "${BLUE}${NC} Upgrading Mole..."
local upgrade_output
upgrade_output=$(brew upgrade mole 2>&1) || true
if echo "$upgrade_output" | grep -q "already installed"; then
# Get current version from brew
local current_version
current_version=$(brew info mole 2>/dev/null | grep "mole:" | awk '{print $3}' | head -1)
echo -e "${GREEN}${NC} Already on latest version (${current_version:-$VERSION})"
elif echo "$upgrade_output" | grep -q "Error:"; then
log_error "Update failed. Try: brew update && brew upgrade mole"
exit 1
else
# Show upgrade output (exclude headers and warnings)
echo "$upgrade_output" | grep -v "^==>" | grep -v "^Updating Homebrew" | grep -v "^Warning:"
# Get new version
local new_version
new_version=$(brew info mole 2>/dev/null | grep "mole:" | awk '{print $3}' | head -1)
echo -e "${GREEN}${NC} Updated to latest version (${new_version:-$VERSION})"
fi
rm -f "$HOME/.cache/mole/version_check" "$HOME/.cache/mole/update_message"
exit 0
fi
local installed_version
installed_version="$(get_installed_version || true)"
@@ -454,16 +488,15 @@ perform_update() {
fi
if [[ "$installed_version" == "$target_version" ]]; then
log_success "Mole is already up to date (version $installed_version)!"
echo -e "${GREEN}${NC} Already on latest version ($installed_version)"
exit 0
fi
log_info "Updating Mole from $installed_version to $target_version..."
create_directories
install_files
verify_installation
setup_path
# Update silently
create_directories >/dev/null 2>&1
install_files >/dev/null 2>&1
verify_installation >/dev/null 2>&1
setup_path >/dev/null 2>&1
local updated_version
updated_version="$(get_installed_version || true)"
@@ -472,7 +505,7 @@ perform_update() {
updated_version="$target_version"
fi
print_usage_summary "updated" "$updated_version" "$installed_version"
echo -e "${GREEN}${NC} Updated to latest version ($updated_version)"
}
# Run requested action

59
mole
View File

@@ -143,29 +143,50 @@ show_help() {
update_mole() {
# Check if installed via Homebrew
if command -v brew >/dev/null 2>&1 && brew list mole >/dev/null 2>&1; then
echo -e "${GREEN}Homebrew detected, use: ${BLUE}brew upgrade mole${NC}"
echo -e "${BLUE}→${NC} Updating Homebrew..."
# Update Homebrew with real-time output
brew update 2>&1 | grep -v "^==>" | grep -v "^Already up-to-date" || true
echo -e "${BLUE}→${NC} Upgrading Mole..."
local upgrade_output
upgrade_output=$(brew upgrade mole 2>&1) || true
if echo "$upgrade_output" | grep -q "already installed"; then
echo -e "${GREEN}✓${NC} Already on latest version ($VERSION)"
elif echo "$upgrade_output" | grep -q "Error:"; then
log_error "Update failed. Try: brew update && brew upgrade mole"
exit 1
else
# Show upgrade output (exclude headers and warnings)
echo "$upgrade_output" | grep -v "^==>" | grep -v "^Updating Homebrew" | grep -v "^Warning:"
# Get new version
local new_version
new_version=$(brew info mole 2>/dev/null | grep "mole:" | awk '{print $3}' | head -1)
echo -e "${GREEN}✓${NC} Updated to latest version (${new_version:-$VERSION})"
fi
rm -f "$HOME/.cache/mole/version_check" "$HOME/.cache/mole/update_message"
exit 0
fi
log_info "Updating Mole..."
# Download and run installer silently
local installer_url="https://raw.githubusercontent.com/tw93/mole/main/install.sh"
local tmp_installer
tmp_installer="$(mktemp)" || { log_error "Failed to create temp file"; exit 1; }
tmp_installer="$(mktemp)" || { log_error "Update failed"; exit 1; }
# Download installer with timeout
# Download installer silently
if command -v curl >/dev/null 2>&1; then
if ! curl -fsSL --connect-timeout 10 --max-time 60 "$installer_url" -o "$tmp_installer"; then
curl -fsSL --connect-timeout 10 --max-time 60 "$installer_url" -o "$tmp_installer" 2>/dev/null || {
rm -f "$tmp_installer"
log_error "Failed to download installer"
log_error "Update failed. Check network connection."
exit 1
fi
}
elif command -v wget >/dev/null 2>&1; then
if ! wget --timeout=10 --tries=3 -qO "$tmp_installer" "$installer_url"; then
wget --timeout=10 --tries=3 -qO "$tmp_installer" "$installer_url" 2>/dev/null || {
rm -f "$tmp_installer"
log_error "Failed to download installer"
log_error "Update failed. Check network connection."
exit 1
fi
}
else
rm -f "$tmp_installer"
log_error "curl or wget required"
@@ -180,12 +201,16 @@ update_mole() {
local install_dir
install_dir="$(cd "$(dirname "$mole_path")" && pwd)"
# Run installer quietly
if "$tmp_installer" --prefix "$install_dir" --config "$HOME/.config/mole" --update 2>/dev/null; then
log_success "Updated successfully"
# Run installer quietly and get new version
if "$tmp_installer" --prefix "$install_dir" --config "$HOME/.config/mole" --update >/dev/null 2>&1; then
local new_version
new_version=$(grep '^VERSION=' "$tmp_installer" 2>/dev/null | head -1 | sed 's/VERSION="\(.*\)"/\1/' || echo "")
echo -e "${GREEN}✓${NC} Updated to latest version (${new_version:-unknown})"
else
if "$tmp_installer" --prefix "$install_dir" --config "$HOME/.config/mole" 2>/dev/null; then
log_success "Updated successfully"
if "$tmp_installer" --prefix "$install_dir" --config "$HOME/.config/mole" >/dev/null 2>&1; then
local new_version
new_version=$(grep '^VERSION=' "$tmp_installer" 2>/dev/null | head -1 | sed 's/VERSION="\(.*\)"/\1/' || echo "")
echo -e "${GREEN}✓${NC} Updated to latest version (${new_version:-unknown})"
else
rm -f "$tmp_installer"
log_error "Update failed"
@@ -194,8 +219,6 @@ update_mole() {
fi
rm -f "$tmp_installer"
# Clear version check cache after successful update
rm -f "$HOME/.cache/mole/version_check" "$HOME/.cache/mole/update_message"
}