Class: Thrifty::ThriftFile

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

Constant Summary collapse

DUMMY_DIRECTORY =
File.expand_path('../dummy', __FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manager, relative_path, options = {}) ⇒ ThriftFile



12
13
14
15
16
# File 'lib/thrifty/thrift_file.rb', line 12

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.



10
11
12
# File 'lib/thrifty/thrift_file.rb', line 10

def options
  @options
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



10
11
12
# File 'lib/thrifty/thrift_file.rb', line 10

def relative_path
  @relative_path
end

Instance Method Details

#build_directoryObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/thrifty/thrift_file.rb', line 71

def build_directory
  # Use the basename for informational purposes, and the SHA1 to avoid
  # collisions.
  #
  # Use relative_path since that should be invariant across machines
  # (i.e. once you've deployed the service).
  base = File.basename(path)
  sha1 = Digest::SHA1.hexdigest(relative_path)
  File.join(build_root, "#{base}-#{sha1}")
end

#build_rootObject



82
83
84
85
86
87
88
89
90
# File 'lib/thrifty/thrift_file.rb', line 82

def build_root
  if build_root = @options[:build_root]
    expand_relative_path(build_root)
  elsif @options[:relative_to]
    File.join(File.dirname(path), 'build-thrifty')
  else
    @manager.build_root # Don't expand the manager's build root
  end
end

#compile_onceObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/thrifty/thrift_file.rb', line 54

def compile_once
  with_cache do
    begin
      FileUtils.mkdir_p(build_directory)
    rescue Errno::EACCES => e
      e.message << ' (HINT: this probably means you forgot to precompile your Thrift file, or the relative path has changed between your build and deploy machines)'
      raise e
    end
    includes = @manager.include_path.map {|x| ['-I', x]}.flatten
    Rubysh('thrift', *includes, '--gen', 'rb', '-out', build_directory, path).check_call
  end
end

#defines?(generated_file) ⇒ Boolean



26
27
28
29
# File 'lib/thrifty/thrift_file.rb', line 26

def defines?(generated_file)
  compile_once
  File.exists?(File.join(build_directory, generated_file + '.rb'))
end

#pathObject



22
23
24
# File 'lib/thrifty/thrift_file.rb', line 22

def path
  expand_relative_path(@relative_path)
end

#require(generated_file) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/thrifty/thrift_file.rb', line 31

def require(generated_file)
  compile_once

  # This is a bit dicey, as a generated thrift <file> will also
  # include <file>_types and <file>_constants as well as
  # 'thrift'. We need to make sure we can handle file names that may
  # have already been required -- this path munging should
  # successfully do so.

  super('thrift')
  log.debug('Requiring', file: generated_file, idl: path, build_directory: build_directory)

  orig_load_path = $:.dup

  begin
    $:[0..-1] = @manager.require_path + [DUMMY_DIRECTORY]
    # Global require
    super(generated_file)
  ensure
    $:[0..-1] = orig_load_path
  end
end

#validate_existenceObject



18
19
20
# File 'lib/thrifty/thrift_file.rb', line 18

def validate_existence
  raise "No such Thrift file #{path.inspect}" unless File.exists?(path)
end

#version_fileObject



67
68
69
# File 'lib/thrifty/thrift_file.rb', line 67

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