Class: VFastDev

Inherits:
Object
  • Object
show all
Defined in:
lib/vfastdev.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ VFastDev

Returns a new instance of VFastDev.



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
# File 'lib/vfastdev.rb', line 46

def initialize(options)
  @vs_project_names = parse_vs_project_names
  @vs_framework_dir = 'VSFrameworks'
  @fastdev_dir = 'FastDev'
  @project_kept_names = options[:keep] || []
  @no_download_libs = options[:no_download_lib]
  @sync_main_project = options[:sync]

  @project_kept_names.each do |each|
    unless @vs_project_names.include?(each)
      puts "指定工程#{each}不存在"
      exit 0
    end
  end
  @project_kept_names << "VSBaseModule"
  @project_kept_names << "VSPublic"
  if @sync_main_project
    puts "还没实现"
  else
    init_context
    ajust_project
    unless @no_download_libs
      print_version_info
      download_libs
      unzip_libs
    end
    say_after_done
  end
end

Instance Method Details

#ajust_projectObject



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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
207
208
209
210
211
212
# File 'lib/vfastdev.rb', line 112

def ajust_project
  exclude_framework_names = [] #排除在外的静态库
  @project_kept_names.each do |name|
    exclude_framework_names << name + ".framework"
  end

  vs_framework_paths = [] #所有静态库路径
  @vs_project_names.each do |name|
    vs_framework_paths << name + ".framework"
  end
  project_path = 'Spec.xcodeproj'
  project = Xcodeproj::Project.open(project_path)

  puts '更改Spec.xcodeproj的main group的引用路径'
  project.main_group.set_source_tree('SOURCE_ROOT')
  project.main_group.set_path('../Spec')


  puts '更改Spec.xcodeproj中info.plist,pch,entitlements的引用路径'
# 修改info.plist,pch,entitlements的引用路径
  project.targets.each do |target|
    target.build_configurations.each do |config|
      if config.build_settings.has_key?('INFOPLIST_FILE')
        info_plist_config = config.build_settings['INFOPLIST_FILE']

        info_plist_config = "../Spec/" + info_plist_config
        config.build_settings['INFOPLIST_FILE'] = info_plist_config
      end
      if config.build_settings.has_key?('GCC_PREFIX_HEADER')
        pch_config = config.build_settings['GCC_PREFIX_HEADER']
        pch_config = "../Spec/" + pch_config
        config.build_settings['GCC_PREFIX_HEADER'] = pch_config
      end
      if config.build_settings.has_key?('CODE_SIGN_ENTITLEMENTS')
        entitlements_config = config.build_settings['CODE_SIGN_ENTITLEMENTS']
        entitlements_config = "../Spec/" + entitlements_config
        config.build_settings['CODE_SIGN_ENTITLEMENTS'] = entitlements_config
      end
    end
  end

  puts 'FRAMEWORK_SEARCH_PATHS新增路径'
  project.build_configuration_list.build_configurations.each do |config|
    if config.build_settings.has_key?('FRAMEWORK_SEARCH_PATHS')
      paths = config.build_settings['FRAMEWORK_SEARCH_PATHS']
      if paths.is_a?(String)
        config.build_settings['FRAMEWORK_SEARCH_PATHS'] = [paths, @vs_framework_dir]
      else
        config.build_settings['FRAMEWORK_SEARCH_PATHS'] << @vs_framework_dir
      end
    end
  end

  puts '修改静态库库的引用'
  project.files.each do |file_ref|
    if file_ref.last_known_file_type == "wrapper.framework" && vs_framework_paths.include?(file_ref.name) && !exclude_framework_names.include?(file_ref.name)
      file_ref.set_source_tree("SOURCE_ROOT")
      file_ref.set_path(@vs_framework_dir + "/" + file_ref.name)
    elsif file_ref.explicit_file_type == "wrapper.framework" && vs_framework_paths.include?(file_ref.path) && !exclude_framework_names.include?(file_ref.path)
      file_ref.set_source_tree("SOURCE_ROOT")
      file_ref.name = file_ref.path
      file_ref.set_path(@vs_framework_dir + "/" + file_ref.path)
    end
  end

  puts '修改Copy Modular Resources 脚本:"${SRCROOT}/Resources/Support/modular-resources-copy.sh" => "${SRCROOT}/../Spec/Resources/Support/modular-resources-copy.sh"'
  project.targets.each do |target|
    target.shell_script_build_phases.each do |phase|
      if phase.name == 'Copy Modular Resources'
        phase.shell_script = '"${SRCROOT}/../Spec/Resources/Support/modular-resources-copy.sh"'
      end
    end
  end
  project.save

  puts '生成新的workspace'
  workspace_xml = <<XML
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
 version = "1.0">
</Workspace>
XML


  workspace = Xcodeproj::Workspace.from_s(workspace_xml)
  spec_file_ref = Xcodeproj::Workspace::FileReference.new("Spec.xcodeproj")
  workspace << spec_file_ref

  @project_kept_names.each do |each|
    fileRef = Xcodeproj::Workspace::FileReference.new("../#{each}/#{each}.xcodeproj")
    workspace << fileRef
  end

  pods_file_ref = Xcodeproj::Workspace::FileReference.new("../Pods/Pods.xcodeproj")
  workspace << pods_file_ref

  workspace_path = "Spec.xcworkspace"
  workspace.save_as(workspace_path)


end

#download_libsObject

下载静态库



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/vfastdev.rb', line 240

def download_libs
  @vs_project_names.each do |name|
    puts '下载:' + name
    File.open("#{@vs_framework_dir}/#{name}.framework.zip", 'wb') do |f|
      framework_name = "#{name}.framework"
      url = "http://192.168.33.79:8000/lib/dowload_framework_file?app_id=Spec&name=#{framework_name}"
      begin
        f.write(open(url) do |f1|
          f1.read
        end)
      rescue StandardError, Timeout::Error, SystemCallError, Errno::ECONNREFUSED
        puts $!
      end
    end
  end
end

#init_contextObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vfastdev.rb', line 78

def init_context
  unless File.exist?(@fastdev_dir)
    puts "创建#{@fastdev_dir}目录"
    Dir.mkdir(@fastdev_dir)
  end
  Dir.chdir(@fastdev_dir)

  if File.exist?('Spec.xcodeproj')
    puts '删除FastDev/Spec.xcodeproj'
    FileUtils.rm_rf('Spec.xcodeproj')
  end
  puts '拷贝Spec.xcodeproj到FastDev'
  FileUtils.cp_r('../Spec/Spec.xcodeproj', 'Spec.xcodeproj')

  if File.exist?('Spec.xcworkspace')
    puts '删除FastDev/Spec.xcworkspace'
    FileUtils.rm_rf('Spec.xcworkspace')
  end
  puts '拷贝Spec.xcworkspace到FastDev'
  FileUtils.cp_r('../Spec.xcworkspace', 'Spec.xcworkspace')

  if File.exist?(@vs_framework_dir)
    unless @no_download_libs
      puts "删除FastDev/#{@vs_framework_dir}"
      FileUtils.rm_rf(@vs_framework_dir)
      puts "创建目录FastDev/#{@vs_framework_dir}"
      Dir.mkdir(@vs_framework_dir)
    end
  else
    puts "创建目录FastDev/#{@vs_framework_dir}"
    Dir.mkdir(@vs_framework_dir)
  end
end

#parse_vs_project_namesObject



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/vfastdev.rb', line 225

def parse_vs_project_names
  # 分析xcworkspace文件,查找VS开头的工程名字
  names = []
  tmp_workspace = Xcodeproj::Workspace.new_from_xcworkspace("Spec.xcworkspace")
  tmp_workspace.file_references.each do |file_ref|
    match = /VS[a-zA-Z0-9]*?(?=.xcodeproj)/.match(file_ref.path)
    if match != nil
      name = match[0]
      names << name
    end
  end
  return names
end

输出静态库版本信息



215
216
217
218
219
220
221
222
223
# File 'lib/vfastdev.rb', line 215

def print_version_info
  begin
    open('http://192.168.33.79:8000/lib/latest_framework_info?app_id=Spec') do |f|
      puts "静态库版本信息: " + f.read
    end
  rescue StandardError, Timeout::Error, SystemCallError, Errno::ECONNREFUSED
    puts "获取静态库版本信息失败。"
  end
end

#say_after_doneObject



273
274
275
# File 'lib/vfastdev.rb', line 273

def say_after_done
  puts 'FastDev完成。关闭旧的Spec.xcworkspace,打开FastDev/Spec.xcworkspace文件开始开发。'
end

#unzip_libsObject

解压静态库



258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/vfastdev.rb', line 258

def unzip_libs
  puts '解压静态库'
  Dir.chdir(@vs_framework_dir)
# 调用shell解压更简单,ruby不好写解压
  system `
zip_names=$(ls)
for name in $zip_names
do
  unzip -qq $name
  rm $name
done
`
  Dir.chdir('..')
end