package_examples.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env ruby
  2. require 'fileutils'
  3. require 'xcodeproj'
  4. ##########################
  5. # Helpers
  6. ##########################
  7. def remove_reference_to_realm_xcode_project(workspace_path)
  8. workspace = Xcodeproj::Workspace.new_from_xcworkspace(workspace_path)
  9. file_references = workspace.file_references.reject do |file_reference|
  10. file_reference.path == '../../../Realm.xcodeproj'
  11. end
  12. workspace = Xcodeproj::Workspace.new(nil)
  13. file_references.each { |ref| workspace << ref }
  14. workspace.save_as(workspace_path)
  15. end
  16. def replace_in_file(filepath, pattern, replacement)
  17. contents = File.read(filepath)
  18. File.open(filepath, "w") do |file|
  19. file.puts contents.gsub(pattern, replacement)
  20. end
  21. end
  22. def replace_framework(example, path)
  23. project_path = "#{example}/RealmExamples.xcodeproj"
  24. replace_in_file("#{project_path}/project.pbxproj",
  25. /lastKnownFileType = wrapper.framework; path = (Realm|RealmSwift).framework; sourceTree = BUILT_PRODUCTS_DIR;/,
  26. "lastKnownFileType = wrapper.xcframework; name = \\1.xcframework; path = \"#{path}/\\1.xcframework\"; sourceTree = \"<group>\";")
  27. replace_in_file("#{project_path}/project.pbxproj",
  28. /(Realm|RealmSwift).framework/, "\\1.xcframework")
  29. end
  30. ##########################
  31. # Script
  32. ##########################
  33. base_examples = [
  34. "examples/ios/objc",
  35. "examples/osx/objc",
  36. "examples/tvos/objc",
  37. "examples/ios/swift",
  38. "examples/tvos/swift",
  39. ]
  40. xcode_versions = %w(13.4.1 14.1 14.2 14.3)
  41. # Remove reference to Realm.xcodeproj from all example workspaces.
  42. base_examples.each do |example|
  43. remove_reference_to_realm_xcode_project("#{example}/RealmExamples.xcworkspace")
  44. end
  45. # Make a copy of each Swift example for each Swift version.
  46. base_examples.each do |example|
  47. if example =~ /\/swift$/
  48. xcode_versions.each do |xcode_version|
  49. FileUtils.cp_r example, "#{example}-#{xcode_version}"
  50. end
  51. FileUtils.rm_r example
  52. end
  53. end
  54. # Update the paths to the prebuilt frameworks
  55. replace_framework('examples/ios/objc', '../../../ios-static')
  56. replace_framework('examples/osx/objc', '../../..')
  57. replace_framework('examples/tvos/objc', '../../..')
  58. xcode_versions.each do |xcode_version|
  59. replace_framework("examples/ios/swift-#{xcode_version}", "../../../#{xcode_version}")
  60. replace_framework("examples/tvos/swift-#{xcode_version}", "../../../#{xcode_version}")
  61. end
  62. # Update Playground imports and instructions
  63. xcode_versions.each do |xcode_version|
  64. playground_file = "examples/ios/swift-#{xcode_version}/GettingStarted.playground/Contents.swift"
  65. replace_in_file(playground_file, 'choose RealmSwift', 'choose PlaygroundFrameworkWrapper')
  66. replace_in_file(playground_file,
  67. "import Foundation\n",
  68. "import Foundation\nimport PlaygroundFrameworkWrapper // only necessary to use a binary release of Realm Swift in this playground.\n")
  69. end