tango 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/README.md +3 -3
- data/TODO.md +6 -0
- data/lib/tango.rb +1 -0
- data/lib/tango/contexts/helpers.rb +2 -2
- data/lib/tango/helpers.rb +1 -0
- data/lib/tango/helpers/file_manipulation_helpers.rb +70 -0
- data/lib/tango/runner.rb +3 -1
- data/lib/tango/version.rb +1 -1
- data/spec/helpers/file_manipulation_helpers_spec.rb +171 -0
- metadata +9 -4
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -35,7 +35,7 @@ Example Runner
|
|
35
35
|
|
36
36
|
step :install do |package|
|
37
37
|
met? { installed?(package) }
|
38
|
-
meet { shell("apt-get", "install", "-y", package) }
|
38
|
+
meet { shell!("apt-get", "install", "-y", package) }
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -46,7 +46,7 @@ Example Runner
|
|
46
46
|
|
47
47
|
step :install do |gem|
|
48
48
|
met? { installed?(gem) }
|
49
|
-
meet { shell("gem", "install", gem) }
|
49
|
+
meet { shell!("gem", "install", gem) }
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
@@ -133,7 +133,7 @@ You can use these together like this:
|
|
133
133
|
end
|
134
134
|
end
|
135
135
|
|
136
|
-
### Changing the
|
136
|
+
### Changing the Umask
|
137
137
|
|
138
138
|
step :install do
|
139
139
|
with_umask 0077 do
|
data/TODO.md
ADDED
data/lib/tango.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
require 'tango/helpers/file_manipulation_helpers'
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Tango
|
2
|
+
module Helpers
|
3
|
+
module FileManipulationHelpers
|
4
|
+
|
5
|
+
require 'pathname'
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
# shameless stolen from babushka and ported to work with plain ruby without so much magic
|
10
|
+
def insert_into_file(insert_before, path, lines)
|
11
|
+
#@insert_after = nil
|
12
|
+
|
13
|
+
nlines = lines.split("\n").length
|
14
|
+
before, after = cut(Pathname.new(path).readlines, insert_before)
|
15
|
+
|
16
|
+
write path, [ before, comment_string, lines, after ].join
|
17
|
+
end
|
18
|
+
|
19
|
+
def comment_string
|
20
|
+
comment_char = '#'
|
21
|
+
"#{comment_char} Envato config added #{Time.now}\n"
|
22
|
+
end
|
23
|
+
|
24
|
+
def cut(data, cut_before)
|
25
|
+
index = data.index{ |l| l.strip == cut_before.strip }
|
26
|
+
raise "Couldn't find the spot to cut." if index.nil?
|
27
|
+
[data[0...index], data[index..-1]]
|
28
|
+
end
|
29
|
+
|
30
|
+
def change_line(line_regex, replacement, filename)
|
31
|
+
replaced = false
|
32
|
+
contents = Pathname.new(filename).readlines.collect do |line|
|
33
|
+
if line =~ line_regex
|
34
|
+
replaced = true
|
35
|
+
[comment_string, "#{replacement.chomp}\n"]
|
36
|
+
else
|
37
|
+
line
|
38
|
+
end
|
39
|
+
end.flatten
|
40
|
+
raise "couldnt find the specified line" unless replaced
|
41
|
+
File.open(filename, 'w') do |f|
|
42
|
+
contents.each{|line| f.write line }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def grep(pattern, file)
|
47
|
+
!!(File.read(file) =~ pattern)
|
48
|
+
end
|
49
|
+
|
50
|
+
# for files where attributes are specified:
|
51
|
+
# attribute_name value
|
52
|
+
def change_config_attribute(keyword, from, to, file)
|
53
|
+
# Remove the incorrect setting if it's there
|
54
|
+
contents = File.open(file).reject do |line|
|
55
|
+
line.match(/^#{keyword}\s+#{from}/)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Add the correct setting unless it's already there
|
59
|
+
if contents.none?{|line| line.match /^#{keyword}\s+#{to}/ }
|
60
|
+
contents << "#{keyword} #{to}\n"
|
61
|
+
end
|
62
|
+
|
63
|
+
File.open(file, 'w') do |f|
|
64
|
+
contents.each{|line| f.write line}
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/tango/runner.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'tango/config_files'
|
2
2
|
require 'tango/contexts/helpers'
|
3
|
+
require 'tango/helpers/file_manipulation_helpers'
|
3
4
|
require 'tango/delegate'
|
4
5
|
require 'tango/fetch'
|
5
6
|
require 'tango/met_and_meet'
|
@@ -13,7 +14,8 @@ module Tango
|
|
13
14
|
include Fetch
|
14
15
|
include MetAndMeet
|
15
16
|
include Shell
|
16
|
-
|
17
|
+
include Helpers::FileManipulationHelpers
|
18
|
+
|
17
19
|
def self.step(step_name, &block)
|
18
20
|
define_method(step_name) do |*args|
|
19
21
|
description = step_description(step_name, args)
|
data/lib/tango/version.rb
CHANGED
@@ -0,0 +1,171 @@
|
|
1
|
+
require 'tango'
|
2
|
+
|
3
|
+
module Tango::Helpers
|
4
|
+
describe FileManipulationHelpers do
|
5
|
+
include FileManipulationHelpers
|
6
|
+
include ::Tango::ConfigFiles
|
7
|
+
|
8
|
+
describe "#grep" do
|
9
|
+
before do
|
10
|
+
@filename = '/tmp/file_manipulation_helper_spec.txt'
|
11
|
+
File.open(@filename, 'w') do |f|
|
12
|
+
f.write <<-FILE
|
13
|
+
anna
|
14
|
+
bob
|
15
|
+
curtis
|
16
|
+
donald
|
17
|
+
FILE
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should return true if the pattern matches' do
|
22
|
+
grep(/bob/, @filename).should be_true
|
23
|
+
end
|
24
|
+
it 'should return false if the pattern doesnt match' do
|
25
|
+
grep(/robert/, @filename).should be_false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#change_line" do
|
30
|
+
|
31
|
+
def file_contents
|
32
|
+
File.read(@filename)
|
33
|
+
end
|
34
|
+
|
35
|
+
before do
|
36
|
+
@filename = '/tmp/file_manipulation_helper_spec.txt'
|
37
|
+
File.open(@filename, 'w') do |f|
|
38
|
+
f.write <<-FILE
|
39
|
+
anna
|
40
|
+
bob
|
41
|
+
curtis
|
42
|
+
donald
|
43
|
+
FILE
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should replace the line matching the regex with the specified line' do
|
48
|
+
expect {
|
49
|
+
change_line(/^bob/, "jimbo", @filename)
|
50
|
+
}.to change {
|
51
|
+
#ignore comment
|
52
|
+
file_contents.gsub /#.*\n/, ''
|
53
|
+
}.to(<<-FILE)
|
54
|
+
anna
|
55
|
+
jimbo
|
56
|
+
curtis
|
57
|
+
donald
|
58
|
+
FILE
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "#insert_into_file" do
|
63
|
+
def file_contents
|
64
|
+
File.read(@filename)
|
65
|
+
end
|
66
|
+
|
67
|
+
before do
|
68
|
+
@filename = '/tmp/file_manipulation_helper_spec.txt'
|
69
|
+
File.open(@filename, 'w') do |f|
|
70
|
+
f.write <<-FILE
|
71
|
+
a
|
72
|
+
b
|
73
|
+
c
|
74
|
+
d
|
75
|
+
FILE
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should insert a comment above the inserted line' do
|
80
|
+
insert_into_file('c', @filename, "z\n")
|
81
|
+
file_contents.should =~ /# Envato config added.*\nz\n/
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should insert text before the specified place' do
|
85
|
+
expect {
|
86
|
+
insert_into_file('c', @filename, "z\n")
|
87
|
+
}.to change {
|
88
|
+
#ignore comment
|
89
|
+
file_contents.gsub /#.*\n/, ''
|
90
|
+
}.to(<<-FILE)
|
91
|
+
a
|
92
|
+
b
|
93
|
+
z
|
94
|
+
c
|
95
|
+
d
|
96
|
+
FILE
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe "#cut" do
|
101
|
+
|
102
|
+
let(:data) { %w[a b c d] }
|
103
|
+
|
104
|
+
it "should cut the lines into two" do
|
105
|
+
cut(data, 'c').should == [%w[a b], %w[c d]]
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should ignore whitespace around the string to cut on" do
|
109
|
+
cut(data, ' c ').should == [%w[a b], %w[c d]]
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should cut the lines into two" do
|
113
|
+
data = ['a', 'b', ' c ', 'd']
|
114
|
+
cut(data, 'c').should == [%w[a b], [' c ', 'd']]
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'should raise an error if the cut-point cant be found' do
|
118
|
+
expect {
|
119
|
+
cut(data, 'z')
|
120
|
+
}.to raise_error
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe "#change_config_attribute" do
|
125
|
+
|
126
|
+
def file_contents
|
127
|
+
File.read(@filename)
|
128
|
+
end
|
129
|
+
|
130
|
+
before do
|
131
|
+
@filename = '/tmp/file_manipulation_helper_spec.txt'
|
132
|
+
File.open(@filename, 'w') do |f|
|
133
|
+
f.write <<-FILE
|
134
|
+
AwesomeStuff false
|
135
|
+
NotAwesomeStuff true
|
136
|
+
FILE
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should replace the incorrect setting if it's there" do
|
141
|
+
expect {
|
142
|
+
change_config_attribute('AwesomeStuff', 'false', 'true', @filename)
|
143
|
+
}.to change {
|
144
|
+
file_contents
|
145
|
+
}.to(<<-FILE)
|
146
|
+
NotAwesomeStuff true
|
147
|
+
AwesomeStuff true
|
148
|
+
FILE
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should add the correct setting if it is unconfigured" do
|
152
|
+
expect {
|
153
|
+
change_config_attribute('OtherAwesomeStuff', 'false', 'true', @filename)
|
154
|
+
}.to change {
|
155
|
+
file_contents
|
156
|
+
}.to(<<-FILE)
|
157
|
+
AwesomeStuff false
|
158
|
+
NotAwesomeStuff true
|
159
|
+
OtherAwesomeStuff true
|
160
|
+
FILE
|
161
|
+
end
|
162
|
+
it "should leave the correct setting alone" do
|
163
|
+
expect {
|
164
|
+
change_config_attribute('AwesomeStuff', 'true', 'false', @filename)
|
165
|
+
}.to_not change {
|
166
|
+
file_contents
|
167
|
+
}
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tango
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 5
|
10
|
+
version: 0.1.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Pete Yandell
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-09-20 00:00:00 +10:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- LICENSE
|
50
50
|
- README.md
|
51
51
|
- Rakefile
|
52
|
+
- TODO.md
|
52
53
|
- lib/tango.rb
|
53
54
|
- lib/tango/config_files.rb
|
54
55
|
- lib/tango/contexts.rb
|
@@ -59,6 +60,8 @@ files:
|
|
59
60
|
- lib/tango/contexts/user.rb
|
60
61
|
- lib/tango/delegate.rb
|
61
62
|
- lib/tango/fetch.rb
|
63
|
+
- lib/tango/helpers.rb
|
64
|
+
- lib/tango/helpers/file_manipulation_helpers.rb
|
62
65
|
- lib/tango/logger.rb
|
63
66
|
- lib/tango/met_and_meet.rb
|
64
67
|
- lib/tango/runner.rb
|
@@ -69,6 +72,7 @@ files:
|
|
69
72
|
- spec/contexts/chain_spec.rb
|
70
73
|
- spec/contexts/directory_spec.rb
|
71
74
|
- spec/contexts/umask_spec.rb
|
75
|
+
- spec/helpers/file_manipulation_helpers_spec.rb
|
72
76
|
- spec/logger_spec.rb
|
73
77
|
- spec/met_and_meet_spec.rb
|
74
78
|
- spec/runner_spec.rb
|
@@ -114,6 +118,7 @@ test_files:
|
|
114
118
|
- spec/contexts/chain_spec.rb
|
115
119
|
- spec/contexts/directory_spec.rb
|
116
120
|
- spec/contexts/umask_spec.rb
|
121
|
+
- spec/helpers/file_manipulation_helpers_spec.rb
|
117
122
|
- spec/logger_spec.rb
|
118
123
|
- spec/met_and_meet_spec.rb
|
119
124
|
- spec/runner_spec.rb
|