Class: Pod::Command::OceanModulemap

Inherits:
Pod::Command show all
Defined in:
lib/cocoapods-ocean_modulemap/command/ocean_modulemap.rb

Overview

TODO:

Create a PR to add your plugin to CocoaPods/cocoapods.org in the plugins.json file, once your plugin is released.

This is an example of a cocoapods plugin adding a top-level subcommand to the ‘pod’ command.

You can also create subcommands of existing or new commands. Say you wanted to add a subcommand to list to show newly deprecated pods, (e.g. ‘pod list deprecated`), there are a few things that would need to change.

  • move this file to lib/pod/command/list/deprecated.rb and update the class to exist in the the Pod::Command::List namespace

  • change this class to extend from List instead of Command. This tells the plugin system that it is a subcommand of list.

  • edit lib/cocoapods_plugins.rb to require this file

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ OceanModulemap

Returns a new instance of OceanModulemap.



29
30
31
32
33
# File 'lib/cocoapods-ocean_modulemap/command/ocean_modulemap.rb', line 29

def initialize(argv)
  # 获取参数
  @target_dir = argv.shift_argument
  super
end

Instance Method Details

#fetch_final_dirString

获取最终的文件夹目录

Returns:

  • (String)

    最终的文件夹目录



62
63
64
65
66
67
68
69
70
# File 'lib/cocoapods-ocean_modulemap/command/ocean_modulemap.rb', line 62

def fetch_final_dir
  # 目标文件夹,默认是当前目录
  dir = Dir.pwd
  dir = @target_dir.to_s if @target_dir

  puts "\n you not set target dir, so use current dir (pwd) !!!\n" unless @target_dir
  puts 'final dir: ' + dir.to_s
  dir.to_s
end

#find_all_framework_paths(dir = '') ⇒ String

查找指定目录内所有的 framework

Parameters:

  • dir (defaults to: '')

    目录

Returns:

  • (String)

    framework 的路径



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cocoapods-ocean_modulemap/command/ocean_modulemap.rb', line 75

def find_all_framework_paths(dir = '')
  # 遍历所有的文件
  find_framework_cmd = 'find ' + dir.to_s
  find_framework_cmd += ' -name *.framework'
  find_framework_res = %x(#{find_framework_cmd})

  framework_paths = find_framework_res.to_s.split
  puts '-----'
  puts 'find framework path result: '
  puts framework_paths
  puts '-----'
  framework_paths
end

#find_all_header_paths(dir = '') ⇒ Object

查找所有的 headers(.h) 文件路径

Parameters:

  • dir (defaults to: '')

    目录路径

Returns:

  • 所有的头文件路径



145
146
147
148
149
150
151
152
# File 'lib/cocoapods-ocean_modulemap/command/ocean_modulemap.rb', line 145

def find_all_header_paths(dir = '')
  # 遍历所有的文件
  framework_headers_cmd = 'find ' + dir.to_s
  framework_headers_cmd += ' -name *.h'
  framework_headers_res = %x(#{framework_headers_cmd})

  framework_headers_res.to_s.split
end

#generate_header_content(header = '') ⇒ Object

根据头文件类型,生成在 moudulemap 中显示的内容

Parameters:

  • header (defaults to: '')

    头文件

Returns:

  • 生成的内容



166
167
168
169
170
171
172
173
174
# File 'lib/cocoapods-ocean_modulemap/command/ocean_modulemap.rb', line 166

def generate_header_content(header = '')
  content = if header.include?('-umbrella.h')
              'umbrella header ' + '"' + header.to_s + '"'
            else
              'header ' + '"' + header.to_s + '"'
            end
  puts "header: #{header}, module map content: #{content}"
  content
end

#generate_moudulemap_content(framework_name = '', umbrella_headers = [], normal_headers = []) ⇒ Object

生成 moudulemap 文件内容

Parameters:

  • framework_name (defaults to: '')

    framework 名称

  • umbrella_headers (defaults to: [])

    umbrella headers 文件名称列表

  • normal_headers (defaults to: [])

    normal headers 文件名称列表

Returns:

  • 内容



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/cocoapods-ocean_modulemap/command/ocean_modulemap.rb', line 181

def generate_moudulemap_content(framework_name = '', umbrella_headers = [], normal_headers = [])

  # 生成 header 对应的内容
  normal_headers_contents = normal_headers.map do |header_path|
    generate_header_content(header_path)
  end
  umbrella_headers_contents = umbrella_headers.map do |header_path|
    generate_header_content(header_path)
  end

  # 生成 modulemap 文件内容
  content = "framework module \#{framework_name} {\n  \#{umbrella_headers_contents.join(\"\\n  \")}\n  \#{normal_headers_contents.join(\"\\n  \")}\n\n  export *\n  module * { export * }\n}\n  MODULE_MAP\n\n  puts '\\n generate_moudulemap_content result: '\n  puts content\n\n  content\nend\n"

#handle_framework_modulemap(path = '') ⇒ Object

处理 framework 的 modulemap 文件

Parameters:

  • path (String) (defaults to: '')

    framework的路径



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cocoapods-ocean_modulemap/command/ocean_modulemap.rb', line 91

def handle_framework_modulemap(path = '')
  puts "\nhandle_framework_modulemap at path: " + path.to_s

  name = File.basename(path)
  puts 'basename: ' + name.to_s
  framework_name = File.basename(path, '.framework')
  puts 'framework_name: ' + framework_name.to_s

  # /Modules 目录处理
  module_dir = path + '/Modules'
  puts 'module dir: ' + module_dir.to_s
  # 不存在则进行创建
  if File.exist?(module_dir)
    puts 'already exist !'
  else
    puts 'not exist, need mkdir !'
    FileUtils.mkdir_p(module_dir.to_s)
  end

  # modulemap 文件处理, module.modulemap
  modulemap_path = module_dir.to_s + '/module.modulemap'
  puts 'modulemap path: ' + modulemap_path.to_s

  if File.exist?(modulemap_path)
    puts 'already exist, need return, do nothings !'
  else
    puts 'not exist, need generate modulemap file !'

    # 从 Headers 目录中找到所有的 头文件
    headers_dir = path + '/Headers'
    framework_headers_paths = find_all_header_paths(headers_dir)
    # 普通的.h文件
    framework_normal_headers_paths = framework_headers_paths.reject do |header_path|
      header_path.to_s.include?('-umbrella.h')
    end
    # -umbrella.h 文件
    framework_umbrella_headers_paths = framework_headers_paths - framework_normal_headers_paths

    # 文件名称
    framework_normal_headers = map_basename(framework_normal_headers_paths)
    framework_umbrella_headers = map_basename(framework_umbrella_headers_paths)

    content = generate_moudulemap_content(framework_name, framework_umbrella_headers, framework_normal_headers)

    # 保存到文件中
    Pathname.new(modulemap_path.to_s).open('w') do |f|
      f.write(content)
    end
  end
end

#map_basename(paths = []) ⇒ Object

把路径映射为文件名称

Parameters:

  • paths (defaults to: [])

    路径列表

Returns:

  • names 文件名称列表



157
158
159
160
161
# File 'lib/cocoapods-ocean_modulemap/command/ocean_modulemap.rb', line 157

def map_basename(paths = [])
  paths.map do |path|
    File.basename(path)
  end
end

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cocoapods-ocean_modulemap/command/ocean_modulemap.rb', line 40

def run
  # UI.puts "Add your implementation for the cocoapods-ocean_modulemap plugin in #{__FILE__}"

  # 找到pods目录进行 framework 文件中 modulemap 文件的生成

  # 最终的目录
  dir = fetch_final_dir
  # 文件夹校验
  unless File.directory?(dir)
    puts "#{dir} is not a valid directory !!\n"
    exit(1)
  end

  # 获取所有的 framework 路径
  framework_paths = find_all_framework_paths(dir)
  framework_paths.each do |path|
    handle_framework_modulemap(path)
  end
end

#validate!Object



35
36
37
38
# File 'lib/cocoapods-ocean_modulemap/command/ocean_modulemap.rb', line 35

def validate!
  super
  # help! 'A Pod name is required.' unless @name
end