Skip to content
Kward Search API index

Class: Kward::Kwshrc

Inherits:
Object
  • Object
show all
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

VARIABLE_NAME =
/\A[A-Za-z_][A-Za-z0-9_]*\z/
VARIABLE_PATTERN =
/[A-Za-z_][A-Za-z0-9_]*/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV.to_h) ⇒ Kwshrc

Returns a new instance of Kwshrc.



33
34
35
36
37
38
# File 'lib/kward/shell/kwshrc.rb', line 33

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

.read(paths, env: ENV.to_h) ⇒ Object



17
18
19
# File 'lib/kward/shell/kwshrc.rb', line 17

def self.read(paths, env: ENV.to_h)
  new(env: env).read(paths)
end

.read_file(path, env: ENV.to_h) ⇒ Object



21
22
23
24
# File 'lib/kward/shell/kwshrc.rb', line 21

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



26
27
28
29
30
31
# File 'lib/kward/shell/kwshrc.rb', line 26

def self.resolve_path(path, cwd:, env: ENV.to_h)
  parser = new(env: env)
  expanded = parser.send(:expand_variables, path.to_s)
  expanded = expanded.sub(/\A~(?=\/|\z)/, Dir.home)
  File.expand_path(expanded, cwd)
end

Instance Method Details

#read(paths) ⇒ Object



40
41
42
43
# File 'lib/kward/shell/kwshrc.rb', line 40

def read(paths)
  Array(paths).each { |path| read_file(path, required: false) }
  configuration
end

#read_path(path) ⇒ Object



45
46
47
48
# File 'lib/kward/shell/kwshrc.rb', line 45

def read_path(path)
  read_file(path, required: true)
  configuration
end