From 5c9cc2223ed5b8d554d06375cf3c19be87c76d73 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 15 Jul 2014 15:26:35 -0400 Subject: [PATCH] set up Ruby load path for runtime modules; add runtime.rb --- runtime/lib/runtime.rb | 71 ++++++++++++++++++++++++++++++++++++++++++ runtime/main.rb | 16 +++++++--- 2 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 runtime/lib/runtime.rb diff --git a/runtime/lib/runtime.rb b/runtime/lib/runtime.rb new file mode 100644 index 0000000..736d8c1 --- /dev/null +++ b/runtime/lib/runtime.rb @@ -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 diff --git a/runtime/main.rb b/runtime/main.rb index f50f476..ea1dd16 100644 --- a/runtime/main.rb +++ b/runtime/main.rb @@ -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)