Class: Outsider::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/outsider/outsider.rb

Overview

Class which installs and uninstalls files

Defined Under Namespace

Classes: InvalidRecordFile

Constant Summary collapse

DEFAULT_RECORD_FILE =

The path of the record file to use if the OUTSIDER_RECORD_FILE environment variable is unset

File.join '/', 'var', 'lib', 'outsider', 'installed_files'

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Installer

Creates a new instance

dir is the directory where to look for the outsider_files file and the files to install



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/outsider/outsider.rb', line 40

def initialize dir
  @gem_dir = dir
  @user_install = dir.start_with? ENV['HOME']
  @data = begin 
    YAML.load File.read(File.join(dir, 'outsider_files'))
  rescue SystemCallError
  end
  # If either the outsider_files file doesn't exist or it's empt
  # (in which case YAML.load returns false), set @data to an empty hash
  @data ||={}
  @gem_name = File.basename(dir)
end

Instance Method Details

#install_filesObject

Installs the files according to the instructions in the outsider_files file

Returns nil



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/outsider/outsider.rb', line 57

def install_files
  installed_files = []
  @data.each_pair do |k, v|
    dest = install_destination v
    dest = File.join dest, File.basename(k) if dest.end_with? '/'
    orig = File.join(@gem_dir, k)
    installed_files << [orig, dest] if install_file orig, dest
  end
  record_installed_files installed_files unless installed_files.empty?
  nil
end

#uninstall_filesObject

Uninstalls the files associated with the gem in the current directory

If any of those file is owned also by other gems (including by other versions of the same gem), then the files are only uninstalled if the gem is the last to have been installed. In this case, the file belonging to the previously installed gem is copied. If that file doesn’t exist, then the one previous to it is tried, and so on.

The record is updated to remove all mentions of the gem being uninstalled

Returns nil



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/outsider/outsider.rb', line 80

def uninstall_files
  rec_file = record_file
  files = begin read_record_file rec_file
  rescue InvalidRecordFile then nil
  end
  return unless files
  files.each_pair do |f, data|
    gem_data = data.find{|i| i[:gem] == @gem_name}
    next unless gem_data
    if data.last[:gem] == @gem_name
      FileUtils.rm_f f 
      replacement = data[0..-2].reverse_each.find{|d| File.exist? d[:origin]}
      if replacement
        FileUtils.cp replacement[:origin], f 
        puts "Replaced #{f} with #{replacement[:origin]}"
      else
        puts "Uninstalled #{f}"
      end
    end
    data.delete gem_data
  end
  files.delete_if{|k, v| v.empty?}
  write_record_file rec_file, files
  nil
end