Module: Rant::Sys

Extended by:
Sys
Includes:
FileUtils::Verbose
Included in:
Sys, SysObject
Defined in:
lib/rant/init.rb,
lib/rant/rantsys.rb,
lib/rant/import/sys/tgz.rb,
lib/rant/import/sys/zip.rb,
lib/rant/import/sys/more.rb

Overview

module Env

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

Returns the value of attribute symlink_supported.



163
164
165
# File 'lib/rant/rantsys.rb', line 163

def symlink_supported
  @symlink_supported
end

Instance Method Details

#absolute_path?(path) ⇒ Boolean

how many drive letters are really allowed on windows?

Returns:

  • (Boolean)


260
261
262
# File 'lib/rant/rantsys.rb', line 260

def absolute_path?(path)
    path =~ %r{\A([a-zA-Z]+:)?(/|\\)}
end

#cd(dir, &block) ⇒ Object

Returns the value of block if a block is given, a true value otherwise.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/rant/rantsys.rb', line 212

def cd(dir, &block)
    fu_output_message "cd #{dir}"
    orig_pwd = Dir.pwd
    Dir.chdir dir
    if block
        begin
            block.arity == 0 ? block.call : block.call(Dir.pwd)
        ensure
            fu_output_message "cd -"
            Dir.chdir orig_pwd
        end
    else
        self
    end
end

#clean(entry) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/rant/import/sys/more.rb', line 22

def clean(entry)
    if test ?f, entry
        rm_f entry
    elsif test ?e, entry
        rm_rf entry
    end
end

#escape(arg) ⇒ Object

Escape special shell characters (currently only spaces). Flattens arrays and returns always a single string.



161
162
163
164
165
166
167
# File 'lib/rant/init.rb', line 161

def escape(arg)
    if arg.respond_to? :to_ary
        arg.to_ary.map{ |e| escape e }.join(' ')
    else
        _escaped arg
    end
end

#ln_f(src, dest) ⇒ Object



245
246
247
# File 'lib/rant/rantsys.rb', line 245

def ln_f(src, dest)
    ln(src, dest, :force => true)
end

#regular_filename(fn) ⇒ Object



178
179
180
# File 'lib/rant/init.rb', line 178

def regular_filename(fn)
    fn.to_str.tr("\\", "/").gsub(%r{/{2,}}, "/")
end

#root_dir?(path) ⇒ Boolean

Returns:

  • (Boolean)


254
255
256
257
258
259
# File 'lib/rant/rantsys.rb', line 254

def root_dir?(path)
    path == "/" || path == "\\" ||
        path =~ %r{\A[a-zA-Z]+:(\\|/)\Z}
    # how many drive letters are really allowed on
    # windows?
end

#ruby(*args, &block) ⇒ Object

Run a new Ruby interpreter with the given arguments:

sys.ruby "install.rb"


200
201
202
203
204
205
206
207
208
209
# File 'lib/rant/rantsys.rb', line 200

def ruby(*args, &block)
           if args.empty?
               # The empty string argument ensures that +system+
               # doesn't start a subshell but invokes ruby directly.
               # The empty string argument is ignored by ruby.
               sh(Env::RUBY_EXE, '', &block)
           else
               sh(args.unshift(Env::RUBY_EXE), &block)
           end
end

#safe_ln(src, dest) ⇒ Object

If supported, make a hardlink, otherwise fall back to copying.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/rant/rantsys.rb', line 230

def safe_ln(src, dest)
           dest = dest.to_str
           src = src.respond_to?(:to_ary) ? src.to_ary : src.to_str
    unless Sys.symlink_supported
	cp(src, dest)
    else
	begin
	    ln(src, dest)
	rescue Exception # SystemCallError # Errno::EOPNOTSUPP
	    Sys.symlink_supported = false
	    cp(src, dest)
	end
    end
end

#sh(*cmd_args, &block) ⇒ Object

Run an external command. When given one argument, this is subject to shell interpretation. Otherwise the first argument is the program to run, following arguments are given as arguments to the program.

Note: This method is called on sys <some_string> invocation in an Rantfile.



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rant/rantsys.rb', line 186

def sh(*cmd_args, &block)
    cmd_args.flatten!
    cmd = cmd_args.join(" ")
    fu_output_message cmd
           success = system(*cmd_args)
    if block_given?
               block[$?]
           elsif !success
	raise CommandError.new(cmd, $?)
    end
end

#sp(arg) ⇒ Object

Returns a string that can be used as a valid path argument on the shell respecting portability issues.



152
153
154
155
156
157
158
# File 'lib/rant/init.rb', line 152

def sp(arg)
           if arg.respond_to? :to_ary
               arg.to_ary.map{ |e| sp e }.join(' ')
           else
               _escaped_path arg
           end
end

#split_all(path) ⇒ Object

Split a path in all elements.



193
194
195
196
197
# File 'lib/rant/init.rb', line 193

def split_all(path)
           names = regular_filename(path).split(%r{/})
           names[0] = "/" if names[0] && names[0].empty?
           names
end

#split_path(str) ⇒ Object



249
250
251
# File 'lib/rant/rantsys.rb', line 249

def split_path(str)
    str.split(Env.on_windows? ? ";" : ":")
end

#unpack_tgz(archive, opts = {}) ⇒ Object

Unpack the gzipped tar archive, to which the archive path points. Use the :in => "some/dir" option to specify a output directory. It defaults to the working directory.



13
14
15
16
17
18
19
20
21
22
# File 'lib/rant/import/sys/tgz.rb', line 13

def unpack_tgz(archive, opts={})
    output_dir = opts[:to] || opts[:in] || "."
    mkpath output_dir unless test ?d, output_dir
    if Env.have_tar?
        sh "tar", "-xzf", archive, "-C", output_dir
    else
        minitar_unpack(archive, output_dir)
    end
    nil
end

#unpack_zip(archive, opts = {}) ⇒ Object

Unpack the zip archive, to which the archive path points. Use the :in => "some/dir" option to specify a output directory. It defaults to the working directory.



13
14
15
16
17
18
19
20
21
22
# File 'lib/rant/import/sys/zip.rb', line 13

def unpack_zip(archive, opts={})
    output_dir = opts[:to] || opts[:in] || "."
    mkpath output_dir unless test ?d, output_dir
    if Env.find_bin("unzip")
        sh "unzip", "-q", archive, "-d", output_dir
    else
        rubyzip_unpack(archive, output_dir)
    end
    nil
end

#write_to_binfile(fn, content) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/rant/import/sys/more.rb', line 15

def write_to_binfile(fn, content)
    content = content.to_str
    fu_output_message "writing #{content.size} bytes to file `#{fn}'"
    File.open fn, "wb" do |f|
        f.write content
    end
end

#write_to_file(fn, content) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/rant/import/sys/more.rb', line 8

def write_to_file(fn, content)
    content = content.to_str
    fu_output_message "writing #{content.size} bytes to file `#{fn}'"
    File.open fn, "w" do |f|
        f.write content
    end
end