Class: Brown::ACLLoader

Inherits:
Object
  • Object
show all
Extended by:
Logger
Defined in:
lib/brown/acl_loader.rb

Class Method Summary collapse

Methods included from Logger

backtrace, log_level, logger

Class Method Details

.load_all(*dirs) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/brown/acl_loader.rb', line 11

def self.load_all(*dirs)
	dirs.flatten!
	pfiles = dirs.each_with_object([]) do |dir, list|
		list << Dir["#{dir}/*.proto"]
	end.flatten

	load_proto_files(pfiles, dirs)
end

.load_proto_files(pfiles, dirs) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/brown/acl_loader.rb', line 20

def self.load_proto_files(pfiles, dirs)
	orig_load_path = $LOAD_PATH

	Dir.mktmpdir do |tmpdir|
		dirs = pfiles.map { |f| File.dirname(f) }.uniq + [tmpdir]
		dirs.each { |d| $LOAD_PATH.unshift(d) }

		compiles = []

		pfiles.each do |f|
			pbrbfile = f.gsub(/\.proto$/, ".pb.rb")
			unless File.exists?(pbrbfile) and File.stat(pfile).mtime <= File.stat(pbrbfile).mtime
				compiles << f
			end
		end

		includes = dirs.map { |d| "-I '#{d}'" }.join(" ")

		unless compiles.empty?
			cmd = "protoc --ruby_out='#{tmpdir}' #{includes} #{compiles.map { |f| "'#{f}'" }.join(' ')} 2>&1"
			output = nil

			IO.popen(cmd) { |fd| output = fd.read }

			if $?.exitstatus != 0
				logger.fatal { "protoc failed: #{output}" }
				raise RuntimeError, output
			end
		end

		pfiles.each do |f|
			require "#{File.basename(f, '.proto')}.pb"
		end
	end
ensure
	$LOAD_PATH.replace(orig_load_path)
end