lib/cocoapods-flutter/native/archive/archive.rb in cocoapods-flutter-dt-0.2.7 vs lib/cocoapods-flutter/native/archive/archive.rb in cocoapods-flutter-dt-0.4.1

- old
+ new

@@ -3,10 +3,11 @@ require 'zip' require 'cocoapods-flutter/native/archive/zip_tool' require 'cocoapods-flutter/native/archive/remote' require 'molinillo' require 'plist' +require 'yaml' class Archiver class Plugins attr_reader :spec @@ -52,71 +53,55 @@ # File.open('/Users/dreamtracer/Desktop/flutter.zip', "w+") do |file| # file.write u.read.force_encoding("UTF-8") # end # end + #使用fvm切换flutter 版本 + flutter_sdk_version_switch @flutter_version + @pub_upgrade ? pub_upgrade : pub_get if local_podfile_exists? use_local_podfile else setup_pod_sources end if @build_run excute_build_run end + build_framework @plugins = fetch_plugins if @build_modes.include?('debug') # Debug瘦身 - thin_arm64 "#{@product_dir}Debug/App.framework/App" + #thin_arm64 "#{@product_dir}Debug/App.framework/App" debug end if @build_modes.include?('release') release end Pod::UserInterface.message "All is ready to use!, now you can use 'flutter_pod '#{@module_name}', '#{@version}', :mode=>'debug/release' to use the pod" end - def make_flutter_pods(mode) - @pub_upgrade ? pub_upgrade : pub_get - setup_pod_sources - if @build_run - excute_build_run - end + private - build_app mode + def flutter_sdk_version_switch(version) - flutter_spec_dir = @product_dir + 'Flutter' - flutter_spec_file = flutter_spec_dir + '/Flutter.podspec.json' - flutter_spec = Pod::Spec.from_file @working_dir + '/.ios/Flutter/engine/Flutter.podspec' - flutter_spec.vendored_frameworks = ['Flutter.framework', 'App.framework'] - plugin_registrant_spec_file = @working_dir + '/.ios/Flutter/FlutterPluginRegistrant/FlutterPluginRegistrant.podspec' + commands = [ + 'fvm', + 'use', + version + ] - unless Dir.exist? flutter_spec_dir - FileUtils.mkdir_p flutter_spec_dir + if CommandRunner.run(*commands) == false + # raise "Error running #{commands.join ' '} " end - - File.open flutter_spec_file, "w+" do |file| - file.write flutter_spec.to_pretty_json - end - - FileUtils.cp_r @working_dir + '/.ios/Flutter/engine/Flutter.framework', flutter_spec_dir - FileUtils.cp_r @working_dir + '/.ios/Flutter/App.framework', flutter_spec_dir - - plugins = fetch_plugins - plugins.append Plugins.new('Flutter',flutter_spec_file,flutter_spec) - plugins.append Plugins.new('FlutterPluginRegistrant',plugin_registrant_spec_file, Pod::Spec.from_file(plugin_registrant_spec_file)) - - plugins end - private - def thin_arm64(path) # thin arm64 thin_commands = [ 'lipo', path, @@ -142,17 +127,17 @@ upload_files app_file, sdk_file, 'Debug' make_pod 'Debug' end def pub_upgrade - if CommandRunner.run(@flutter_wrapper, 'pub', 'upgrade') == false + if CommandRunner.run('fvm', @flutter_wrapper, 'pub', 'upgrade') == false raise "Pub upgrade fail" end end def pub_get - if CommandRunner.run(@flutter_wrapper, 'pub', 'get') == false + if CommandRunner.run('fvm', @flutter_wrapper, 'pub', 'get') == false raise "Pub get fail" end end def local_podfile_exists? @@ -217,18 +202,19 @@ local_podfile_dir = @working_dir + '/local_podfile' FileUtils.copy_file local_podfile_dir, origin_pod_file_dir end def excute_build_run - if CommandRunner.run(@flutter_wrapper, 'packages','pub','run','build_runner','build','--delete-conflicting-outputs') == false + if CommandRunner.run('fvm', @flutter_wrapper, 'packages','pub','run','build_runner','build','--delete-conflicting-outputs') == false raise "Error running #{@flutter_wrapper} packages pub run build_runner build --delete-conflicting-outputs" end end def build_framework commands = [ + 'fvm', @flutter_wrapper, 'build', 'ios-framework', "--output=#{@product_dir}", '--no-profile' @@ -247,29 +233,60 @@ raise "Error running #{commands.join ' '} " end end - def build_app(mode) - if CommandRunner.run(@flutter_wrapper, 'build','ios',"--#{mode}" ,"--no-codesign") == false - # FileUtils.remove_dir product_dir, true - raise "Error running #{@flutter_wrapper} build ios --#{mode} --no-codesign" - end - end + # def build_app(mode) + # if CommandRunner.run('fvm', @flutter_wrapper, 'build','ios',"--#{mode}" ,"--no-codesign") == false + # # FileUtils.remove_dir product_dir, true + # raise "Error running #{@flutter_wrapper} build ios --#{mode} --no-codesign" + # end + # end def fetch_plugins results = [] flutter_plugins_list = @working_dir + '/.flutter-plugins' + lines = IO.readlines(flutter_plugins_list) lines.each do |line| unless line =~ /^#/ key_val = line.split '=' key = key_val[0] value = key_val[1] value = value.gsub /\n/, '' - path = "#{value}ios/#{key}.podspec" - results.append Plugins.new(key, path, Pod::Specification.from_file(path)) + yaml_content = YAML.load_file value + 'pubspec.yaml' + f_h = yaml_content['flutter'] + if f_h != nil then + p_h = f_h['plugin'] + if p_h != nil then + pl_h = p_h['platforms'] + if pl_h != nil then + if pl_h['ios'] != nil + path = "#{value}ios/#{key}.podspec" + if File.exist? path + results.append Plugins.new(key, path, Pod::Specification.from_file(path)) + end + end + else + path = "#{value}ios/#{key}.podspec" + if File.exist? path + results.append Plugins.new(key, path, Pod::Specification.from_file(path)) + end + end + else + path = "#{value}ios/#{key}.podspec" + if File.exist? path + results.append Plugins.new(key, path, Pod::Specification.from_file(path)) + end + end + else + path = "#{value}ios/#{key}.podspec" + if File.exist? path + results.append Plugins.new(key, path, Pod::Specification.from_file(path)) + end + end + end end results end @@ -292,16 +309,16 @@ } json = JSON.pretty_generate info_hash zipfile.get_output_stream("info.json") { |f| f.write json } zipfile.add 'pubspec.lock', "#{@working_dir}/pubspec.lock" zipfile.get_output_stream("download_sdk.rb") { |f| f.write sdk_download } - zipfile.add_dir "App.framework", "#{product_dir}/App.framework" - zipfile.add_dir "FlutterPluginRegistrant.framework","#{product_dir}/FlutterPluginRegistrant.framework" + zipfile.add_dir "App.xcframework", "#{product_dir}/App.xcframework" + zipfile.add_dir "FlutterPluginRegistrant.xcframework","#{product_dir}/FlutterPluginRegistrant.xcframework" plugins.each do |plugin| - file = File.join(product_dir, "#{plugin.spec.name}.framework") + file = File.join(product_dir, "#{plugin.spec.name}.xcframework") if Dir.exist? file - zipfile.add_dir("#{plugin.spec.name}.framework", file) + zipfile.add_dir("#{plugin.spec.name}.xcframework", file) end end end sdk_file = zip_file_dir + "/Flutter.zip" @@ -312,14 +329,15 @@ if Dir.exist?(flutter_sdk_dsym_dir) == false Dir.mkdir flutter_sdk_dsym_dir end Zip::File.open sdk_file, Zip::File::CREATE do |zipfile| - file = File.join(product_dir, "Flutter.framework") - zipfile.add_dir "Flutter/Flutter.framework", file + file = File.join(product_dir, "Flutter.xcframework") + zipfile.add_dir "Flutter/Flutter.xcframework", file #读取Flutter.framework中的info.plist文件中的FlutterEngine的值 - info_plist_path = File.join(file, "Info.plist") + info_plist_path = info_plist_from_xcframework file, 1 + # info_plist_path = File.join(file, "Info.plist") info_plist = Plist.parse_xml(info_plist_path) engine_hash = info_plist["FlutterEngine"] dsym_dir = File.join(flutter_sdk_dsym_dir, engine_hash) dsym_zip_file_path = File.join(dsym_dir, 'Flutter.framework.dSYM.zip') dsym_file_path = File.join(dsym_dir, 'Flutter.framework.dSYM') @@ -355,10 +373,27 @@ zipfile.add_dir "Flutter/Flutter.framework.dSYM", dsym_file_path end [zip_file, sdk_file] end + #depth 1为第一层 + def info_plist_from_xcframework(path, depth) + children = Dir.children path + for child in children + child_path = File.join path, child + if File.directory? child_path then + r = info_plist_from_xcframework child_path, depth + 1 + if r != nil + return r + end + elsif depth > 1 && child == 'Info.plist' then + return child_path + end + end + return nil + end + def sdk_download ruby_code = <<-CODE #!/usr/bin/env ruby @@ -371,11 +406,11 @@ Digest::MD5.hexdigest url end def download(url, dest) open(url) do |u| File.open(dest, "w+") do |file| - file.write u.read.force_encoding("UTF-8") + file.write u.read end end end home_dir = Dir.home cache_dir = home_dir + "/.cocoapods/.cache" @@ -398,10 +433,11 @@ ruby_code end def upload_files(app_file, flutter_sdk, mode) + Pod::UserInterface.info 'Start upload resources' remote = Remote.new() Pod::UserInterface.info "Checking #{@module_name}_#{@version}_#{mode.downcase}..." result = remote.exist? @module_name, @version, mode.downcase if result @@ -438,11 +474,11 @@ def make_pod(mode) dependency_graph = Molinillo::DependencyGraph.new @plugins.each do |plugin| - plugin.spec.all_dependencies.each do |dep| + plugin.spec.all_dependencies('ios').each do |dep| unless ignore_dependency? dep.name dependency_graph.add_vertex(dep.name, dep, true) end end end @@ -462,12 +498,12 @@ s.source = { :http => app_download_url } s.ios.deployment_target = '10.0' s.prepare_command = "ruby download_sdk.rb #{sdk_download_url}" s.pod_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64 x86_64' } s.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64 x86_64' } - vendored_frameworks = ["App.framework", "Flutter/Flutter.framework", "FlutterPluginRegistrant.framework"] + vendored_frameworks = ["App.xcframework", "Flutter/Flutter.xcframework", "FlutterPluginRegistrant.xcframework"] @plugins.each do |plugin| - vendored_frameworks.append "#{plugin.name}.framework" + vendored_frameworks.append "#{plugin.name}.xcframework" end s.vendored_frameworks = vendored_frameworks dependency_graph.each do |vertex| s.dependency vertex.payload.name, vertex.payload.requirement.to_s