Guest User

Untitled

a guest
Mar 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'bundler/setup'
  3. require 'fileutils'
  4. require 'pathname'
  5. require 'xcodeproj'
  6.  
  7. # Check if argument is empty
  8. if ARGV.last.to_s.empty? then
  9. raise "Xcode Project required, eg: ruby #{__FILE__} foo/bar/Example/Example.xcodeproj"
  10. end
  11.  
  12. # Check if argument is an Xcode project that exists (use File.exist? because it's a directory)
  13. xcode_project_path = ARGV.last.chomp('/').chomp('\\')
  14. if !xcode_project_path.end_with?(".xcodeproj") or !File.exist?(xcode_project_path) then
  15. raise "Argument should be Xcode Project"
  16. end
  17. xcode_project_path = File.expand_path(xcode_project_path)
  18. xcode_project_name = File.basename(File.dirname(xcode_project_path))
  19.  
  20. # Configuration
  21. symlink_instead_of_copy = false
  22. bundle_id = ""
  23.  
  24. ARGV[0...-1].each do |arg|
  25. if arg == '--symlink-instead-of-copy' then
  26. symlink_instead_of_copy = true
  27. elsif arg =~ /--bundle-id=(.*)/ then
  28. bundle_id = $1
  29. end
  30. end
  31.  
  32. puts "Adding Teak Notification Extensions to: #{xcode_project_path}"
  33. xcode_proj = Xcodeproj::Project.open(xcode_project_path)
  34.  
  35. # List of Teak extensions
  36. teak_extensions = [
  37. ["TeakNotificationService", ["MobileCoreServices", "UserNotifications", "UIKit", "SystemConfiguration"]],
  38. ["TeakNotificationContent", ["UserNotifications", "UserNotificationsUI", "AVFoundation", "UIKit", "ImageIO", "CoreGraphics"]]
  39. ]
  40. teak_extensions.each do |service, deps|
  41.  
  42. # Copy our files
  43. puts symlink_instead_of_copy ? "Creating symbolic links (--symlink-instead-of-copy)" : "Copying files..."
  44. target_path = File.join(File.dirname(xcode_project_path), service)
  45. FileUtils.mkdir_p(target_path)
  46.  
  47. # Find or create PBXGroup
  48. product_group = xcode_proj[service] || xcode_proj.new_group(service, service)
  49.  
  50. # Get or create target
  51. target = xcode_proj.native_targets.detect { |e| e.name == service} ||
  52. xcode_proj.new_target(:app_extension, service, :ios, nil, xcode_proj.products_group, :objc)
  53.  
  54. # Add target dependencies
  55. deps.each do |framework|
  56. file_ref = xcode_proj.frameworks_group.new_reference("System/Library/Frameworks/#{framework}.framework")
  57. file_ref.name = "#{framework}.framework"
  58. file_ref.source_tree = 'SDKROOT'
  59. target.frameworks_build_phase.add_file_reference(file_ref, true)
  60. end
  61.  
  62. # Add dependency on libTeak.a
  63. teak_framework_ref = xcode_proj.frameworks_group.new_reference("libTeak.a")
  64. teak_framework_ref.name = "libTeak.a"
  65. teak_framework_ref.source_tree = 'SOURCE_ROOT'
  66. target.frameworks_build_phase.add_file_reference(teak_framework_ref, true)
  67.  
  68. Dir.glob(File.expand_path("#{service}/**/*", File.dirname(__FILE__))).map(&File.method(:realpath)).each do |file|
  69. target_file = File.join(target_path, File.basename(file))
  70. FileUtils.rm_f(target_file)
  71.  
  72. puts "#{file} -> #{target_file}"
  73. if symlink_instead_of_copy then
  74. first = Pathname.new file
  75. second = Pathname.new File.dirname(target_file)
  76. FileUtils.ln_s(first.relative_path_from(second), target_file, :force => true)
  77. else
  78. FileUtils.cp(file, target_file)
  79. end
  80.  
  81. # Find or add file
  82. file_ref = product_group[File.basename(file)] || product_group.new_reference(File.basename(file))
  83.  
  84. # Add *.m files to build phase
  85. if File.extname(file) == ".m" then
  86. target.source_build_phase.add_file_reference(file_ref, true)
  87. end
  88. end
  89.  
  90. # Add Resources build phase
  91. target.resources_build_phase
  92.  
  93. # Assign build configurations
  94. target.build_configurations.each do |config|
  95. build_settings = xcode_proj.native_targets.detect { |e| e.name == xcode_project_name }.build_settings(config.name)
  96. next if not build_settings
  97. config.build_settings = {
  98. :CODE_SIGN_STYLE => "Automatic",
  99. :IPHONEOS_DEPLOYMENT_TARGET => 10.0,
  100. :CODE_SIGN_IDENTITY => build_settings['CODE_SIGN_IDENTITY'],
  101. :DEVELOPMENT_TEAM => build_settings['DEVELOPMENT_TEAM'],
  102. #"CODE_SIGN_IDENTITY[sdk=iphoneos*]" => "iPhone Developer", # Causes parse errors
  103. :LIBRARY_SEARCH_PATHS => [
  104. "$(SRCROOT)/Libraries/Teak/Plugins/iOS" # Unity path
  105. ],
  106. :INFOPLIST_FILE => "#{service}/Info.plist",
  107. :LD_RUNPATH_SEARCH_PATHS => "$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks",
  108. :PRODUCT_BUNDLE_IDENTIFIER => "#{bundle_id}.#{service}",
  109. :PRODUCT_NAME => "$(TARGET_NAME)",
  110. :SKIP_INSTALL => :YES
  111. }
  112. end
  113.  
  114. # Add to native targets
  115. xcode_proj.native_targets.each do |native_target|
  116. next if native_target.to_s != xcode_project_name
  117.  
  118. puts "Adding #{target} as a dependency for #{native_target}"
  119. native_target.add_dependency(target)
  120.  
  121. copy_phase = native_target.build_phases.detect { |e| e.respond_to?(:name) && e.name == "Embed Teak App Extensions" } || native_target.new_copy_files_build_phase("Embed Teak App Extensions")
  122. copy_phase.dst_subfolder_spec = '13'
  123. copy_phase.add_file_reference(target.product_reference, true)
  124. end
  125. end
  126.  
  127. xcode_proj.save
Add Comment
Please, Sign In to add comment