Class: Treebis::Task::RunContext

Inherits:
Object
  • Object
show all
Includes:
Colorize, PathString, Shellopts, Stylize
Defined in:
lib/treebis.rb

Constant Summary

Constants included from Colorize

Colorize::Codes

Constants included from Stylize

Stylize::ReasonStyles, Stylize::StyleCodes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Colorize

colorize

Methods included from PathString

#no_leading_dot_slash

Methods included from Shellopts

#shellopts

Methods included from Stylize

#get_style, #notice

Constructor Details

#initialize(task, block, from_path, on_path, file_utils) ⇒ RunContext

Returns a new instance of RunContext.



610
611
612
613
614
615
616
617
# File 'lib/treebis.rb', line 610

def initialize task, block, from_path, on_path, file_utils
  @task, @block, @from_path, @on_path = task, block, from_path, on_path
  @file_utils = file_utils
  @noop = false # no setters yet
  @opts = {}
  @overwrite = false
  @unindent = true # no setters yet
end

Instance Attribute Details

#file_utilsObject

Returns the value of attribute file_utils.



618
619
620
# File 'lib/treebis.rb', line 618

def file_utils
  @file_utils
end

#optsObject

Returns the value of attribute opts.



618
619
620
# File 'lib/treebis.rb', line 618

def opts
  @opts
end

Class Method Details

.glob_to_regex(glob) ⇒ Object

this is not for general use



815
816
817
818
819
820
821
822
823
# File 'lib/treebis.rb', line 815

def glob_to_regex glob
  fail("this is really strict: (only) 1 '*': #{glob.inspect}") unless
    glob.scan(/\*/).size == 1
  a, b = glob.split('*')
  re = Regexp.new(
    '\\A(.*'+Regexp.escape(a)+')(.*?)('+Regexp.escape(b)+')\\Z'
  )
  re
end

Instance Method Details

#apply(diff_file, opts = {}) ⇒ Object

if this is operating on a whole folder (if the patch filename is called ‘diff’ as opposed to ‘somefile.diff’), depending on how you build the patch you may want the –posix flag



624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/treebis.rb', line 624

def apply diff_file, opts={}
  shell_opts = shellopts(opts)
  patchfile, _ = normalize_from diff_file
  /\A(?:()|(.+)\.)diff\Z/ =~ diff_file or fail("patch needs .diff ext.")
  basename = $1 || $2
  if ! File.exist?(patchfile) && @opts[:patch_hack]
    return patch_hack(diff_file)
  end
  target, _ = normalize_on(basename)
  cmd = nil
  if target == './'
    shell_opts.unshift('-p0') unless shell_opts.grep(/\A-p\d+\Z/).any?
    cmd = ['patch '+Shellwords.shelljoin(shell_opts)+' < '+
      Shellwords.shellescape(patchfile)]
  else
    target_arr = (target == './') ? [] : [target]
    cmd = ['patch', '-u', *shell_opts] + target_arr + [patchfile]
  end
  notice "patching:", cmd.join(' ')
  out, err = Sopen2::sopen2(*cmd)
  if err.length > 0
    cmd_str = cmd.shelljoin
    msg = sprintf(
      "Failed to run patch command: %s\nstdout was: %sstderr was: %s",
      cmd_str, out, err)
    fail msg
  else
    pretty_puts_apply out, cmd
  end
end

#copy(path, tgt = nil) ⇒ Object



664
665
666
667
668
669
670
671
# File 'lib/treebis.rb', line 664

def copy path, tgt=nil
  if path.index('*')
    copy_glob(path)
  else
    full, local = normalize_from path
    copy_go full, local, path, tgt
  end
end

#copy_glob(path) ⇒ Object



672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/treebis.rb', line 672

def copy_glob path
  path.index('*') or fail("not glob: #{path.inspect}")
  full, local = normalize_from path
  these = Dir[full]
  if these.empty?
    notice('copy', "no files matched #{path.inspect}")
  else
    opts = these.map do |this|
      [this, * copy_unglob(this, local, path)]
    end
    opts.each {|a| copy_go(*a) }
  end
end

#erb(path) ⇒ Object



654
655
656
657
658
659
660
661
662
663
# File 'lib/treebis.rb', line 654

def erb path
  require 'erb'
  in_full, in_local = normalize_from path
  out_full, out_local = normalize_on path
  tmpl = ERB.new(File.read(in_full))
  tmpl.filename = in_full # just used when errors
  bnd = @task.erb_values_binding
  out = tmpl.result(bnd)
  write out_local, out
end

#from(path) ⇒ Object



712
713
714
# File 'lib/treebis.rb', line 712

def from path
  @from_path = path
end

#mkdir_p_unless_exists(dir_basename = nil) ⇒ Object Also known as: mkdir_p



715
716
717
718
719
720
721
722
723
724
725
726
# File 'lib/treebis.rb', line 715

def mkdir_p_unless_exists dir_basename=nil
  if dir_basename
    full, short = normalize_on(dir_basename)
  else
    full, short = @on_path, @on_path
  end
  if File.exist?(full)
    report_action :exists, short
  else
    file_utils.mkdir_p(full, :verbose => true, :noop=>@noop)
  end
end

#move(from, to) ⇒ Object



707
708
709
710
711
# File 'lib/treebis.rb', line 707

def move from, to
  fr_full, fr_local = normalize_on from
  to_full, fr_lcoal = normalize_on   to
  file_utils.mv(fr_full, to_full, :verbose=>true, :noop=>@noop)
end

#prefixObject



743
744
745
# File 'lib/treebis.rb', line 743

def prefix
  @file_utils.prefix
end

#remove(path) ⇒ Object



728
729
730
731
# File 'lib/treebis.rb', line 728

def remove path
  full, _ = normalize_on(path)
  file_utils.rm(full, :verbose => true, :noop => @noop)
end

#rm_rf_if_existObject Also known as: rm_rf



732
733
734
735
736
737
# File 'lib/treebis.rb', line 732

def rm_rf_if_exist
  path = @on_path
  if File.exist?(path) && rm_rf_sanity_check(path)
    file_utils.remove_entry_secure(@on_path)
  end
end

#run(opts = {}) ⇒ Object



739
740
741
742
# File 'lib/treebis.rb', line 739

def run opts={}
  @opts = @opts.merge(opts.reject{|k,v| v.nil?}) # not sure about this
  self.instance_eval(&@block)
end

#uiObject



746
747
748
# File 'lib/treebis.rb', line 746

def ui
  @file_utils.ui
end

#write(path, content) ⇒ Object



749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# File 'lib/treebis.rb', line 749

def write path, content
  out_path, short = normalize_on path
  content = Treebis.unindent(content) if @unindent
  action = if File.exist? out_path
    File.read(out_path) == content ? :identical :
    ( @overwrite ? :changed : :"won't overwrite" )
  else
    :wrote
  end
  xtra = nil
  if [:changed, :wrote].include?(action)
    b = fh = nil
    File.open(out_path,'w'){|fh| b = fh.write(content)}
    xtra = "(#{b} bytes)"
  end
  report_action action, short, xtra
end