Skip to content

Commit c526b39

Browse files
committed
Add a routes.rb generator
1 parent 395f627 commit c526b39

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

spec/generate_foobified_rails_files_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@
5858
command.paths_to_source_code["Gemfile"]
5959
).to include('gem "foobara-rails-command-connector"')
6060
end
61+
62+
it "generates the expected files" do
63+
expect(outcome).to be_success
64+
65+
expect(
66+
command.paths_to_source_code.keys
67+
).to contain_exactly(
68+
"Gemfile",
69+
"config/application.rb",
70+
"app/commands/construct_greeting.rb",
71+
"spec/commands/construct_greeting_spec.rb",
72+
"config/routes.rb"
73+
)
74+
end
6175
end
6276

6377
context "when using foobara-active-record-type" do

src/generators/base_generator.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def manifest_to_generator_classes(manifest)
1313
Generators::GemfileGenerator,
1414
ConfigApplicationGenerator,
1515
SampleCommandGenerator,
16-
SampleCommandSpecGenerator
16+
SampleCommandSpecGenerator,
17+
RoutesGenerator
1718
]
1819
else
1920
# :nocov:

src/generators/routes_generator.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require_relative "base_generator"
2+
3+
module Foobara
4+
module Generators
5+
module FoobifyRailsApp
6+
module Generators
7+
class RoutesGenerator < BaseGenerator
8+
def applicable?
9+
use_rails_command_connector? &&
10+
File.exist?(template_path) &&
11+
routes_rb_contents !~ /require.*foobara\/rails_command_connector/
12+
end
13+
14+
def template_path
15+
"config/routes.rb"
16+
end
17+
18+
def target_path
19+
template_path
20+
end
21+
22+
def generate(_elements_to_generate)
23+
with_requires_prepended = <<~HERE
24+
require "foobara/rails_command_connector"
25+
# require "foobara/auth_http"
26+
# Foobara::CommandConnectors::RailsCommandConnector.new(authenticator: Foobara::AuthHttp::BearerAuthenticator)
27+
Foobara::CommandConnectors::RailsCommandConnector.new
28+
require "foobara/rails/routes"
29+
HERE
30+
31+
with_requires_prepended = "#{with_requires_prepended}\n#{routes_rb_contents}"
32+
33+
match = with_requires_prepended.match(/Rails.application.routes.draw do/)
34+
35+
if match
36+
# You can hit /run/ConstructGreeting or /help/ConstructGreeting
37+
new_entry = " command ConstructGreeting"
38+
39+
unless include_sample_command?
40+
new_entry = "# #{new_entry}"
41+
end
42+
43+
"#{match.pre_match}\n#{match}\n#{new_entry}#{match.post_match}"
44+
else
45+
# TODO: maybe print a warning and return the original Gemfile
46+
# :nocov:
47+
raise "Not sure how to inject foobara into the Gemfile"
48+
# :nocov:
49+
end
50+
end
51+
52+
def routes_rb_contents
53+
File.read(template_path)
54+
end
55+
end
56+
end
57+
end
58+
end
59+
end

0 commit comments

Comments
 (0)