-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for gem consumer custom templates #39
base: main
Are you sure you want to change the base?
Changes from 4 commits
16c9c6e
0fb8149
23aa536
1f84dba
5436604
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails/generators/named_base' | ||
|
||
module Onesie | ||
module Generators | ||
# Generates a new Onesie template | ||
class TemplateGenerator < Rails::Generators::NamedBase | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add documentation in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can do! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @timlkelly Done! |
||
desc 'Generate a new Onesie template' | ||
source_root File.expand_path('./templates', __dir__) | ||
|
||
TEMPLATE_FILENAME = 'template.rb' | ||
|
||
def create_template | ||
template(TEMPLATE_FILENAME, filename) | ||
end | ||
|
||
private | ||
|
||
def filename | ||
"onesie/templates/#{file_name}.rb" | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ class <%= class_name %> < Onesie::Task | |
manual_task enabled: false | ||
|
||
def run | ||
# Write your Onesie Task here | ||
<%= run_contents.chomp.indent(8) %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this line not work if it is intended? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you see a template in which consumers of the gem do not want the output to be formatted properly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it should be formatted correctly. I was wondering why it was required to call the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if required is the right term here. From a formatting perspective, if we don't include the indent, a onesie generated via template will appear as: # frozen_string_literal: true
module Onesie
module Tasks
class <%= class_name %> < Onesie::Task
allowed_environments :all
manual_task enabled: false
def run
template here
end
end
end
end Which will either make engineer's eyes twitch (or rubocop 😂 ) |
||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Write the contents of Onesie::Tasks#run here! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
using Rainbow | ||
|
||
module Onesie | ||
# Responsible for loading custom template files | ||
class TemplateReader | ||
def self.read_template(path) | ||
unless File.exist?(path) | ||
puts "Custom Template could not be found at #{path}".red | ||
return | ||
Comment on lines
+9
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @timlkelly this is to resolve the file being created despite the template not existing. I was not able to find a way to prevent the generator to not create the file, even with raising an exception here, so I'm defaulting to an error message being printed, and in the consumer of this, defaulting back to the default template. |
||
end | ||
|
||
File.read(path) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'generator_spec' | ||
require 'generators/onesie/template_generator' | ||
|
||
RSpec.describe Onesie::Generators::TemplateGenerator, type: :generator do | ||
destination File.expand_path('../../../tmp', __dir__) | ||
arguments %w[ExampleTemplate] | ||
|
||
before do | ||
prepare_destination | ||
run_generator | ||
end | ||
|
||
it 'creates a new template file' do | ||
path = 'tmp/onesie/templates/example_template.rb' | ||
expect(File.exist?(path)).to be(true) | ||
|
||
contents = File.read(path) | ||
|
||
expect(contents).to eq("# Write the contents of Onesie::Tasks#run here!\n") | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timlkelly let me know if this is what you'd expect. I figured since we now accept multiple args, using options is acceptable like this.