# 这里算是一个主要的方法了,主要在这里做了语言上的区分以及采用哪个demo的方法。MessageBank.rb这个文件主要是进行一些在终端打印的提示处理类defrun
@message_bank.welcome_message
platform = self.ask_with_answers("What platform do you want to use?", ["iOS", "macOS"]).to_sym
#这里开始区分语言,因为语言不同,代码不同所以进行调用的脚本也是不同的case platform
when:macos
ConfigureMacOSSwift.perform(configurator:self)
when:ios
framework = self.ask_with_answers("What language do you want to use?", ["Swift", "ObjC"]).to_sym
case framework
when:swift
ConfigureSwift.perform(configurator:self)
#假设我们这边进行了oc版本的选择,然后他开始执行ConfigureIOS下的perform方法进行处理when:objc
ConfigureIOS.perform(configurator:self)
endend
....
end
2、ConfigureiOS.rb
modulePodclassConfigureIOSattr_reader:configuratordefself.perform(options)
new(options).perform
enddefinitialize(options)
@configurator = options.fetch(:configurator)
enddefperform# 这里是选择一些选项,按照我们pod lib create 走下去的话,这些问题都会在命令行进行展示的
keep_demo = configurator.ask_with_answers("Would you like to include a demo application with your library", ["Yes", "No"]).to_sym
framework = configurator.ask_with_answers("Which testing frameworks will you use", ["Specta", "Kiwi", "None"]).to_sym
case framework
when:specta
configurator.add_pod_to_podfile "Specta"
configurator.add_pod_to_podfile "Expecta"
configurator.add_line_to_pch "@import Specta;"
configurator.add_line_to_pch "@import Expecta;"
configurator.set_test_framework("specta", "m", "ios")
when:kiwi
configurator.add_pod_to_podfile "Kiwi"
configurator.add_line_to_pch "@import Kiwi;"
configurator.set_test_framework("kiwi", "m", "ios")
when:none
configurator.set_test_framework("xctest", "m", "ios")
end
snapshots = configurator.ask_with_answers("Would you like to do view based testing", ["Yes", "No"]).to_sym
case snapshots
when:yes
configurator.add_pod_to_podfile "FBSnapshotTestCase"
configurator.add_line_to_pch "@import FBSnapshotTestCase;"if keep_demo == :no
puts " Putting demo application back in, you cannot do view tests without a host application."
keep_demo = :yesendif framework == :specta
configurator.add_pod_to_podfile "Expecta+Snapshots"
configurator.add_line_to_pch "@import Expecta_Snapshots;"endend
prefix = nil
loop do
prefix = configurator.ask("What is your class prefix").upcase
if prefix.include?(' ')
puts 'Your class prefix cannot contain spaces.'.red
elsebreakendend#这里需要利用到ProjectManipulator.rb文件进行demo工程上的修改,这里主要是讲部分信息传了过去,然后进行run方便的调用。
Pod::ProjectManipulator.new({
:configurator => @configurator,
:xcodeproj_path => "templates/ios/Example/PROJECT.xcodeproj",
:platform => :ios,
:remove_demo_project => (keep_demo == :no),
:prefix => prefix
}).run
# There has to be a single file in the Classes dir# or a framework won't be created, which is now default`touch Pod/Classes/ReplaceMe.m``mv ./templates/ios/* ./`# remove podspec for osx`rm ./NAME-osx.podspec`endendend
3、ProjectManipulator.rb
#这里主要用到了xcodeproj这个组件,进行对xcode文件的修改,这里应该才是精髓,这里是涉及到对xcodeproj的使用。defrun
@string_replacements = {
"PROJECT_OWNER" => @configurator.user_name,
"TODAYS_DATE" => @configurator.date,
"TODAYS_YEAR" => @configurator.year,
"PROJECT" => @configurator.pod_name,
"CPD" => @prefix
}
#这个方法是拿到了项目的文件,然后进行文件内容的读取替换
replace_internal_project_settings
@project = Xcodeproj::Project.open(@xcodeproj_path)
#这里是添加.podspec、README.md、LICENSE文件
add_podspec_metadata
#这里是移除了demo工程以及重写Podfile文件
remove_demo_project if @remove_demo_target
@project.save
#这里是进行一些重命名的方法,这里他写的不太好的地方就是是固定类去改变了,例如,可以看到下面是demo中CPDAppDelegate的类名,如果这里可以改一下还是会好一点# unless @remove_demo_target# # change app file prefixes# ["CPDAppDelegate.h", "CPDAppDelegate.m", "CPDViewController.h", "CPDViewController.m"].each do |file|# before = project_folder + "/PROJECT/" + file# next unless File.exists? before# after = project_folder + "/PROJECT/" + file.gsub("CPD", prefix)# File.rename before, after# end# # rename project related files# ["PROJECT-Info.plist", "PROJECT-Prefix.pch", "PROJECT.entitlements"].each do |file|# before = project_folder + "/PROJECT/" + file# next unless File.exists? before# after = project_folder + "/PROJECT/" + file.gsub("PROJECT", @configurator.pod_name)# File.rename before, after# end# end
rename_files
#文件夹的命名修改
rename_project_folder
end#这里部分细节代码可以尝试跑一下看看实现