patch-carthage-project.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. require 'xcodeproj'
  2. project_path = ARGV[0]
  3. project = Xcodeproj::Project.open(project_path)
  4. # 1) Remove targets that we do not want Carthage to build, and set the deployment target to 9.0.
  5. carthage_targets = ["BoringSSL", "CgRPC", "SwiftGRPC", "SwiftProtobuf"]
  6. targets_to_remove = project.targets.select { |target| !carthage_targets.include?(target.name) }
  7. targets_to_remove.each do |target|
  8. target.remove_from_project
  9. end
  10. # 2) Prevent linking of nghttp2 library
  11. project.targets.each do |target|
  12. target.build_configurations.each do |conf|
  13. current_ldflags = target.build_settings(conf.name)["OTHER_LDFLAGS"]
  14. if current_ldflags.is_a? String
  15. target.build_settings(conf.name)["OTHER_LDFLAGS"] = "" if current_ldflags.downcase().include?("nghttp2")
  16. else
  17. cleaned_ldflags = current_ldflags.select { |flag| !flag.downcase().include?("nghttp2") }
  18. target.build_settings(conf.name)["OTHER_LDFLAGS"] = cleaned_ldflags
  19. end
  20. end
  21. end
  22. project.save
  23. # 3) Add SwiftProtobuf to the build actions list
  24. schemePath = Xcodeproj::XCScheme.shared_data_dir(project_path) + "SwiftGRPC-Package.xcscheme"
  25. scheme = Xcodeproj::XCScheme.new(schemePath)
  26. target = project.targets.select { |target| target.name == "SwiftProtobuf" }.first
  27. newBuildAction = Xcodeproj::XCScheme::BuildAction::Entry.new(target)
  28. newBuildAction.build_for_archiving = true
  29. newBuildAction.build_for_profiling = true
  30. newBuildAction.build_for_running = true
  31. newBuildAction.build_for_testing = true
  32. scheme.build_action.add_entry(newBuildAction)
  33. # 4) Add a "Pre-Actions" script to the "BuildAction" of SwiftGRPC-Package.xcscheme.
  34. # The Pre-Actions script will resolve the SPM dependencies and fix the corresponding paths in SwiftGRPC-Carthage.xcodeproj before the BuildAction
  35. buildActions = scheme.build_action.xml_element
  36. preActions = REXML::Element.new("PreActions")
  37. executionAction = REXML::Element.new("ExecutionAction", preActions)
  38. executionAction.add_attribute("ActionType","Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction")
  39. actionContent = REXML::Element.new("ActionContent", executionAction)
  40. actionContent.add_attribute("title", "Run Script")
  41. scriptText = "cd ${PROJECT_DIR}; swift package resolve; ruby fix-carthage-paths.rb SwiftGRPC-Carthage.xcodeproj"
  42. actionContent.add_attribute("scriptText", scriptText)
  43. environmentBuildable = REXML::Element.new("EnvironmentBuildable", actionContent)
  44. buildableReference = REXML::Element.new("BuildableReference", environmentBuildable)
  45. buildableReference.add_attribute("BuildableIdentifier","primary")
  46. buildableReference.add_attribute("BlueprintIdentifier","SwiftProtobuf::SwiftProtobuf")
  47. buildableReference.add_attribute("BuildableName","SwiftProtobuf.framework")
  48. buildableReference.add_attribute("BlueprintName","SwiftProtobuf")
  49. buildableReference.add_attribute("ReferencedContainer","container:SwiftGRPC-Carthage.xcodeproj")
  50. buildActions.unshift(preActions)
  51. scheme.save!