Merge pull request #4141 from udzura/add-test-for-attr-nil-guard
[mruby.git] / lib / mruby / core_ext.rb
blob1ad528c2637a58fc646ed2599cf094022cf77993
1 autoload :Pathname, 'pathname'
3 class Object
4   class << self
5     def attr_block(*syms)
6       syms.flatten.each do |sym|
7         class_eval "def #{sym}(&block);block.call(@#{sym}) if block_given?;@#{sym};end"
8       end
9     end
10   end
11 end
13 class String
14   def relative_path_from(dir)
15     Pathname.new(File.expand_path(self)).relative_path_from(Pathname.new(File.expand_path(dir))).to_s
16   end
18   def relative_path
19     relative_path_from(Dir.pwd)
20   end
22   def remove_leading_parents
23     Pathname.new(".#{Pathname.new("/#{self}").cleanpath}").cleanpath.to_s
24   end
25 end
27 def install_D(src, dst)
28   _pp "INSTALL", src.relative_path, dst.relative_path
29   rm_f dst
30   mkdir_p File.dirname(dst)
31   cp src, dst
32 end
34 def _pp(cmd, src, tgt=nil, indent: nil)
35   return if Rake.application.options.silent
37   width = 5
38   template = indent ? "%#{width * indent}s %s %s" : "%-#{width}s %s %s"
39   puts template % [cmd, src, tgt ? "-> #{tgt}" : nil]
40 end