Class: Installer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src = '.', dest = '.', options = {}, &block) ⇒ Installer

Initializes the installer. Will install files in src to dest, using the options provided. Allowed options are user (the file owner), group (the group owner), and mode. If none are provided user and group default to root, and the mode defaults to the file’s current permissions on the filesystem. dmode is the mode for any directories created.

If no block is provided, the Installer will glob all files in src. If a block is provided, it must return a list of files (scoped to the src directory) that will be installed.

All files copied will maintain their directory structure under the src given. src and dest default to the current working directory, if not given.

Examples:

# 'foo/**/*' => 'bar'
Installer.new('foo', 'bar')

# 'foo/*.rb' => 'bar'
Installer.new('foo', 'bar') { FileList['*.rb'] }

# 'foo/bar/**/*' => 'baz/bar'
Installer.new('foo', 'baz') { FileList['bar/**/*'] }


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/installer.rb', line 35

def initialize(src = '.', dest = '.', options = {}, &block)

   self.src   = src
   self.dest  = dest

   self.user  = options[:user]  || 'root'
   self.group = options[:group] || 'root'
   self.mode  = options[:mode]
   self.dmode = options[:dmode]

   @block = block || lambda { FileList['**/*'] }
   
end

Instance Attribute Details

#destObject

Returns the value of attribute dest.



5
6
7
# File 'lib/installer.rb', line 5

def dest
  @dest
end

#dmodeObject

Returns the value of attribute dmode.



7
8
9
# File 'lib/installer.rb', line 7

def dmode
  @dmode
end

#groupObject

Returns the value of attribute group.



6
7
8
# File 'lib/installer.rb', line 6

def group
  @group
end

#modeObject

Returns the value of attribute mode.



7
8
9
# File 'lib/installer.rb', line 7

def mode
  @mode
end

#srcObject

Returns the value of attribute src.



5
6
7
# File 'lib/installer.rb', line 5

def src
  @src
end

#userObject

Returns the value of attribute user.



6
7
8
# File 'lib/installer.rb', line 6

def user
  @user
end

Instance Method Details

#install(prefix) ⇒ Object

Installs the files specified during initialization to

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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/installer.rb', line 50

def install(prefix)
  
  ##############################################
  # Begin sadly misplace bit of code
  #   - We do this section here, since not all files may have been
  #   - generated by the time of initialization
  ##############################################

  files = []
  src   = self.src
  dest  = File.join(prefix, self.dest)

  if File.directory?(src)
    # let the user specify files from within that directory, otherwise
    # assume a complete glob
    FileUtils.chdir(src) { files = @block.call.to_a }
  else
    # src is an individual file
    src, files = File.dirname(self.src), File.basename(self.src)
  end

  # check for special files like device nodes
  specials = files.map {|f| File.join(src, f) }.find_all do |f|
    !File.file?(f) and !File.directory?(f)
  end

  raise ArgumentError, "special files #{specials.join ', '} are not allowed" \
    unless specials.empty?

  ##############################################
  # End sadly misplaced bit of code
  ##############################################
 
  # split into files and dirs, so we can handle directory permissions
  # correctly
  files, dirs = files.partition {|f| File.file?(File.join(src, f)) }

  # make sure the parent directory gets created
  FileUtils.mkdir_p(dest) unless File.directory?(dest)

  # sorting by length is an "easy" way of making sure parent
  # directories are handled first
  dirs.sort_by {|dir| dir.length }.each do |dir|
    FileUtils.mkdir(File.join(dest, dir), :mode => dmode) \
      unless File.directory?(File.join(dest, dir))
    FileUtils.chown(user, group, dest)
  end

  # install the files into their proper place
  files.each do |file|
    FileUtils.install(File.join(src, file), File.join(dest, file), :mode => mode)
    FileUtils.chown(user, group, File.join(dest, file))
  end

end