Class: Senren::Rails::ComponentCopier

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

Overview

Copies component files from the gem’s templates/ tree into the host Rails app, and updates .senren/installed_components.yml.

Defined Under Namespace

Classes: MissingTemplate

Constant Summary collapse

INSTALL_GENERATOR_TEMPLATES =
File.expand_path(
  '../../generators/senren/install/templates', __dir__
).freeze
BASE_COMPONENT_TEMPLATE =
File.join(INSTALL_GENERATOR_TEMPLATES, 'base_component.rb.tt').freeze
BASE_URL_HELPER_PATCH =
<<~RUBY

  # Added by senren:add for compatibility with URL-aware component templates.
  require 'uri'

  module Senren
    class BaseComponent
      SAFE_URL_PROTOCOLS = %w[http https mailto tel].freeze unless const_defined?(:SAFE_URL_PROTOCOLS)
      SAFE_MEDIA_URL_PROTOCOLS = %w[http https].freeze unless const_defined?(:SAFE_MEDIA_URL_PROTOCOLS)

      private

      def safe_url(value, fallback: '#', protocols: SAFE_URL_PROTOCOLS)
        url = value.to_s.strip
        return fallback if url.empty?
        return url if url.start_with?('#')
        return url if url.start_with?('/') && !url.start_with?('//')

        uri = URI.parse(url)
        return url if uri.scheme && Array(protocols).map(&:to_s).include?(uri.scheme.downcase)
        return fallback if uri.host
        return url unless uri.scheme

        fallback
      rescue URI::InvalidURIError
        fallback
      end

      def safe_media_url(value, fallback: nil)
        safe_url(value, fallback: fallback, protocols: SAFE_MEDIA_URL_PROTOCOLS)
      end
    end
  end
RUBY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ComponentCopier.



55
56
57
58
59
# File 'lib/senren/rails/component_copier.rb', line 55

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.



53
54
55
# File 'lib/senren/rails/component_copier.rb', line 53

def paths
  @paths
end

#registryObject (readonly)

Returns the value of attribute registry.



53
54
55
# File 'lib/senren/rails/component_copier.rb', line 53

def registry
  @registry
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



53
54
55
# File 'lib/senren/rails/component_copier.rb', line 53

def stdout
  @stdout
end

Instance Method Details

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

Installs a list of components (with deps), respecting the override flag. Returns the ordered list of component names actually installed.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/senren/rails/component_copier.rb', line 63

def install(component_names, client_override: nil, force: false)
  wanted = registry.dependencies(*component_names)
  paths.ensure_dirs!
  ensure_base_component_url_helpers!

  wanted.each do |name|
    comp = registry.fetch(name)
    install_component(comp, client_override: client_override, force: force)
  end

  update_installed_ledger(wanted, client_override: client_override)
  wanted
end