Class: CableReady::Installer

Inherits:
Object
  • Object
show all
Includes:
Thor::Actions, Thor::Base
Defined in:
lib/cable_ready/installer.rb

Class Method Summary collapse

Class Method Details

.action_cable_initializer_pathObject



211
212
213
# File 'lib/cable_ready/installer.rb', line 211

def self.action_cable_initializer_path
  @action_cable_initializer_path ||= Rails.root.join("config/initializers/action_cable.rb")
end

.action_cable_initializer_working_pathObject



215
216
217
# File 'lib/cable_ready/installer.rb', line 215

def self.action_cable_initializer_working_path
  @action_cable_initializer_working_path ||= Rails.root.join(working, "action_cable.rb")
end

.add_dev_package(name) ⇒ Object



107
108
109
110
# File 'lib/cable_ready/installer.rb', line 107

def self.add_dev_package(name)
  create_or_append(dev_package_list, "#{name}\n", verbose: false)
  say "☑️  Enqueued #{name} to be added to dev dependencies"
end

.add_gem(name) ⇒ Object



92
93
94
95
# File 'lib/cable_ready/installer.rb', line 92

def self.add_gem(name)
  create_or_append(add_gem_list, "#{name}\n", verbose: false)
  say "☑️  Added #{name} to the Gemfile"
end

.add_gem_listObject



231
232
233
# File 'lib/cable_ready/installer.rb', line 231

def self.add_gem_list
  @add_gem_list ||= Rails.root.join("tmp/cable_ready_installer/add_gem_list")
end

.add_package(name) ⇒ Object



102
103
104
105
# File 'lib/cable_ready/installer.rb', line 102

def self.add_package(name)
  create_or_append(package_list, "#{name}\n", verbose: false)
  say "☑️  Enqueued #{name} to be added to dependencies"
end

.append_fileObject



18
19
20
# File 'lib/cable_ready/installer.rb', line 18

def self.append_file(...)
  new.append_file(...)
end

.application_record_pathObject



207
208
209
# File 'lib/cable_ready/installer.rb', line 207

def self.application_record_path
  @application_record_path ||= Rails.root.join("app/models/application_record.rb")
end

.backup(path, delete: false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cable_ready/installer.rb', line 62

def self.backup(path, delete: false)
  if !path.exist?
    yield
    return
  end

  backup_path = Pathname.new("#{path}.bak")
  old_path = path.relative_path_from(Rails.root).to_s
  filename = path.to_path.split("/").last

  if backup_path.exist?
    if backup_path.read == path.read
      path.delete if delete
      yield
      return
    end
    backup_path.delete
  end

  copy_file(path, backup_path, verbose: false)
  path.delete if delete

  yield

  if path.read != backup_path.read
    create_or_append(backups_path, "#{old_path}\n", verbose: false)
  end
  say "📦 #{old_path} backed up as #{filename}.bak"
end

.backups_pathObject



227
228
229
# File 'lib/cable_ready/installer.rb', line 227

def self.backups_path
  @backups_path ||= Rails.root.join("tmp/cable_ready_installer/backups")
end

.bundlerObject



135
136
137
# File 'lib/cable_ready/installer.rb', line 135

def self.bundler
  @bundler ||= File.read("tmp/cable_ready_installer/bundler")
end

.complete_step(step) ⇒ Object



39
40
41
# File 'lib/cable_ready/installer.rb', line 39

def self.complete_step(step)
  create_file "tmp/cable_ready_installer/#{step}", verbose: false
end

.config_pathObject



139
140
141
# File 'lib/cable_ready/installer.rb', line 139

def self.config_path
  @config_path ||= Rails.root.join(entrypoint, "config")
end

.controllers_pathObject



183
184
185
# File 'lib/cable_ready/installer.rb', line 183

def self.controllers_path
  @controllers_path ||= Rails.root.join(entrypoint, "controllers")
end

.copy_fileObject



22
23
24
# File 'lib/cable_ready/installer.rb', line 22

def self.copy_file(...)
  new.copy_file(...)
end

.cr_npm_versionObject

memoized values



123
124
125
# File 'lib/cable_ready/installer.rb', line 123

def self.cr_npm_version
  @cr_npm_version ||= CableReady::VERSION.gsub(".pre", "-pre")
end

.create_fileObject

Thor wrapper



14
15
16
# File 'lib/cable_ready/installer.rb', line 14

def self.create_file(...)
  new.create_file(...)
end

.create_or_append(path, *args, &block) ⇒ Object



43
44
45
46
# File 'lib/cable_ready/installer.rb', line 43

def self.create_or_append(path, *args, &block)
  FileUtils.touch(path)
  append_file(path, *args, &block)
end

.current_templateObject



48
49
50
# File 'lib/cable_ready/installer.rb', line 48

def self.current_template
  ENV["LOCATION"].split("/").last.gsub(".rb", "")
end

.dev_package_listObject



171
172
173
# File 'lib/cable_ready/installer.rb', line 171

def self.dev_package_list
  @dev_package_list ||= Rails.root.join("tmp/cable_ready_installer/npm_dev_package_list")
end

.development_pathObject



219
220
221
# File 'lib/cable_ready/installer.rb', line 219

def self.development_path
  @development_path ||= Rails.root.join("config/environments/development.rb")
end

.development_working_pathObject



223
224
225
# File 'lib/cable_ready/installer.rb', line 223

def self.development_working_path
  @development_working_path ||= Rails.root.join(working, "development.rb")
end

.drop_package(name) ⇒ Object



112
113
114
115
# File 'lib/cable_ready/installer.rb', line 112

def self.drop_package(name)
  create_or_append(drop_package_list, "#{name}\n", verbose: false)
  say "❎ Enqueued #{name} to be removed from dependencies"
end

.drop_package_listObject



175
176
177
# File 'lib/cable_ready/installer.rb', line 175

def self.drop_package_list
  @drop_package_list ||= Rails.root.join("tmp/cable_ready_installer/drop_npm_package_list")
end

.entrypointObject



131
132
133
# File 'lib/cable_ready/installer.rb', line 131

def self.entrypoint
  @entrypoint ||= File.read("tmp/cable_ready_installer/entrypoint")
end

.fetch(step_path, file) ⇒ Object

general utilities



32
33
34
35
36
37
# File 'lib/cable_ready/installer.rb', line 32

def self.fetch(step_path, file)
  relative_path = step_path + file
  location = template_src + relative_path

  Pathname.new(location)
end

.friendly_importmap_pathObject



147
148
149
# File 'lib/cable_ready/installer.rb', line 147

def self.friendly_importmap_path
  @friendly_importmap_path ||= importmap_path.relative_path_from(Rails.root).to_s
end

.friendly_pack_pathObject



155
156
157
# File 'lib/cable_ready/installer.rb', line 155

def self.friendly_pack_path
  @friendly_pack_path ||= pack_path.relative_path_from(Rails.root).to_s
end

.gemfileObject



191
192
193
# File 'lib/cable_ready/installer.rb', line 191

def self.gemfile
  @gemfile ||= gemfile_path.read
end

.gemfile_hashObject



117
118
119
# File 'lib/cable_ready/installer.rb', line 117

def self.gemfile_hash
  Digest::MD5.hexdigest(gemfile_path.read)
end

.gemfile_pathObject



187
188
189
# File 'lib/cable_ready/installer.rb', line 187

def self.gemfile_path
  @gemfile_path ||= Rails.root.join("Gemfile")
end

.halt(message) ⇒ Object



57
58
59
60
# File 'lib/cable_ready/installer.rb', line 57

def self.halt(message)
  say "#{message}", :red
  create_file "tmp/cable_ready_installer/halt", verbose: false
end

.importmap_pathObject



143
144
145
# File 'lib/cable_ready/installer.rb', line 143

def self.importmap_path
  @importmap_path ||= Rails.root.join("config/importmap.rb")
end

.optionsObject



243
244
245
# File 'lib/cable_ready/installer.rb', line 243

def self.options
  @options ||= YAML.safe_load(File.read(options_path))
end

.options_pathObject



239
240
241
# File 'lib/cable_ready/installer.rb', line 239

def self.options_path
  @options_path ||= Rails.root.join("tmp/cable_ready_installer/options")
end

.packObject



151
152
153
# File 'lib/cable_ready/installer.rb', line 151

def self.pack
  @pack ||= pack_path.read
end

.pack_pathObject



159
160
161
162
163
164
165
# File 'lib/cable_ready/installer.rb', line 159

def self.pack_path
  @pack_path ||= [
    Rails.root.join(entrypoint, "application.js"),
    Rails.root.join(entrypoint, "packs/application.js"),
    Rails.root.join(entrypoint, "entrypoints/application.js")
  ].find(&:exist?)
end

.pack_path_missing?Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/cable_ready/installer.rb', line 52

def self.pack_path_missing?
  return false unless pack_path.nil?
  halt "#{friendly_pack_path} is missing. You need a valid application pack file to proceed."
end

.package_jsonObject



127
128
129
# File 'lib/cable_ready/installer.rb', line 127

def self.package_json
  @package_json ||= Rails.root.join("package.json")
end

.package_listObject



167
168
169
# File 'lib/cable_ready/installer.rb', line 167

def self.package_list
  @package_list ||= Rails.root.join("tmp/cable_ready_installer/npm_package_list")
end

.prefixObject



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/cable_ready/installer.rb', line 195

def self.prefix
  # standard:disable Style/RedundantStringEscape
  @prefix ||= {
    "vite" => "..\/",
    "webpacker" => "",
    "shakapacker" => "",
    "importmap" => "",
    "esbuild" => ".\/"
  }[bundler]
  # standard:enable Style/RedundantStringEscape
end

.remove_gem(name) ⇒ Object



97
98
99
100
# File 'lib/cable_ready/installer.rb', line 97

def self.remove_gem(name)
  create_or_append(remove_gem_list, "#{name}\n", verbose: false)
  say "❎ Removed #{name} from Gemfile"
end

.remove_gem_listObject



235
236
237
# File 'lib/cable_ready/installer.rb', line 235

def self.remove_gem_list
  @remove_gem_list ||= Rails.root.join("tmp/cable_ready_installer/remove_gem_list")
end

.sayObject



26
27
28
# File 'lib/cable_ready/installer.rb', line 26

def self.say(...)
  new.say(...)
end

.template_srcObject



179
180
181
# File 'lib/cable_ready/installer.rb', line 179

def self.template_src
  @template_src ||= File.read("tmp/cable_ready_installer/template_src")
end

.workingObject



247
248
249
# File 'lib/cable_ready/installer.rb', line 247

def self.working
  @working ||= Rails.root.join("tmp/cable_ready_installer/working")
end