Class: Jets::Booter

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/booter.rb

Class Method Summary collapse

Class Method Details

.app_initializersObject



47
48
49
50
51
# File 'lib/jets/booter.rb', line 47

def app_initializers
  Dir.glob("#{Jets.root}/config/initializers/**/*").each do |path|
    load path
  end
end

.boot!(options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jets/booter.rb', line 4

def boot!(options={})
  return if @booted

  turbo_charge
  confirm_jets_project!
  require_bundle_gems unless bypass_bundler_setup?
  Jets::Dotenv.load!

  Jets.application.setup!
  app_initializers
  turbine_initializers
  Jets.application.finish!

  Jets.eager_load!
  setup_db
  # build_middleware_stack # TODO: figure out how to build middleware during Jets.boot without breaking jets new and webpacker:install

  @booted = true
end

.build_middleware_stackObject

Builds and memoize stack so it only gets built on bootup



35
36
37
# File 'lib/jets/booter.rb', line 35

def build_middleware_stack
  Jets.application.build_stack
end

.bundler_groupsObject



103
104
105
# File 'lib/jets/booter.rb', line 103

def bundler_groups
  [:default, Jets.env.to_sym]
end

.bypass_bundler_setup?Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/jets/booter.rb', line 24

def bypass_bundler_setup?
  command = ARGV.first
  %w[build delete deploy url].include?(command)
end

.check_config_ru!Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/jets/booter.rb', line 119

def check_config_ru!
  config_ru = File.read("#{Jets.root}/config.ru")
  unless config_ru.include?("Jets.boot")
    puts 'The config.ru file is missing Jets.boot.  Please add Jets.boot after require "jets"'.color(:red)
    puts "This was changed as made in Jets v1.1.0."
    puts "To have Jets update the config.fu file for you, you can run:\n\n"
    puts "  jets upgrade"
    exit 1
  end
end

.confirm_jets_project!Object

Cannot call this for the jets new



108
109
110
111
112
113
# File 'lib/jets/booter.rb', line 108

def confirm_jets_project!
  unless File.exist?("#{Jets.root}/config/application.rb")
    puts "It does not look like you are running this command within a jets project.  Please confirm that you are in a jets project and try again.".color(:red)
    exit 1
  end
end

.messageObject



115
116
117
# File 'lib/jets/booter.rb', line 115

def message
  "Jets booting up in #{Jets.env.color(:green)} mode!"
end

.require_bundle_gemsObject

require_bundle_gems called when environment boots up via Jets.boot. It also useful for when to loading Rake tasks in Jets::Commands::RakeTasks.load!

For example, some gems like webpacker that load rake tasks are specified with a git based source:

gem "webpacker", git: "https://github.com/tongueroo/webpacker.git"

This results in the user having to specific bundle exec in front of jets for those rake tasks to show up in jets help.

Instead, when the user is within the project folder, jets automatically requires bundler for the user. So the rake tasks show up when calling jets help.

When the user calls jets help from outside the project folder, bundler is not used and the load errors get rescued gracefully. This is done in Jets::Commands::RakeTasks.load! In the case when there are in another project with another Gemfile, the load errors will still be rescued.



73
74
75
76
77
78
79
80
# File 'lib/jets/booter.rb', line 73

def require_bundle_gems
  # NOTE: Dont think ENV['BUNDLE_GEMFILE'] is quite working right.  We still need
  # to be in the project directory.  Leaving logic in here for when it gets fix.
  if ENV['BUNDLE_GEMFILE'] || File.exist?("Gemfile")
    require "bundler/setup"
    Bundler.require(*bundler_groups)
  end
end

.setup_dbObject

Only connects connect to database for ActiveRecord and when config/database.yml exists. Dynomite handles connecting to the clients lazily.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/jets/booter.rb', line 85

def setup_db
  return unless File.exist?("#{Jets.root}/config/database.yml")

  db_configs = Jets.application.config.database
  # DatabaseTasks.database_configuration for db:create db:migrate tasks
  # Documented in DatabaseTasks that this is the right way to set it when
  # using ActiveRecord rake tasks outside of Rails.
  ActiveRecord::Tasks::DatabaseTasks.database_configuration = db_configs

  current_config = db_configs[Jets.env]
  if current_config.empty?
    abort("ERROR: config/database.yml exists but no environment section configured for #{Jets.env}")
  end
  # Using ActiveRecord rake tasks outside of Rails, so we need to set up the
  # db connection ourselves
  ActiveRecord::Base.establish_connection(current_config)
end

.turbine_initializersObject



39
40
41
42
43
44
45
# File 'lib/jets/booter.rb', line 39

def turbine_initializers
  Jets::Turbine.subclasses.each do |subclass|
    subclass.initializers.each do |label, block|
      block.call(Jets.application)
    end
  end
end

.turbo_chargeObject



29
30
31
32
# File 'lib/jets/booter.rb', line 29

def turbo_charge
  turbo = Jets::Turbo.new
  turbo.charge
end