Class: RubyAPI::CLI

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

Class Method Summary collapse

Class Method Details

.config_ru_contentObject



66
67
68
69
70
71
72
73
# File 'lib/rubyapi/cli.rb', line 66

def self.config_ru_content
  <<~RUBY
    require_relative "app/main"
    require_relative "app/routes"

    run App.new
  RUBY
end

.gemfile_content(name) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/rubyapi/cli.rb', line 56

def self.gemfile_content(name)
  <<~GEMFILE
    source "https://rubygems.org"

    gem "fastrb", "~> #{RubyAPI::VERSION}"
    gem "rackup"
    gem "puma"
  GEMFILE
end

.hello_spec_contentObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rubyapi/cli.rb', line 122

def self.hello_spec_content
  <<~RUBY
    require "spec_helper"

    RSpec.describe "GET /hello" do
      include Rack::Test::Methods

      def app
        App.new
      end

      it "returns hello world" do
        get "/hello"
        expect(last_response.status).to eq(200)
        expect(JSON.parse(last_response.body)["message"]).to eq("Hello World")
      end
    end
  RUBY
end

.invoke(args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubyapi/cli.rb', line 6

def self.invoke(args)
  command = args[0]
  name = args[1]

  case command
  when "new"
    new_project(name)
  when "server"
    start_server
  when "version"
    puts "rubyapi #{VERSION}"
  else
    puts "Usage: rubyapi <command> [options]"
    puts ""
    puts "Commands:"
    puts "  new <name>    Create a new RubyAPI project"
    puts "  server        Start the server (Falcon/Puma)"
    puts "  version       Show version"
    exit 1
  end
end

.main_rb_contentObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubyapi/cli.rb', line 75

def self.main_rb_content
  <<~RUBY
    require "rubyapi"

    class App < RubyAPI::App
      def initialize
        super do
          # Middleware
          # use MyMiddleware

          # Routes
          include Routes
        end
      end
    end
  RUBY
end

.new_project(name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubyapi/cli.rb', line 28

def self.new_project(name)
  puts "Creating new RubyAPI project: #{name}"

  FileUtils.mkdir_p(name)
  FileUtils.mkdir_p("#{name}/app")
  FileUtils.mkdir_p("#{name}/config/environments")
  FileUtils.mkdir_p("#{name}/public")
  FileUtils.mkdir_p("#{name}/spec/unit")
  FileUtils.mkdir_p("#{name}/spec/requests")

  File.write("#{name}/Gemfile", gemfile_content(name))
  File.write("#{name}/config.ru", config_ru_content)
  File.write("#{name}/app/main.rb", main_rb_content)
  File.write("#{name}/app/routes.rb", routes_rb_content)
  File.write("#{name}/.rspec", ".rspec")
  File.write("#{name}/spec/spec_helper.rb", spec_helper_content)
  File.write("#{name}/spec/requests/hello_spec.rb", hello_spec_content)
  File.write("#{name}/README.md", readme_content(name))

  puts "Done! cd #{name} && bundle install && rubyapi server"
end

.readme_content(name) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rubyapi/cli.rb', line 142

def self.readme_content(name)
  <<~MARKDOWN
    # #{name}

    A RubyAPI application.

    ## Setup

        bundle install

    ## Running

        rubyapi server

    ## Testing

        bundle exec rspec
  MARKDOWN
end

.routes_rb_contentObject



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rubyapi/cli.rb', line 93

def self.routes_rb_content
  <<~RUBY
    module Routes
      def self.included(base)
        base.get "/hello" do
          { message: "Hello World" }
        end
      end
    end
  RUBY
end

.spec_helper_contentObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rubyapi/cli.rb', line 105

def self.spec_helper_content
  <<~RUBY
    require "rubyapi"
    require "rack/test"

    RSpec.configure do |config|
      config.expect_with :rspec do |expectations|
        expectations.include_chain_clauses_in_custom_matcher_descriptions = true
      end

      config.mock_with :rspec do |mocks|
        mocks.verify_partial_doubles = true
      end
    end
  RUBY
end

.start_serverObject



50
51
52
# File 'lib/rubyapi/cli.rb', line 50

def self.start_server
  require_relative "../../config.ru"
end