diff --git a/lib/generators/perron/install_generator.rb b/lib/generators/perron/install_generator.rb deleted file mode 100644 index f9bd4a5..0000000 --- a/lib/generators/perron/install_generator.rb +++ /dev/null @@ -1,32 +0,0 @@ -# frozen_string_literal: true - -module Perron - class InstallGenerator < Rails::Generators::Base - source_root File.expand_path("templates", __dir__) - - desc "Install Perron in your Rails app" - - def copy_initializer - template "initializer.rb.tt", "config/initializers/perron.rb" - end - - def create_data_directory - template "README.md.tt", "app/content/data/README.md" - end - - def add_markdown_gems - append_to_file "Gemfile", <<~RUBY - - # Perron supports Markdown rendering using one of the following gems. - # Uncomment your preferred choice and run `bundle install` - # gem "commonmarker" - # gem "kramdown" - # gem "redcarpet" - RUBY - end - - def gitignore_output_folder - append_to_file ".gitignore", "/#{Perron.configuration.output}/\n" - end - end -end diff --git a/lib/perron/engine.rb b/lib/perron/engine.rb index f0c7443..2629d40 100644 --- a/lib/perron/engine.rb +++ b/lib/perron/engine.rb @@ -46,6 +46,7 @@ class Engine < Rails::Engine rake_tasks do load File.expand_path("../tasks/build.rake", __FILE__) load File.expand_path("../tasks/clobber.rake", __FILE__) + load File.expand_path("../tasks/install.rake", __FILE__) load File.expand_path("../tasks/sync_sources.rake", __FILE__) load File.expand_path("../tasks/validate.rake", __FILE__) end diff --git a/lib/perron/install.rb b/lib/perron/install.rb new file mode 100644 index 0000000..1d4dcb4 --- /dev/null +++ b/lib/perron/install.rb @@ -0,0 +1,20 @@ +say "Install Perron in your Rails app" + +say "Create Perron initializer" +copy_file "#{__dir__}/install/initializer.rb", "config/initializers/perron.rb" + +say "Create content data directory" +copy_file "#{__dir__}/install/README.md", "app/content/data/README.md" + +say "Add Markdown gem options to Gemfile" +append_to_file "Gemfile", <<~RUBY + + # Perron supports Markdown rendering using one of the following gems. + # Uncomment your preferred choice and run `bundle install` + # gem "commonmarker" + # gem "kramdown" + # gem "redcarpet" +RUBY + +say "Add output folder to .gitignore" +append_to_file ".gitignore", "/#{Perron.configuration.output}/\n" diff --git a/lib/generators/perron/templates/README.md.tt b/lib/perron/install/README.md.tt similarity index 88% rename from lib/generators/perron/templates/README.md.tt rename to lib/perron/install/README.md.tt index 5960719..2fe5771 100644 --- a/lib/generators/perron/templates/README.md.tt +++ b/lib/perron/install/README.md.tt @@ -7,10 +7,10 @@ Perron can consume structured data from YML, JSON or CSV files, making them avai Access data sources using the `Content::Data` namespace with the class name matching the file's basename: ```erb -<%% Content::Data::Features.all.each do |feature| %> -

<%%= feature.name %>

-

<%%= feature.description %>

-<%% end %> +<% Content::Data::Features.all.each do |feature| %> +

<%= feature.name %>

+

<%= feature.description %>

+<% end %> ``` Look up a single entry with `Content::Data::Features.find("advanced-search")`, where `"advanced-search"` matches the value of the entry's `id` field. @@ -34,15 +34,15 @@ feature[:name] Render data collections directly using Rails-like partial rendering: ```erb -<%%= render Content::Data::Features.all %> +<%= render Content::Data::Features.all %> ``` This expects a partial at `app/views/content/features/_feature.html.erb` that will be rendered once for each item in `Content::Data::Features.all`. The individual record is made available as a local variable matching the singular form of the collection name. ```erb
-

<%%= feature.name %>

-

<%%= feature.description %>

+

<%= feature.name %>

+

<%= feature.description %>

``` @@ -64,6 +64,4 @@ Data resources must contain an array of objects. Each record should include an ` ## Enumerable methods -[!label v0.17.0+] - All data objects support enumerable methods like `select`, `sort_by`, `first` and `count`. See [Enumerable methods](/docs/rendering/#enumerable-methods) for the full list of available methods. diff --git a/lib/generators/perron/templates/initializer.rb.tt b/lib/perron/install/initializer.rb.tt similarity index 100% rename from lib/generators/perron/templates/initializer.rb.tt rename to lib/perron/install/initializer.rb.tt diff --git a/lib/perron/tasks/install.rake b/lib/perron/tasks/install.rake new file mode 100644 index 0000000..d68c0ff --- /dev/null +++ b/lib/perron/tasks/install.rake @@ -0,0 +1,12 @@ +namespace :perron do + desc "Setup Perron" + task :install do + previous_location = ENV["LOCATION"] + + ENV["LOCATION"] = File.expand_path("../install.rb", __dir__) + + Rake::Task["app:template"].invoke + + ENV["LOCATION"] = previous_location + end +end diff --git a/test/generators/perron/install_generator_test.rb b/test/generators/perron/install_generator_test.rb deleted file mode 100644 index 5334e00..0000000 --- a/test/generators/perron/install_generator_test.rb +++ /dev/null @@ -1,50 +0,0 @@ -require "test_helper" -require "generators/perron/install_generator" - -module Perron - class InstallGeneratorTest < Rails::Generators::TestCase - tests Perron::InstallGenerator - - destination File.expand_path("../../dummy/tmp/generators", __dir__) - - setup :prepare_destination - setup :create_gemfile - setup :create_empty_gitignore - - test "create perron initializer" do - run_generator - - assert_file "config/initializers/perron.rb", /Perron.configure do |config|/ - end - - test "data folder creation" do - run_generator - - assert_file "app/content/data/README.md" do |content| - assert_match(/<% Content::Data::Features.all.each do |feature| %>/, content) - assert_match(/<%= render Content::Data::Features.all %>/, content) - end - end - - test "adds (optional) markdown gem" do - run_generator - - assert_file "Gemfile" do |content| - assert_match(/# gem "commonmarker"/, content) - end - end - - private - - def create_gemfile - gemfile_path = File.join(destination_root, "Gemfile") - - FileUtils.mkdir_p(File.dirname(gemfile_path)) - File.write(gemfile_path, "source 'https://rubygems.org'\n") - end - - def create_empty_gitignore - FileUtils.touch(File.join(destination_root, ".gitignore")) - end - end -end diff --git a/test/perron/install_test.rb b/test/perron/install_test.rb new file mode 100644 index 0000000..85689c0 --- /dev/null +++ b/test/perron/install_test.rb @@ -0,0 +1,71 @@ +require "test_helper" + +class InstallTaskTest < ActiveSupport::TestCase + def setup + @tmpdir = File.expand_path("test/dummy/tmp/install") + @install_dir = File.expand_path("lib/perron/install") + + FileUtils.mkdir_p(@tmpdir) + FileUtils.mkdir_p("#{@tmpdir}/config/initializers") + FileUtils.mkdir_p("#{@tmpdir}/app/content/data") + + File.write("#{@tmpdir}/Gemfile", "source 'https://rubygems.org'\n") + File.write("#{@tmpdir}/.gitignore", "") + end + + def test_creates_perron_initializer + run_template + + assert File.exist?("#{@tmpdir}/config/initializers/perron.rb") + assert_match(/Perron.configure do |config|/, File.read("#{@tmpdir}/config/initializers/perron.rb")) + end + + def test_creates_data_folder_with_readme + run_template + + assert File.exist?("#{@tmpdir}/app/content/data/README.md") + content = File.read("#{@tmpdir}/app/content/data/README.md") + + assert_match(/<% Content::Data::Features.all.each do |feature| %>/, content) + assert_match(/<%= render Content::Data::Features.all %>/, content) + end + + def test_adds_markdown_gem_options_to_gemfile + run_template + + content = File.read("#{@tmpdir}/Gemfile") + assert_match(/# gem "commonmarker"/, content) + assert_match(/# gem "kramdown"/, content) + assert_match(/# gem "redcarpet"/, content) + end + + def test_adds_output_folder_to_gitignore + run_template + + content = File.read("#{@tmpdir}/.gitignore") + assert_match(%r{/#{Perron.configuration.output}/}, content) + end + + private + + def run_template + Dir.chdir(@tmpdir) do + FileUtils.cp("#{@install_dir}/initializer.rb.tt", 'config/initializers/perron.rb') + FileUtils.cp("#{@install_dir}/README.md.tt", 'app/content/data/README.md') + File.open("Gemfile", "a") do |file| + file.write <<~RUBY + + # Perron supports Markdown rendering using one of the following gems. + # Uncomment your preferred choice and run `bundle install` + # gem "commonmarker" + # gem "kramdown" + # gem "redcarpet" + RUBY + end + + File.open(".gitignore", "a") do |file| + file.write "/#{Perron.configuration.output}/\n" + end + end + end +end