Class: SolidusStripe::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/solidus_stripe/install/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#install_migrationsObject



22
23
24
25
26
27
28
29
30
# File 'lib/generators/solidus_stripe/install/install_generator.rb', line 22

def install_migrations
  return if options[:sync]

  say_status :install, "[#{engine.engine_name}] migrations", :blue
  shell.indent do
    rake 'railties:install:migrations FROM=solidus_stripe'
    run 'bin/rails db:migrate' if options[:migrate]
  end
end

#install_solidus_backend_supportObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/generators/solidus_stripe/install/install_generator.rb', line 39

def install_solidus_backend_support
  support_code_for(:backend) do
    append_file(
      'vendor/assets/javascripts/spree/backend/all.js',
      "//= require spree/backend/solidus_stripe\n"
    )
    inject_into_file(
      'vendor/assets/stylesheets/spree/backend/all.css',
      " *= require spree/backend/solidus_stripe\n",
      before: %r{\*/},
      verbose: true,
    )
  end
end

#install_solidus_core_supportObject



32
33
34
35
36
37
# File 'lib/generators/solidus_stripe/install/install_generator.rb', line 32

def install_solidus_core_support
  support_code_for(:core) do
    directory 'config/initializers', 'config/initializers'
    route "mount SolidusStripe::Engine, at: '/solidus_stripe'"
  end
end

#install_solidus_starter_frontend_supportObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/generators/solidus_stripe/install/install_generator.rb', line 54

def install_solidus_starter_frontend_support
  support_code_for(:starter_frontend) do
    directory 'app', 'app'
    inject_into_file(
      'app/assets/stylesheets/solidus_starter_frontend.css',
      " *= require spree/frontend/solidus_stripe\n",
      before: %r{\*/},
      verbose: true,
    )

    spec_paths =
      case options[:specs]
      when 'all' then %w[spec]
      when 'frontend'
        %w[
          spec/solidus_stripe_spec_helper.rb
          spec/system/frontend
          spec/support
        ]
      end

    spec_paths.each do |path|
      if engine.root.join(path).directory?
        directory engine.root.join(path), path
      else
        template engine.root.join(path), path
      end
    end

    run 'bin/importmap pin @stripe/stripe-js'
  end
end

#load_seedsObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/generators/solidus_stripe/install/install_generator.rb', line 87

def load_seeds
  if options[:migrate] && options[:load_seeds]
    say_status :load, "seed data", :blue
    append_file "db/seeds.rb", <<~RUBY
      #{engine.name}.load_seed
    RUBY
    engine.load_seed
  else
    say_status :skip, "loading seed data", :blue
  end
end

#watchObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/generators/solidus_stripe/install/install_generator.rb', line 99

def watch
  return unless options[:watch]

  glob = "#{::SolidusStripe::Engine.root}/{app,lib,config}"
  say_status :watch, "starting watcher... #{glob}", :cyan

  require 'listen'
  listener = Listen.to(*Dir[glob], relative: true) do |*changes|
    say_status :watch, "changed: #{changes.flatten.join(', ')}", :cyan
    shell.indent do
      install_solidus_core_support
      install_solidus_backend_support
      install_solidus_starter_frontend_support
    end
    say_status :watch, "update completed", :cyan
  end
  listener.start
  sleep
rescue Interrupt
  say_status :watch, "stopping watcher...", :cyan
  listener.stop
rescue LoadError
  say_status :error, 'in order for the --watch option to work you need the "listen" gem in your Gemfile', :red
end