Class: Rockit::Application
- Inherits:
-
Object
- Object
- Rockit::Application
- Defined in:
- lib/rockit/application.rb
Constant Summary collapse
- CONFIG_FILES =
Default configuration file names
['rockitfile', 'Rockitfile', 'rockitfile.rb', 'Rockitfile.rb']
Instance Method Summary collapse
-
#clear_cache ⇒ Object
Remove the cache directory.
-
#command(command, options) ⇒ Object
Determine if the command exists on the current system (uses which).
-
#debug(val = true) ⇒ Object
Turn on debugging.
-
#if_directory_changed(directory, &block) ⇒ Object
If the directly file listing changes, execute the block.
-
#if_file_changed(file, &block) ⇒ Object
If the digest of the file is different from the stored digest, execute the block.
-
#if_first_time(&block) ⇒ Object
First time executed call the block.
-
#if_string_changed(key, new_value, &block) ⇒ Object
Execute the given block if the input is different from the output .
-
#if_string_digest_changed(key, input, &block) ⇒ Object
If the digest of the input is different from the stored key, execute the block.
-
#initialize(store = nil) ⇒ Application
constructor
A new instance of Application.
-
#run ⇒ Object
Run a Rockit configuration file and Rails dependency checks unless turned off by configuration.
-
#service(service_name, options = {}) ⇒ Object
Identify if a service is running on the system (uses ps).
-
#string_keys(hash = {}) ⇒ Object
Pulling from ActiveSupport::CoreExtensions::Hash::Keys to avoid having to include the entire gem.
-
#system_command(command) ⇒ Object
Execute a system command and return the result.
-
#system_exit_on_error(command, options = {}) ⇒ Object
Run system commands and if not successful exit and print out an error message.
Constructor Details
#initialize(store = nil) ⇒ Application
Returns a new instance of Application.
14 15 16 17 |
# File 'lib/rockit/application.rb', line 14 def initialize(store=nil) @hash_store = store || HashStore.new @debug = false end |
Instance Method Details
#clear_cache ⇒ Object
Remove the cache directory
33 34 35 |
# File 'lib/rockit/application.rb', line 33 def clear_cache @hash_store.clear end |
#command(command, options) ⇒ Object
Determine if the command exists on the current system (uses which). If it does not hard exit with a message to stdout.
command - the string of the command to find options - see system_exit_on_error
return only if it finishes successfully
44 45 46 47 48 49 50 |
# File 'lib/rockit/application.rb', line 44 def command(command, ) = { 'print_command' => false, 'failure_message' => "required command '#{command}' is not available." }.merge(string_keys()) system_exit_on_error("which #{command}", ) end |
#debug(val = true) ⇒ Object
Turn on debugging
20 21 22 |
# File 'lib/rockit/application.rb', line 20 def debug(val=true) @debug = val end |
#if_directory_changed(directory, &block) ⇒ Object
If the directly file listing changes, execute the block.
68 69 70 |
# File 'lib/rockit/application.rb', line 68 def if_directory_changed(directory, &block) if_string_digest_changed(directory, Dir.glob("#{directory}/*").join(","), &block) end |
#if_file_changed(file, &block) ⇒ Object
If the digest of the file is different from the stored digest, execute the block.
83 84 85 |
# File 'lib/rockit/application.rb', line 83 def if_file_changed(file, &block) if_string_changed(file, Digest::SHA256.file(file).hexdigest.to_s, &block) end |
#if_first_time(&block) ⇒ Object
First time executed call the block.
73 74 75 |
# File 'lib/rockit/application.rb', line 73 def if_first_time(&block) if_string_changed("first_time", "done", &block) end |
#if_string_changed(key, new_value, &block) ⇒ Object
Execute the given block if the input is different from the output .
key - the key to lookup the stored hash value new_value - the value to compare with the stored hash value block - block to execute if the hash value does not match the stored hash value
return if the block was not executed, false, if it is executed, the return
status of the block
96 97 98 99 100 101 102 |
# File 'lib/rockit/application.rb', line 96 def if_string_changed(key, new_value, &block) if new_value != @hash_store[key] old_value = @hash_store[key] @hash_store[key] = new_value block.call(key, new_value, old_value) if block_given? end end |
#if_string_digest_changed(key, input, &block) ⇒ Object
If the digest of the input is different from the stored key, execute the block.
78 79 80 |
# File 'lib/rockit/application.rb', line 78 def if_string_digest_changed(key, input, &block) if_string_changed(key, Digest::SHA256.new.update(input.to_s).hexdigest.to_s, &block) end |
#run ⇒ Object
Run a Rockit configuration file and Rails dependency checks unless turned off by configuration.
26 27 28 29 30 |
# File 'lib/rockit/application.rb', line 26 def run rockit_file = CONFIG_FILES.select { |f| File.exists?(f) }.first raise ArgumentError "No Rockitfile found (looking for: #{CONFIG_FILES.join(',')})" unless rockit_file Dsl.new(self).instance_eval(File.read(rockit_file), rockit_file) end |
#service(service_name, options = {}) ⇒ Object
Identify if a service is running on the system (uses ps). If it does not hard exit with a message to stdout.
service_name - the name of the service to find in ps options - see system_exit_on_error
return only if it finishes successfully
59 60 61 62 63 64 65 |
# File 'lib/rockit/application.rb', line 59 def service(service_name, ={}) = { 'print_command' => false, 'failure_message' => "required service '#{service_name}' is not running." }.merge(string_keys()) system_exit_on_error("ps ax | grep '#{service_name.gsub(/^(.)/, "[\\1]")}'", ) end |
#string_keys(hash = {}) ⇒ Object
Pulling from ActiveSupport::CoreExtensions::Hash::Keys to avoid having to include the entire gem.
hash - the hash to convert keys to strings
returns a new hash with only string keys
152 153 154 155 156 157 |
# File 'lib/rockit/application.rb', line 152 def string_keys(hash={}) hash.inject({}) do |, (key, value)| [key.to_s] = value end end |
#system_command(command) ⇒ Object
Execute a system command and return the result. Guard against calls to ‘rm -rf`.
command - the system command to execute
returns the result of the command execution
raises exception on calls to ‘rm -rf`
141 142 143 144 |
# File 'lib/rockit/application.rb', line 141 def system_command(command) raise "No I'm not going to delete your hd" if command.strip == "rm -rf" `#{command}` end |
#system_exit_on_error(command, options = {}) ⇒ Object
Run system commands and if not successful exit and print out an error message. Default behavior is to print output of a command when it does not return success.
command - the system command you want to execute options - ‘error_message’ - a message to print when command is not successful
'print_command' - displays the command being run
'failure_callback' - Proc to execute when the command fails. If a callback returns
true then it will avoid
'on_success' - Proc to execute when the command is successful
returns only true, will perform exit() when not successful
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rockit/application.rb', line 117 def system_exit_on_error(command, ={}) = {'print_command' => true}.merge(string_keys()) output command if ['print_command'] || @debug command_output = system_command(command) output command_output if @debug unless last_process.success? result = ['on_failure'].call(command, ) if ['on_failure'].is_a?(Proc) return true if result output ['failure_message'] || command_output return exit(last_process.exitstatus) end ['on_success'].call(command, ) if ['on_success'].is_a?(Proc) true end |