From c850a4ac012bf03f69d8b76ae266058bc80aaa91 Mon Sep 17 00:00:00 2001 From: Drew Date: Wed, 8 Sep 2021 11:09:59 -0400 Subject: [PATCH 1/2] Create an install generator with helpful output --- lib/generators/polaris_view_components/USAGE | 5 +++ .../install_generator.rb | 35 +++++++++++++++++++ .../polaris_view_components/templates/README | 14 ++++++++ .../templates/stimulus_index.js | 7 ++++ 4 files changed, 61 insertions(+) create mode 100644 lib/generators/polaris_view_components/USAGE create mode 100644 lib/generators/polaris_view_components/install_generator.rb create mode 100644 lib/generators/polaris_view_components/templates/README create mode 100644 lib/generators/polaris_view_components/templates/stimulus_index.js diff --git a/lib/generators/polaris_view_components/USAGE b/lib/generators/polaris_view_components/USAGE new file mode 100644 index 00000000..8248497c --- /dev/null +++ b/lib/generators/polaris_view_components/USAGE @@ -0,0 +1,5 @@ +Description: + Creates or adds import of NPM package to your application + additional installation steps. + +Example: + rails generate polaris_view_components:install diff --git a/lib/generators/polaris_view_components/install_generator.rb b/lib/generators/polaris_view_components/install_generator.rb new file mode 100644 index 00000000..03310b97 --- /dev/null +++ b/lib/generators/polaris_view_components/install_generator.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'rails/generators/active_record' + +module PolarisViewComponents + class InstallGenerator < Rails::Generators::Base + source_root File.expand_path('templates', __dir__) + + def add_npm_package + say "Adding NPM package", :green + run "yarn add polaris-view-components" + end + + def add_to_stimulus_controller + say "Adding import to to Stimulus controller", :green + dir_path = "app/javascript/controllers" + empty_directory('app/javascript') + empty_directory(dir_path) + + file_path = "#{dir_path}/index.js" + + unless File.exist?(file_path) + copy_file 'stimulus_index.js', file_path + end + + append_to_file file_path do + "import { registerPolarisControllers } from 'polaris-view-components'\nregisterPolarisControllers(application)" + end + end + + def show_readme + readme 'README' + end + end +end diff --git a/lib/generators/polaris_view_components/templates/README b/lib/generators/polaris_view_components/templates/README new file mode 100644 index 00000000..35c7fdaf --- /dev/null +++ b/lib/generators/polaris_view_components/templates/README @@ -0,0 +1,14 @@ +=============================================================================== + +Some manual setup is still required: + + 1. Setup Polaris styles in your layouts tag: + + + <%= stylesheet_link_tag 'polaris_view_components' %> + + 2. Define Polaris style on your tag: + + + +=============================================================================== diff --git a/lib/generators/polaris_view_components/templates/stimulus_index.js b/lib/generators/polaris_view_components/templates/stimulus_index.js new file mode 100644 index 00000000..2ed8cf86 --- /dev/null +++ b/lib/generators/polaris_view_components/templates/stimulus_index.js @@ -0,0 +1,7 @@ +import { Application } from 'stimulus' +import { definitionsFromContext } from 'stimulus/webpack-helpers' + +const application = Application.start(document.documentElement) +const context = require.context('.', true, /_controller\.js$/) +application.load(definitionsFromContext(context)) + From b33615cf412634f9f193da8d9a131bde121a4503 Mon Sep 17 00:00:00 2001 From: Drew Date: Wed, 8 Sep 2021 11:14:33 -0400 Subject: [PATCH 2/2] Add install generator to README --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 1ba12832..9b688cfa 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,11 @@ In `Gemfile`, add: gem 'polaris_view_components' ``` +Run install generator: +```bash +rails generate polaris_view_components:install +``` + Setup Polaris styles in your layouts `` tag: ```erb