Dahistory 0.4.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +6 -4
- data/lib/Dahistory.rb +36 -22
- data/lib/Dahistory/version.rb +1 -1
- data/spec/tests/Dashistory.rb +24 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -13,7 +13,7 @@ are overwriting a file you have encountered before.
|
|
13
13
|
Installation
|
14
14
|
------------
|
15
15
|
|
16
|
-
gem
|
16
|
+
gem install Dahistory
|
17
17
|
|
18
18
|
Usage
|
19
19
|
------
|
@@ -24,7 +24,7 @@ Usage
|
|
24
24
|
|
25
25
|
Dahistory "some/file.txt"
|
26
26
|
|
27
|
-
# Checks your directory (default "./history").
|
27
|
+
# Checks your history directory (default "./history").
|
28
28
|
# If not found there, saves copy of file in ./pending dir and
|
29
29
|
# raises Dahistory::Pending_File, "pending/HOSTNAME,path,some,file.txt.TIMESTAMP"
|
30
30
|
#
|
@@ -32,12 +32,14 @@ Usage
|
|
32
32
|
# move the file from the pending directory to your source/history dir, and
|
33
33
|
# run your recipe/code again.
|
34
34
|
|
35
|
-
|
35
|
+
You can also specify different source directories using :dirs.
|
36
|
+
Below is how you use :dirs and other settings:
|
36
37
|
|
37
38
|
Dahistory { |o|
|
38
39
|
|
39
40
|
o.file "file/name.txt"
|
40
|
-
o.dirs "dir1", "dir2"
|
41
|
+
o.dirs "dir1", "dir2" # -> default: []
|
42
|
+
o.history "./history"
|
41
43
|
o.pending_dir "./pending"
|
42
44
|
o.backup_file "#{`hostname`}.name.txt.#{Time.now.to_i}"
|
43
45
|
o.on_raise_pending {
|
data/lib/Dahistory.rb
CHANGED
@@ -21,7 +21,8 @@ class Dahistory
|
|
21
21
|
@git = false
|
22
22
|
@file = nil
|
23
23
|
file!(file_path) if file_path
|
24
|
-
dirs
|
24
|
+
dirs []
|
25
|
+
history './history'
|
25
26
|
pending_dir './pending'
|
26
27
|
@on_raise_pending = nil
|
27
28
|
yield(self) if block_given?
|
@@ -33,8 +34,12 @@ class Dahistory
|
|
33
34
|
@file = File.expand_path(path)
|
34
35
|
end
|
35
36
|
|
36
|
-
def dirs *
|
37
|
-
@dirs =
|
37
|
+
def dirs *raw
|
38
|
+
@dirs = raw.flatten.map { |dir| File.expand_path dir }
|
39
|
+
end
|
40
|
+
|
41
|
+
def history raw
|
42
|
+
@history = File.expand_path raw
|
38
43
|
end
|
39
44
|
|
40
45
|
def pending_dir dir
|
@@ -77,6 +82,10 @@ class Dahistory
|
|
77
82
|
|
78
83
|
}
|
79
84
|
|
85
|
+
def backup_basename
|
86
|
+
File.basename( backup_file )
|
87
|
+
end
|
88
|
+
|
80
89
|
def backup_file str = :RETURN
|
81
90
|
if str == :RETURN
|
82
91
|
@backup_file ||= "#{`hostname`.strip}-#{file.gsub('/',',')}.#{Time.now.utc.strftime "%Y.%m.%d.%H.%M.%S"}"
|
@@ -99,29 +108,34 @@ class Dahistory
|
|
99
108
|
content = File.read(file)
|
100
109
|
standard = content.gsub("\r", '')
|
101
110
|
|
102
|
-
|
111
|
+
in_dirs = self.class.find_file_copy file, dirs
|
112
|
+
in_history = self.class.find_file_copy file, history
|
103
113
|
in_pending = self.class.find_file_copy file, pending_dir
|
104
114
|
|
105
|
-
if
|
106
|
-
|
107
|
-
if !in_pending
|
108
|
-
|
109
|
-
File.write(backup_file, content)
|
115
|
+
return true if in_history
|
110
116
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
end
|
115
|
-
|
116
|
-
if @git.is_a?(String)
|
117
|
-
Exit_Zero %! git push #{@git} !
|
118
|
-
end
|
119
|
-
|
120
|
-
end # === if !in_pending
|
121
|
-
|
122
|
-
on_raise_pending.call if on_raise_pending
|
123
|
-
raise Pending, backup_file
|
117
|
+
if in_dirs
|
118
|
+
File.write(File.join(history, backup_basename), content)
|
119
|
+
return true
|
124
120
|
end
|
121
|
+
|
122
|
+
if !in_pending
|
123
|
+
|
124
|
+
File.write(backup_file, content)
|
125
|
+
|
126
|
+
if @git
|
127
|
+
Exit_Zero "git add #{backup_file}"
|
128
|
+
Exit_Zero %! git commit -m "Backup: #{backup_file}"!
|
129
|
+
end
|
130
|
+
|
131
|
+
if @git.is_a?(String)
|
132
|
+
Exit_Zero %! git push #{@git} !
|
133
|
+
end
|
134
|
+
|
135
|
+
end # === if !in_pending
|
136
|
+
|
137
|
+
on_raise_pending.call if on_raise_pending
|
138
|
+
raise Pending, backup_file
|
125
139
|
|
126
140
|
end # === def
|
127
141
|
|
data/lib/Dahistory/version.rb
CHANGED
data/spec/tests/Dashistory.rb
CHANGED
@@ -14,6 +14,30 @@ describe "Dahistory" do
|
|
14
14
|
|
15
15
|
end # === Dahistory
|
16
16
|
|
17
|
+
describe "Dahistory: source files" do
|
18
|
+
|
19
|
+
before { reset_dirs }
|
20
|
+
|
21
|
+
it "copies target file from source dir to history dir" do
|
22
|
+
file = "files/a.txt"
|
23
|
+
target = rand(1000).to_s
|
24
|
+
chdir {
|
25
|
+
|
26
|
+
File.write(file, target)
|
27
|
+
Dahistory { |o|
|
28
|
+
o.file file
|
29
|
+
o.dirs "./files"
|
30
|
+
}
|
31
|
+
|
32
|
+
name = `ls history`.strip.split.first
|
33
|
+
File.read(File.join "history", name).should == target
|
34
|
+
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
end # === Dahistory: source files
|
39
|
+
|
40
|
+
|
17
41
|
describe "Dahistory: pending file" do
|
18
42
|
|
19
43
|
before { reset_dirs }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Dahistory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bacon
|