Module: SecureKeys::Swift::Xcodeproj

Defined in:
lib/core/utils/swift/xcodeproj.rb

Class Method Summary collapse

Class Method Details

.add_framework_search_path(xcodeproj_target:, configurations: %w[Debug Release])) ⇒ Object

Add the SecureKeys XCFramework to the Xcodeproj target build settings

Parameters:

  • target_name (String)

    The target name to add the XCFramework

  • configurations (Array<String>) (defaults to: %w[Debug Release]))

    The configurations to add the XCFramework



14
15
16
17
18
19
# File 'lib/core/utils/swift/xcodeproj.rb', line 14

def add_framework_search_path(xcodeproj_target:, configurations: %w[Debug Release])
  configurations.each do |config|
    paths = ['$(inherited)', "$(SRCROOT)/#{xcframework_relative_path}"]
    xcodeproj_target.build_settings(config)['FRAMEWORK_SEARCH_PATHS'] = paths
  end
end

.add_xcframework_to_build_phases(xcodeproj:, xcodeproj_target:) ⇒ Object

Add the SecureKeys XCFramework to the Xcodeproj target build phases

Parameters:

  • xcodeproj (Xcodeproj::Project)

    The Xcodeproj to add the XCFramework

  • xcodeproj_target (Xcodeproj)

    The Xcodeproj target to add the XCFramework



24
25
26
27
28
# File 'lib/core/utils/swift/xcodeproj.rb', line 24

def add_xcframework_to_build_phases(xcodeproj:, xcodeproj_target:)
  Core::Console::Logger.crash!(message: "The xcodeproj #{xcodeproj} already have the #{XCFRAMEWORK_DIRECTORY}") if xcodeproj_has_secure_keys_xcframework?(xcodeproj:)
  xcframework_reference = xcodeproj.frameworks_group.new_file(xcframework_relative_path)
  xcodeproj_target.frameworks_build_phase.add_file_reference(xcframework_reference)
end

.remove_xcframework(xcodeproj:, xcodeproj_target:, xcframework_path:) ⇒ Object

Remove all references to SecureKeys.xcframework from the Xcodeproj

Parameters:

  • xcodeproj (Xcodeproj::Project)

    The Xcodeproj to remove the XCFramework

  • xcodeproj_target (Xcodeproj::Project::Object::PBXNativeTarget)

    The target where the XCFramework is linked

  • xcframework_path (String)

    The XCFramework path to remove



52
53
54
55
56
57
58
59
# File 'lib/core/utils/swift/xcodeproj.rb', line 52

def remove_xcframework(xcodeproj:, xcodeproj_target:, xcframework_path:)
  xcframework_filename = File.basename(xcframework_path)

  remove_xcframework_from_build_phase(xcodeproj_target:, xcframework_filename:)
  remove_xcframework_file_references(xcodeproj:, xcframework_filename:)
  remove_xcframework_from_groups(xcodeproj:, xcframework_filename:)
  remove_xcframework_from_search_paths(xcodeproj_target:, xcframework_filename:)
end

.remove_xcframework_file_references(xcodeproj:, xcframework_filename:) ⇒ Object

Remove the XCFramework from the file references

Parameters:

  • xcodeproj (Xcodeproj::Project)

    The Xcodeproj to remove the XCFramework

  • xcframework_filename (String)

    The XCFramework filename to remove



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/core/utils/swift/xcodeproj.rb', line 98

def remove_xcframework_file_references(xcodeproj:, xcframework_filename:)
  file_references = xcodeproj.files.select { |file| file.path&.include?(xcframework_filename) }
  file_references.each do |file_ref|
    file_ref.remove_from_project
    Core::Console::Logger.verbose(message: "Removed #{xcframework_filename} file reference")
  end

  # Ensure no orphaned references remain
  xcodeproj.objects.select { |obj| obj.isa == 'PBXFileReference' && obj.path&.include?(xcframework_filename) }.each(&:remove_from_project)
  xcodeproj.objects.select { |obj| obj.isa == 'PBXBuildFile' && obj.file_ref&.path&.include?(xcframework_filename) }.each(&:remove_from_project)
end

.remove_xcframework_from_build_phase(xcodeproj_target:, xcframework_filename:) ⇒ Object

Remove the XCFramework from the “Link Binary With Libraries” build phase

Parameters:

  • xcodeproj_target (Xcodeproj::Project::Object::PBXNativeTarget)

    The target where the XCFramework is linked

  • xcframework_filename (String)

    The XCFramework filename to remove



84
85
86
87
88
89
90
91
92
93
# File 'lib/core/utils/swift/xcodeproj.rb', line 84

def remove_xcframework_from_build_phase(xcodeproj_target:, xcframework_filename:)
  build_phase_files = xcodeproj_target.frameworks_build_phase.files.select do |file|
    file.file_ref&.path&.include?(xcframework_filename)
  end

  build_phase_files.each do |file|
    file.remove_from_project
    Core::Console::Logger.verbose(message: "Removed #{xcframework_filename} from Link Binary With Libraries in target #{xcodeproj_target.name}")
  end
end

.remove_xcframework_from_groups(xcodeproj:, xcframework_filename:) ⇒ Object

Remove the XCFramework from groups (e.g., Frameworks Group)

Parameters:

  • xcodeproj (Xcodeproj::Project)

    The Xcodeproj to remove the XCFramework

  • xcframework_filename (String)

    The XCFramework filename to remove



113
114
115
116
117
118
119
120
121
# File 'lib/core/utils/swift/xcodeproj.rb', line 113

def remove_xcframework_from_groups(xcodeproj:, xcframework_filename:)
  frameworks_group = xcodeproj.frameworks_group
  group_references = frameworks_group.files.select { |file| file.path&.include?(xcframework_filename) }

  group_references.each do |file_ref|
    frameworks_group.remove_reference(file_ref)
    Core::Console::Logger.verbose(message: "Removed #{xcframework_filename} from Frameworks group")
  end
end

.remove_xcframework_from_search_paths(xcodeproj_target:, xcframework_filename:) ⇒ Object

Remove the XCFramework from FRAMEWORK_SEARCH_PATHS

Parameters:

  • xcodeproj_target (Xcodeproj::Project::Object::PBXNativeTarget)

    The target where the XCFramework is linked

  • xcframework_filename (String)

    The XCFramework filename to remove



126
127
128
129
130
131
132
# File 'lib/core/utils/swift/xcodeproj.rb', line 126

def remove_xcframework_from_search_paths(xcodeproj_target:, xcframework_filename:)
  xcodeproj_target.build_configurations.each do |config|
    framework_search_paths = config.build_settings['FRAMEWORK_SEARCH_PATHS'] || []
    new_search_paths = framework_search_paths.reject { |path| path.include?(xcframework_filename) }
    config.build_settings['FRAMEWORK_SEARCH_PATHS'] = new_search_paths unless framework_search_paths == new_search_paths
  end
end

.xcframework_relative_pathPathname

Get the XCFramework relative path

Returns:

  • (Pathname)

    The XCFramework relative path



63
64
65
66
# File 'lib/core/utils/swift/xcodeproj.rb', line 63

def xcframework_relative_path
  Pathname.new(Globals.secure_keys_xcframework_path)
          .relative_path_from(Pathname.new(Globals.xcodeproj_path).dirname)
end

.xcodeprojXcodeproj

Get the Xcodeproj

Returns:



44
45
46
# File 'lib/core/utils/swift/xcodeproj.rb', line 44

def xcodeproj
  ::Xcodeproj::Project.open(Globals.xcodeproj_path)
end

.xcodeproj_has_secure_keys_xcframework?(xcodeproj:) ⇒ Bool

Check if the Xcode project has the secure keys XCFramework

Parameters:

  • xcodeproj (Xcodeproj::Project)

    The Xcode project

Returns:

  • (Bool)

    true if the Xcode project has the secure keys XCFramework



71
72
73
74
75
76
77
78
79
# File 'lib/core/utils/swift/xcodeproj.rb', line 71

def xcodeproj_has_secure_keys_xcframework?(xcodeproj:)
  xcodeproj.targets.any? do |target|
    target.frameworks_build_phase.files.any? do |file|
      return false if file.file_ref.nil?

      file.file_ref.path.include?(Globals.secure_keys_xcframework_path)
    end
  end
end

.xcodeproj_target_by_target_name(xcodeproj:, target_name:) ⇒ Xcodeproj

Get the Xcodeproj target by target name

Parameters:

  • xcodeproj (Xcodeproj::Project)

    The Xcodeproj to get the target

  • target_name (String)

    The target name to get

Returns:

Raises:

  • (StandardError)

    If the target was not found



35
36
37
38
39
40
# File 'lib/core/utils/swift/xcodeproj.rb', line 35

def xcodeproj_target_by_target_name(xcodeproj:, target_name:)
  xcodeproj_target = xcodeproj.targets.find { |target| target.name.eql?(target_name) }
  Core::Console::Logger.crash!(message: "The target #{target_name} was not found") if xcodeproj_target.nil?

  xcodeproj_target
end