Class: Installer
- Inherits:
-
Object
- Object
- Installer
- Defined in:
- lib/installer.rb
Instance Attribute Summary collapse
-
#dest ⇒ Object
Returns the value of attribute dest.
-
#dmode ⇒ Object
Returns the value of attribute dmode.
-
#group ⇒ Object
Returns the value of attribute group.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#src ⇒ Object
Returns the value of attribute src.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(src = '.', dest = '.', options = {}, &block) ⇒ Installer
constructor
Initializes the installer.
-
#install(prefix) ⇒ Object
Installs the files specified during initialization to.
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 = '.', = {}, &block) self.src = src self.dest = dest self.user = [:user] || 'root' self.group = [:group] || 'root' self.mode = [:mode] self.dmode = [:dmode] @block = block || lambda { FileList['**/*'] } end |
Instance Attribute Details
#dest ⇒ Object
Returns the value of attribute dest.
5 6 7 |
# File 'lib/installer.rb', line 5 def dest @dest end |
#dmode ⇒ Object
Returns the value of attribute dmode.
7 8 9 |
# File 'lib/installer.rb', line 7 def dmode @dmode end |
#group ⇒ Object
Returns the value of attribute group.
6 7 8 |
# File 'lib/installer.rb', line 6 def group @group end |
#mode ⇒ Object
Returns the value of attribute mode.
7 8 9 |
# File 'lib/installer.rb', line 7 def mode @mode end |
#src ⇒ Object
Returns the value of attribute src.
5 6 7 |
# File 'lib/installer.rb', line 5 def src @src end |
#user ⇒ Object
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
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 |