Class: Markymark::AppInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/markymark/app_installer.rb

Overview

Installs Markymark as a macOS application for file handling

Constant Summary collapse

APP_NAME =
'Markymark.app'
BUNDLE_ID =
'com.markymark.app'
APP_DIR =
File.expand_path('~/Applications')
APP_PATH =
File.join(APP_DIR, APP_NAME)

Class Method Summary collapse

Class Method Details

.duti_available?Boolean



127
128
129
# File 'lib/markymark/app_installer.rb', line 127

def duti_available?
  system('which duti > /dev/null 2>&1')
end

.homebrew_available?Boolean



131
132
133
# File 'lib/markymark/app_installer.rb', line 131

def homebrew_available?
  system('which brew > /dev/null 2>&1')
end

.install(force: false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/markymark/app_installer.rb', line 15

def install(force: false)
  unless macos?
    warn "App installation is only supported on macOS"
    return false
  end

  if File.exist?(APP_PATH) && !force
    print "Markymark.app already exists. Overwrite? [y/N] "
    response = $stdin.gets&.strip&.downcase
    return false unless response == 'y'
  end

  puts "Installing Markymark.app to ~/Applications..."

  begin
    create_app_bundle
    puts "Successfully installed Markymark.app"
    puts "Location: #{APP_PATH}"
    true
  rescue => e
    warn "Failed to install app: #{e.message}"
    false
  end
end

.installed?Boolean



135
136
137
# File 'lib/markymark/app_installer.rb', line 135

def installed?
  File.exist?(APP_PATH)
end

.set_default_handlerObject



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
# File 'lib/markymark/app_installer.rb', line 60

def set_default_handler
  unless macos?
    warn "Setting default handler is only supported on macOS"
    return false
  end

  unless File.exist?(APP_PATH)
    warn "Markymark.app must be installed first. Run: markymark --install-app"
    return false
  end

  puts "Setting Markymark as default handler for .md files..."

  # Try duti first (more reliable)
  if duti_available?
    return set_default_with_duti
  end

  # duti not installed - offer to install it
  if homebrew_available?
    puts ""
    puts "For automatic default app registration, 'duti' is recommended."
    print "Install duti via Homebrew? [Y/n] "
    response = $stdin.gets&.strip&.downcase

    if response.nil? || response.empty? || response == 'y' || response == 'yes'
      puts "Installing duti..."
      if system('brew install duti')
        puts "duti installed successfully."
        return set_default_with_duti
      else
        warn "Failed to install duti."
      end
    end
  end

  # Fall back to lsregister + manual instructions
  lsregister = '/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister'
  if File.exist?(lsregister)
    # Register the app
    system("#{lsregister} -f '#{APP_PATH}'")
    puts ""
    puts "Registered Markymark.app with Launch Services."
    puts ""
    puts "To complete setup, please manually set Markymark as default:"
    puts "  1. Right-click any .md file in Finder"
    puts "  2. Select 'Get Info'"
    puts "  3. Under 'Open with:', select 'Markymark'"
    puts "  4. Click 'Change All...'"
    return true
  end

  warn "Could not set default handler automatically."
  puts "Please install 'duti' (brew install duti) or set manually via Finder."
  false
end

.set_default_with_dutiObject



117
118
119
120
121
122
123
124
125
# File 'lib/markymark/app_installer.rb', line 117

def set_default_with_duti
  success = system("duti -s #{BUNDLE_ID} .md all") &&
            system("duti -s #{BUNDLE_ID} .markdown all")
  if success
    puts "Successfully set Markymark as default handler for .md files"
    return true
  end
  false
end

.statusObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/markymark/app_installer.rb', line 139

def status
  return nil unless macos?

  unless File.exist?(APP_PATH)
    return { installed: false, message: "Markymark.app is not installed" }
  end

  launcher_path = File.join(APP_PATH, 'Contents', 'MacOS', 'markymark-launcher')
  unless File.exist?(launcher_path)
    return { installed: true, valid: false, message: "Markymark.app is corrupted (missing launcher)" }
  end

  # Check if the baked-in Ruby path still exists (stored in helper script)
  helper_path = File.join(APP_PATH, 'Contents', 'MacOS', 'markymark-helper')
  unless File.exist?(helper_path)
    return { installed: true, valid: false, message: "Markymark.app is corrupted (missing helper)" }
  end

  helper_content = File.read(helper_path)
  ruby_path = helper_content[/RUBY_PATH="([^"]+)"/, 1]

  if ruby_path && !File.exist?(ruby_path)
    return {
      installed: true,
      valid: false,
      ruby_path: ruby_path,
      message: "Markymark.app needs reinstalling (Ruby path changed: #{ruby_path})"
    }
  end

  {
    installed: true,
    valid: true,
    ruby_path: ruby_path,
    message: "Markymark.app is installed and valid"
  }
end

.uninstallObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/markymark/app_installer.rb', line 40

def uninstall
  unless macos?
    warn "App uninstallation is only supported on macOS"
    return false
  end

  unless File.exist?(APP_PATH)
    puts "Markymark.app is not installed"
    return true
  end

  puts "Removing Markymark.app..."
  FileUtils.rm_rf(APP_PATH)
  puts "Successfully removed Markymark.app"
  true
rescue => e
  warn "Failed to uninstall app: #{e.message}"
  false
end