Module: Ric::Files

Defined in:
lib/ric/files.rb

Instance Method Summary collapse

Instance Method Details

#xcopy(from, to, glob_files, opts = {}) ⇒ Object

# This tries to implement the xcopy for my git programs

# similar to xcopy

Originally called 'tra va sa'


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ric/files.rb', line 10

def xcopy(from,to,glob_files, opts={})
  n_actions = 0
  puts "+ Travasing: #{yellow from} ==> #{green to}"
  verbose = opts.fetch :verbose, true
  dryrun  = opts.fetch :dryrun,  true  # i scared of copying files!

  unless File.exists?("#{to}/.git")
    fatal 11,"Sorry cant travase data to an unversioned dir. Please version it with git (or add a .git dir/file to trick me)"
    exit 1
  end
  unless File.exists?("#{to}/.safe_xcopy")
    fatal 12, "Sorry I refuse to xcopy data unless you explicitly ask me to. You have to do this before:\n  #{yellow "touch #{to}/.safe_xcopy"} . 
    You are for sure a very smart person but there are a LOT of people out there who could destroy theyr file system! Thanks"
  end

  # With this i can understand what has been deleted, with lots of magic from git on both ends.. :)
  deb "+ First the differences:"
  deb `diff -qr #{from} #{to} |egrep -v '\\.git' `
  
  puts "Dryrun is: #{azure dryrun}"

  puts "+ Files: #{cyan glob_files}"
  Dir.chdir(from)
  Dir.glob(glob_files).each{ |file|
    from_file = "#{from}/#{file}"
    to_file   = "#{to}/#{file}"
    destdir = File.dirname(to_file)
    deb "Copying: #{yellow from_file}.."
    #  could need creating the dir..
    if File.exists?(destdir)
      # just copy the file
      command = "cp \"#{from_file}\" \"#{to_file}\""
    else
      # mkdir dest AND copy file
      pred "Hey, Dir '#{destdir}' doesnt exist! Creating it.."
      command = "mkdir -p \"#{destdir}\" && cp \"#{from_file}\" \"#{to_file}\""
    end

    if dryrun
      puts "[DRYRUN] Skipping #{gray command}"
    else
      ret =  `#{command} 2>&1`
      puts "EXE: #{gray command}" if verbose
      n_actions += 1
      print( "[ret=$?]", ret, "\n") if (ret.length > 1 || $? != 0) # if output or not zero
    end
  }
  puts "#{n_actions} commands executed."
end