reorganize Rscons module class methods

This commit is contained in:
Josh Holtrop 2015-02-15 18:33:00 -05:00
parent 7a04bec2ff
commit 34beda844e

View File

@ -19,6 +19,7 @@ require_relative "rscons/builders/simple_builder"
# Namespace module for rscons classes # Namespace module for rscons classes
module Rscons module Rscons
# Names of the default builders which will be added to all newly created # Names of the default builders which will be added to all newly created
# {Environment} objects. # {Environment} objects.
DEFAULT_BUILDERS = [ DEFAULT_BUILDERS = [
@ -37,110 +38,114 @@ module Rscons
# Class to represent a fatal error while building a target. # Class to represent a fatal error while building a target.
class BuildError < RuntimeError; end class BuildError < RuntimeError; end
# Remove all generated files. class << self
#
# @return [void] # Remove all generated files.
def self.clean #
cache = Cache.instance # @return [void]
# remove all built files def clean
cache.targets.each do |target| cache = Cache.instance
FileUtils.rm_f(target) # remove all built files
end cache.targets.each do |target|
# remove all created directories if they are empty FileUtils.rm_f(target)
cache.directories.sort {|a, b| b.size <=> a.size}.each do |directory|
next unless File.directory?(directory)
if (Dir.entries(directory) - ['.', '..']).empty?
Dir.rmdir(directory) rescue nil
end end
end # remove all created directories if they are empty
cache.clear cache.directories.sort {|a, b| b.size <=> a.size}.each do |directory|
end next unless File.directory?(directory)
if (Dir.entries(directory) - ['.', '..']).empty?
# Return whether the given path is an absolute filesystem path. Dir.rmdir(directory) rescue nil
#
# @param path [String] the path to examine.
#
# @return [Boolean] Whether the given path is an absolute filesystem path.
def self.absolute_path?(path)
path =~ %r{^(/|\w:[\\/])}
end
# Return whether the given target is a phony target.
#
# @param target [Symbol, String] Target name.
#
# @return [Boolean] Whether the given target is a phony target.
def self.phony_target?(target)
target.is_a?(Symbol)
end
# Return a new path by changing the suffix in path to suffix.
#
# @param path [String] The path to alter.
# @param suffix [String] The new filename suffix, e.g. ".exe".
#
# @return [String] New path.
def self.set_suffix(path, suffix)
path.sub(/\.[^.]*$/, "") + suffix
end
# Return the system shell and arguments for executing a shell command.
#
# @return [Array<String>] The shell and flag.
def self.get_system_shell
@@shell ||=
begin
test_shell = lambda do |*args|
begin
"success" == IO.popen([*args, "echo success"]) do |io|
io.read.strip
end
rescue
false
end
end end
if ENV["SHELL"] and ENV["SHELL"] != "" and test_shell[ENV["SHELL"], "-c"] end
[ENV["SHELL"], "-c"] cache.clear
elsif Object.const_get("RUBY_PLATFORM") =~ /mingw/ end
if test_shell["sh", "-c"]
# Using Rscons from MSYS should use MSYS's shell. # Return whether the given path is an absolute filesystem path.
["sh", "-c"] #
# @param path [String] the path to examine.
#
# @return [Boolean] Whether the given path is an absolute filesystem path.
def absolute_path?(path)
path =~ %r{^(/|\w:[\\/])}
end
# Return whether the given target is a phony target.
#
# @param target [Symbol, String] Target name.
#
# @return [Boolean] Whether the given target is a phony target.
def phony_target?(target)
target.is_a?(Symbol)
end
# Return a new path by changing the suffix in path to suffix.
#
# @param path [String] The path to alter.
# @param suffix [String] The new filename suffix, e.g. ".exe".
#
# @return [String] New path.
def set_suffix(path, suffix)
path.sub(/\.[^.]*$/, "") + suffix
end
# Return the system shell and arguments for executing a shell command.
#
# @return [Array<String>] The shell and flag.
def get_system_shell
@@shell ||=
begin
test_shell = lambda do |*args|
begin
"success" == IO.popen([*args, "echo success"]) do |io|
io.read.strip
end
rescue
false
end
end
if ENV["SHELL"] and ENV["SHELL"] != "" and test_shell[ENV["SHELL"], "-c"]
[ENV["SHELL"], "-c"]
elsif Object.const_get("RUBY_PLATFORM") =~ /mingw/
if test_shell["sh", "-c"]
# Using Rscons from MSYS should use MSYS's shell.
["sh", "-c"]
else
["cmd", "/c"]
end
else else
["cmd", "/c"] ["sh", "-c"]
end end
else
["sh", "-c"]
end end
end end
end
# Return an Array containing a command used to execute commands. # Return an Array containing a command used to execute commands.
# #
# This will normally be an empty Array, but on Windows if Rscons detects # This will normally be an empty Array, but on Windows if Rscons detects
# that it is running in MSYS then ["env"] will be returned. # that it is running in MSYS then ["env"] will be returned.
# #
# @return [Array<String>] Command used to execute commands. # @return [Array<String>] Command used to execute commands.
def self.command_executer def command_executer
@@command_executer ||= @@command_executer ||=
if Object.const_get("RUBY_PLATFORM") =~ /mingw/ if Object.const_get("RUBY_PLATFORM") =~ /mingw/
if ENV.keys.find {|key| key =~ /MSYS/} if ENV.keys.find {|key| key =~ /MSYS/}
begin begin
if IO.popen(["env", "echo", "success"]) {|io| io.read.strip} == "success" if IO.popen(["env", "echo", "success"]) {|io| io.read.strip} == "success"
["env"] ["env"]
end
rescue
end end
rescue
end end
end end || []
end || [] end
end
# Set the command executer array.
#
# @param val [Array<String>] Command used to execute commands.
#
# @return [Array<String>] Command used to execute commands.
def command_executer=(val)
@@command_executer = val
end
# Set the command executer array.
#
# @param val [Array<String>] Command used to execute commands.
#
# @return [Array<String>] Command used to execute commands.
def self.command_executer=(val)
@@command_executer = val
end end
end end