Class: WebRubyUI::CLI

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

Instance Method Summary collapse

Instance Method Details

#build(app_dir = '.') ⇒ Object



34
35
36
37
38
39
40
# File 'lib/web_ruby_ui/cli.rb', line 34

def build(app_dir = '.')
  target_dir = File.expand_path(app_dir)
  index_path = File.join(target_dir, 'index.html')

  ensure_index_html(index_path)
  say "✨ Successfully built WebRubyUI Wasm package in #{target_dir}/index.html", :green
end

#dev(app_dir = '.') ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/web_ruby_ui/cli.rb', line 13

def dev(app_dir = '.')
  port = options[:port]
  say "🚀 Starting WebRubyUI Wasm dev server on http://localhost:#{port}...", :green

  target_dir = File.expand_path(app_dir)
  index_path = File.join(target_dir, 'index.html')

  ensure_index_html(index_path)

  server = WEBrick::HTTPServer.new(
    Port: port,
    DocumentRoot: target_dir,
    AccessLog: [],
    Logger: WEBrick::Log.new(File::NULL)
  )

  trap('INT') { server.shutdown }
  server.start
end

#new_project(app_name) ⇒ Object



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

def new_project(app_name)
  target_dir = File.expand_path(app_name)
  FileUtils.mkdir_p(File.join(target_dir, 'app'))

  app_rb = <<~RUBY
    # frozen_string_literal: true

    require 'web_ruby_ui'

    class App < WebRubyUI::Component
      state :count, default: 0

      def render
        div(class: 'card') do
          h1 { "Welcome to WebRubyUI!" }
          p { "Count: \#{count}" }
          button(onclick: -> { self.count += 1 }) { "Increment" }
        end
      end
    end
  RUBY

  File.write(File.join(target_dir, 'app', 'app.rb'), app_rb)
  ensure_index_html(File.join(target_dir, 'index.html'))
  say "🎉 Created new WebRubyUI project in #{target_dir}!", :green
end