freyia 0.5.3 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/README.md +41 -1
- data/lib/freyia/automations/create_file.rb +12 -22
- data/lib/freyia/automations/create_link.rb +11 -21
- data/lib/freyia/automations/directory.rb +27 -34
- data/lib/freyia/automations/empty_directory.rb +16 -35
- data/lib/freyia/automations/file_manipulation.rb +136 -185
- data/lib/freyia/automations/inject_into_file.rb +26 -49
- data/lib/freyia/automations.rb +3 -5
- data/lib/freyia/line_editor/basic.rb +1 -1
- data/lib/freyia/line_editor.rb +2 -2
- data/lib/freyia/shell/basic.rb +19 -20
- data/lib/freyia/shell/color.rb +11 -0
- data/lib/freyia/shell/column_printer.rb +1 -1
- data/lib/freyia/shell/lcs_diff.rb +3 -3
- data/lib/freyia/shell/table_printer.rb +39 -15
- data/lib/freyia/shell.rb +2 -2
- data/lib/freyia/version.rb +1 -1
- data/lib/freyia.rb +9 -1
- metadata +2 -2
|
@@ -7,24 +7,19 @@ module Freyia
|
|
|
7
7
|
# Copies the file from the relative source to the relative destination. If
|
|
8
8
|
# the destination is not given it's assumed to be equal to the source.
|
|
9
9
|
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
# :mode => :preserve, to preserve the file mode from the source.
|
|
15
|
-
#
|
|
16
|
-
# ==== Examples
|
|
10
|
+
# @param source [String] the relative path to the source root.
|
|
11
|
+
# @param destination [String] the relative path to the destination root.
|
|
12
|
+
# @param config [Hash] give `verbose: false` to not log the status, and
|
|
13
|
+
# `mode: :preserve`, to preserve the file mode from the source.
|
|
17
14
|
#
|
|
15
|
+
# @example Copy a file to a new destination
|
|
18
16
|
# copy_file "README", "doc/README"
|
|
19
|
-
#
|
|
17
|
+
# @example Copy a file straight from source to destination
|
|
20
18
|
# copy_file "doc/README"
|
|
21
|
-
|
|
22
|
-
def copy_file(source, *args, &block)
|
|
23
|
-
config = args.last.is_a?(Hash) ? args.pop : {}
|
|
24
|
-
destination = args.first || source
|
|
19
|
+
def copy_file(source, destination = source, **config, &block)
|
|
25
20
|
source = File.expand_path(find_in_source_paths(source.to_s))
|
|
26
21
|
|
|
27
|
-
resulting_destination = create_file destination, nil, config do
|
|
22
|
+
resulting_destination = create_file destination, nil, **config do
|
|
28
23
|
content = File.binread(source)
|
|
29
24
|
content = yield(content) if block
|
|
30
25
|
content
|
|
@@ -32,58 +27,46 @@ module Freyia
|
|
|
32
27
|
return unless config[:mode] == :preserve
|
|
33
28
|
|
|
34
29
|
mode = File.stat(source).mode
|
|
35
|
-
chmod(resulting_destination, mode, config)
|
|
30
|
+
chmod(resulting_destination, mode, **config)
|
|
36
31
|
end
|
|
37
32
|
|
|
38
33
|
# Links the file from the relative source to the relative destination. If
|
|
39
34
|
# the destination is not given it's assumed to be equal to the source.
|
|
40
35
|
#
|
|
41
|
-
#
|
|
42
|
-
#
|
|
43
|
-
#
|
|
44
|
-
# config<Hash>:: give :verbose => false to not log the status.
|
|
45
|
-
#
|
|
46
|
-
# ==== Examples
|
|
36
|
+
# @param source [String] the relative path to the source root.
|
|
37
|
+
# @param destination [String] the relative path to the destination root.
|
|
38
|
+
# @param config [Hash] give :verbose => false to not log the status.
|
|
47
39
|
#
|
|
40
|
+
# @example Link a file to a new destination
|
|
48
41
|
# link_file "README", "doc/README"
|
|
49
|
-
#
|
|
42
|
+
# @example Link a file straight from source to destination
|
|
50
43
|
# link_file "doc/README"
|
|
51
|
-
|
|
52
|
-
def link_file(source, *args)
|
|
53
|
-
config = args.last.is_a?(Hash) ? args.pop : {}
|
|
54
|
-
destination = args.first || source
|
|
44
|
+
def link_file(source, destination = source, **config)
|
|
55
45
|
source = File.expand_path(find_in_source_paths(source.to_s))
|
|
56
46
|
|
|
57
|
-
create_link destination, source, config
|
|
47
|
+
create_link destination, source, **config
|
|
58
48
|
end
|
|
59
49
|
|
|
60
50
|
# Gets the content at the given address and places it at the given relative
|
|
61
51
|
# destination. If a block is given instead of destination, the content of
|
|
62
52
|
# the url is yielded and used as location.
|
|
63
53
|
#
|
|
64
|
-
#
|
|
54
|
+
# `get` relies on open-uri, so passing application user input would provide
|
|
65
55
|
# a command injection attack vector.
|
|
66
56
|
#
|
|
67
|
-
#
|
|
68
|
-
#
|
|
69
|
-
#
|
|
70
|
-
#
|
|
71
|
-
#
|
|
72
|
-
#
|
|
73
|
-
# ==== Examples
|
|
74
|
-
#
|
|
57
|
+
# @param source [String] the address of the given content.
|
|
58
|
+
# @param destination [String] the relative path to the destination root.
|
|
59
|
+
# @param config [Hash] give `verbose: false` to not log the status, and
|
|
60
|
+
# `http_headers: <Hash>` to add headers to an http request.
|
|
61
|
+
# @example Create files from remote URLs
|
|
75
62
|
# get "http://gist.github.com/103208", "doc/README"
|
|
76
63
|
#
|
|
77
64
|
# get "http://gist.github.com/103208", "doc/README", :http_headers => {"Content-Type" => "application/json"}
|
|
78
|
-
#
|
|
65
|
+
# @example Use a block to modify the remote content
|
|
79
66
|
# get "http://gist.github.com/103208" do |content|
|
|
80
67
|
# content.split("\n").first
|
|
81
68
|
# end
|
|
82
|
-
|
|
83
|
-
def get(source, *args, &block) # rubocop:todo Metrics
|
|
84
|
-
config = args.last.is_a?(Hash) ? args.pop : {}
|
|
85
|
-
destination = args.first
|
|
86
|
-
|
|
69
|
+
def get(source, destination = nil, **config, &block)
|
|
87
70
|
render = if %r{^https?://}.match?(source)
|
|
88
71
|
require "open-uri"
|
|
89
72
|
URI.send(:open, source, config.fetch(:http_headers, {})) do |input|
|
|
@@ -100,54 +83,67 @@ module Freyia
|
|
|
100
83
|
File.basename(source)
|
|
101
84
|
end
|
|
102
85
|
|
|
103
|
-
create_file destination, render, config
|
|
86
|
+
create_file destination, render, **config
|
|
104
87
|
end
|
|
105
88
|
|
|
106
89
|
# Gets an ERB template at the relative source, executes it and makes a copy
|
|
107
90
|
# at the relative destination. If the destination is not given it's assumed
|
|
108
|
-
# to be equal to the source removing
|
|
109
|
-
#
|
|
110
|
-
# ==== Parameters
|
|
111
|
-
# source<String>:: the relative path to the source root.
|
|
112
|
-
# destination<String>:: the relative path to the destination root.
|
|
113
|
-
# config<Hash>:: give :verbose => false to not log the status.
|
|
91
|
+
# to be equal to the source removing `.tmpl` from the filename.
|
|
114
92
|
#
|
|
115
|
-
#
|
|
93
|
+
# @param source [String] the relative path to the source root.
|
|
94
|
+
# @param destination [String] the relative path to the destination root.
|
|
95
|
+
# @param context [Binding] if you want access to local variables from the caller
|
|
96
|
+
# @param type [Symbol] to use a template type that differs from the default,
|
|
97
|
+
# specify here (`:erb` or `:serbea`)
|
|
98
|
+
# @param config [Hash] give `verbose: false` to not log the status.
|
|
116
99
|
#
|
|
100
|
+
# @example Process `README.tmpl` and save to a new destination
|
|
117
101
|
# template "README", "doc/README"
|
|
118
|
-
#
|
|
102
|
+
# @example Process from source and save to destination
|
|
119
103
|
# template "doc/README"
|
|
120
|
-
#
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
104
|
+
def template(source, destination = nil, context: nil, type: nil, **config, &block) # rubocop:todo Metrics
|
|
105
|
+
destination ||= source.sub(%r{#{TEMPLATE_EXTNAME}$}o, "")
|
|
106
|
+
source = File.expand_path(find_in_source_paths(source.to_s))
|
|
107
|
+
type ||= self.class.template_type
|
|
108
|
+
|
|
109
|
+
create_file destination, nil, **config do
|
|
110
|
+
if type == :serbea
|
|
111
|
+
unless @_included_serbea
|
|
112
|
+
require "serbea"
|
|
113
|
+
singleton_class.include Serbea::Helpers
|
|
114
|
+
@_included_serbea = true
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
variables = if context
|
|
118
|
+
context.local_variables.to_h { [_1, context.local_variable_get(_1)] }
|
|
119
|
+
else
|
|
120
|
+
{}
|
|
121
|
+
end
|
|
122
|
+
tmpl = Tilt::SerbeaTemplate.new(source, strip_front_matter: false) { ::File.binread(source) }
|
|
123
|
+
tmpl.render(self, variables)
|
|
124
|
+
elsif type == :erb
|
|
125
|
+
context ||= instance_eval("binding", __FILE__, __LINE__)
|
|
126
|
+
capturable_erb = CapturableERB.new(
|
|
127
|
+
::File.binread(source), trim_mode: "-", eoutvar: "@output_buffer"
|
|
128
|
+
)
|
|
129
|
+
content = capturable_erb.tap do |erb|
|
|
130
|
+
erb.filename = source
|
|
131
|
+
end.result(context)
|
|
132
|
+
content = yield(content) if block
|
|
133
|
+
content
|
|
134
|
+
end
|
|
136
135
|
end
|
|
137
136
|
end
|
|
138
137
|
|
|
139
138
|
# Changes the mode of the given file or directory.
|
|
140
139
|
#
|
|
141
|
-
#
|
|
142
|
-
#
|
|
143
|
-
#
|
|
144
|
-
# config<Hash>:: give :verbose => false to not log the status.
|
|
145
|
-
#
|
|
146
|
-
# ==== Example
|
|
140
|
+
# @param mode [Integer] the file mode
|
|
141
|
+
# @param path [String] the name of the file to change mode
|
|
142
|
+
# @param config [Hash] give `verbose: false` to not log the status.
|
|
147
143
|
#
|
|
144
|
+
# @example Update mode of the destination file `script/server`
|
|
148
145
|
# chmod "script/server", 0755
|
|
149
|
-
|
|
150
|
-
def chmod(path, mode, config = {})
|
|
146
|
+
def chmod(path, mode, **config)
|
|
151
147
|
path = File.expand_path(path, destination_root)
|
|
152
148
|
say_status :chmod, relative_to_original_destination_root(path), config.fetch(:verbose, true)
|
|
153
149
|
return if options[:pretend]
|
|
@@ -156,151 +152,117 @@ module Freyia
|
|
|
156
152
|
FileUtils.chmod_R(mode, path)
|
|
157
153
|
end
|
|
158
154
|
|
|
159
|
-
# Prepend text to a file.
|
|
155
|
+
# Prepend text to a file.
|
|
160
156
|
#
|
|
161
|
-
#
|
|
162
|
-
#
|
|
163
|
-
#
|
|
164
|
-
# config<Hash>:: give :verbose => false to not log the status.
|
|
165
|
-
#
|
|
166
|
-
# ==== Example
|
|
157
|
+
# @param path [String] path of the file to be changed
|
|
158
|
+
# @param data [String] the data to prepend to the file, can be also given as a block.
|
|
159
|
+
# @param config [Hash] give `verbose: false` to not log the status.
|
|
167
160
|
#
|
|
161
|
+
# @example Prepend destination file with a string
|
|
168
162
|
# prepend_to_file 'config/environments/test.rb', 'config.gem "rspec"'
|
|
169
|
-
#
|
|
163
|
+
# @example Prepend destination file with results of block
|
|
170
164
|
# prepend_to_file 'config/environments/test.rb' do
|
|
171
165
|
# 'config.gem "rspec"'
|
|
172
166
|
# end
|
|
173
|
-
|
|
174
|
-
def prepend_to_file(path, *args, &)
|
|
175
|
-
config = args.last.is_a?(Hash) ? args.pop : {}
|
|
167
|
+
def prepend_to_file(path, *, **config, &)
|
|
176
168
|
config[:after] = %r{\A}
|
|
177
|
-
insert_into_file(path,
|
|
169
|
+
insert_into_file(path, *, **config, &)
|
|
178
170
|
end
|
|
179
171
|
alias_method :prepend_file, :prepend_to_file
|
|
180
172
|
|
|
181
|
-
# Append text to a file.
|
|
173
|
+
# Append text to a file.
|
|
182
174
|
#
|
|
183
|
-
#
|
|
184
|
-
#
|
|
185
|
-
#
|
|
186
|
-
# config<Hash>:: give :verbose => false to not log the status.
|
|
187
|
-
#
|
|
188
|
-
# ==== Example
|
|
175
|
+
# @param path [String] path of the file to be changed
|
|
176
|
+
# @param data [String] the data to append to the file, can be also given as a block.
|
|
177
|
+
# @param config [Hash] give `verbose: false` to not log the status.
|
|
189
178
|
#
|
|
179
|
+
# @example Prepend destination file with a string
|
|
190
180
|
# append_to_file 'config/environments/test.rb', 'config.gem "rspec"'
|
|
191
|
-
#
|
|
181
|
+
# @example Prepend destination file with results of block
|
|
192
182
|
# append_to_file 'config/environments/test.rb' do
|
|
193
183
|
# 'config.gem "rspec"'
|
|
194
184
|
# end
|
|
195
|
-
|
|
196
|
-
def append_to_file(path, *args, &)
|
|
197
|
-
config = args.last.is_a?(Hash) ? args.pop : {}
|
|
185
|
+
def append_to_file(path, *, **config, &)
|
|
198
186
|
config[:before] = %r{\z}
|
|
199
|
-
insert_into_file(path,
|
|
187
|
+
insert_into_file(path, *, **config, &)
|
|
200
188
|
end
|
|
201
189
|
alias_method :append_file, :append_to_file
|
|
202
190
|
|
|
203
|
-
# Injects text right after the class definition.
|
|
204
|
-
# insert_into_file, it's reversible.
|
|
191
|
+
# Injects text right after the class definition.
|
|
205
192
|
#
|
|
206
|
-
#
|
|
207
|
-
#
|
|
208
|
-
#
|
|
209
|
-
#
|
|
210
|
-
# config<Hash>:: give :verbose => false to not log the status.
|
|
211
|
-
#
|
|
212
|
-
# ==== Examples
|
|
193
|
+
# @param path [String] path of the file to be changed
|
|
194
|
+
# @param klass [String|Class] the class to be manipulated
|
|
195
|
+
# @param data [String] the data to append to the class, can be also given as a block.
|
|
196
|
+
# @param config [Hash] give `verbose: false` to not log the status.
|
|
213
197
|
#
|
|
198
|
+
# @example Inject class using provided string
|
|
214
199
|
# inject_into_class "app/controllers/application_controller.rb",
|
|
215
200
|
# "ApplicationController",
|
|
216
201
|
# " filter_parameter :password\n"
|
|
217
202
|
#
|
|
203
|
+
# @example Inject class using result from block
|
|
218
204
|
# inject_into_class "app/controllers/application_controller.rb", "ApplicationController" do
|
|
219
205
|
# " filter_parameter :password\n"
|
|
220
206
|
# end
|
|
221
|
-
|
|
222
|
-
def inject_into_class(path, klass, *args, &)
|
|
223
|
-
config = args.last.is_a?(Hash) ? args.pop : {}
|
|
207
|
+
def inject_into_class(path, klass, *, **config, &)
|
|
224
208
|
config[:after] = %r{class #{klass}\n|class #{klass} .*\n}
|
|
225
|
-
insert_into_file(path,
|
|
209
|
+
insert_into_file(path, *, **config, &)
|
|
226
210
|
end
|
|
227
211
|
|
|
228
|
-
# Injects text right after the module definition.
|
|
229
|
-
# insert_into_file, it's reversible.
|
|
230
|
-
#
|
|
231
|
-
# ==== Parameters
|
|
232
|
-
# path<String>:: path of the file to be changed
|
|
233
|
-
# module_name<String|Class>:: the module to be manipulated
|
|
234
|
-
# data<String>:: the data to append to the class, can be also given as a block.
|
|
235
|
-
# config<Hash>:: give :verbose => false to not log the status.
|
|
212
|
+
# Injects text right after the module definition.
|
|
236
213
|
#
|
|
237
|
-
#
|
|
214
|
+
# @param path [String] path of the file to be changed
|
|
215
|
+
# @param module_name [String|Class] the module to be manipulated
|
|
216
|
+
# @param data [String] the data to append to the class, can be also given as a block.
|
|
217
|
+
# @param config [Hash] give :verbose => false to not log the status.
|
|
238
218
|
#
|
|
219
|
+
# @example Inject module using provided string
|
|
239
220
|
# inject_into_module "app/helpers/application_helper.rb",
|
|
240
221
|
# "ApplicationHelper",
|
|
241
222
|
# " def help; 'help'; end\n"
|
|
242
223
|
#
|
|
224
|
+
# @example Inject module using result from block
|
|
243
225
|
# inject_into_module "app/helpers/application_helper.rb", "ApplicationHelper" do
|
|
244
226
|
# " def help; 'help'; end\n"
|
|
245
227
|
# end
|
|
246
|
-
|
|
247
|
-
def inject_into_module(path, module_name, *args, &)
|
|
248
|
-
config = args.last.is_a?(Hash) ? args.pop : {}
|
|
228
|
+
def inject_into_module(path, module_name, *, **config, &)
|
|
249
229
|
config[:after] = %r{module #{module_name}\n|module #{module_name} .*\n}
|
|
250
|
-
insert_into_file(path,
|
|
230
|
+
insert_into_file(path, *, **config, &)
|
|
251
231
|
end
|
|
252
232
|
|
|
253
233
|
# Run a regular expression replacement on a file, raising an error if the
|
|
254
234
|
# contents of the file are not changed.
|
|
255
235
|
#
|
|
256
|
-
#
|
|
257
|
-
#
|
|
258
|
-
#
|
|
259
|
-
#
|
|
260
|
-
#
|
|
261
|
-
# :force => true, to force the replacement regardless of runner behavior.
|
|
262
|
-
#
|
|
263
|
-
# ==== Example
|
|
236
|
+
# @param path [String] path of the file to be changed
|
|
237
|
+
# @param flag [Regexp|String] the regexp or string to be replaced
|
|
238
|
+
# @param replacement [String] the replacement, can be also given as a block
|
|
239
|
+
# @param config [Hash] give `verbose: false` to not log the status, and
|
|
240
|
+
# `force: true`, to force the replacement regardless of behavior.
|
|
264
241
|
#
|
|
242
|
+
# @example Modify destination file
|
|
265
243
|
# gsub_file! 'app/controllers/application_controller.rb',
|
|
266
244
|
# /#\s*(filter_parameter_logging :password)/, '\1'
|
|
267
|
-
|
|
268
|
-
# gsub_file! 'README', /rake/, :green do |match|
|
|
269
|
-
# match << " no more. Use freyia!"
|
|
270
|
-
# end
|
|
271
|
-
#
|
|
272
|
-
def gsub_file!(path, flag, *args, &)
|
|
273
|
-
config = args.last.is_a?(Hash) ? args.pop : {}
|
|
274
|
-
|
|
245
|
+
def gsub_file!(path, flag, *args, verbose: true, &)
|
|
275
246
|
path = File.expand_path(path, destination_root)
|
|
276
|
-
say_status :gsub, relative_to_original_destination_root(path),
|
|
247
|
+
say_status :gsub, relative_to_original_destination_root(path), verbose
|
|
277
248
|
|
|
278
249
|
actually_gsub_file(path, flag, args, true, &) unless options[:pretend]
|
|
279
250
|
end
|
|
280
251
|
|
|
281
252
|
# Run a regular expression replacement on a file.
|
|
282
253
|
#
|
|
283
|
-
#
|
|
284
|
-
#
|
|
285
|
-
#
|
|
286
|
-
#
|
|
287
|
-
#
|
|
288
|
-
# :force => true, to force the replacement regardless of runner behavior.
|
|
289
|
-
#
|
|
290
|
-
# ==== Example
|
|
254
|
+
# @param path [String] path of the file to be changed
|
|
255
|
+
# @param flag [Regexp|String] the regexp or string to be replaced
|
|
256
|
+
# @param replacement [String] the replacement, can be also given as a block
|
|
257
|
+
# @param config [Hash] give `verbose: false` to not log the status, and
|
|
258
|
+
# `force: true`, to force the replacement regardless of behavior.
|
|
291
259
|
#
|
|
260
|
+
# @example Modify destination file
|
|
292
261
|
# gsub_file 'app/controllers/application_controller.rb',
|
|
293
262
|
# /#\s*(filter_parameter_logging :password)/, '\1'
|
|
294
|
-
|
|
295
|
-
# gsub_file 'README', /rake/, :green do |match|
|
|
296
|
-
# match << " no more. Use freyia!"
|
|
297
|
-
# end
|
|
298
|
-
#
|
|
299
|
-
def gsub_file(path, flag, *args, &)
|
|
300
|
-
config = args.last.is_a?(Hash) ? args.pop : {}
|
|
301
|
-
|
|
263
|
+
def gsub_file(path, flag, *args, verbose: true, &)
|
|
302
264
|
path = File.expand_path(path, destination_root)
|
|
303
|
-
say_status :gsub, relative_to_original_destination_root(path),
|
|
265
|
+
say_status :gsub, relative_to_original_destination_root(path), verbose
|
|
304
266
|
|
|
305
267
|
actually_gsub_file(path, flag, args, false, &) unless options[:pretend]
|
|
306
268
|
end
|
|
@@ -308,57 +270,46 @@ module Freyia
|
|
|
308
270
|
# Uncomment all lines matching a given regex. Preserves indentation before
|
|
309
271
|
# the comment hash and removes the hash and any immediate following space.
|
|
310
272
|
#
|
|
311
|
-
#
|
|
312
|
-
#
|
|
313
|
-
# flag<Regexp|String>:: the regexp or string used to decide which lines to uncomment
|
|
314
|
-
# config<Hash>:: give :verbose => false to not log the status.
|
|
315
|
-
#
|
|
316
|
-
# ==== Example
|
|
273
|
+
# @param path [String] path of the file to be changed
|
|
274
|
+
# @param flag [Regexp|String] the regexp or string used to decide which lines to uncomment
|
|
317
275
|
#
|
|
276
|
+
# @example Uncomment the lines which match the pattern
|
|
318
277
|
# uncomment_lines 'config/initializers/session_store.rb', /active_record/
|
|
319
|
-
|
|
320
|
-
def uncomment_lines(path, flag, *)
|
|
278
|
+
def uncomment_lines(path, flag)
|
|
321
279
|
flag = flag.source if flag.respond_to?(:source)
|
|
322
280
|
|
|
323
|
-
gsub_file(path, %r{^(\s*)#[[:blank:]]?(.*#{flag})}, '\1\2'
|
|
281
|
+
gsub_file(path, %r{^(\s*)#[[:blank:]]?(.*#{flag})}, '\1\2')
|
|
324
282
|
end
|
|
325
283
|
|
|
326
284
|
# Comment all lines matching a given regex. It will leave the space
|
|
327
285
|
# which existed before the beginning of the line in tact and will insert
|
|
328
286
|
# a single space after the comment hash.
|
|
329
287
|
#
|
|
330
|
-
#
|
|
331
|
-
#
|
|
332
|
-
# flag<Regexp|String>:: the regexp or string used to decide which lines to comment
|
|
333
|
-
# config<Hash>:: give :verbose => false to not log the status.
|
|
334
|
-
#
|
|
335
|
-
# ==== Example
|
|
288
|
+
# @param path [String] path of the file to be changed
|
|
289
|
+
# @param flag [Regexp|String] the regexp or string used to decide which lines to comment
|
|
336
290
|
#
|
|
291
|
+
# @example Comment lines which match the pattern
|
|
337
292
|
# comment_lines 'config/initializers/session_store.rb', /cookie_store/
|
|
338
|
-
|
|
339
|
-
def comment_lines(path, flag, *)
|
|
293
|
+
def comment_lines(path, flag)
|
|
340
294
|
flag = flag.source if flag.respond_to?(:source)
|
|
341
295
|
|
|
342
|
-
gsub_file(path, %r{^(\s*)([^#\n]*#{flag})}, '\1# \2'
|
|
296
|
+
gsub_file(path, %r{^(\s*)([^#\n]*#{flag})}, '\1# \2')
|
|
343
297
|
end
|
|
344
298
|
|
|
345
299
|
# Removes a file at the given location.
|
|
346
300
|
#
|
|
347
|
-
#
|
|
348
|
-
#
|
|
349
|
-
# config<Hash>:: give :verbose => false to not log the status.
|
|
350
|
-
#
|
|
351
|
-
# ==== Example
|
|
301
|
+
# @param path [String] path of the file to be changed
|
|
302
|
+
# @param config [Hash] give `verbose: false` to not log the status.
|
|
352
303
|
#
|
|
304
|
+
# @example Removing files
|
|
353
305
|
# remove_file 'README'
|
|
354
306
|
# remove_file 'app/controllers/application_controller.rb'
|
|
355
|
-
|
|
356
|
-
def remove_file(path, config = {})
|
|
307
|
+
def remove_file(path, verbose: true)
|
|
357
308
|
return unless behavior == :invoke
|
|
358
309
|
|
|
359
310
|
path = File.expand_path(path, destination_root)
|
|
360
311
|
|
|
361
|
-
say_status :remove, relative_to_original_destination_root(path),
|
|
312
|
+
say_status :remove, relative_to_original_destination_root(path), verbose
|
|
362
313
|
return unless !options[:pretend] && (File.exist?(path) || File.symlink?(path))
|
|
363
314
|
|
|
364
315
|
require "fileutils"
|
|
@@ -379,7 +330,7 @@ module Freyia
|
|
|
379
330
|
with_output_buffer { yield(*args) }
|
|
380
331
|
end
|
|
381
332
|
|
|
382
|
-
def with_output_buffer(buf = +"")
|
|
333
|
+
def with_output_buffer(buf = +"")
|
|
383
334
|
raise ArgumentError, "Buffer cannot be a frozen object" if buf.frozen?
|
|
384
335
|
|
|
385
336
|
old_buffer = output_buffer
|
|
@@ -10,71 +10,63 @@ module Freyia
|
|
|
10
10
|
}.freeze
|
|
11
11
|
|
|
12
12
|
# Injects the given content into a file, raising an error if the contents of
|
|
13
|
-
# the file are not changed.
|
|
13
|
+
# the file are not changed.
|
|
14
14
|
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
# insert two or more times the same content.
|
|
15
|
+
# @param destination [String] Relative path to the destination root
|
|
16
|
+
# @param data [String] Data to add to the file. Can be given as a block.
|
|
17
|
+
# @param config [Hash] give `verbose: false` to not log the status and the flag
|
|
18
|
+
# for injection (`after:` or `before:`) or `force: true` for
|
|
19
|
+
# inserting the same content multiple times.
|
|
21
20
|
#
|
|
22
|
-
#
|
|
21
|
+
# @example Inserting text after a match
|
|
22
|
+
# insert_into_file! "config/environment.rb", "config.gem :freyia",
|
|
23
|
+
# after: "Rails::Initializer.run do |config|\n"
|
|
23
24
|
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
# insert_into_file "config/environment.rb", after: "Rails::Initializer.run do |config|\n" do
|
|
25
|
+
# @example Inserting text based on input within a block
|
|
26
|
+
# insert_into_file! "config/environment.rb", after: "Rails::Initializer.run do |config|\n" do
|
|
28
27
|
# gems = ask "Which gems would you like to add?"
|
|
29
28
|
# gems.split(" ").map{ |gem| " config.gem :#{gem}" }.join("\n")
|
|
30
29
|
# end
|
|
31
|
-
|
|
32
|
-
def insert_into_file!(destination, *args, &block)
|
|
30
|
+
def insert_into_file!(destination, *args, **config, &block)
|
|
33
31
|
data = block_given? ? block : args.shift
|
|
34
32
|
|
|
35
|
-
config = args.shift || {}
|
|
36
33
|
config[:after] = %r{\z} unless config.key?(:before) || config.key?(:after)
|
|
37
|
-
config = config.merge({ error_on_no_change: true })
|
|
38
34
|
|
|
39
|
-
InjectIntoFile.new(self, destination, data, config).()
|
|
35
|
+
InjectIntoFile.new(self, destination, data, error_on_no_change: true, **config).()
|
|
40
36
|
end
|
|
41
37
|
alias_method :inject_into_file!, :insert_into_file!
|
|
42
38
|
|
|
43
|
-
# Injects the given content into a file.
|
|
44
|
-
# method is reversible.
|
|
45
|
-
#
|
|
46
|
-
# ==== Parameters
|
|
47
|
-
# destination<String>:: Relative path to the destination root
|
|
48
|
-
# data<String>:: Data to add to the file. Can be given as a block.
|
|
49
|
-
# config<Hash>:: give :verbose => false to not log the status and the flag
|
|
50
|
-
# for injection (:after or :before) or :force => true for
|
|
51
|
-
# insert two or more times the same content.
|
|
39
|
+
# Injects the given content into a file.
|
|
52
40
|
#
|
|
53
|
-
#
|
|
41
|
+
# @param destination [String] Relative path to the destination root
|
|
42
|
+
# @param data [String] Data to add to the file. Can be given as a block.
|
|
43
|
+
# @param config [Hash] give `verbose: false` to not log the status and the flag
|
|
44
|
+
# for injection (`after:` or `before:`) or `force: true` for
|
|
45
|
+
# inserting the same content multiple times.
|
|
54
46
|
#
|
|
47
|
+
# @example Inserting text after a match
|
|
55
48
|
# insert_into_file "config/environment.rb", "config.gem :freyia",
|
|
56
49
|
# after: "Rails::Initializer.run do |config|\n"
|
|
57
50
|
#
|
|
51
|
+
# @example Inserting text based on input within a block
|
|
58
52
|
# insert_into_file "config/environment.rb", after: "Rails::Initializer.run do |config|\n" do
|
|
59
53
|
# gems = ask "Which gems would you like to add?"
|
|
60
54
|
# gems.split(" ").map{ |gem| " config.gem :#{gem}" }.join("\n")
|
|
61
55
|
# end
|
|
62
|
-
|
|
63
|
-
def insert_into_file(destination, *args, &block)
|
|
56
|
+
def insert_into_file(destination, *args, **config, &block)
|
|
64
57
|
data = block_given? ? block : args.shift
|
|
65
58
|
|
|
66
|
-
config = args.shift || {}
|
|
67
59
|
config[:after] = %r{\z} unless config.key?(:before) || config.key?(:after)
|
|
68
60
|
|
|
69
|
-
InjectIntoFile.new(self, destination, data, config).()
|
|
61
|
+
InjectIntoFile.new(self, destination, data, **config).()
|
|
70
62
|
end
|
|
71
63
|
alias_method :inject_into_file, :insert_into_file
|
|
72
64
|
|
|
73
|
-
class InjectIntoFile < EmptyDirectory
|
|
65
|
+
class InjectIntoFile < EmptyDirectory
|
|
74
66
|
attr_reader :replacement, :flag, :behavior
|
|
75
67
|
|
|
76
|
-
def initialize(base, destination, data, config)
|
|
77
|
-
super(base, destination,
|
|
68
|
+
def initialize(base, destination, data, **config)
|
|
69
|
+
super(base, destination, verbose: true, **config)
|
|
78
70
|
|
|
79
71
|
@behavior, @flag = if @config.key?(:after)
|
|
80
72
|
[:after, @config.delete(:after)]
|
|
@@ -109,20 +101,6 @@ module Freyia
|
|
|
109
101
|
end
|
|
110
102
|
end
|
|
111
103
|
|
|
112
|
-
def revoke!
|
|
113
|
-
say_status :revoke
|
|
114
|
-
|
|
115
|
-
regexp = if @behavior == :after
|
|
116
|
-
content = '\1\2'
|
|
117
|
-
%r{(#{flag})(.*)(#{Regexp.escape(replacement)})}m
|
|
118
|
-
else
|
|
119
|
-
content = '\2\3'
|
|
120
|
-
%r{(#{Regexp.escape(replacement)})(.*)(#{flag})}m
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
replace!(regexp, content, true)
|
|
124
|
-
end
|
|
125
|
-
|
|
126
104
|
protected
|
|
127
105
|
|
|
128
106
|
def say_status(behavior, warning: nil, color: nil) # rubocop:todo Metrics
|
|
@@ -154,7 +132,6 @@ module Freyia
|
|
|
154
132
|
end
|
|
155
133
|
|
|
156
134
|
# Adds the content to the file.
|
|
157
|
-
#
|
|
158
135
|
def replace!(regexp, string, force)
|
|
159
136
|
return unless force || !replacement_present?
|
|
160
137
|
|
data/lib/freyia/automations.rb
CHANGED
|
@@ -85,8 +85,7 @@ module Freyia
|
|
|
85
85
|
# dir<String>:: the directory to move to.
|
|
86
86
|
# config<Hash>:: give :verbose => true to log and use padding.
|
|
87
87
|
#
|
|
88
|
-
def inside(dir = "",
|
|
89
|
-
verbose = config.fetch(:verbose, false)
|
|
88
|
+
def inside(dir = "", verbose: false, &block) # rubocop:todo Metrics
|
|
90
89
|
pretend = options[:pretend]
|
|
91
90
|
|
|
92
91
|
say_status :inside, dir, verbose
|
|
@@ -133,8 +132,7 @@ module Freyia
|
|
|
133
132
|
#
|
|
134
133
|
# apply "recipes/jquery.rb"
|
|
135
134
|
#
|
|
136
|
-
def apply(path,
|
|
137
|
-
verbose = config.fetch(:verbose, true)
|
|
135
|
+
def apply(path, verbose: true)
|
|
138
136
|
is_uri = path =~ %r{^https?://}
|
|
139
137
|
path = find_in_source_paths(path) unless is_uri
|
|
140
138
|
|
|
@@ -165,7 +163,7 @@ module Freyia
|
|
|
165
163
|
# run('ln -s ~/edge rails')
|
|
166
164
|
# end
|
|
167
165
|
#
|
|
168
|
-
def run(command, config
|
|
166
|
+
def run(command, **config) # rubocop:todo Metrics
|
|
169
167
|
destination = relative_to_original_destination_root(destination_root, remove_dot: false)
|
|
170
168
|
desc = "#{command} from #{destination.inspect}"
|
|
171
169
|
|