package_examples.rb 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 set_framework_search_path(project_path, search_path)
  17. project = Xcodeproj::Project.open(project_path)
  18. project.build_configuration_list.set_setting("FRAMEWORK_SEARCH_PATHS", search_path)
  19. project.save
  20. end
  21. def replace_in_file(filepath, pattern, replacement)
  22. contents = File.read(filepath)
  23. File.open(filepath, "w") do |file|
  24. file.puts contents.gsub(pattern, replacement)
  25. end
  26. end
  27. ##########################
  28. # Script
  29. ##########################
  30. base_examples = [
  31. "examples/ios/objc",
  32. "examples/osx/objc",
  33. "examples/tvos/objc",
  34. "examples/ios/swift",
  35. "examples/tvos/swift",
  36. ]
  37. xcode_versions = %w(10.0 10.1 10.2.1 10.3 11.0)
  38. # Remove reference to Realm.xcodeproj from all example workspaces.
  39. base_examples.each do |example|
  40. remove_reference_to_realm_xcode_project("#{example}/RealmExamples.xcworkspace")
  41. end
  42. # Make a copy of each Swift example for each Swift version.
  43. base_examples.each do |example|
  44. if example =~ /\/swift$/
  45. xcode_versions.each do |xcode_version|
  46. FileUtils.cp_r example, "#{example}-#{xcode_version}"
  47. end
  48. FileUtils.rm_r example
  49. end
  50. end
  51. framework_directory_for_example = {
  52. 'examples/ios/objc' => '../../../ios/static',
  53. 'examples/osx/objc' => '../../../osx',
  54. 'examples/tvos/objc' => '../../../tvos'
  55. }
  56. xcode_versions.each do |xcode_version|
  57. framework_directory_for_example["examples/ios/swift-#{xcode_version}"] = "../../../ios/swift-#{xcode_version}"
  58. framework_directory_for_example["examples/tvos/swift-#{xcode_version}"] = "../../../tvos/swift-#{xcode_version}"
  59. end
  60. # Update the paths to the prebuilt frameworks
  61. framework_directory_for_example.each do |example, framework_directory|
  62. project_path = "#{example}/RealmExamples.xcodeproj"
  63. replace_in_file("#{project_path}/project.pbxproj", /path = (Realm|RealmSwift).framework; sourceTree = BUILT_PRODUCTS_DIR;/, "path = \"#{framework_directory}/\\1.framework\"; sourceTree = SOURCE_ROOT;")
  64. set_framework_search_path(project_path, framework_directory)
  65. end
  66. # Update Playground imports and instructions
  67. xcode_versions.each do |xcode_version|
  68. playground_file = "examples/ios/swift-#{xcode_version}/GettingStarted.playground/Contents.swift"
  69. replace_in_file(playground_file, 'choose RealmSwift', 'choose PlaygroundFrameworkWrapper')
  70. replace_in_file(playground_file,
  71. "import Foundation\n",
  72. "import Foundation\nimport PlaygroundFrameworkWrapper // only necessary to use a binary release of Realm Swift in this playground.\n")
  73. end
  74. # Update RubyMotion sample
  75. replace_in_file('examples/ios/rubymotion/Simple/Rakefile', '/build/ios-', '/ios/')