-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcode_secure_bundle_version.rb
executable file
·38 lines (30 loc) · 1.57 KB
/
xcode_secure_bundle_version.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/ruby
#
# This script copies CFBundleShortVersionString in Info.plist to a header file of your choice.
# Add this as a "Run Script" phase to your Xcode build.
# This is a useful security measure to prevent external manipulation of the bundle version in info.plist
# Fail if not run from Xcode
raise "Must be run from Xcode's Run Script Build Phase" unless ENV['XCODE_VERSION_ACTUAL']
# Info.plist
plist_file = "#{ENV['BUILT_PRODUCTS_DIR']}/#{ENV['INFOPLIST_PATH']}"
header_file = 'Classes/defines.h'
# Convert the binary plist to xml based
`/usr/bin/plutil -convert xml1 #{plist_file}`
# Open Info.plist and get the line after the CFBundleShortVersionString, which contains our version number,
# read that line and pull out the value from the XML string
target_line = nil
File.open(plist_file, 'r').each_with_index { |line, line_number| target_line = line_number + 1 if line =~/<key>CFBundleShortVersionString<\/key>/ }
raise "No version number found" if target_line == nil
version = IO.readlines(plist_file)[target_line].scan(/<string>(.*?)<\/string>/)
# Convert back to binary plist
`/usr/bin/plutil -convert binary1 #{plist_file}`
# Copy the bundle version number to the header file
file = IO.readlines(header_file).join
if file =~ /#define kBundleVersion/
file.gsub!(/(#define kBundleVersion\s)(.+)/, "\\1@\"#{version}\"")
else
file << "\n// Secure version number, do not edit manually, handled by script! \n#define kBundleVersion #{version}"
end
File.open(header_file, 'w') {|f| f.puts file}
# Report to the user
puts "CFBundleVersion #{version} copied into #{header_file}"