Top Level Namespace

Defined Under Namespace

Classes: Environment

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.method_missing(m, *args, &block) ⇒ Object

still could absolutely be more cleaned up, but it works



171
172
173
174
175
176
177
178
# File 'lib/rash.rb', line 171

def self.method_missing(m, *args, &block) 
  exe = which(m.to_s)
  if exe || ($env.alias?(m) && !$env.aliasing_disabled)
    $env.dispatch(m, *args)
  else
    super
  end
end

Instance Method Details

#as_background(&block) ⇒ Object



17
18
19
# File 'lib/rash/jobcontrol.rb', line 17

def as_background(&block)
  $env.async(&block)
end

#cd(dir = nil) ⇒ Object

note for later documentation: any aliases of cd must be functions, not environmental aliases. Limitation of implementation.



114
115
116
117
118
119
120
121
# File 'lib/rash.rb', line 114

def cd(dir = nil)
  d = dir
  case d
  when File, Dir
    d = d.path if File.directory?(d.path)
  end
  $env.chdir(d)
end

#in_pipeline(&block) ⇒ Object



106
107
108
# File 'lib/rash/pipeline.rb', line 106

def in_pipeline(&block) 
  $env.make_pipeline(&block)
end

#run(file, *args) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/rash.rb', line 124

def run(file, *args)
  filename = file.to_s
  exe = (filename.start_with?("/") ? filename : File.expand_path(filename.strip))
  unless File.executable?(exe)
    raise SystemCallError.new("No such executable file - #{exe}", Errno::ENOENT::Errno)
  end
  $env.dispatch(exe, *args, literal: true)
  # system(exe, *args.flatten.map{|a| a.to_s}, {out: $stdout, err: $stderr, in: $stdin, umask: $env.umask})
end

#sourcesh(file) ⇒ Object

Defines ‘bash` psuedo-compatibility. Filesystem effects happen like normal and environmental variable changes are copied

This is an artifact of an old design and is deprecated until further notice.



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rash.rb', line 140

def sourcesh(file) 
  bash_env = lambda do |cmd = nil|
    tmpenv = `#{cmd + ';' if cmd} printenv`
    tmpenv.split("\n").grep(/[a-zA-Z0-9_]+=.*/).map {|l| l.split("=")}
  end
  bash_source = lambda do |f|
    Hash[bash_env.call("source #{File.realpath f}") - bash_env.()]
  end

  bash_source.call(file).each {|k,v| ENV[k] = v if k != "SHLVL" && k != "_"}
end

#which(command) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rash.rb', line 153

def which(command)
  cmd = File.expand_path(command)
  return cmd if File.executable?(cmd) && !File.directory?(cmd)
  
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |pt|
    path = File.expand_path(pt)
    exts.each do |ext|
      exe = File.join(path, "#{command}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  nil
end

#with_all_out_as(file) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rash/redirection.rb', line 142

def with_all_out_as(file)
  $env.stdout = file
  $env.stderr = $stdout
  if block_given?
    begin
      yield $stdout
    ensure
      $env.reset_stdout
      $env.reset_stderr
    end
  end
end

#with_stderr_as(file = STDERR) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/rash/redirection.rb', line 98

def with_stderr_as(file = STDERR)
  $env.stderr = file
  if block_given?
    begin
      yield $stderr
    ensure
      $env.reset_stderr
    end
  end 
end

#with_stderr_as_stdoutObject



131
132
133
134
135
136
137
138
139
140
# File 'lib/rash/redirection.rb', line 131

def with_stderr_as_stdout
  $env.stderr = $stdout
  if block_given?
    begin
      yield $stderr
    ensure
      $env.reset_stderr
    end
  end
end

#with_stdin_as(file = STDIN) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/rash/redirection.rb', line 109

def with_stdin_as(file = STDIN)
  $env.stdin = file
  if block_given?
    begin
      yield $stdin
    ensure
      $env.reset_stdin
    end
  end
end

#with_stdout_as(file = STDOUT) ⇒ Object

If you want to append, you need to get the file object yourself. Check if not flushing immediately is a concern. If so, set $stdout.sync for files



87
88
89
90
91
92
93
94
95
96
# File 'lib/rash/redirection.rb', line 87

def with_stdout_as(file = STDOUT)
  $env.stdout = file
  if block_given?
    begin
      yield $stdout
    ensure
      $env.reset_stdout
    end
  end
end

#with_stdout_as_stderrObject



120
121
122
123
124
125
126
127
128
129
# File 'lib/rash/redirection.rb', line 120

def with_stdout_as_stderr
  $env.stdout = $stderr
  if block_given?
    begin
      yield $stdout
    ensure
      $env.reset_stdout
    end
  end
end