Last active
March 4, 2016 06:57
-
-
Save chuganzy/d88d56e9b402153d687a to your computer and use it in GitHub Desktop.
Carthageの面倒な作業をどうにかする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'xcodeproj' | |
require 'colorize' | |
Dir.chdir "#{__dir__}/../" | |
class LibraryUpdateManager | |
attr_accessor :update_carthage, :update_cocoapods, :acknowledgements_path | |
def initialize | |
@update_carthage = true | |
@update_cocoapods = File.exists?('Podfile') | |
end | |
def perform_update | |
begin | |
if @update_carthage | |
puts 'update carthage🏃'.bold | |
_update_carthage | |
puts 'finish updating carthage🍺'.green | |
end | |
if @update_cocoapods | |
puts 'update cocoapods🏃'.bold | |
_update_cocoapods | |
puts 'finish updating cocoapods🍺'.green | |
end | |
if !@acknowledgements_path.nil? | |
puts 'update acknowledgements🏃'.bold | |
_update_acknowledgements | |
puts 'finish updating acknowledgements🍺'.green | |
end | |
puts 'complete update libraries🎉'.green | |
rescue => error | |
puts "#{error.message}😱".red | |
end | |
end | |
private | |
def _update_carthage | |
raise 'carthage update failure' if !system('carthage update --platform iOS') | |
_update_projects | |
end | |
def _update_projects | |
project = Xcodeproj::Project.open(*Dir.glob('*.xcodeproj')) | |
carthage_group = project.groups.find do |group| | |
group.path == 'Carthage' | |
end | |
if !carthage_group | |
raise 'carthage group not found' | |
end | |
existing_file_names = carthage_group.files.map do |file| | |
file.name | |
end | |
new_framework_paths = Dir.glob('Carthage/Build/iOS/*.framework').select do |framework| | |
!existing_file_names.include?(File.basename(framework)) | |
end | |
if new_framework_paths.empty? | |
return | |
end | |
framework_references = new_framework_paths.map do |framework_path| | |
path = File.basename(framework_path) | |
carthage_group.new_file("Build/iOS/#{path}") | |
end | |
project.targets.each do |target| | |
next if target.product_type == 'com.apple.product-type.bundle.unit-test' | |
run_carthage_script_phase = target.build_phases.find do |phase| | |
phase.display_name == 'Run Carthage Script' | |
end | |
next if run_carthage_script_phase.nil? | |
framework_references.each do |reference| | |
target.frameworks_build_phase.add_file_reference(reference) | |
end | |
new_input_paths = new_framework_paths.map do |path| | |
"$(SRCROOT)/#{path}" | |
end | |
input_paths_set = run_carthage_script_phase.input_paths.to_set | |
run_carthage_script_phase.input_paths = input_paths_set.merge(new_input_paths).to_a | |
end | |
project.save | |
end | |
def _update_cocoapods | |
raise 'pod install failure' if !system('pod install') | |
end | |
def _update_acknowledgements | |
cocoapods_acknowledgement = Xcodeproj::PlistHelper.read('Pods/Target Support Files/Pods/Pods-Acknowledgements.plist') | |
carthage_preference_specifiers = Dir.glob('Carthage/Checkouts/**/LICENSE').map do |license| | |
{ | |
:FooterText => IO.read(license, :encoding => 'utf-8'), | |
:Title => File.basename(File.dirname(license)), | |
:Type => 'PSGroupSpecifier', | |
} | |
end | |
key = 'PreferenceSpecifiers' | |
cocoapods_acknowledgement[key].insert(cocoapods_acknowledgement[key].length - 1, *carthage_preference_specifiers) | |
Xcodeproj::PlistHelper.write(cocoapods_acknowledgement, @acknowledgements_path) | |
end | |
end | |
if __FILE__ == $PROGRAM_NAME | |
manager = LibraryUpdateManager.new | |
manager.perform_update | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment