Class: Thrifty::ThriftFile

Inherits:
Object
  • Object
show all
Includes:
Chalk::Log
Defined in:
lib/thrifty/thrift_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  @manager = manager
  @relative_path = relative_path
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/thrifty/thrift_file.rb', line 8

def options
  @options
end

#relative_pathObject (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_directoryObject



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_rootObject



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]
    expand_relative_path(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_onceObject



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

Returns:

  • (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

#pathObject



20
21
22
# File 'lib/thrifty/thrift_file.rb', line 20

def path
  expand_relative_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_existenceObject



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_fileObject



57
58
59
# File 'lib/thrifty/thrift_file.rb', line 57

def version_file
  File.join(build_directory, 'VERSION')
end