Module: Dir2svn

Defined in:
lib/dir2svn.rb,
lib/dir2svn/version.rb

Defined Under Namespace

Classes: Sync

Constant Summary collapse

ERR1 =
'Provide the source and target path before doing a sync'
ERR2 =
'Source folder does not exist'
ERR3 =
'Target needs to be in a Subversion managed folder'
VERSION =
'0.1.6'

Class Method Summary collapse

Class Method Details

.find_files(path, exclude, do_puts) ⇒ Object



133
134
135
# File 'lib/dir2svn.rb', line 133

def find_files(path, exclude, do_puts)
  Dir2svn.find_paths(path, exclude, do_puts)[:files]
end

.find_folders(path, exclude, do_puts) ⇒ Object



138
139
140
# File 'lib/dir2svn.rb', line 138

def find_folders(path, exclude, do_puts)
  Dir2svn.find_paths(path, exclude, do_puts)[:folders]
end

.find_paths(path, exclude, _do_puts) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/dir2svn.rb', line 143

def find_paths(path, exclude, _do_puts)
  fls = []
  fldrs = []
  Find.find(path) do |p|
    if FileTest.directory?(p)
      if exclude.include?(File.basename(p))
        Find.prune # Don't look any further into this directory.
      else
        fldrs << p
        next
      end
    else # ignore .folder files
      fls << p unless exclude.include?(File.basename(p))
    end
  end
  { files: fls, folders: fldrs }
end

.sync_file_paths(src, dest, exclude, do_puts) ⇒ Object

See SvnRepository#sync_paths



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dir2svn.rb', line 63

def sync_file_paths(src, dest, exclude, do_puts)
  fls_dest = Dir2svn.find_files(dest, exclude, do_puts).collect { |p| p.sub(dest, '') }
  fls_src = Dir2svn.find_files(src, exclude, do_puts).collect { |p| p.sub(src, '') }
  fls_d = fls_dest - fls_src # deleted: in DEST but not (anymore) in SRC
  fls_a = fls_src - fls_dest # added: in SRC but not (yet) in SRC
  fls_f = fls_src - fls_a # found: in SRC and DEST: everything in SRC that we didn't add
  fls_u = [] # updated
  fls_e = [] # equal

  # Update files
  fls_f.collect do |p| # update files
    p1 = File.expand_path(p, src) # new version
    p0 = File.expand_path(p, dest) # existing version
    if File.identical?(p1, p0)
      fls_e << p
    else
      FileUtils.cp(p1, p0)
      fls_u << p
    end
  end

  # Add files
  fls_a.each do |p|
    p1 = File.expand_path(p, src) # new version
    p0 = File.expand_path(p, dest)
    unless File.exist?(File.dirname(p0)) # create parent dir of new file unless it exists
      # self.makedir_parents(File.dirname(p0)) unless File.exists?(File.dirname(p0))
      cmd = "svn mkdir --parents \"#{File.dirname(p0)}\""
      out = `#{cmd}`
      puts "#{cmd}\n#{out}" if do_puts
    end
    FileUtils.cp(p1, p0)
    cmd = "svn add \"#{p0}\""
    out = `#{cmd}`
    puts "#{cmd}\n#{out}" if do_puts
  end

  # delete files
  fls_d.each do |p|
    p0 = File.expand_path(p, dest)
    cmd = "svn rm \"#{p0}\""
    out = `#{cmd}`
    puts "#{cmd}\n#{out}" if do_puts
  end
  { deleted: fls_d, added: fls_a, updated: fls_u, equal: fls_e }
end

.sync_folder_paths(src, dest, exclude, do_puts) ⇒ Object

See SvnRepository#sync_paths



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dir2svn.rb', line 112

def sync_folder_paths(src, dest, exclude, do_puts)
  fldrs_src = Dir2svn.find_folders(src, exclude, do_puts).collect { |p| p.sub(src, '') }
  fldrs_dest = Dir2svn.find_folders(dest, exclude, do_puts).collect { |p| p.sub(dest, '') }
  fldrs_d = (fldrs_dest - fldrs_src) # deleted folders: see deleted files
  fldrs_a = (fldrs_src - fldrs_dest) # add folders: see added files
  fldrs_d.each do |p| # deleted folders
    p0 = File.expand_path(p, dest)
    next unless File.exist?(p0)
    cmd = "svn rm \"#{p0}\""
    out = `#{cmd}`
    puts "#{cmd}\n#{out}" if do_puts
  end
  fldrs_a.each do |p| # added folders (must be empty, not very useful but..)
    p0 = File.expand_path(p, dest)
    cmd = "svn mkdir --parents \"#{p0}\""
    out = `#{cmd}`
    puts "#{cmd}\n#{out}" if do_puts
  end
end

.sync_paths(src, dest, exclude, do_puts) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/dir2svn.rb', line 50

def sync_paths(src, dest, exclude, do_puts)
  src = File.expand_path(src) + '/'
  dest = File.expand_path(dest) + '/'
  results = nil
  # First files, then folders,
  # as the processing of files can also sync folders
  results = Dir2svn.sync_file_paths(src, dest, exclude, do_puts)
  Dir2svn.sync_folder_paths(src, dest, exclude, do_puts)
  results
end