Module: Pod::Podfile::DSL

Defined in:
lib/cocoapods-binary-flutter/hook/podfile.rb

Constant Summary collapse

@@enable_fix_flutter_post_install =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enable_fix_flutter_post_installObject



17
18
19
# File 'lib/cocoapods-binary-flutter/hook/podfile.rb', line 17

def self.enable_fix_flutter_post_install
  @@enable_fix_flutter_post_install
end

Instance Method Details

#enable_fix_flutter_post_install!Object



21
22
23
# File 'lib/cocoapods-binary-flutter/hook/podfile.rb', line 21

def enable_fix_flutter_post_install!
  @@enable_fix_flutter_post_install = true
end

#install_all_lzflutter_pods(flutter_binary_path) ⇒ Object

安装



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cocoapods-binary-flutter/hook/podfile.rb', line 104

def install_all_lzflutter_pods(flutter_binary_path)
  # defined_in_file is a Pathname to the Podfile set by CocoaPods.
  pod_contents = File.read(defined_in_file)
  if pod_contents.include? 'flutter_post_install(installer)'

    unless pod_contents.include? 'flutter_post_install(installer) if defined?(flutter_post_install)'

      raise "产物依赖需要修改 flutter_post_install(installer) 增加define判断` in Podfile `post_install` block
      flutter_post_install(installer) if defined?(flutter_post_install)
      "

    end

  end

  install_lzflutter_engine_pod(flutter_binary_path)
  install_lzflutter_plugin_pods(flutter_binary_path)
  install_lzflutter_application_pod(flutter_binary_path)
end

#install_lzflutter_application_pod(flutter_binary_path) ⇒ Object

Install Flutter application pod.



164
165
166
# File 'lib/cocoapods-binary-flutter/hook/podfile.rb', line 164

def install_lzflutter_application_pod(flutter_binary_path)
  pod 'App', :path => File.join(flutter_binary_path, 'flutter'), :inhibit_warnings => true
end

#install_lzflutter_engine_pod(flutter_binary_path) ⇒ Object

安装flutter引擎



125
126
127
# File 'lib/cocoapods-binary-flutter/hook/podfile.rb', line 125

def install_lzflutter_engine_pod(flutter_binary_path)
  pod 'Flutter', :podspec => File.join(flutter_binary_path, 'flutter', 'Flutter.podspec'), :inhibit_warnings => true
end

#install_lzflutter_plugin_pods(flutter_binary_path) ⇒ Object

Install Flutter plugin pods.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/cocoapods-binary-flutter/hook/podfile.rb', line 130

def install_lzflutter_plugin_pods(flutter_binary_path)
  # Keep pod path relative so it can be checked into Podfile.lock.
  # Process will be run from project directory.

  registrantPath = File.join(flutter_binary_path, 'flutter', 'FlutterPluginRegistrant')
  if File.exist?(registrantPath)
    pod 'FlutterPluginRegistrant', :path => registrantPath, :inhibit_warnings => true
  end
  
  #插件目录
  ios_plugins_directory_pathname = File.join(flutter_binary_path, 'plugins')

  #plugins遍历
  Dir.foreach(ios_plugins_directory_pathname) do |item|
    next if item == '.' or item == '..' or File::directory?(File.join(ios_plugins_directory_pathname, item)) == false
    # do work on real items
    ios_path = File.join(ios_plugins_directory_pathname, item, 'ios')
    darwin_path = File.join(ios_plugins_directory_pathname, item, 'darwin')      
    if File.exist?(ios_path)
      # 执行逻辑:存在 ios 目录
      pod item, path: ios_path, inhibit_warnings: true
    elsif File.exist?(darwin_path)
      # 执行逻辑:存在 darwin 目录
      pod item, path: darwin_path, inhibit_warnings: true
    else
      # 抛出异常:插件依赖解析失败
      error_message = "插件依赖解析失败: #{item}"
      puts error_message
      raise error_message
    end                
  end
end

#install_remote_flutter_binary(url = nil) ⇒ Object

下载



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cocoapods-binary-flutter/hook/podfile.rb', line 27

def install_remote_flutter_binary(url = nil)
  #md5
  md5 = Digest::MD5.new               # =>#<Digest::MD5>
  md5 << url
  md5value = md5.hexdigest                        # => "78e73102..."
  flutter_f_home = Dir.home+'/Library/Caches/CocoaPods/Flutter/'
  flutter_binary_home = flutter_f_home+md5value
  flutter_binary_path = flutter_binary_home+'/binary'
  
  # 创建目录(如果不存在)
  FileUtils.mkdir_p(flutter_binary_home) unless Dir.exist?(flutter_binary_home)

  # 获取flutter_f_home目录下的所有子目录
  subdirectories = Dir.entries(flutter_f_home).select { |entry| File.directory?(File.join(flutter_f_home, entry)) && !(entry =='.' || entry == '..' || entry == '.DS_Store') }

  # 清除超过30天的缓存只有当子目录数量大于5个时才执行
  if subdirectories.length > 5
    subdirectories.each do |subdirectory|
      next if subdirectory == md5value  # 跳过flutter_binary_home目录
      dirpath = File.join(flutter_f_home, subdirectory)
      a = File.mtime(dirpath)
      t = Time.now
      if (t - a).round / 60 / 60 / 24 > 30
        FileUtils.remove_dir(dirpath)
      end
    end
  end
  
  #下载解压
  if File.directory?(flutter_binary_path) == false
    # 获取文件大小
    uri = URI(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true if uri.scheme == 'https'
    response = http.head(uri.request_uri)
    expected_size = response['Content-Length'].to_i

    # 下载文件
    puts "开始下载 " + url
    download_file = open(File.join(flutter_binary_home, "binary.zip"), "wb")
    Net::HTTP.start(uri.host, uri.port,
      :use_ssl => uri.scheme == 'https', 
      :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|

      request = Net::HTTP::Get.new uri.request_uri
      request.basic_auth 'zhiya_official', '202525631b169f4446103671d4ff0661'
      begin
        http.request(request) do |resp|
          resp.read_body { |segment| download_file.write(segment) }
        end
      ensure
        download_file.close
      end
    end

    # 校验文件大小
    actual_size = File.size(File.join(flutter_binary_home, "binary.zip"))
    if actual_size != expected_size
      puts "文件大小和预期不匹配"
      raise "文件大小和预期不匹配"
    end

    # 解压文件
    puts "开始解压 " + flutter_binary_home
    begin
      system("unzip", "-n", File.join(flutter_binary_home, "binary.zip"), "-d", flutter_binary_home)
    rescue => exception
      puts "解压失败: " + exception.message
      raise "解压失败"
    end
  end

  #集成
  install_all_lzflutter_pods(flutter_binary_path)
end