Skip to content

Instantly share code, notes, and snippets.

@STAR-ZERO
Last active June 12, 2023 10:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save STAR-ZERO/9066120 to your computer and use it in GitHub Desktop.
Save STAR-ZERO/9066120 to your computer and use it in GitHub Desktop.
UnityのiOSビルド時にFrameworkを追加する
#!/usr/bin/env ruby
require 'xcodeproj'
require 'fileutils'
PROJECT = 'Unity-iPhone'
TARGET = 'Unity-iPhone'
LIBRARY = 'Libraries'
# システムFramework追加
def add_frameworks(project, names, optional = false)
project.targets.each do |target|
next unless TARGET == target.name
build_phase = target.frameworks_build_phase
framework_group = project.frameworks_group
names.each do |name|
next if exist_framework?(build_phase, name)
path = "System/Library/Frameworks/#{name}.framework"
file_ref = framework_group.new_reference(path)
file_ref.name = "#{name}.framework"
file_ref.source_tree = 'SDKROOT'
build_file = build_phase.add_file_reference(file_ref)
if optional
build_file.settings = { 'ATTRIBUTES' => ['Weak'] }
end
end
end
end
# 外部Framework追加
def add_external_frameworks(project, names)
project.targets.each do |target|
next unless TARGET == target.name
target.build_configurations.each do |configuration|
# Framework Search Pathsを設定
configuration.build_settings['FRAMEWORK_SEARCH_PATHS'] = configuration.build_settings['LIBRARY_SEARCH_PATHS']
end
build_phase = target.frameworks_build_phase
library_group = project.main_group.children.find {|child| child.path == LIBRARY}
names.each do |name|
next if exist_framework?(build_phase, name)
copy_library(name)
path = "#{LIBRARY}/#{name}.framework"
file_ref = library_group.new_reference(path)
file_ref.name = "#{name}.framework"
file_ref.source_tree = 'SOURCE_ROOT'
build_phase.add_file_reference(file_ref)
end
end
end
# Framework追加済みか
def exist_framework?(build_phase, name)
build_phase.files.each do |file|
return true if file.file_ref.name == "#{name}.framework"
end
false
end
# 外部FrameworkをUnityのディレクトリからXcodeのディレクトリへコピー
def copy_library(name)
from = File.expand_path("../../#{LIBRARY}/#{name}.framework", __FILE__)
to = "#{ARGV[0]}/#{LIBRARY}/#{name}.framework"
FileUtils.copy_entry(from, to)
end
project_path = ARGV[0] + "/#{PROJECT}.xcodeproj"
project = Xcodeproj::Project.new(project_path)
project.initialize_from_file
## 例
# require
add_frameworks(project, ["CoreMedia", "Security"])
# optional
add_frameworks(project, ["AdSupport"], true)
# 外部Framework
add_external_frameworks(project, ["Hoge"])
project.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment