-
Notifications
You must be signed in to change notification settings - Fork 0
/
_utils.rb
70 lines (61 loc) · 1.97 KB
/
_utils.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def git_commit(message, &block)
yield if block_given?
git :add => "."
git :commit => "--quiet --message '#{message}'"
end
def run_and_commit(command, options={})
commit_message = options[:message] ? "#{options[:message]}\n\n#{command}" : command
git_commit(commit_message) do
run command
end
end
def bundle(command, options = {})
default_options = { :quiet => true, :local => false }
actual_options = default_options.merge(options)
options = actual_options.inject("") { |memo, kv| memo << " --#{kv.first}" if kv.last; memo }
run "bundle #{command} #{options}"
end
# Install a file
#
# Options:
#
# :method => Thor::Action to use to install, e.g. :get, :copy_file or :template
# Default is :get (for http repository) or :copy_file (local repository)
# :source => Use this source file (relative to file_templates/)
# :flavor => Subdir of file_templates to look for source file of same name as dest file (cannot be used with :source)
#
def install_file(path, options = {})
source_path = "#{TEMPLATES_REPOSITORY}/file_templates/" +
if options[:source] ; options[:source]
elsif options[:flavor] ; "#{options[:flavor]}/#{path}"
else path
end
method = options.delete(:method) || (TEMPLATES_REPOSITORY =~ /^https?:/ ? :get : :copy_file)
send method, source_path, path
end
def replace_file(path, options = {})
remove_file path
install_file path, options
end
def replace_file_with(path, string)
File.open(path, "w") { |file| file.write string }
end
class Hash
def deep_merge!(other_hash)
other_hash.keys.each do |key|
if other_hash[key].is_a?(Hash) && self[key].is_a?(Hash)
self[key].deep_merge!(other_hash[key])
else
self[key] = other_hash[key]
end
end
end
end
require 'yaml'
def add_to_locale(hash, locale_file = 'config/locales/en.yml')
en_strings = YAML::load_file(locale_file)
en_strings['en'].deep_merge!(hash)
File.open(locale_file, 'w') do |file|
file.write YAML.dump(en_strings)
end
end