Class: Thrifty::ThriftFile
- Inherits:
-
Object
- Object
- Thrifty::ThriftFile
- Includes:
- Chalk::Log
- Defined in:
- lib/thrifty/thrift_file.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#relative_path ⇒ Object
readonly
Returns the value of attribute relative_path.
Instance Method Summary collapse
- #build_directory ⇒ Object
- #build_root ⇒ Object
- #compile_once ⇒ Object
- #defines?(generated_file) ⇒ Boolean
-
#initialize(manager, relative_path, options = {}) ⇒ ThriftFile
constructor
A new instance of ThriftFile.
- #path ⇒ Object
- #require(generated_file) ⇒ Object
- #validate_existence ⇒ Object
- #version_file ⇒ Object
Constructor Details
#initialize(manager, relative_path, options = {}) ⇒ ThriftFile
Returns a new instance of ThriftFile.
10 11 12 13 14 |
# File 'lib/thrifty/thrift_file.rb', line 10 def initialize(manager, relative_path, ={}) @manager = manager @relative_path = relative_path @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/thrifty/thrift_file.rb', line 8 def @options end |
#relative_path ⇒ Object (readonly)
Returns the value of attribute relative_path.
8 9 10 |
# File 'lib/thrifty/thrift_file.rb', line 8 def relative_path @relative_path end |
Instance Method Details
#build_directory ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/thrifty/thrift_file.rb', line 61 def build_directory # Use the basename for informational purposes, and the SHA1 for # avoid collisions. base = File.basename(path) sha1 = Digest::SHA1.hexdigest(path) File.join(build_root, "#{base}-#{sha1}") end |
#build_root ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/thrifty/thrift_file.rb', line 69 def build_root if build_root = @options[:build_root] (build_root) elsif @options[:relative_to] File.join(File.dirname(path), 'thrifty') else @manager.build_root # Don't expand the manager's build root end end |
#compile_once ⇒ Object
49 50 51 52 53 54 |
# File 'lib/thrifty/thrift_file.rb', line 49 def compile_once with_cache do FileUtils.mkdir_p(build_directory) Rubysh('thrift', '--gen', 'rb', '-out', build_directory, path).check_call end end |
#defines?(generated_file) ⇒ Boolean
24 25 26 27 |
# File 'lib/thrifty/thrift_file.rb', line 24 def defines?(generated_file) compile_once File.exists?(File.join(build_directory, generated_file + '.rb')) end |
#path ⇒ Object
20 21 22 |
# File 'lib/thrifty/thrift_file.rb', line 20 def path (@relative_path) end |
#require(generated_file) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/thrifty/thrift_file.rb', line 29 def require(generated_file) compile_once $:.unshift(build_directory) begin log.info('Requiring', file: generated_file, idl: path) # Global require super(generated_file) ensure # Not sure what to do if someone changed $: in the # meanwhile. Could happen due a signal handler or something # crazy like that. if $:.first == build_directory $:.shift else log.error('Unexpected first element in load path; not removing', build_directory: build_directory, load_path: $:.inspect) end end end |
#validate_existence ⇒ Object
16 17 18 |
# File 'lib/thrifty/thrift_file.rb', line 16 def validate_existence raise "No such Thrift file #{path.inspect}" unless File.exists?(path) end |
#version_file ⇒ Object
57 58 59 |
# File 'lib/thrifty/thrift_file.rb', line 57 def version_file File.join(build_directory, 'VERSION') end |