Class: Pod::Generator::CopyXCFrameworksScript

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods/generator/copy_xcframework_script.rb

Instance Attribute Summary collapse

Private Helpers collapse

Instance Method Summary collapse

Constructor Details

#initialize(xcframeworks, sandbox_root, platform) ⇒ CopyXCFrameworksScript

Creates a script for copying XCFramework slcies into an intermediate build directory

Parameters:

  • xcframeworks (Array<Pod::Xcode::XCFramework>)

    the list of xcframeworks to copy

  • sandbox_root (Pathname)

    the root of the Sandbox into which this script will be installed

  • platform (Platform)

    the platform of the target for which this script will be run


29
30
31
32
33
# File 'lib/cocoapods/generator/copy_xcframework_script.rb', line 29

def initialize(xcframeworks, sandbox_root, platform)
  @xcframeworks = xcframeworks
  @sandbox_root = sandbox_root
  @platform = platform
end

Instance Attribute Details

#platformPlatform (readonly)

Returns the platform of the target for which this script will run.

Returns:

  • (Platform)

    the platform of the target for which this script will run


16
17
18
# File 'lib/cocoapods/generator/copy_xcframework_script.rb', line 16

def platform
  @platform
end

#sandbox_rootPathname (readonly)

Returns the root directory of the sandbox.

Returns:

  • (Pathname)

    the root directory of the sandbox


12
13
14
# File 'lib/cocoapods/generator/copy_xcframework_script.rb', line 12

def sandbox_root
  @sandbox_root
end

#xcframeworksArray<Pod::Xcode::XCFramework> (readonly)

Returns List of xcframeworks to copy.

Returns:


8
9
10
# File 'lib/cocoapods/generator/copy_xcframework_script.rb', line 8

def xcframeworks
  @xcframeworks
end

Class Method Details

.dsym_folder(xcframework_path) ⇒ Array<Pathname>

Returns all found .dSYM paths.

Parameters:

  • xcframework_path (Pathname)

    the base path of the .xcframework bundle

Returns:

  • (Array<Pathname>)

    all found .dSYM paths


236
237
238
239
240
241
# File 'lib/cocoapods/generator/copy_xcframework_script.rb', line 236

def dsym_folder(xcframework_path)
  basename = File.basename(xcframework_path, '.xcframework')
  dsym_basename = basename + '.dSYMs'
  path = xcframework_path.dirname + dsym_basename
  Pathname.new(path) if File.directory?(path)
end

Instance Method Details

#generateString

Returns The contents of the embed frameworks script.

Returns:

  • (String)

    The contents of the embed frameworks script.


51
52
53
# File 'lib/cocoapods/generator/copy_xcframework_script.rb', line 51

def generate
  script
end

#install_xcframework_args(xcframework, slices) ⇒ Object (private)


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/cocoapods/generator/copy_xcframework_script.rb', line 213

def install_xcframework_args(xcframework, slices)
  root = xcframework.path
  args = [shell_escape("${PODS_ROOT}/#{root.relative_path_from(sandbox_root)}")]
  args << shell_escape(xcframework.name)
  is_framework = xcframework.build_type.framework?
  args << shell_escape(is_framework ? 'framework' : 'library')
  slices.each do |slice|
    args << if is_framework
              shell_escape(slice.path.relative_path_from(root))
            else
              # We don't want the path to the library binary, we want the dir that contains it
              shell_escape(slice.path.dirname.relative_path_from(root))
            end
  end
  args.join(' ')
end

#save_as(pathname) ⇒ void

This method returns an undefined value.

Saves the resource script to the given pathname.

Parameters:

  • pathname (Pathname)

    The path where the embed frameworks script should be saved.


42
43
44
45
46
47
# File 'lib/cocoapods/generator/copy_xcframework_script.rb', line 42

def save_as(pathname)
  pathname.open('w') do |file|
    file.puts(script)
  end
  File.chmod(0o755, pathname.to_s)
end

#scriptString (private)

Returns The contents of the prepare artifacts script.

Returns:

  • (String)

    The contents of the prepare artifacts script.


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/cocoapods/generator/copy_xcframework_script.rb', line 61

def script
  script = <<-SH.strip_heredoc
#{Pod::Generator::ScriptPhaseConstants::DEFAULT_SCRIPT_PHASE_HEADER}

#{Pod::Generator::ScriptPhaseConstants::RSYNC_PROTECT_TMP_FILES}

copy_dir()
{
  local source="$1"
  local destination="$2"

  # Use filter instead of exclude so missing patterns don't throw errors.
  echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter \\"- CVS/\\" --filter \\"- .svn/\\" --filter \\"- .git/\\" --filter \\"- .hg/\\" \\"${source}\\" \\"${destination}\\""
  rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" "${source}" "${destination}"
}

SELECT_SLICE_RETVAL=""

select_slice() {
  local paths=("[email protected]")
  # Locate the correct slice of the .xcframework for the current architectures
  local target_path=""

  # Split archs on space so we can find a slice that has all the needed archs
  local target_archs=$(echo $ARCHS | tr " " "\\n")

  local target_variant=""
  if [[ "$PLATFORM_NAME" == *"simulator" ]]; then
    target_variant="simulator"
  fi
  if [[ ! -z ${EFFECTIVE_PLATFORM_NAME+x} && "$EFFECTIVE_PLATFORM_NAME" == *"maccatalyst" ]]; then
    target_variant="maccatalyst"
  fi
  for i in ${!paths[@]}; do
    local matched_all_archs="1"
    for target_arch in $target_archs
    do
if ! [[ "${paths[$i]}" == *"$target_variant"* ]]; then
  matched_all_archs="0"
  break
fi

# This regex matches all possible variants of the arch in the folder name:
# Let's say the folder name is: ios-armv7_armv7s_arm64_arm64e/CoconutLib.framework
# We match the following: -armv7_, _armv7s_, _arm64_ and _arm64e/.
# If we have a specific variant: ios-i386_x86_64-simulator/CoconutLib.framework
# We match the following: -i386_ and _x86_64-
local target_arch_regex="[_\\-]${target_arch}[\\/_\\-]"
if ! [[ "${paths[$i]}" =~ $target_arch_regex ]]; then
  matched_all_archs="0"
  break
fi
    done

    if [[ "$matched_all_archs" == "1" ]]; then
# Found a matching slice
echo "Selected xcframework slice ${paths[$i]}"
SELECT_SLICE_RETVAL=${paths[$i]}
break
    fi
  done
}

install_library() {
  local source="$1"
  local name="$2"
  local destination="#{Target::BuildSettings::XCFRAMEWORKS_BUILD_DIR_VARIABLE}/${name}"

  # Libraries can contain headers, module maps, and a binary, so we'll copy everything in the folder over

  local source="$binary"
  echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter \\"- CVS/\\" --filter \\"- .svn/\\" --filter \\"- .git/\\" --filter \\"- .hg/\\" \\"${source}/*\\" \\"${destination}\\""
  rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" "${source}/*" "${destination}"
}

# Copies a framework to derived data for use in later build phases
install_framework()
{
  local source="$1"
  local name="$2"
  local destination="#{Pod::Target::BuildSettings::XCFRAMEWORKS_BUILD_DIR_VARIABLE}/${name}"

  if [ ! -d "$destination" ]; then
    mkdir -p "$destination"
  fi

  copy_dir "$source" "$destination"
  echo "Copied $source to $destination"
}

install_xcframework_library() {
  local basepath="$1"
  local name="$2"
  local paths=("[email protected]")

  # Locate the correct slice of the .xcframework for the current architectures
  select_slice "${paths[@]}"
  local target_path="$SELECT_SLICE_RETVAL"
  if [[ -z "$target_path" ]]; then
    echo "warning: [CP] Unable to find matching .xcframework slice in '${paths[@]}' for the current build architectures ($ARCHS)."
    return
  fi

  install_framework "$basepath/$target_path" "$name"
}

install_xcframework() {
  local basepath="$1"
  local name="$2"
  local package_type="$3"
  local paths=("[email protected]")

  # Locate the correct slice of the .xcframework for the current architectures
  select_slice "${paths[@]}"
  local target_path="$SELECT_SLICE_RETVAL"
  if [[ -z "$target_path" ]]; then
    echo "warning: [CP] Unable to find matching .xcframework slice in '${paths[@]}' for the current build architectures ($ARCHS)."
    return
  fi
  local source="$basepath/$target_path"

  local destination="#{Pod::Target::BuildSettings::XCFRAMEWORKS_BUILD_DIR_VARIABLE}/${name}"

  if [ ! -d "$destination" ]; then
    mkdir -p "$destination"
  fi

  if [[ "$package_type" == "library" ]]; then
    # Libraries can contain headers, module maps, and a binary, so we'll copy everything in the folder over
    copy_dir "$source/" "$destination"
  elif [[ "$package_type" == "framework" ]]; then
    copy_dir "$source" "$destination"
  fi
  echo "Copied $source to $destination"
}

  SH
  xcframeworks.each do |xcframework|
    slices = xcframework.slices.select { |f| f.platform.symbolic_name == platform.symbolic_name }
    next if slices.empty?
    args = install_xcframework_args(xcframework, slices)
    script << "install_xcframework #{args}\n"
  end

  script << "\n" unless xcframeworks.empty?
  script
end

#shell_escape(value) ⇒ Object (private)


209
210
211
# File 'lib/cocoapods/generator/copy_xcframework_script.rb', line 209

def shell_escape(value)
  "\"#{value}\""
end