Class: GitPusshuTen::Hooks
- Inherits:
-
Object
- Object
- GitPusshuTen::Hooks
- Defined in:
- lib/gitpusshuten/hooks.rb
Instance Attribute Summary collapse
-
#commands_to_run ⇒ Object
Contains an array of commands to run for the currently parsed hook This gets reset to [] every time a new hook is being parsed.
-
#configuration ⇒ Object
Contains the configuration object.
-
#environment ⇒ Object
Contains the environment on the remote server name which is extracted from the selected configuration in the configuration file.
-
#to_perform ⇒ Object
Contains an array of GitPusshuTen::Hook objects for the current environment.
Instance Method Summary collapse
-
#initialize(environment, configuration) ⇒ Hooks
constructor
Initializes a new Hooks object Provide the environment (e.g. :staging, :production) to parse.
-
#parse!(hooks_file) ⇒ Object
Parses the configuration file and loads all the configuration values into the GitPusshuTen::Configuration instance.
-
#parse_modules! ⇒ Object
Parses any modules that are set in the configuration (config.rb) file.
-
#perform_on(*environments, &configuration) ⇒ Object
Perform On Helper method used to configure the hooks.rb file.
-
#post(name, &commands) ⇒ Object
Post A method for setting post-hooks inside the perform_on block Resets the “commands_to_run” variable to an empty array so that there’s a clean array to work with the next set of commands.
-
#post_hooks ⇒ Object
Post Hooks Returns an array of post-hooks.
-
#pre(name, &commands) ⇒ Object
Pre A method for setting pre-hooks inside the perform_on block Resets the “commands_to_run” variable to an empty array so that there’s a clean array to work with the next set of commands.
-
#pre_hooks ⇒ Object
Pre Hooks Returns an array of pre-hooks.
-
#render_commands(hooks) ⇒ Object
Takes an array of hooks and renders them a Hash that contains the name of the hook, as well as all the commands bundled in a single string, separated by semi-colons.
-
#run(command) ⇒ Object
Run A method for setting commands on a post-hook or pre-hook inside the perform_on block.
Constructor Details
#initialize(environment, configuration) ⇒ Hooks
Initializes a new Hooks object Provide the environment (e.g. :staging, :production) to parse
25 26 27 28 29 30 |
# File 'lib/gitpusshuten/hooks.rb', line 25 def initialize(environment, configuration) @environment = environment @configuration = configuration @to_perform = [] @commands_to_run = [] end |
Instance Attribute Details
#commands_to_run ⇒ Object
Contains an array of commands to run for the currently parsed hook This gets reset to [] every time a new hook is being parsed
20 21 22 |
# File 'lib/gitpusshuten/hooks.rb', line 20 def commands_to_run @commands_to_run end |
#configuration ⇒ Object
Contains the configuration object
11 12 13 |
# File 'lib/gitpusshuten/hooks.rb', line 11 def configuration @configuration end |
#environment ⇒ Object
Contains the environment on the remote server name which is extracted from the selected configuration in the configuration file
7 8 9 |
# File 'lib/gitpusshuten/hooks.rb', line 7 def environment @environment end |
#to_perform ⇒ Object
Contains an array of GitPusshuTen::Hook objects for the current environment
15 16 17 |
# File 'lib/gitpusshuten/hooks.rb', line 15 def to_perform @to_perform end |
Instance Method Details
#parse!(hooks_file) ⇒ Object
Parses the configuration file and loads all the configuration values into the GitPusshuTen::Configuration instance
35 36 37 38 39 40 41 42 |
# File 'lib/gitpusshuten/hooks.rb', line 35 def parse!(hooks_file) if File.exist?(hooks_file) instance_eval(File.read(hooks_file)) else GitPusshuTen::Log.warning "Could not locate the hooks.rb file." end self end |
#parse_modules! ⇒ Object
Parses any modules that are set in the configuration (config.rb) file
46 47 48 49 50 51 52 53 54 |
# File 'lib/gitpusshuten/hooks.rb', line 46 def parse_modules! configuration.additional_modules.each do |additional_module| module_file = File.join(File.dirname(__FILE__), 'modules', additional_module.to_s, 'hooks.rb') if File.exist?(module_file) instance_eval(File.read(module_file)) end end self end |
#perform_on(*environments, &configuration) ⇒ Object
Perform On Helper method used to configure the hooks.rb file
59 60 61 62 63 |
# File 'lib/gitpusshuten/hooks.rb', line 59 def perform_on(*environments, &configuration) if environments.flatten.include?(environment) configuration.call end end |
#post(name, &commands) ⇒ Object
Post A method for setting post-hooks inside the perform_on block Resets the “commands_to_run” variable to an empty array so that there’s a clean array to work with the next set of commands. The “commands.call” invokes all the “run(<command>)” the user provided in the hooks.rb configuration file and extracts the strings of commands to run. This array is then passed into a newly made Hook object which is again stored into the “to_perform” array.
93 94 95 96 97 98 99 100 101 |
# File 'lib/gitpusshuten/hooks.rb', line 93 def post(name, &commands) @commands_to_run = [] commands.call @to_perform << Hook.new({ :type => :post, :name => name, :commands => commands_to_run }) end |
#post_hooks ⇒ Object
Post Hooks Returns an array of post-hooks
124 125 126 127 128 129 |
# File 'lib/gitpusshuten/hooks.rb', line 124 def post_hooks @to_perform.map do |hook| next unless hook.type.eql? :post hook end.compact end |
#pre(name, &commands) ⇒ Object
Pre A method for setting pre-hooks inside the perform_on block Resets the “commands_to_run” variable to an empty array so that there’s a clean array to work with the next set of commands. The “commands.call” invokes all the “run(<command>)” the user provided in the hooks.rb configuration file and extracts the strings of commands to run. This array is then passed into a newly made Hook object which is again stored into the “to_perform” array.
74 75 76 77 78 79 80 81 82 |
# File 'lib/gitpusshuten/hooks.rb', line 74 def pre(name, &commands) @commands_to_run = [] commands.call @to_perform << Hook.new({ :type => :pre, :name => name, :commands => commands_to_run }) end |
#pre_hooks ⇒ Object
Pre Hooks Returns an array of pre-hooks
114 115 116 117 118 119 |
# File 'lib/gitpusshuten/hooks.rb', line 114 def pre_hooks @to_perform.map do |hook| next unless hook.type.eql? :pre hook end.compact end |
#render_commands(hooks) ⇒ Object
Takes an array of hooks and renders them a Hash that contains the name of the hook, as well as all the commands bundled in a single string, separated by semi-colons.
Note: Using a hack to avoid “Hash” sorting issues between Ruby versions which cause the hooks to invoke in the incorrect order. This has been addressed by prefixing the Hash’s “key” with the index of the array and sorting based on that. Then the d+)s gets #sub’d for friendly user output
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/gitpusshuten/hooks.rb', line 140 def render_commands(hooks) hooks_hash = {} hooks.each_with_index do |hook, index| hooks_hash["#{index}) #{hook.name}"] = '' hook.commands.each do |command| hooks_hash["#{index}) #{hook.name}"] += "#{command};".gsub(/;{2,}/, ';') end end hooks_hash end |
#run(command) ⇒ Object
Run A method for setting commands on a post-hook or pre-hook inside the perform_on block
107 108 109 |
# File 'lib/gitpusshuten/hooks.rb', line 107 def run(command) @commands_to_run << command end |