Class: WhatsAppNotifier::Generators::InstallGenerator

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

Instance Method Summary collapse

Instance Method Details

#create_initializerObject



9
10
11
12
13
# File 'lib/generators/whatsapp_notifier/install_generator.rb', line 9

def create_initializer
  return if File.exist?("config/initializers/whatsapp_notifier.rb")

  template "whatsapp_notifier.rb", "config/initializers/whatsapp_notifier.rb"
end

#ensure_gitignore_entriesObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/generators/whatsapp_notifier/install_generator.rb', line 44

def ensure_gitignore_entries
  entries = [
    "# WhatsApp Notifier",
    "/tmp/whatsapp_notifier",
    "/whatsapp_service/node_modules",
    "/whatsapp_service/.wwebjs_cache",
    "/whatsapp_service/.wwebjs_auth"
  ]

  content = File.exist?(".gitignore") ? File.read(".gitignore") : ""
  missing = entries.reject { |entry| content.include?(entry) }
  return if missing.empty?

  append_to_file(".gitignore", "\n#{missing.join("\n")}\n")
end

#ensure_procfile_entryObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/generators/whatsapp_notifier/install_generator.rb', line 29

def ensure_procfile_entry
  procfile = "Procfile.dev"
  line = "whatsapp: bundle exec whatsapp_notifier service"

  unless File.exist?(procfile)
    create_file(procfile, "#{line}\n")
    return
  end

  content = File.read(procfile)
  return if content.lines.any? { |existing| existing.strip == line }

  append_to_file(procfile, "\n#{line}\n")
end

#mount_engineObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/generators/whatsapp_notifier/install_generator.rb', line 15

def mount_engine
  routes_path = "config/routes.rb"
  return unless File.exist?(routes_path)

  content = File.read(routes_path)
  return if content.include?("mount WhatsAppNotifier::Engine")

  inject_into_file(
    routes_path,
    %(  mount WhatsAppNotifier::Engine, at: "/whatsapp"\n),
    after: /Rails\.application\.routes\.draw do\n/
  )
end

#next_stepsObject



68
69
70
71
# File 'lib/generators/whatsapp_notifier/install_generator.rb', line 68

def next_steps
  say("\nSetup complete.", :green)
  say("Run `bin/dev`, open `/dashboard/whatsapp/qr`, then send a test message.", :green)
end

#run_doctorObject

Raises:

  • (Thor::Error)


60
61
62
63
64
65
66
# File 'lib/generators/whatsapp_notifier/install_generator.rb', line 60

def run_doctor
  say("\nRunning setup doctor...", :yellow)
  ok = WhatsAppNotifier::Doctor.run(io: $stdout, app_root: destination_root)
  return if ok

  raise Thor::Error, "Setup checks failed. Fix items above, then run `bundle exec whatsapp_notifier doctor`."
end