Created
June 7, 2012 18:20
-
-
Save nixpulvis/2890564 to your computer and use it in GitHub Desktop.
Dynamic content require (for interpreted languages)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# returns an array of files that are required by the given file | |
def get_requirements( file ) | |
requirements = Array.new | |
File.readlines("#{file}.rb").each do |line| | |
regex = /(?<=require.')(.*)(?=';)/ # matches the requirements in a file | |
requirement = line.match(regex) | |
if requirement | |
requirements << requirement.to_s | |
end | |
end | |
return requirements | |
end | |
# requires all the files in the given directory | |
def require_all( dir ) | |
@dir = dir | |
@to_require = Dir.entries @dir | |
@to_require.delete '.' and @to_require.delete '..' # remove unneeded dirs | |
@to_require.map! { |e| e.sub(/.rb$/, '') } | |
# loads file {name from array} and all requirements recursively | |
def bubble_load( root_file ) | |
puts "LOADING: #{root_file}" | |
# load requirments | |
get_requirements("#{@dir}/#{root_file}").each do |file| | |
if @to_require.include? "#{file}" | |
bubble_load "#{file}" | |
end | |
end | |
# load file | |
require_relative "#{@dir}/#{root_file}" | |
@to_require.delete "#{root_file}" | |
end | |
# requires (via bubble) first file in @to_require untill to_require is empty | |
until @to_require.empty? do | |
puts "\033[31m"+"STARTING LOAD @: #{@to_require.first}"+"\033[0m" | |
bubble_load @to_require.first | |
end | |
end | |
require_all 'protogen' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is generally a awful way to go about doing things. However it's kinda cool :P