Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Extension Generator 2.0 (#12851)
Damian Legawiec6e1ec11120dc
diff --git a/cli/lib/spree_cli/extension.rb b/cli/lib/spree_cli/extension.rbindex 8c4f7ec4ff..edd220dc36 100644--- a/cli/lib/spree_cli/extension.rb+++ b/cli/lib/spree_cli/extension.rb@@ -19,14 +19,15 @@ module SpreeClidirectory 'bin', "#{file_name}/bin"directory 'spec', "#{file_name}/spec"- empty_directory "#{file_name}/app/models/spree"empty_directory "#{file_name}/app/models/#{file_name}"empty_directory "#{file_name}/app/views/spree"empty_directory "#{file_name}/app/views/#{file_name}"- empty_directory "#{file_name}/app/controllers/spree"+ empty_directory "#{file_name}/app/controllers/spree/admin"empty_directory "#{file_name}/app/controllers/#{file_name}"- empty_directory "#{file_name}/app/services/spree"empty_directory "#{file_name}/app/services/#{file_name}"+ empty_directory "#{file_name}/app/serializers/spree/v2/storefront"+ empty_directory "#{file_name}/app/serializers/spree/api/v2/platform"+ empty_directory "#{file_name}/vendor/javascript"chmod "#{file_name}/bin/rails", 0o755@@ -38,6 +39,9 @@ module SpreeClitemplate 'README.md', "#{file_name}/README.md"template 'config/routes.rb', "#{file_name}/config/routes.rb"template 'config/locales/en.yml', "#{file_name}/config/locales/en.yml"+ template 'config/initializers/spree.rb', "#{file_name}/config/initializers/spree.rb"+ template 'config/importmap.rb', "#{file_name}/config/importmap.rb"+template 'rspec', "#{file_name}/.rspec"template '.circleci/config.yml', "#{file_name}/.circleci/config.yml"template '.github/.dependabot.yml', "#{file_name}/.github/.dependabot.yml"diff --git a/cli/lib/spree_cli/templates/extension/app/assets/config/%file_name%_manifest.js.tt b/cli/lib/spree_cli/templates/extension/app/assets/config/%file_name%_manifest.js.ttnew file mode 100644index 0000000000..96463bbaf6--- /dev/null+++ b/cli/lib/spree_cli/templates/extension/app/assets/config/%file_name%_manifest.js.tt@@ -0,0 +1,4 @@+//= link_tree ../images+//= link <%= file_name %>/application.js+//= link_tree ../../javascript/<%= file_name %>/controllers .js+//= link_tree ../../../vendor/javascript .jsdiff --git a/cli/lib/spree_cli/templates/extension/app/controllers/.gitkeep b/cli/lib/spree_cli/templates/extension/app/assets/images/.keepsimilarity index 100%rename from cli/lib/spree_cli/templates/extension/app/controllers/.gitkeeprename to cli/lib/spree_cli/templates/extension/app/assets/images/.keepdiff --git a/cli/lib/spree_cli/templates/extension/app/javascript/%file_name%/application.js.tt b/cli/lib/spree_cli/templates/extension/app/javascript/%file_name%/application.js.ttnew file mode 100644index 0000000000..cd0cd2a8e8--- /dev/null+++ b/cli/lib/spree_cli/templates/extension/app/javascript/%file_name%/application.js.tt@@ -0,0 +1,16 @@+import '@hotwired/turbo-rails'+import { Application } from '@hotwired/stimulus'++let application++if (typeof window.Stimulus === "undefined") {+ application = Application.start()+ application.debug = false+ window.Stimulus = application+} else {+ application = window.Stimulus+}++import <%= class_name %>Controller from '<%= file_name %>/controllers/<%= file_name %>_controller'++application.register('<%= file_name %>', <%= class_name %>Controller)\ No newline at end of filediff --git a/cli/lib/spree_cli/templates/extension/app/javascript/%file_name%/controllers/%file_name%_controller.js.tt b/cli/lib/spree_cli/templates/extension/app/javascript/%file_name%/controllers/%file_name%_controller.js.ttnew file mode 100644index 0000000000..3c0d96fcad--- /dev/null+++ b/cli/lib/spree_cli/templates/extension/app/javascript/%file_name%/controllers/%file_name%_controller.js.tt@@ -0,0 +1,7 @@+import { Controller } from '@hotwired/stimulus'++export default class extends Controller {+ connect() {+ console.log('Hello, <%= class_name %>!')+ }+}\ No newline at end of filediff --git a/cli/lib/spree_cli/templates/extension/app/jobs/%file_name%/base_job.rb.tt b/cli/lib/spree_cli/templates/extension/app/jobs/%file_name%/base_job.rb.ttnew file mode 100644index 0000000000..0d8a067950--- /dev/null+++ b/cli/lib/spree_cli/templates/extension/app/jobs/%file_name%/base_job.rb.tt@@ -0,0 +1,5 @@+module <%= class_name %>+ class BaseJob < Spree::BaseJob+ queue_as <%= class_name %>.queue+ end+enddiff --git a/cli/lib/spree_cli/templates/extension/app/models/.gitkeep b/cli/lib/spree_cli/templates/extension/app/models/.gitkeepdeleted file mode 100644index e69de29bb2..0000000000diff --git a/cli/lib/spree_cli/templates/extension/app/serializers/.gitkeep b/cli/lib/spree_cli/templates/extension/app/serializers/.gitkeepdeleted file mode 100644index e69de29bb2..0000000000diff --git a/cli/lib/spree_cli/templates/extension/app/services/.gitkeep b/cli/lib/spree_cli/templates/extension/app/services/.gitkeepdeleted file mode 100644index e69de29bb2..0000000000diff --git a/cli/lib/spree_cli/templates/extension/app/views/%file_name%/_head.html.erb.tt b/cli/lib/spree_cli/templates/extension/app/views/%file_name%/_head.html.erb.ttnew file mode 100644index 0000000000..739a7d9f39--- /dev/null+++ b/cli/lib/spree_cli/templates/extension/app/views/%file_name%/_head.html.erb.tt@@ -0,0 +1 @@+<%%= javascript_import_module_tag 'application-<%= file_name.gsub('_', '-') %>' %>\ No newline at end of filediff --git a/cli/lib/spree_cli/templates/extension/config/i18n-tasks.yml b/cli/lib/spree_cli/templates/extension/config/i18n-tasks.ymlnew file mode 100644index 0000000000..d78aa47c8e--- /dev/null+++ b/cli/lib/spree_cli/templates/extension/config/i18n-tasks.yml@@ -0,0 +1,173 @@+# i18n-tasks finds and manages missing and unused translations: https://github.com/glebm/i18n-tasks++# The "main" locale.+base_locale: en+## All available locales are inferred from the data by default. Alternatively, specify them explicitly:+# locales: [es, fr]+## Reporting locale, default: en. Available: en, ru.+# internal_locale: en++# Read and write translations.+data:+ ## Translations are read from the file system. Supported format: YAML, JSON.+ ## Provide a custom adapter:+ # adapter: I18n::Tasks::Data::FileSystem++ # Locale files or `Dir.glob` patterns where translations are read from:+ read:+ ## Default:+ - config/locales/%{locale}.yml+ ## More files:+ # - config/locales/**/*.%{locale}.yml++ # Locale files to write new keys to, based on a list of key pattern => file rules. Matched from top to bottom:+ # `i18n-tasks normalize -p` will force move the keys according to these rules+ write:+ ## For example, write devise and simple form keys to their respective files:+ # - ['{devise, simple_form}.*', 'config/locales/\1.%{locale}.yml']+ ## Catch-all default:+ # - config/locales/%{locale}.yml++ # External locale data (e.g. gems).+ # This data is not considered unused and is never written to.+ external:+ ## Example (replace %#= with %=):+ # - "<%#= %x[bundle info vagrant --path].chomp %>/templates/locales/%{locale}.yml"+ - "<%= %x[bundle info spree_core --path].chomp %>/config/locales/%{locale}.yml"+ - "<%= %x[bundle info spree_storefront --path].chomp %>/config/locales/%{locale}.yml"+ ## Specify the router (see Readme for details). Valid values: conservative_router, pattern_router, or a custom class.+ # router: conservative_router++ yaml:+ write:+ # do not wrap lines at 80 characters+ line_width: -1++ ## Pretty-print JSON:+ # json:+ # write:+ # indent: ' '+ # space: ' '+ # object_nl: "\n"+ # array_nl: "\n"++# Find translate calls+search:+ ## Paths or `Find.find` patterns to search in:+ # paths:+ # - app/++ ## Root directories for relative keys resolution.+ # relative_roots:+ # - app/controllers+ # - app/helpers+ # - app/mailers+ # - app/presenters+ # - app/views++ ## Directories where method names which should not be part of a relative key resolution.+ # By default, if a relative translation is used inside a method, the name of the method will be considered part of the resolved key.+ # Directories listed here will not consider the name of the method part of the resolved key+ #+ # relative_exclude_method_name_paths:+ # -++ ## Files or `File.fnmatch` patterns to exclude from search. Some files are always excluded regardless of this setting:+ ## *.jpg *.jpeg *.png *.gif *.svg *.ico *.eot *.otf *.ttf *.woff *.woff2 *.pdf *.css *.sass *.scss *.less+ ## *.yml *.json *.zip *.tar.gz *.swf *.flv *.mp3 *.wav *.flac *.webm *.mp4 *.ogg *.opus *.webp *.map *.xlsx+ exclude:+ - app/assets/images+ - app/assets/fonts+ - app/assets/videos+ - app/assets/builds++ ## Alternatively, the only files or `File.fnmatch patterns` to search in `paths`:+ ## If specified, this settings takes priority over `exclude`, but `exclude` still applies.+ # only: ["*.rb", "*.html.slim"]++ ## If `strict` is `false`, guess usages such as t("categories.#{category}.title"). The default is `true`.+ strict: false++ ## Allows adding ast_matchers for finding translations using the AST-scanners+ ## The available matchers are:+ ## - RailsModelMatcher+ ## Matches ActiveRecord translations like+ ## User.human_attribute_name(:email) and User.model_name.human+ ## - DefaultI18nSubjectMatcher+ ## Matches ActionMailer's default_i18n_subject method+ ##+ ## To implement your own, please see `I18n::Tasks::Scanners::AstMatchers::BaseMatcher`.+ # ast_matchers:+ # - 'I18n::Tasks::Scanners::AstMatchers::RailsModelMatcher'+ # - 'I18n::Tasks::Scanners::AstMatchers::DefaultI18nSubjectMatcher'++ ## Multiple scanners can be used. Their results are merged.+ ## The options specified above are passed down to each scanner. Per-scanner options can be specified as well.+ ## See this example of a custom scanner: https://github.com/glebm/i18n-tasks/wiki/A-custom-scanner-example++## Translation Services+# translation:+# # Google Translate+# # Get an API key and set billing info at https://code.google.com/apis/console to use Google Translate+# google_translate_api_key: "AbC-dEf5"+# # DeepL Pro Translate+# # Get an API key and subscription at https://www.deepl.com/pro to use DeepL Pro+# deepl_api_key: "48E92789-57A3-466A-9959-1A1A1A1A1A1A"+# # deepl_host: "https://api.deepl.com"+# # deepl_version: "v2"+# # deepl_glossary_ids:+# # - f28106eb-0e06-489e-82c6-8215d6f95089+# # - 2c6415be-1852-4f54-9e1b-d800463496b4+# # add additional options to the DeepL.translate call: https://www.deepl.com/docs-api/translate-text/translate-text/+# deepl_options:+# formality: prefer_less+# # OpenAI+# openai_api_key: "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"+# # openai_model: "gpt-3.5-turbo" # see https://platform.openai.com/docs/models+# # may contain `%{from}` and `%{to}`, which will be replaced by source and target locale codes, respectively (using `Kernel.format`)+# # openai_system_prompt: >-+# # You are a professional translator that translates content from the %{from} locale+# # to the %{to} locale in an i18n locale array.+# #+# # The array has a structured format and contains multiple strings. Your task is to translate+# # each of these strings and create a new array with the translated strings.+# #+# # HTML markups (enclosed in < and > characters) must not be changed under any circumstance.+# # Variables (starting with %%{ and ending with }) must not be changed under any circumstance.+# #+# # Keep in mind the context of all the strings for a more accurate translation.++## Do not consider these keys missing:+# ignore_missing:+# - 'errors.messages.{accepted,blank,invalid,too_short,too_long}'+# - '{devise,simple_form}.*'++## Consider these keys used:+# ignore_unused:++## Exclude these keys from the `i18n-tasks eq-base' report:+# ignore_eq_base:+# all:+# - common.ok+# fr,es:+# - common.brand++## Exclude these keys from the `i18n-tasks check-consistent-interpolations` report:+# ignore_inconsistent_interpolations:+# - 'activerecord.attributes.*'++## Ignore these keys completely:+# ignore:+# - kaminari.*++## Sometimes, it isn't possible for i18n-tasks to match the key correctly,+## e.g. in case of a relative key defined in a helper method.+## In these cases you can use the built-in PatternMapper to map patterns to keys, e.g.:+#+# <%# I18n::Tasks.add_scanner 'I18n::Tasks::Scanners::PatternMapper',+# only: %w(*.html.haml *.html.slim),+# patterns: [['= title\b', '.page_title']] %>+#+# The PatternMapper can also match key literals via a special %{key} interpolation, e.g.:+#+<% I18n::Tasks.add_scanner 'I18n::Tasks::Scanners::PatternMapper', patterns: [['\bSpree\.t[( ]\s*%{key}', 'spree.%{key}']] %>\ No newline at end of filediff --git a/cli/lib/spree_cli/templates/extension/config/importmap.rb.tt b/cli/lib/spree_cli/templates/extension/config/importmap.rb.ttnew file mode 100644index 0000000000..8472a6302c--- /dev/null+++ b/cli/lib/spree_cli/templates/extension/config/importmap.rb.tt@@ -0,0 +1,6 @@+pin 'application-<%= file_name.tr("_","-") %>', to: '<%= file_name %>/application.js', preload: false++pin_all_from <%= class_name %>::Engine.root.join('app/javascript/<%= file_name %>/controllers'),+ under: '<%= file_name %>/controllers',+ to: '<%= file_name %>/controllers',+ preload: 'application-<%= file_name.tr("_","-") %>'diff --git a/cli/lib/spree_cli/templates/extension/config/initializers/spree.rb.tt b/cli/lib/spree_cli/templates/extension/config/initializers/spree.rb.ttnew file mode 100644index 0000000000..c8785973a8--- /dev/null+++ b/cli/lib/spree_cli/templates/extension/config/initializers/spree.rb.tt@@ -0,0 +1,32 @@+# Uncomment lines below to add your own custom business logic+# such as promotions, shipping methods, etc.+Rails.application.config.after_initialize do+ # Rails.application.config.spree.shipping_methods << Spree::ShippingMethods::SuperExpensiveNotVeryFastShipping+ # Rails.application.config.spree.payment_methods << Spree::PaymentMethods::VerySafeAndReliablePaymentMethod++ # Rails.application.config.spree.calculators.tax_rates << Spree::TaxRates::FinanceTeamForcedMeToCodeThis++ # Rails.application.config.spree.stock_splitters << Spree::Stock::Splitters::SecretLogicSplitter++ # Rails.application.config.spree.adjusters << Spree::Adjustable::Adjuster::TaxTheRich++ # Custom promotions+ # Rails.application.config.spree.calculators.promotion_actions_create_adjustments << Spree::Calculators::PromotionActions::CreateAdjustments::AddDiscountForFriends+ # Rails.application.config.spree.calculators.promotion_actions_create_item_adjustments << Spree::Calculators::PromotionActions::CreateItemAdjustments::FinanceTeamForcedMeToCodeThis+ # Rails.application.config.spree.promotions.rules << Spree::Promotions::Rules::OnlyForVIPCustomers+ # Rails.application.config.spree.promotions.actions << Spree::Promotions::Actions::GiftWithPurchase++ # Rails.application.config.spree.taxon_rules << Spree::TaxonRules::ProductsWithColor++ # Rails.application.config.spree.exports << Spree::Exports::Payments+ # Rails.application.config.spree.reports << Spree::Reports::MassivelyOvercomplexReportForCfo++ # Themes and page builder+ # Rails.application.config.spree.themes << Spree::Themes::NewShinyTheme+ # Rails.application.config.spree.theme_layout_sections << Spree::PageSections::SuperImportantCeoBio+ # Rails.application.config.spree.page_sections << Spree::PageSections::ContactFormToGetInTouch+ # Rails.application.config.spree.page_blocks << Spree::PageBlocks::BigRedButtonToCallSales++ # Storefront partials+ Rails.application.config.spree_storefront.head_partials << '<%= file_name %>/head'+enddiff --git a/cli/lib/spree_cli/templates/extension/config/locales/en.yml b/cli/lib/spree_cli/templates/extension/config/locales/en.yml.ttsimilarity index 68%rename from cli/lib/spree_cli/templates/extension/config/locales/en.ymlrename to cli/lib/spree_cli/templates/extension/config/locales/en.yml.ttindex 179c14ca52..7caf9f7e5e 100644--- a/cli/lib/spree_cli/templates/extension/config/locales/en.yml+++ b/cli/lib/spree_cli/templates/extension/config/locales/en.yml.tt@@ -2,4 +2,6 @@# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.en:- hello: "Hello world"+ spree:+ <%= file_name.gsub('spree_', '') %>:+ hello: "Hello <%= class_name %>"diff --git a/cli/lib/spree_cli/templates/extension/lib/%file_name%.rb.tt b/cli/lib/spree_cli/templates/extension/lib/%file_name%.rb.ttindex f279f52529..d98d0a6ed0 100644--- a/cli/lib/spree_cli/templates/extension/lib/%file_name%.rb.tt+++ b/cli/lib/spree_cli/templates/extension/lib/%file_name%.rb.tt@@ -3,3 +3,9 @@ require 'spree_extension'require '<%=file_name%>/engine'require '<%=file_name%>/version'require '<%=file_name%>/configuration'++module <%= class_name %>+ def self.queue+ 'default'+ end+end\ No newline at end of filediff --git a/cli/lib/spree_cli/templates/extension/lib/%file_name%/engine.rb.tt b/cli/lib/spree_cli/templates/extension/lib/%file_name%/engine.rb.ttindex 69f2b5c490..f12a326d1d 100644--- a/cli/lib/spree_cli/templates/extension/lib/%file_name%/engine.rb.tt+++ b/cli/lib/spree_cli/templates/extension/lib/%file_name%/engine.rb.tt@@ -13,6 +13,18 @@ module <%= class_name %><%= class_name %>::Config = <%= class_name %>::Configuration.newend+ initializer '<%= file_name %>.assets' do |app|+ app.config.assets.paths << root.join('app/javascript')+ app.config.assets.paths << root.join('vendor/javascript')+ app.config.assets.precompile += %w[<%= file_name %>_manifest]+ end++ initializer '<%= file_name %>.importmap', before: 'importmap' do |app|+ app.config.importmap.paths << root.join('config/importmap.rb')+ # https://github.com/rails/importmap-rails?tab=readme-ov-file#sweeping-the-cache-in-development-and-test+ app.config.importmap.cache_sweepers << root.join('app/javascript')+ end+def self.activateDir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|Rails.configuration.cache_classes ? require(c) : load(c)diff --git a/cli/lib/spree_cli/templates/extension/spec/feature/home_page_spec.rb b/cli/lib/spree_cli/templates/extension/spec/feature/home_page_spec.rbnew file mode 100644index 0000000000..be107cc8b5--- /dev/null+++ b/cli/lib/spree_cli/templates/extension/spec/feature/home_page_spec.rb@@ -0,0 +1,15 @@+require 'spec_helper'++RSpec.describe 'Home Page', type: :feature, js: true do+ let(:store) { Spree::Store.default }++ describe 'visiting the home page' do+ before do+ visit '/'+ end++ it 'loads successfully' do+ expect(page).to have_content(store.name)+ end+ end+enddiff --git a/cli/lib/spree_cli/templates/extension/spec/i18n_spec.rb b/cli/lib/spree_cli/templates/extension/spec/i18n_spec.rbnew file mode 100644index 0000000000..eacd60d02f--- /dev/null+++ b/cli/lib/spree_cli/templates/extension/spec/i18n_spec.rb@@ -0,0 +1,27 @@+require 'spec_helper'+require 'i18n/tasks'++RSpec.describe I18n do+ let(:i18n) { I18n::Tasks::BaseTask.new }+ let(:missing_keys) { i18n.missing_keys }+ let(:inconsistent_interpolations) { i18n.inconsistent_interpolations }++ it 'does not have missing keys' do+ expect(missing_keys).to be_empty,+ "Missing #{missing_keys.leaves.count} i18n keys, run `i18n-tasks missing' to show them"+ end++ it 'files are normalized' do+ non_normalized = i18n.non_normalized_paths+ error_message = "The following files need to be normalized:\n" \+ "#{non_normalized.map { |path| " #{path}" }.join("\n")}\n" \+ "Please run `i18n-tasks normalize' to fix"+ expect(non_normalized).to be_empty, error_message+ end++ it 'does not have inconsistent interpolations' do+ error_message = "#{inconsistent_interpolations.leaves.count} i18n keys have inconsistent interpolations.\n" \+ "Run `i18n-tasks check-consistent-interpolations' to show them"+ expect(inconsistent_interpolations).to be_empty, error_message+ end+enddiff --git a/cli/lib/spree_cli/templates/extension/spec/spec_helper.rb b/cli/lib/spree_cli/templates/extension/spec/spec_helper.rb.ttsimilarity index 65%rename from cli/lib/spree_cli/templates/extension/spec/spec_helper.rbrename to cli/lib/spree_cli/templates/extension/spec/spec_helper.rb.ttindex 5ffbc56f21..7b743dda1c 100644--- a/cli/lib/spree_cli/templates/extension/spec/spec_helper.rb+++ b/cli/lib/spree_cli/templates/extension/spec/spec_helper.rb.tt@@ -1,10 +1,22 @@# Configure Rails EnvironmentENV['RAILS_ENV'] = 'test'+require 'dotenv/load'+require File.expand_path('../dummy/config/environment.rb', __FILE__)require 'spree_dev_tools/rspec/spec_helper'+require '<%= file_name %>/factories'# Requires supporting ruby files with custom matchers and macros, etc,# in spec/support/ and its subdirectories.Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].sort.each { |f| require f }++def json_response+ case body = JSON.parse(response.body)+ when Hash+ body.with_indifferent_access+ when Array+ body+ end+enddiff --git a/cli/lib/spree_cli/templates/extension/spec/zeitwerk_spec.rb b/cli/lib/spree_cli/templates/extension/spec/zeitwerk_spec.rbnew file mode 100644index 0000000000..59b6c709b0--- /dev/null+++ b/cli/lib/spree_cli/templates/extension/spec/zeitwerk_spec.rb@@ -0,0 +1,9 @@+require 'spec_helper'++describe 'Zeitwerk' do+ it 'eager loads all files' do+ expect do+ Zeitwerk::Loader.eager_load_all+ end.not_to raise_error+ end+end