Class: Nugrant::Helper::Env
- Inherits:
-
Object
- Object
- Nugrant::Helper::Env
- Defined in:
- lib/nugrant/helper/env.rb
Constant Summary collapse
- @@DEFAULT_SCRIPT_PATH =
"./nugrant2env.sh"
Class Method Summary collapse
- .commands(type, bag, options = {}) ⇒ Object
-
.default_namer(char = "_") ⇒ Object
Returns the default namer, which join segments together using a character and upcase the result.
-
.export_command(key, value, options = {}) ⇒ Object
Returns a string representation of the command that needs to be used on the current platform to export an environment variable.
-
.export_commands(bag, options = {}) ⇒ Object
Generate the list of export commands that must be executed so each bag variables is export to an environment variables.
-
.prefix_namer(prefix, delegate_namer) ⇒ Object
Returns the prefix namer, which add a prefix to segments and delegate its work to another namer.
-
.unset_command(key, value, options = {}) ⇒ Object
Returns a string representation of the command that needs to be used on the current platform to unset an environment variable.
-
.unset_commands(bag, options = {}) ⇒ Object
Generate the list of unset commands that must be executed so each bag variables is unset from the environment variables.
-
.write_commands(bag, options = {}, io = $stdout) ⇒ side-effect
Creates a bash script containing the commands that are required to export or unset a bunch of environment variables taken from the bag.
-
.write_script(bag, options = {}) ⇒ side-effect
Creates a bash script containing the commands that are required to export or unset a bunch of environment variables taken from the bag.
Class Method Details
.commands(type, bag, options = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/nugrant/helper/env.rb', line 51 def self.commands(type, bag, = {}) # TODO: Replace by a map type => function name case when type == :export export_commands(bag, ) when type == :unset unset_commands(bag, ) end end |
.default_namer(char = "_") ⇒ Object
Returns the default namer, which join segments together using a character and upcase the result.
29 30 31 32 33 |
# File 'lib/nugrant/helper/env.rb', line 29 def self.default_namer(char = "_") lambda do |segments| segments.join(char).upcase() end end |
.export_command(key, value, options = {}) ⇒ Object
Returns a string representation of the command that needs to be used on the current platform to export an environment variable.
Options:
* :escape_value (true) => If true, escape the value to export.
143 144 145 146 147 148 149 |
# File 'lib/nugrant/helper/env.rb', line 143 def self.export_command(key, value, = {}) value = value.to_s() value = Shellwords.escape(value) if [:escape_value] == nil || [:escape_value] # TODO: Handle platform differently "export #{key}=#{value}" end |
.export_commands(bag, options = {}) ⇒ Object
Generate the list of export commands that must be executed so each bag variables is export to an environment variables
Options:
* :escape_value (true) => If true, escape the value to export.
* :namer (nil) => A block taking as the full path of
an export variable key and return what
the name the should be exported.
-
:override (true) => If true, an export command will be put
in the list even if it already exist in the ENV array.
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/nugrant/helper/env.rb', line 82 def self.export_commands(bag, = {}) namer = [:namer] || default_namer() override = .fetch(:override, true) commands = [] walk_bag(bag) do |segments, key, value| key = namer.call(segments) commands << export_command(key, value, ) if override or not ENV[key] end commands end |
.prefix_namer(prefix, delegate_namer) ⇒ Object
Returns the prefix namer, which add a prefix to segments and delegate its work to another namer.
45 46 47 48 49 |
# File 'lib/nugrant/helper/env.rb', line 45 def self.prefix_namer(prefix, delegate_namer) lambda do |segments| delegate_namer.call([prefix] + segments) end end |
.unset_command(key, value, options = {}) ⇒ Object
Returns a string representation of the command that needs to be used on the current platform to unset an environment variable.
161 162 163 164 |
# File 'lib/nugrant/helper/env.rb', line 161 def self.unset_command(key, value, = {}) # TODO: Handle platform differently "unset #{key}" end |
.unset_commands(bag, options = {}) ⇒ Object
Generate the list of unset commands that must be executed so each bag variables is unset from the environment variables
Options:
* :namer (nil) => A block taking as the full path of
an export variable key and return what
the name the should be exported.
-
:override (true) => If true, an export command will be put
in the list even if it already exist in the ENV array.
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/nugrant/helper/env.rb', line 115 def self.unset_commands(bag, = {}) namer = [:namer] || default_namer() override = .fetch(:override, true) commands = [] walk_bag(bag) do |segments, key, value| key = namer.call(segments) commands << unset_command(key, value, ) if override or not ENV[key] end commands end |
.write_commands(bag, options = {}, io = $stdout) ⇒ side-effect
Creates a bash script containing the commands that are required to export or unset a bunch of environment variables taken from the bag.
The
Options:
* :type => The type of command, default to :export
* See commands, export_commands and unset_commands for further .
209 210 211 212 213 214 215 |
# File 'lib/nugrant/helper/env.rb', line 209 def self.write_commands(bag, = {}, io = $stdout) commands = commands([:type] || :export, bag, ) commands.each do |command| io.puts(command) end end |
.write_script(bag, options = {}) ⇒ side-effect
Creates a bash script containing the commands that are required to export or unset a bunch of environment variables taken from the bag.
Options:
* :type => The type of command, default to :export
* :script_path => The path where to write the script, defaults to `./nugrant2env.sh`.
* See commands, export_commands and unset_commands for further .
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/nugrant/helper/env.rb', line 182 def self.write_script(bag, = {}) file = File.open(File.([:script_path] || @@DEFAULT_SCRIPT_PATH), "w") file.puts("#!/bin/env sh") file.puts() write_commands(bag, , file) ensure file.close() if file end |