-
Notifications
You must be signed in to change notification settings - Fork 0
/
versions.rb
48 lines (36 loc) · 1.15 KB
/
versions.rb
1
2
3
4
5
6
7
8
9
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
#!/usr/bin/env ruby
require 'fileutils'
#require 'pry'
DAY_IN_SECS = 24.0 * 60.0 * 60.0
TWO_HOURS_IN_SECS = 2.0 * 60.0 * 60.0
INTERVAL_IN_SECS = 5.0 * 60.0
def versions
files = Dir.glob("./**/*.{doc,docx,xls,xlsx,ods,odt,csv,txt,rtf,psd,rb}")
files.each do |filepath|
# skip backup files
next if filepath.include?('Versions')
# skip file if not modified recently
next if Time.now - File.mtime(filepath) > INTERVAL_IN_SECS
# split filepath into name and path
name = File.basename(filepath)
path = filepath[2..filepath.size-name.size-2]
# skip this file
next if name == 'versions' || name == 'versions.rb'
# make sure /Versions directory exists
versions_dir = "./#{path}/Versions".gsub('//', '/')
Dir.mkdir(versions_dir) unless Dir.exists?(versions_dir)
# create versioned filename
prefix = Time.now.strftime('%Y.%m.%d %H.%M')
version_path = "#{versions_dir[2..versions_dir.size-1]}/#{prefix}-#{name}"
# copy file
FileUtils.cp(filepath, "./#{version_path}")
# tell the world
puts version_path
end
end
# main loop
puts "Versions started..."
loop do
versions
sleep(INTERVAL_IN_SECS)
end