set up Ruby load path for runtime modules; add runtime.rb

This commit is contained in:
Josh Holtrop 2014-07-15 15:26:35 -04:00
parent 71578566fe
commit 5c9cc2223e
2 changed files with 83 additions and 4 deletions

71
runtime/lib/runtime.rb Normal file
View File

@ -0,0 +1,71 @@
module Runtime
class << self
# Return the base paths to the runtime directories.
#
# @return [Array] Current runtime paths.
def paths
@paths ||= default_paths
end
# Find a runtime file.
#
# @param type [Symbol]
# Type of runtime file to find. Options:
# - :shader
# - :font
# @param name [String]
# Name of the runtime file to locate.
#
# @return [String, nil]
# Path to the runtime file, or +nil+ if not found.
def find(type, name)
path = nil
paths.find do |runtime_path|
path = find_in(type, name, runtime_path)
end
path
end
private
# Return the default runtime paths.
#
# @return [Array] Default runtime paths.
def default_paths
[
File.expand_path("~/.jes"),
File.expand_path("../../runtime", $0),
]
end
# Find a runtime file within a single runtime path.
#
# @param type [Symbol]
# Type of runtime file to find. Options:
# - :shader
# - :font
# @param name [String]
# Name of the runtime file to locate.
# @param runtime_path [String]
# The runtime path to look in.
#
# @return [String, nil]
# Path to the runtime file, or +nil+ if not found.
def find_in(type, name, runtime_path)
path =
case type
when :shader
File.join(runtime_path, "shaders", name)
else
raise "Unknown runtime type: #{type.inspect}"
end
if File.exists?(path)
path
else
nil
end
end
end
end

View File

@ -1,6 +1,14 @@
def main
puts "hi from main.rb"
p ARGV
def init_loadpath
$LOAD_PATH.unshift(File.expand_path("../../runtime/lib", $0))
end
main
def load_lib_files
require "runtime"
end
def main(argv)
init_loadpath
load_lib_files
end
main(ARGV)