Class: Senren::Rails::ComponentInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/senren/rails/component_installer.rb

Overview

Installs one or more registered Senren components into the host app and refreshes the generated guidance files afterward.

Constant Summary collapse

USAGE =
'Usage: bin/rails senren:add NAME [NAME...] [--client | --no-client]'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry: Registry.load!, paths: HostPaths.new, stdout: $stdout) ⇒ ComponentInstaller

Returns a new instance of ComponentInstaller.



26
27
28
29
30
# File 'lib/senren/rails/component_installer.rb', line 26

def initialize(registry: Registry.load!, paths: HostPaths.new, stdout: $stdout)
  @registry = registry
  @paths    = paths
  @stdout   = stdout
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



16
17
18
# File 'lib/senren/rails/component_installer.rb', line 16

def paths
  @paths
end

#registryObject (readonly)

Returns the value of attribute registry.



16
17
18
# File 'lib/senren/rails/component_installer.rb', line 16

def registry
  @registry
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



16
17
18
# File 'lib/senren/rails/component_installer.rb', line 16

def stdout
  @stdout
end

Class Method Details

.normalize_names(names) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/senren/rails/component_installer.rb', line 18

def self.normalize_names(names)
  Array(names)
    .flatten
    .flat_map { |entry| entry.to_s.split(/[,\s]+/) }
    .reject { |entry| entry.empty? || entry.start_with?('-') }
    .uniq
end

Instance Method Details

#install(names:, client_override: nil, force: false) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/senren/rails/component_installer.rb', line 32

def install(names:, client_override: nil, force: false)
  normalized_names = self.class.normalize_names(names)
  raise ArgumentError, USAGE if normalized_names.empty?

  installed = ComponentCopier.new(registry: registry, paths: paths, stdout: stdout)
                             .install(normalized_names, client_override: client_override, force: force)

  SkillWriter.new(registry: registry, paths: paths).sync!
  AgentRulesWriter.new(registry: registry, paths: paths).sync!

  stdout.puts "Installed: #{installed.join(', ')}"
  installed
end