module Ric::Files

Public Instance Methods

xcopy(from,to,glob_files, opts={}) click to toggle source

# This tries to implement the xcopy for my git programs # similar to xcopy

Originally called 'tra va sa'

   # File lib/ric/files.rb
10 def xcopy(from,to,glob_files, opts={})
11   n_actions = 0
12   puts "+ Travasing: #{yellow from} ==> #{green to}"
13   verbose = opts.fetch :verbose, true
14   dryrun  = opts.fetch :dryrun,  true  # i scared of copying files!
15 
16   unless File.exists?("#{to}/.git")
17     fatal 11,"Sorry cant travase data to an unversioned dir. Please version it with git (or add a .git dir/file to trick me)"
18     exit 1
19   end
20   unless File.exists?("#{to}/.safe_xcopy")
21     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"} . 
22     You are for sure a very smart person but there are a LOT of people out there who could destroy theyr file system! Thanks"
23   end
24 
25   # With this i can understand what has been deleted, with lots of magic from git on both ends.. :)
26   deb "+ First the differences:"
27   deb `diff -qr #{from} #{to} |egrep -v '\\.git' `
28   
29   puts "Dryrun is: #{azure dryrun}"
30 
31   puts "+ Files: #{cyan glob_files}"
32   Dir.chdir(from)
33   Dir.glob(glob_files).each{ |file|
34     from_file = "#{from}/#{file}"
35     to_file   = "#{to}/#{file}"
36     destdir = File.dirname(to_file)
37     deb "Copying: #{yellow from_file}.."
38     #  could need creating the dir..
39     if File.exists?(destdir)
40       # just copy the file
41       command = "cp \"#{from_file}\" \"#{to_file}\""
42     else
43       # mkdir dest AND copy file
44       pred "Hey, Dir '#{destdir}' doesnt exist! Creating it.."
45       command = "mkdir -p \"#{destdir}\" && cp \"#{from_file}\" \"#{to_file}\""
46     end
47 
48     if dryrun
49       puts "[DRYRUN] Skipping #{gray command}"
50     else
51       ret =  `#{command} 2>&1`
52       puts "EXE: #{gray command}" if verbose
53       n_actions += 1
54       print( "[ret=$?]", ret, "\n") if (ret.length > 1 || $? != 0) # if output or not zero
55     end
56   }
57   puts "#{n_actions} commands executed."
58 end