Class: MotionHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/motion.h.rb

Constant Summary collapse

BRIDGESUPPORT_DIR =
'build'

Instance Method Summary collapse

Constructor Details

#initialize(header_file, config, options = {}) ⇒ MotionHeader

Returns a new instance of MotionHeader.

Parameters:

  • header_file (String)

    Requested C header file.

  • config (Motion::Project::Config)

    RubyMotion config provided in App.setup.

  • options (Hash) (defaults to: {})

    Options for customizing BridgeSupport file generation

Options Hash (options):

  • :prefix (String)

    Subdirectory of /usr/include used for root of included header files.

  • :bridgesupport_dir (String)

    Path where the generated bridgesupport file is saved. Defaults to ./build



42
43
44
45
46
47
# File 'lib/motion.h.rb', line 42

def initialize(header_file, config, options={})
  @header_file = header_file
  @config = config
  @prefix = options[:prefix]
  @bridgesupport_dir = options[:bridgesupport_dir] || BRIDGESUPPORT_DIR
end

Instance Method Details

#bridgesupport_fileObject



97
98
99
100
# File 'lib/motion.h.rb', line 97

def bridgesupport_file
  file_name = @header_file.tr('/', '_').chomp('.h')
  "#{@bridgesupport_dir}/#{file_name}.bridgesupport"
end

#frameworks_pathObject



82
83
84
85
86
# File 'lib/motion.h.rb', line 82

def frameworks_path
  sdk_dir = sdk_dir(@config.sdk_version)
  path_components = [@config.xcode_dir, *sdk_dir, 'System', 'Library', 'Frameworks'].compact
  File.join(*path_components)
end

#generate_bridgesupport_fileObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/motion.h.rb', line 60

def generate_bridgesupport_file
  return if File.exist?(bridgesupport_file)
  Dir.mkdir(@bridgesupport_dir) unless Dir.exist?(@bridgesupport_dir)
  flag = begin
    case platform
    when :ios
      "--no-64-bit"
    when :osx
      "--64-bit"
    end
  end
  Bundler.with_clean_env do
    `/Library/RubyMotion/bin/gen_bridge_metadata --format complete #{flag} --cflags '-I#{include_path} -F#{frameworks_path}' #{@header_file} > #{bridgesupport_file}`
  end
end

#include_pathObject



76
77
78
79
80
# File 'lib/motion.h.rb', line 76

def include_path
  sdk_dir = sdk_dir(@config.sdk_version)
  path_components = [@config.xcode_dir, *sdk_dir, 'usr', 'include', @prefix].compact
  File.join(*path_components)
end

#integrateObject



49
50
51
52
53
# File 'lib/motion.h.rb', line 49

def integrate
  verify_header_file
  generate_bridgesupport_file
  bridgesupport_file
end

#platformObject



102
103
104
# File 'lib/motion.h.rb', line 102

def platform
  Motion::Project::App.respond_to?(:template) ? Motion::Project::App.template : :ios
end

#sdk_dir(sdk_version) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/motion.h.rb', line 88

def sdk_dir(sdk_version)
  case platform
  when :ios
    ['Platforms', 'iPhoneOS.platform', 'Developer', 'SDKs', "iPhoneOS#{sdk_version}.sdk"]
  when :osx
    ['Platforms', 'MacOSX.platform', 'Developer', 'SDKs', "MacOSX#{sdk_version}.sdk"]
  end
end

#verify_header_fileObject



55
56
57
58
# File 'lib/motion.h.rb', line 55

def verify_header_file
  path = "#{include_path}/#{@header_file}"
  File.exist?(path) or raise "Header file `#{@header_file}' does not exist (#{path})."
end