Class: Carioca::Services::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/carioca/services/setup.rb

Overview

Exiter namespace

facilities for file system commands collapse

Instance Method Summary collapse

Constructor Details

#initializeSetup

Returns a new instance of Setup.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/carioca/services/setup.rb', line 8

def initialize
  registry = Carioca::Registry.get
  @output = registry.get_service name: :output
  @i18n = registry.get_service name: :i18n
  @toolbox = registry.get_service name: :toolbox
  @configuration = registry.get_service name: :configuration
  @finisher = registry.get_service name: :finisher
  @schema = {}
  if @configuration.settings.include? :setup
    @schema = @configuration.settings.setup.include?(:rules) ? @configuration.settings.setup.rules : {}
  end
end

Instance Method Details

#execute!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/carioca/services/setup.rb', line 21

def execute!
  unless @schema.empty?
    begin
      @output.info @i18n.t('setup.execute.start')
      @schema.each do |item|
        action = item[:action]
        item.delete(:action)
        send action, **item
      end
    rescue StandardError
      @finisher.secure_raise message: @i18n.t('setup.error'), error_case: :status_ko
    end
  end
end

#install_file(source:, target:, mode: '644', owner: nil, group: nil, force: true, gem: true, gem_name: 'carioca') ⇒ Object

facility for file installation

Parameters:

  • [String] (Hash)

    a customizable set of options

  • [Bool] (Hash)

    a customizable set of options



47
48
49
50
51
52
53
54
# File 'lib/carioca/services/setup.rb', line 47

def install_file(source:, target:, mode: '644', owner: nil, group: nil, force: true, gem: true, gem_name: 'carioca')
  @output.item @i18n.t('setup.install', file: target)
  full_target = File.expand_path(target)
  source = @toolbox.search_file_in_gem(gem: gem_name, file: source) if gem
  FileUtils.copy source, full_target if force
  FileUtils.chmod mode.to_i(8), full_target
  FileUtils.chown owner, group, full_target if owner && group
end

#make_folder(path:, mode: '644', owner: nil, group: nil) ⇒ Object

facility for folder creation

Parameters:

  • [String] (Hash)

    a customizable set of options



61
62
63
64
65
66
67
# File 'lib/carioca/services/setup.rb', line 61

def make_folder(path:, mode: '644', owner: nil, group: nil)
  full_path = File.expand_path(path)
  @output.item @i18n.t('setup.mkdir', path: full_path)
  FileUtils.mkdir_p path unless File.exist? full_path
  FileUtils.chmod mode.to_i(8), full_path
  FileUtils.chown owner, group, full_path if owner && group
end

facility for Symbolic link

Parameters:

  • [String] (Hash)

    a customizable set of options



72
73
74
75
76
77
78
# File 'lib/carioca/services/setup.rb', line 72

def make_link(source:, link:)
  File.expand_path(source)
  File.expand_path(link)
  @output.item @i18n.t('setup.ln', target: link, source:)
  FileUtils.rm link if File.symlink?(link) && !File.exist?(link)
  FileUtils.ln_s source, link unless File.exist? link
end