Class: PiBrowserTaskbar::Generators::InstallGenerator

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

Constant Summary collapse

DEFAULT_MOUNT =
"/dev/pi-browser-taskbar"
INITIALIZER_PATH =
"config/initializers/pi_browser_taskbar.rb"
ROUTES_PATH =
"config/routes.rb"
ROUTES_DRAW =
/^[ \t]*Rails\.application\.routes\.draw\s+do\b/
LEGACY_ROUTE =
"  mount Pi::Browser::Taskbar::Rails::Engine => \"/dev/pi-browser-taskbar\" if Rails.env.development? && defined?(Pi::Browser::Taskbar::Rails::Engine) && Pi::Browser::Taskbar::Rails.active?"
LEGACY_LAYOUT =
"  <%= Pi::Browser::Taskbar::Rails.layout_bootstrap(self) if Rails.env.development? && defined?(Pi::Browser::Taskbar::Rails) && Pi::Browser::Taskbar::Rails.active? %>"
LEGACY_INITIALIZER =
<<~RUBY.freeze
  # Generated by pi-browser-taskbar-rails. The gem belongs in the development group.
  if Rails.env.development? && defined?(Pi::Browser::Taskbar::Rails)
    Pi::Browser::Taskbar::Rails.configure do |config|
      config.project_root = Rails.root.to_s
    end
  end
RUBY

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.plan!(root, layout: nil, mount: nil) ⇒ Object

Plans and validates every host edit before any write occurs.



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/generators/pi_browser_taskbar/install_generator.rb', line 60

def plan!(root, layout: nil, mount: nil)
  root = canonical_root!(root)
  initializer_path = host_path(root, INITIALIZER_PATH, "initializer")
  installed = File.exist?(initializer_path) ? installed_metadata!(root, initializer_path) : nil
  desired_mount = normalize_mount!(mount || (installed && installed.fetch("mount")) || DEFAULT_MOUNT)

  if installed && desired_mount != installed.fetch("mount")
    raise ArgumentError, "installed mount #{installed.fetch("mount")} differs from #{desired_mount}; run the inverse generator before changing --mount"
  end

  route_path = host_path(root, installed ? installed.fetch("routes") : ROUTES_PATH, "routes metadata")
  selected_layout = layout && explicit_layout!(root, layout)
  layouts = if installed
    installed.fetch("layouts").map { |path| host_path(root, path, "layout metadata") }
  else
    [selected_layout || discover_layout!(root)]
  end
  layouts << selected_layout if selected_layout
  layouts.uniq!

  route_source = read!(route_path, "routes")
  validate_ruby!(route_source, route_path, "routes")
  route_output = install_routes!(route_source, desired_mount)
  validate_ruby!(route_output, route_path, "generated routes")

  layout_outputs = layouts.to_h do |path|
    source = read!(path, "ERB application layout")
    [path, install_layout!(source, desired_mount, path)]
  end

   = {
    "schema" => 1,
    "routes" => relative(route_path, root),
    "layouts" => layouts.map { |path| relative(path, root) },
    "mount" => desired_mount
  }
  initializer_output = initializer_source()
  validate_ruby!(initializer_output, initializer_path, "generated initializer")

  {route_path => route_output, **layout_outputs, initializer_path => initializer_output}
end

.plan_uninstall!(root) ⇒ Object

Preflights all recorded seams before returning one all-or-nothing removal plan.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/generators/pi_browser_taskbar/install_generator.rb', line 103

def plan_uninstall!(root)
  root = canonical_root!(root)
  initializer_path = host_path(root, INITIALIZER_PATH, "initializer")
  unless File.exist?(initializer_path)
    if orphaned_seams?(root)
      raise ArgumentError, "installation metadata is missing while generated host sections remain; manual removal: remove only complete pi-browser-taskbar start/end route and layout blocks"
    end
    return {}
  end

   = installed_metadata!(root, initializer_path)
  route_path = host_path(root, .fetch("routes"), "routes metadata")
  route_source = read!(route_path, "routes")
  route_output = ["legacy"] ? route_source.sub(LEGACY_ROUTE, "") : remove_owned!(route_source, "routes", :ruby)
  validate_ruby!(route_output, route_path, "routes")

  actions = {initializer_path => :delete, route_path => route_output}
  .fetch("layouts").each do |relative_path|
    path = host_path(root, relative_path, "layout metadata")
    source = read!(path, "ERB application layout")
    actions[path] = ["legacy"] ? source.sub(LEGACY_LAYOUT, "") : remove_owned!(source, "layout", :erb)
  end
  actions
end

.refresh_initializer_checksum(source) ⇒ Object

Raises:

  • (ArgumentError)


128
129
130
131
132
133
# File 'lib/generators/pi_browser_taskbar/install_generator.rb', line 128

def refresh_initializer_checksum(source)
  pattern = /^# pi-browser-taskbar:checksum [0-9a-f]{64}\n/
  raise ArgumentError, "unrecognized generated initializer; use manual removal" unless source.match?(pattern)
  without_checksum = source.sub(pattern, "")
  source.sub(pattern, "# pi-browser-taskbar:checksum #{Digest::SHA256.hexdigest(without_checksum)}\n")
end

.run!(root, layout: nil, mount: nil) ⇒ Object



51
52
53
# File 'lib/generators/pi_browser_taskbar/install_generator.rb', line 51

def run!(root, layout: nil, mount: nil)
  apply!(plan!(root, layout: layout, mount: mount)) ? :installed : :current
end

.uninstall!(root) ⇒ Object



55
56
57
# File 'lib/generators/pi_browser_taskbar/install_generator.rb', line 55

def uninstall!(root)
  apply!(plan_uninstall!(root)) ? :uninstalled : :current
end

Instance Method Details

#installObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/generators/pi_browser_taskbar/install_generator.rb', line 32

def install
  result = if behavior == :revoke
    self.class.uninstall!(destination_root)
  else
    self.class.run!(destination_root, layout: options[:layout], mount: options[:mount])
  end

  if behavior == :revoke
    say_status result == :current ? :current : :removed, "Pi Browser Taskbar Rails integration"
    say "Remove the pi-browser-taskbar-rails development-group dependency manually if no longer needed."
    say "Broker runtime artifacts, Pi sessions, credentials, and unrelated Action View configuration were not removed."
  else
    say_status result == :current ? :current : :installed, "Pi Browser Taskbar Rails integration"
  end
rescue ArgumentError => error
  raise ::Rails::Generators::Error, error.message
end