Module: Peony::Utils
- Defined in:
- lib/peony/utils.rb
Instance Method Summary collapse
-
#echo_cmd(str) ⇒ Object
### echo_cmd Converts a bash command to a command that echoes before execution.
-
#erb(file, b = binding) ⇒ Object
### erb Evaluates an ERB block in the current scope and returns a string.
-
#error(str) ⇒ Object
### error Internal: Prints to stdout.
- #find_templates(name, file_only = true) ⇒ Object
-
#invoke(task, options = {}) ⇒ Object
### invoke Invokes another Rake task.
-
#measure(&blk) ⇒ Object
### measure Measures the time (in seconds) a block takes.
-
#method_missing(meth, *args, &blk) ⇒ Object
### method_missing Hook to get settings.
- #mkdir_p(*dirs) ⇒ Object
-
#report_time(&blk) ⇒ Object
### report_time Report time elapsed in the block.
- #run(cmd) ⇒ Object
- #search_paths ⇒ Object
-
#set(key, *args, &block) ⇒ Object
### set Sets settings.
-
#set_default(key, *args, &block) ⇒ Object
### set_default Sets default settings.
-
#settings ⇒ Object
### settings Accesses the settings hash.
- #sudo(cmd, &block) ⇒ Object
- #template(from, to, override = false) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object
### method_missing Hook to get settings. See #settings for an explanation.
Returns things.
178 179 180 181 182 183 184 |
# File 'lib/peony/utils.rb', line 178 def method_missing(meth, *args, &blk) if settings.include? meth settings.send meth, *args, &blk else super end end |
Instance Method Details
#echo_cmd(str) ⇒ Object
### echo_cmd Converts a bash command to a command that echoes before execution. Used to show commands in verbose mode. This does nothing unless verbose mode is on.
Returns a string of the compound bash command, typically in the format of ‘echo xx && xx`. However, if `verbose_mode?` is false, it returns the input string unharmed.
echo_cmd("ln -nfs releases/2 current")
#=> echo "$ ln -nfs releases/2 current" && ln -nfs releases/2 current
113 114 115 116 117 118 119 120 |
# File 'lib/peony/utils.rb', line 113 def echo_cmd(str) if verbose_mode? require 'shellwords' "echo #{Shellwords.escape("$ " + str)} &&\n#{str}" else str end end |
#erb(file, b = binding) ⇒ Object
### erb Evaluates an ERB block in the current scope and returns a string.
a = 1
b = 2
# Assuming foo.erb is <%= a %> and <%= b %>
puts erb('foo.erb')
#=> "1 and 2"
Returns the output string of the ERB template.
65 66 67 68 |
# File 'lib/peony/utils.rb', line 65 def erb(file, b=binding) require 'erb' ERB.new(File.read(file), nil, "-").result(b) end |
#error(str) ⇒ Object
### error Internal: Prints to stdout. Consider using ‘print_error` instead.
99 100 101 |
# File 'lib/peony/utils.rb', line 99 def error(str) $stderr.write "#{str}\n" end |
#find_templates(name, file_only = true) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/peony/utils.rb', line 165 def find_templates(name, file_only=true) templates = [] search_paths.each do|path| templates += Dir[File.(name, path)].reject{|filename| file_only && File.directory?(filename) } end templates end |
#invoke(task, options = {}) ⇒ Object
### invoke Invokes another Rake task.
Invokes the task given in ‘task`. Returns nothing.
invoke :'git:clone'
invoke :restart
Options:
reenable (bool) - Execute the task even next time.
38 39 40 41 |
# File 'lib/peony/utils.rb', line 38 def invoke(task, = {}) Rake.application.invoke_task task Rake::Task[task].reenable if [:reenable] end |
#measure(&blk) ⇒ Object
### measure Measures the time (in seconds) a block takes. Returns a [time, output] tuple.
90 91 92 93 94 |
# File 'lib/peony/utils.rb', line 90 def measure(&blk) t = Time.now output = yield [Time.now - t, output] end |
#mkdir_p(*dirs) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/peony/utils.rb', line 18 def mkdir_p(*dirs) dirs.each do|dir| if !FileTest.exists?(dir) FileUtils.mkdir_p(dir) end fail "#{dir} must be a directory!" unless FileTest.directory?(dir) end end |
#report_time(&blk) ⇒ Object
### report_time Report time elapsed in the block. Returns the output of the block.
report_time do
sleep 2
# do other things
end
# Output:
# Elapsed time: 2.00 seconds
81 82 83 84 85 |
# File 'lib/peony/utils.rb', line 81 def report_time(&blk) time, output = measure &blk print_str "Elapsed time: %.2f seconds" % [time] output end |
#run(cmd) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/peony/utils.rb', line 8 def run(cmd) sh cmd do|res, stat| if block_given? yield res, stat else puts stat.inspect unless res end end end |
#search_paths ⇒ Object
161 162 163 |
# File 'lib/peony/utils.rb', line 161 def search_paths ["#{Dir.pwd}/templates", File.("../../templates", __dir__)] end |
#set(key, *args, &block) ⇒ Object
### set Sets settings. Sets given symbol ‘key` to value in `value`.
Returns the value.
set :domain, 'kickflip.me'
129 130 131 |
# File 'lib/peony/utils.rb', line 129 def set(key, *args, &block) settings.send :"#{key}=", *args, block end |
#set_default(key, *args, &block) ⇒ Object
### set_default Sets default settings. Sets given symbol ‘key` to value in `value` only if the key isn’t set yet.
Returns the value.
set_default :term_mode, :pretty
set :term_mode, :system
settings.term_mode.should == :system
set :term_mode, :system
set_default :term_mode, :pretty
settings.term_mode.should == :system
146 147 148 |
# File 'lib/peony/utils.rb', line 146 def set_default(key, *args, &block) set(key, *args, block) unless settings.send(:"#{key}?") end |
#settings ⇒ Object
### settings Accesses the settings hash.
set :domain, 'kickflip.me'
settings.domain #=> 'kickflip.me'
domain #=> 'kickflip.me'
157 158 159 |
# File 'lib/peony/utils.rb', line 157 def settings @settings ||= Settings.new end |
#sudo(cmd, &block) ⇒ Object
4 5 6 |
# File 'lib/peony/utils.rb', line 4 def sudo(cmd, &block) run "sudo #{cmd}", &block end |
#template(from, to, override = false) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/peony/utils.rb', line 43 def template(from, to, override=false) template = find_templates(from).first raise "Can't find tempalte #{from} in directory #{search_paths}." unless template raise "File #{to} have already exists." if !override && File.exists?(to) puts "copy #{template} to #{to}" open(to, "w+") do|out| out.write(erb(template)) end end |