Add PATH manipulation methods - close #126

This commit is contained in:
Josh Holtrop 2021-11-17 21:39:58 -05:00
parent 80d52f25b8
commit f011b23499
7 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,3 @@
#!/bin/sh
echo "foobar!"

View File

@ -0,0 +1,3 @@
#!/bin/sh
exit 42

View File

@ -0,0 +1,3 @@
#!/bin/sh
echo "flex!"

View File

@ -0,0 +1,10 @@
path_prepend "path_prepend"
path_append "path_append"
build do
Environment.new do |env|
system("flex")
system("foobar")
env.Object("simple.o", "simple.c")
end
end

View File

@ -932,6 +932,22 @@ subsidiary build script.
Subsidiary build scripts are executed from within the directory containing the Subsidiary build scripts are executed from within the directory containing the
build script. build script.
###> PATH Management
`rscons` provides methods for management of the `PATH` environment variable.
The `path_append` and `path_prepend` methods can be used to append or prepend
a path to the `PATH` environment variable.
```ruby
path_prepend "i686-elf-gcc/bin"
```
The `path_set` method sets the `PATH` environment variable to the given
Array or String.
The `path_components` method returns an Array of the components in the `PATH`
environment variable.
##> Extending Rscons ##> Extending Rscons
### Adding New Languages ### Adding New Languages

View File

@ -6,6 +6,47 @@ module Rscons
# Global DSL methods. # Global DSL methods.
class GlobalDsl class GlobalDsl
# Return path components from the PATH variable.
#
# @return [Array<String>]
# Path components from the PATH variable.
def path_components
ENV["PATH"].split(File::PATH_SEPARATOR)
end
# Prepend a path component to the PATH variable.
#
# @param path [String]
# Path to prepend.
#
# @return [void]
def path_prepend(path)
path_set([File.expand_path(path)] + path_components)
end
# Append a path component to the PATH variable.
#
# @param path [String]
# Path to append.
#
# @return [void]
def path_append(path)
path_set(path_components + [File.expand_path(path)])
end
# Set the PATH variable.
#
# @param new_path [String, Array<String>]
# New PATH variable value as an array or string.
#
# @return [void]
def path_set(new_path)
if new_path.is_a?(Array)
new_path = new_path.join(File::PATH_SEPARATOR)
end
ENV["PATH"] = new_path
end
# Invoke rscons in a subprocess for a subsidiary Rsconscript file. # Invoke rscons in a subprocess for a subsidiary Rsconscript file.
# #
# @param path [String] # @param path [String]

View File

@ -1475,6 +1475,15 @@ EOF
expect(result.status).to eq 0 expect(result.status).to eq 0
end end
it "allows prepending and appending to PATH" do
test_dir "simple"
result = run_rscons(rsconscript: "pathing.rb")
expect(result.stderr).to eq ""
expect(result.stdout).to match /flex!/
expect(result.stdout).to match /foobar!/
expect(File.exist?("simple.o")).to be_truthy
end
context "debugging" do context "debugging" do
it "prints a message when the target does not exist" do it "prints a message when the target does not exist" do
test_dir("simple") test_dir("simple")