Last active
May 26, 2016 11:18
-
-
Save cSquirrel/5059212efc5e9448f0c652650f87a084 to your computer and use it in GitHub Desktop.
Update ObjC/Swift bridging header
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'FileUtils' | |
require 'tempfile' | |
HEADER_TO_UPDATE = ARGV[0] | |
FOLDER_TO_SCAN = ARGV[1] | |
p "Bridging header to update: #{HEADER_TO_UPDATE}" | |
p "Folder to scan for ObjC headers: #{FOLDER_TO_SCAN}" | |
TEMP_BRIDGING_HEADER_FILE = "bridging-header.h" | |
# List of ObjC header files you want to be excluded, | |
# Example: | |
# EXCLUDE_HEADERS = %w(DonWantThisHeader.h DontWantThisHeaderEither.h) | |
EXCLUDE_HEADERS = %w() | |
# Find all the ObjC header files | |
files = Dir.glob("#{FOLDER_TO_SCAN}/**/*.h") | |
# Chop off the full path from each ObjC header and leave file name with extension | |
files.map! { |objc_header_file| File.basename(objc_header_file)} | |
# Reject all the excluded ObjC header files | |
files.reject!{ |element| EXCLUDE_HEADERS.include? element} | |
# Create import statement from each ObjC header file name | |
files.map! {|objc_header_file| "\#import \"#{objc_header_file}\"\n" } | |
# Sort the list for easy reading and diffs and merging | |
files.sort! | |
# Create temporary file | |
temp_file = Tempfile.new(TEMP_BRIDGING_HEADER_FILE) | |
p "Using temp file: #{temp_file.path}" | |
# This is to allow for custom content in the bridging header | |
# i.e comments, manualy added imports | |
# | |
# The script will read and preserve the bridging header file | |
# up to the line saying "FOLLOWING LINES ARE AUTO-GENERATED" | |
File.open(HEADER_TO_UPDATE, "r") do |file| | |
file.each do |line| | |
temp_file.write(line) | |
break if line.include? "FOLLOWING LINES ARE AUTO-GENERATED" | |
end | |
end | |
# Append all the import statements for ObjC header files | |
files.each { |value| temp_file.write(value) } | |
# All the content is in the temp file | |
temp_file.close | |
# See if generated content is any different from the existing bridging header | |
# This is to prevent from replacing the bridging if there's no change | |
# thanks to that Xcode will not rebuild the whole project if there is no change | |
has_diffs = false | |
IO.popen("diff --ignore-space-change --side-by-side --suppress-common-lines #{temp_file.path} #{HEADER_TO_UPDATE}") { |io| | |
has_diffs = (io.read.length != 0) | |
} | |
# Replace the original bridging header only if has changes | |
FileUtils.mv temp_file.path, HEADER_TO_UPDATE if has_diffs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment