Top Level Namespace

Defined Under Namespace

Modules: CableReady, CableReadyHelper, ExtendHasMany

Instance Method Summary collapse

Instance Method Details

#action_cable_initializer_pathObject



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

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

#action_cable_initializer_working_pathObject



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

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

#add_dev_package(name) ⇒ Object



82
83
84
85
# File 'lib/cable_ready/installer.rb', line 82

def 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



67
68
69
70
# File 'lib/cable_ready/installer.rb', line 67

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

#add_gem_listObject



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

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

#add_package(name) ⇒ Object



77
78
79
80
# File 'lib/cable_ready/installer.rb', line 77

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

#application_record_pathObject



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

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

#backup(path, delete: false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cable_ready/installer.rb', line 37

def 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



202
203
204
# File 'lib/cable_ready/installer.rb', line 202

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

#bundlerObject



110
111
112
# File 'lib/cable_ready/installer.rb', line 110

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

#complete_step(step) ⇒ Object



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

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

#config_pathObject



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

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

#controllers_pathObject



158
159
160
# File 'lib/cable_ready/installer.rb', line 158

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

#cr_npm_versionObject

memoized values



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

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

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



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

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

#current_templateObject



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

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

#dev_package_listObject



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

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

#development_pathObject



194
195
196
# File 'lib/cable_ready/installer.rb', line 194

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

#development_working_pathObject



198
199
200
# File 'lib/cable_ready/installer.rb', line 198

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

#drop_package(name) ⇒ Object



87
88
89
90
# File 'lib/cable_ready/installer.rb', line 87

def 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



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

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

#entrypointObject



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

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

#fetch(step_path, file) ⇒ Object

general utilities



7
8
9
10
11
12
# File 'lib/cable_ready/installer.rb', line 7

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

  Pathname.new(location)
end

#friendly_importmap_pathObject



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

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

#friendly_pack_pathObject



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

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

#gemfileObject



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

def gemfile
  @gemfile ||= gemfile_path.read
end

#gemfile_hashObject



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

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

#gemfile_pathObject



162
163
164
# File 'lib/cable_ready/installer.rb', line 162

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

#halt(message) ⇒ Object



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

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

#importmap_pathObject



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

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

#optionsObject



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

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

#options_pathObject



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

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

#packObject



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

def pack
  @pack ||= pack_path.read
end

#pack_pathObject



134
135
136
137
138
139
140
# File 'lib/cable_ready/installer.rb', line 134

def 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)


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

def 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



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

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

#package_listObject



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

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

#prefixObject



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cable_ready/installer.rb', line 170

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

#remove_gem(name) ⇒ Object



72
73
74
75
# File 'lib/cable_ready/installer.rb', line 72

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

#remove_gem_listObject



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

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

#template_srcObject



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

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

#workingObject



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

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