Module: Exercise::Instructions
- Includes:
- Commandline, Commandline::Output
- Included in:
- RenderMethods
- Defined in:
- lib/commands/exercise/instructions.rb
Overview
module Instructions - Helper methods to be used within templates
Defined Under Namespace
Classes: EnvironmentVariableMissingError, TimeoutError
Instance Method Summary collapse
-
#after_rendering_run(command) ⇒ String
Runs the given command after the current template has been rendered.
-
#capture(url, filename) ⇒ String
Captures and saves screenshots.
-
#cd(path) ⇒ String
Change directory Change directory so that subsequent commands are executed in the correct context.
-
#command(command, fail_on_error: true) ⇒ String
Execute a command.
-
#command_output(command) ⇒ String
get the output of a command.
-
#env(variable_name) ⇒ String
get value of an Environment variable # @raise [EnvironmentVariableMissingError] if environment variable is undefined.
-
#last_command_output ⇒ String
get the output of the last command that was run.
-
#path(path) ⇒ String
Validate a path.
-
#substitute(hash) ⇒ Object
store strings that should be subsituted in the template on rendering.
-
#wait_until(timeout_after: 5, retry_every: 0.1) ⇒ Object
Wait until the given block evaluates to true.
-
#write_to_file(path, content) ⇒ String
Write the given content to file.
Methods included from Commandline::Output
#error, #ok, #output, #prefix, #say
Methods included from Commandline
#capture_output, #execute, #run
Instance Method Details
#after_rendering_run(command) ⇒ String
Runs the given command after the current template has been rendered. This is useful for running commands to clean clean up. E.g. stopping a server that was previously started.
31 32 33 34 |
# File 'lib/commands/exercise/instructions.rb', line 31 def after_rendering_run(command) after_rendering_commands << command command end |
#capture(url, filename) ⇒ String
Captures and saves screenshots. Useful for adding images to your templates.
44 45 46 47 48 49 50 |
# File 'lib/commands/exercise/instructions.rb', line 44 def capture(url, filename) page_class = Class.new { include PageMagic } session = PageMagic.session(browser: :headless_chrome, url: url) session.visit(page_class, url: url) session.browser.save_screenshot(filename) filename end |
#cd(path) ⇒ String
Change directory Change directory so that subsequent commands are executed in the correct context
59 60 61 62 |
# File 'lib/commands/exercise/instructions.rb', line 59 def cd(path) Dir.chdir(path) "cd #{path}" end |
#command(command, fail_on_error: true) ⇒ String
Execute a command
72 73 74 75 76 77 |
# File 'lib/commands/exercise/instructions.rb', line 72 def command(command, fail_on_error: true) result = test_command(command, fail_on_error: fail_on_error) raise CommandError if result.error? && fail_on_error command end |
#command_output(command) ⇒ String
get the output of a command
85 86 87 88 |
# File 'lib/commands/exercise/instructions.rb', line 85 def command_output(command) command command last_command_output end |
#env(variable_name) ⇒ String
get value of an Environment variable # @raise [EnvironmentVariableMissingError] if environment variable is undefined
97 98 99 |
# File 'lib/commands/exercise/instructions.rb', line 97 def env(variable_name) ENV[variable_name.to_s] || (raise EnvironmentVariableMissingError, variable_name) end |
#last_command_output ⇒ String
get the output of the last command that was run.
106 107 108 |
# File 'lib/commands/exercise/instructions.rb', line 106 def last_command_output Output.new(@result.stdout) end |
#path(path) ⇒ String
Validate a path
116 117 118 119 120 |
# File 'lib/commands/exercise/instructions.rb', line 116 def path(path) raise "#{path} does not exist" unless File.exist?(path) path end |
#substitute(hash) ⇒ Object
store strings that should be subsituted in the template on rendering
127 128 129 |
# File 'lib/commands/exercise/instructions.rb', line 127 def substitute(hash) @substitutes = hash end |
#wait_until(timeout_after: 5, retry_every: 0.1) ⇒ Object
Wait until the given block evaluates to true
142 143 144 145 146 147 148 149 150 |
# File 'lib/commands/exercise/instructions.rb', line 142 def wait_until(timeout_after: 5, retry_every: 0.1) start_time = Time.now until Time.now > start_time + timeout_after return true if yield == true sleep retry_every end raise TimeoutError, 'Action took to long' end |
#write_to_file(path, content) ⇒ String
Write the given content to file
161 162 163 164 165 166 167 |
# File 'lib/commands/exercise/instructions.rb', line 161 def write_to_file(path, content) directory = File.dirname(path) FileUtils.mkdir_p(directory) File.write(path, content) after_rendering_run("rm -rf #{path}") path end |