Class: Appydave::Tools::Configuration::ExampleInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/appydave/tools/configuration/example_installer.rb

Overview

Installs bundled example configuration files into the user’s config directory.

Example files live at config/examples/*.example.json inside the gem. Each file is installed without the ‘.example` segment in its name so that `settings.example.json` becomes `settings.json` in the target directory.

Files are never overwritten — existing files are skipped and reported.

Examples:

Install all examples

result = ExampleInstaller.new.install
result[:installed] #=> ["settings.json", "locations.json"]
result[:skipped]   #=> []

Constant Summary collapse

EXAMPLES_PATH =
File.expand_path('../../../../config/examples', __dir__)

Instance Method Summary collapse

Constructor Details

#initialize(target_path: nil) ⇒ ExampleInstaller

Returns a new instance of ExampleInstaller.

Parameters:

  • target_path (String, nil) (defaults to: nil)

    Directory to install into. Defaults to the active Config.config_path (~/.config/appydave).



23
24
25
# File 'lib/appydave/tools/configuration/example_installer.rb', line 23

def initialize(target_path: nil)
  @target_path = target_path || Config.config_path
end

Instance Method Details

#availableArray<String>

List the filenames that would be installed (target names, not source names).

Returns:

  • (Array<String>)


52
53
54
# File 'lib/appydave/tools/configuration/example_installer.rb', line 52

def available
  example_files.map { |f| target_name(f) }
end

#installHash

Install all bundled example files that do not yet exist.

Returns:

  • (Hash)

    with keys :installed (Array<String>) and :skipped (Array<String>)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/appydave/tools/configuration/example_installer.rb', line 30

def install
  FileUtils.mkdir_p(@target_path)
  results = { installed: [], skipped: [] }

  example_files.each do |src|
    dest = destination_for(src)
    basename = File.basename(dest)

    if File.exist?(dest)
      results[:skipped] << basename
    else
      FileUtils.cp(src, dest)
      results[:installed] << basename
    end
  end

  results
end