Class: Kward::Kwshrc
- Inherits:
-
Object
- Object
- Kward::Kwshrc
- Defined in:
- lib/kward/shell/kwshrc.rb
Overview
Parses the declarative subset of a shell startup file used by kwsh.
Only aliases, exports, and source directives are handled. Other shell syntax is intentionally ignored until kwsh supports scripting.
Defined Under Namespace
Classes: ParseError
Constant Summary collapse
- PROMPT_BUILTINS =
%w[alias capture cd pwd export source unset unalias clear exit logout pty].freeze
- VARIABLE_NAME =
/\A[A-Za-z_][A-Za-z0-9_]*\z/- VARIABLE_PATTERN =
/[A-Za-z_][A-Za-z0-9_]*/
Class Method Summary collapse
- .paths(primary = File.join(ConfigPaths.config_dir, "kwshrc")) ⇒ Object
- .read(paths, env: ENV.to_h) ⇒ Object
- .read_file(path, env: ENV.to_h) ⇒ Object
- .resolve_path(path, cwd:, env: ENV.to_h) ⇒ Object
- .valid_alias_name?(name) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(env: ENV.to_h) ⇒ Kwshrc
constructor
A new instance of Kwshrc.
- #read(paths) ⇒ Object
- #read_path(path) ⇒ Object
Constructor Details
#initialize(env: ENV.to_h) ⇒ Kwshrc
Returns a new instance of Kwshrc.
42 43 44 45 46 47 |
# File 'lib/kward/shell/kwshrc.rb', line 42 def initialize(env: ENV.to_h) @environment = env.to_h.transform_keys(&:to_s).transform_values(&:to_s) @exports = {} @aliases = {} @loading = [] end |
Class Method Details
.paths(primary = File.join(ConfigPaths.config_dir, "kwshrc")) ⇒ Object
18 19 20 |
# File 'lib/kward/shell/kwshrc.rb', line 18 def self.paths(primary = File.join(ConfigPaths.config_dir, "kwshrc")) [primary, File.("~/.kwshrc")].uniq end |
.read(paths, env: ENV.to_h) ⇒ Object
26 27 28 |
# File 'lib/kward/shell/kwshrc.rb', line 26 def self.read(paths, env: ENV.to_h) new(env: env).read(paths) end |
.read_file(path, env: ENV.to_h) ⇒ Object
30 31 32 33 |
# File 'lib/kward/shell/kwshrc.rb', line 30 def self.read_file(path, env: ENV.to_h) parser = new(env: env) parser.read_path(path) end |
.resolve_path(path, cwd:, env: ENV.to_h) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/kward/shell/kwshrc.rb', line 35 def self.resolve_path(path, cwd:, env: ENV.to_h) parser = new(env: env) = parser.send(:expand_variables, path.to_s) = .sub(/\A~(?=\/|\z)/, Dir.home) File.(, cwd) end |
.valid_alias_name?(name) ⇒ Boolean
22 23 24 |
# File 'lib/kward/shell/kwshrc.rb', line 22 def self.valid_alias_name?(name) name.to_s.match?(/\A[A-Za-z_][A-Za-z0-9_-]*\z/) && !PROMPT_BUILTINS.include?(name.to_s) end |
Instance Method Details
#read(paths) ⇒ Object
49 50 51 52 |
# File 'lib/kward/shell/kwshrc.rb', line 49 def read(paths) Array(paths).each { |path| read_file(path, required: false) } configuration end |
#read_path(path) ⇒ Object
54 55 56 57 |
# File 'lib/kward/shell/kwshrc.rb', line 54 def read_path(path) read_file(path, required: true) configuration end |