def parse_KV_file(file, separator='=') # 分析如Generated.xcconfig类的配置文件
file_abs_path = File.expand_path(file)
if !File.exists? file_abs_path
return [];
end
pods_array = []
skip_line_start_symbols = ["#", "/"]
File.foreach(file_abs_path) { |line|
next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ }
plugin = line.split(pattern=separator)
if plugin.length == 2
podname = plugin[0].strip()
path = plugin[1].strip()
podpath = File.expand_path("#{path}", file_abs_path)
pods_array.push({:name => podname, :path => podpath});
else
puts "Invalid plugin specification: #{line}"
end
}
return pods_array
end
def flutter_root(f) # 分析Generated.xcconfig找到FLUTTER_ROOT地址
generated_xcode_build_settings = parse_KV_file(File.join(f, File.join('.ios', 'Flutter', 'Generated.xcconfig')))
if generated_xcode_build_settings.empty?
puts "Generated.xcconfig must exist. Make sure `flutter packages get` is executed in #{f}."
exit
end
generated_xcode_build_settings.map { |p|
if p[:name] == 'FLUTTER_ROOT'
return p[:path]
end
}
end
# If this wasn't specified, assume it's two levels up from the directory of this script.
flutter_application_path ||= File.join(__dir__, '..', '..') # 在主工程Podfile定义的flutter module根目录
framework_dir = File.join(flutter_application_path, '.ios', 'Flutter') # flutter支持代码目录 .ios/Flutter
engine_dir = File.join(framework_dir, 'engine') # flutter engine位置:.ios/Flutter/engine
# 首次初始化时从flutter系统库中复制ios引擎到项目下如 /Users/xxx/Project/Dev/flutter/bin/cache/artifacts/engine/ios
if !File.exist?(engine_dir)
debug_framework_dir = File.join(flutter_root(flutter_application_path), 'bin', 'cache', 'artifacts', 'engine', 'ios')
FileUtils.mkdir_p(engine_dir)
FileUtils.cp_r(File.join(debug_framework_dir, 'Flutter.framework'), engine_dir)
FileUtils.cp(File.join(debug_framework_dir, 'Flutter.podspec'), engine_dir)
end
# 在主工程中引入Fluter Pod插件
pod 'Flutter', :path => engine_dir
pod 'FlutterPluginRegistrant', :path => File.join(framework_dir, 'FlutterPluginRegistrant')
symlinks_dir = File.join(framework_dir, '.symlinks') # .ios/Flutter/.symlinks 不知道干嘛用
FileUtils.mkdir_p(symlinks_dir)
plugin_pods = parse_KV_file(File.join(flutter_application_path, '.flutter-plugins')) # .flutter-plugins 不知道干嘛用
plugin_pods.map { |r|
symlink = File.join(symlinks_dir, r[:name])
FileUtils.rm_f(symlink)
File.symlink(r[:path], symlink)
pod r[:name], :path => File.join(symlink, 'ios')
}
# Ensure that ENABLE_BITCODE is set to NO, add a #include to Generated.xcconfig, and
# add a run script to the Build Phases.
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO' # 所有target bitcode为NO
next if config.base_configuration_reference == nil
xcconfig_path = config.base_configuration_reference.real_path
File.open(xcconfig_path, 'a+') do |file|
file.puts "#include \"#{File.realpath(File.join(framework_dir, 'Generated.xcconfig'))}\""
end
end
end
end
当post_install和主工程的post_install重复导至pod install失败时(报错# [Invalid Podfile file specifying multiple post_install hooks is unsupported]),更换以下代码:
flutter_application_path = 'path/to/my_flutter/'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)
替换为
# ============= Flutter config begin ============== #
flutter_application_path = './fpf_flutter/'
podhelper_path = File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
podhelper_content = File.read(podhelper_path);
podhelper_post_isntall = "post_install do |installer|";
# 当post_install重复时需要去重以避免发生pod install错误:multiple post_install hooks is unsupported
if podhelper_content.scan(/(#{podhelper_post_isntall})/).length > 0 then
podhelper_buffer = podhelper_content.gsub(podhelper_post_isntall, "def update_configs(installer, framework_dir)")
eval(podhelper_buffer, binding)
else
eval(File.read(podhelper_path), binding)
end
# ============= Flutter config end ============== #
并且在post_install代码块开头加入以下代码,如
post_install do |installer_representation|
# ============= Flutter config begin ============== #
flutter_framework_dir = File.join(flutter_application_path, '.ios', 'Flutter')
update_configs(installer_representation, flutter_framework_dir)
# ============= Flutter config end ============== #
附(Ruby 文件内容查找和替换):
@poms.each{|pom|
#search and replace
file=File.read(pom)
if file.scan(/(#{@oldV})/).length<=0 then next end
buffer=file.gsub(/#{@oldV}/,@newV)
if not File.writable? pom then
#check out the file first
File.open(pom).chmod(0755)
end
f=File.write(pom,buffer)
a+=1
}
网友评论