Class: Markymark::CLI

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

Overview

Command-line interface handler

Constant Summary collapse

DEFAULT_PORT =
4545

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



15
16
17
18
19
20
21
22
23
# File 'lib/markymark/cli.rb', line 15

def initialize(args)
  @root_path = Dir.pwd
  @port = DEFAULT_PORT
  @open_browser = true
  @initial_file = nil

  parse_options(args)
  validate!
end

Instance Attribute Details

#initial_fileObject (readonly)

Returns the value of attribute initial_file.



13
14
15
# File 'lib/markymark/cli.rb', line 13

def initial_file
  @initial_file
end

#open_browserObject (readonly)

Returns the value of attribute open_browser.



13
14
15
# File 'lib/markymark/cli.rb', line 13

def open_browser
  @open_browser
end

#portObject (readonly)

Returns the value of attribute port.



13
14
15
# File 'lib/markymark/cli.rb', line 13

def port
  @port
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



13
14
15
# File 'lib/markymark/cli.rb', line 13

def root_path
  @root_path
end

Class Method Details

.run(args) ⇒ Object



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

def self.run(args)
  cli = new(args)

  # Check if pumadev mode is active
  if PumadevManager.active?
    puts "Pumadev mode is active"
    # In pumadev mode - switch directory via pumadev manager
    if PumadevManager.switch_directory(cli.root_path)
      puts "Switched to #{cli.root_path}"
      puts "Access markymark at: http://markymark.test"

      # Open browser if requested
      if cli.open_browser
        require 'launchy'
        url = if cli.initial_file
          "http://markymark.test/?file=#{CGI.escape(cli.initial_file)}"
        else
          'http://markymark.test'
        end
        Launchy.open(url)
      end
    else
      warn "Failed to switch directory in pumadev mode"
      exit 1
    end
    return
  end

  # Check if a standalone server is already running
  existing_server = cli.send(:detect_existing_server)

  if existing_server
    # Server exists - switch it to the new directory
    cli.send(:handle_existing_server, existing_server)
  else
    # No server running - start a new one (check for port conflicts)
    if cli.send(:port_available?, cli.port)
      ServerSimple.launch(cli)
    else
      # Port is busy - prompt user
      cli.send(:handle_port_conflict)
    end
  end
rescue => e
  warn "Error: #{e.message}"
  warn e.backtrace.join("\n")
  exit 1
end

.show_pumadev_instructionsObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/markymark/cli.rb', line 193

def self.show_pumadev_instructions
  puts "    Pumadev Setup for markymark\n    ============================\n\n    Pumadev allows you to access markymark via a .test domain instead of remembering ports.\n\n    Quick Setup:\n      markymark --setup-pumadev [PATH]\n\n    Manual Setup Instructions:\n\n    1. Install pumadev (if not already installed):\n       gem install puma-dev\n\n    2. Set up pumadev:\n       sudo puma-dev -setup\n       puma-dev -install\n\n    3. Find your markymark gem installation:\n       gem which markymark\n\n    4. Create a symlink in ~/.puma-dev/:\n       cd ~/.puma-dev\n       ln -s /path/to/markymark/gem markymark\n\n    5. Access markymark at:\n       http://markymark.test\n\n    Notes for Ruby Version Manager Users:\n    - Install markymark in your global gemset of your default Ruby\n    - This ensures the command is available across all Ruby versions\n    - The symlink points to the gem location, which pumadev will use\n\n    Smart Directory Switching:\n    - The smart directory switching feature works with pumadev\n    - Run 'markymark [PATH]' from any directory to switch the server\n    - The directory setting persists across requests\n\n    For more information: https://github.com/puma/puma-dev\n  INSTRUCTIONS\nend\n"

.show_statusObject



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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/markymark/cli.rb', line 144

def self.show_status
  puts "Markymark Status"
  puts "=" * 40

  # Check macOS app status
  if RUBY_PLATFORM.include?('darwin')
    app_status = AppInstaller.status
    if app_status
      puts ""
      puts "macOS App:"
      puts "  #{app_status[:message]}"
    end
  end

  # Check pumadev
  pumadev_status = PumadevManager.status
  if pumadev_status
    puts ""
    puts "Server Mode: Pumadev"
    puts "  #{pumadev_status[:message]}"
    return
  end

  # Check standalone server
  pid_file = File.expand_path('~/.markymark/server.pid')
  unless File.exist?(pid_file)
    puts ""
    puts "Server: Not running"
    return
  end

  content = File.read(pid_file)
  port = content[/port=(\d+)/, 1]&.to_i
  pid = content[/pid=(\d+)/, 1]&.to_i

  # Check if process is still running
  begin
    Process.kill(0, pid)
    puts ""
    puts "Server Mode: Standalone"
    puts "  Running (PID: #{pid}, port: #{port})"
    puts "  Access at: http://localhost:#{port}"
  rescue Errno::ESRCH
    puts ""
    puts "Server: Not running (stale PID file removed)"
    File.delete(pid_file)
  end
end

.stop_serverObject



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

def self.stop_server
  # Check if pumadev mode is active first
  if PumadevManager.active?
    PumadevManager.teardown
    exit 0
  end

  # Check for standalone server
  pid_file = File.expand_path('~/.markymark/server.pid')

  unless File.exist?(pid_file)
    puts "No markymark server is running"
    exit 0
  end

  # Read PID file
  content = File.read(pid_file)
  port = content[/port=(\d+)/, 1]&.to_i
  pid = content[/pid=(\d+)/, 1]&.to_i

  unless pid
    puts "Invalid PID file"
    File.delete(pid_file)
    exit 1
  end

  # Check if process is still running
  begin
    Process.kill(0, pid)
  rescue Errno::ESRCH
    puts "Server process not found (PID: #{pid})"
    File.delete(pid_file)
    exit 0
  end

  puts "Stopping markymark server (PID: #{pid}, port: #{port})..."

  # Try graceful shutdown first (SIGTERM)
  begin
    Process.kill('TERM', pid)

    # Wait up to 3 seconds for graceful shutdown
    3.times do
      sleep 1
      begin
        Process.kill(0, pid)
      rescue Errno::ESRCH
        # Process has exited
        puts "Server stopped successfully"
        File.delete(pid_file) if File.exist?(pid_file)
        exit 0
      end
    end

    # If still running, force kill
    puts "Server didn't stop gracefully, forcing shutdown..."
    Process.kill('KILL', pid)
    sleep 1

    puts "Server stopped (forced)"
    File.delete(pid_file) if File.exist?(pid_file)
  rescue Errno::ESRCH
    puts "Server stopped successfully"
    File.delete(pid_file) if File.exist?(pid_file)
  rescue => e
    warn "Error stopping server: #{e.message}"
    exit 1
  end
end