podhelper.rb.tmpl 5.78 KB
Newer Older
1 2 3 4 5 6
# Copyright 2014 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

require 'json'

7 8 9 10 11 12 13 14 15 16
# Install pods needed to embed Flutter application, Flutter engine, and plugins
# from the host application Podfile.
#
# @example
#   target 'MyApp' do
#     install_all_flutter_pods 'my_flutter'
#   end
# @param [String] flutter_application_path Path of the root directory of the Flutter module.
#                                          Optional, defaults to two levels up from the directory of this script.
#                                          MyApp/my_flutter/.ios/Flutter/../..
17
def install_all_flutter_pods(flutter_application_path = nil)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  # defined_in_file is a Pathname to the Podfile set by CocoaPods.
  pod_contents = File.read(defined_in_file)
  unless pod_contents.include? 'flutter_post_install'
    puts  <<~POSTINSTALL
Add `flutter_post_install(installer)` to your Podfile `post_install` block to build Flutter plugins:

post_install do |installer|
  flutter_post_install(installer)
end
POSTINSTALL
    raise 'Missing `flutter_post_install(installer)` in Podfile `post_install` block'
  end

  require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

33
  flutter_application_path ||= File.join('..', '..')
34
  install_flutter_engine_pod(flutter_application_path)
35
  install_flutter_plugin_pods(flutter_application_path)
36
  install_flutter_application_pod(flutter_application_path)
37 38
end

39 40 41 42 43 44
# Install Flutter engine pod.
#
# @example
#   target 'MyApp' do
#     install_flutter_engine_pod
#   end
45 46 47
def install_flutter_engine_pod(flutter_application_path = nil)
  flutter_application_path ||= File.join('..', '..')
  ios_application_path = File.join(flutter_application_path, '.ios')
48

49 50
  # flutter_install_ios_engine_pod is in Flutter root podhelper.rb
  flutter_install_ios_engine_pod(ios_application_path)
51 52
end

53 54 55 56 57 58 59 60 61 62 63
# Install Flutter plugin pods.
#
# @example
#   target 'MyApp' do
#     install_flutter_plugin_pods 'my_flutter'
#   end
# @param [String] flutter_application_path Path of the root directory of the Flutter module.
#                                          Optional, defaults to two levels up from the directory of this script.
#                                          MyApp/my_flutter/.ios/Flutter/../..
def install_flutter_plugin_pods(flutter_application_path)
  flutter_application_path ||= File.join('..', '..')
64 65 66
  ios_application_path = File.join(flutter_application_path, '.ios')
  # flutter_install_plugin_pods is in Flutter root podhelper.rb
  flutter_install_plugin_pods(ios_application_path, '.symlinks', 'ios')
67

68
  # Keep pod path relative so it can be checked into Podfile.lock.
69
  relative = flutter_relative_path_from_podfile(ios_application_path)
70
  pod 'FlutterPluginRegistrant', path: File.join(relative, 'Flutter', 'FlutterPluginRegistrant'), inhibit_warnings: true
71 72 73 74 75 76
end

# Install Flutter application pod.
#
# @example
#   target 'MyApp' do
77
#     install_flutter_application_pod '../flutter_settings_repository'
78 79 80 81
#   end
# @param [String] flutter_application_path Path of the root directory of the Flutter module.
#                                          Optional, defaults to two levels up from the directory of this script.
#                                          MyApp/my_flutter/.ios/Flutter/../..
82
def install_flutter_application_pod(flutter_application_path)
83
  flutter_application_path ||= File.join('..', '..')
84

85
  export_script_directory = File.join(flutter_application_path, '.ios', 'Flutter')
86

87 88
  # Keep script phase paths relative so they can be checked into source control.
  relative = flutter_relative_path_from_podfile(export_script_directory)
89

90
  flutter_export_environment_path = File.join('${SRCROOT}', relative, 'flutter_export_environment.sh')
91 92

  # Compile App.framework and move it and Flutter.framework to "BUILT_PRODUCTS_DIR"
93 94 95
  script_phase name: 'Run Flutter Build {{projectName}} Script',
    script: "set -e\nset -u\nsource \"#{flutter_export_environment_path}\"\nexport VERBOSE_SCRIPT_LOGGING=1 && \"$FLUTTER_ROOT\"/packages/flutter_tools/bin/xcode_backend.sh build",
    execution_position: :before_compile
96 97

  # Embed App.framework AND Flutter.framework.
98 99 100
  script_phase name: 'Embed Flutter Build {{projectName}} Script',
    script: "set -e\nset -u\nsource \"#{flutter_export_environment_path}\"\nexport VERBOSE_SCRIPT_LOGGING=1 && \"$FLUTTER_ROOT\"/packages/flutter_tools/bin/xcode_backend.sh embed_and_thin",
    execution_position: :after_compile
101 102 103
end

def flutter_root
104
  generated_xcode_build_settings_path = File.expand_path(File.join('..', '..', 'Flutter', 'Generated.xcconfig'), __FILE__)
105
  raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first" unless File.exist?(generated_xcode_build_settings_path)
106 107

  File.foreach(generated_xcode_build_settings_path) do |line|
108
    matches = line.match(/FLUTTER_ROOT=(.*)/)
109 110 111 112
    return matches[1].strip if matches
  end
  # This should never happen...
  raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
113
end
114 115 116 117 118 119 120 121 122 123 124 125 126 127

# Run the post-install script to set build settings on Flutter plugins.
#
# @example
#   post_install do |installer|
#     flutter_post_install(installer)
#   end
# @param [Pod::Installer] installer Passed to the `post_install` block.
# @param [boolean] skip Do not change any build configurations. Set to suppress
#                       "Missing `flutter_post_install" exception.
def flutter_post_install(installer, skip: false)
  return if skip

  installer.pods_project.targets.each do |target|
128
    target.build_configurations.each do |_build_configuration|
129 130 131 132 133
      # flutter_additional_ios_build_settings is in Flutter root podhelper.rb
      flutter_additional_ios_build_settings(target)
    end
  end
end