forked from inket/update_xcode_plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxcode.rb
129 lines (99 loc) · 2.69 KB
/
xcode.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
require_relative 'bundle'
class Xcode < Bundle
attr_accessor :signed
# Hardcoded paths in case mdfind is not working because Spotlight is disabled
DEFAULT_XCODE_PATHS = [
"/Applications/Xcode.app",
"/Applications/Xcode-beta.app",
"/Applications/Xcode-unsigned.app"
]
XCODE_BUNDLE_IDENTIFIER = "com.apple.dt.Xcode"
def self.find_xcodes
output = `mdfind kMDItemCFBundleIdentifier = "#{XCODE_BUNDLE_IDENTIFIER}"`
paths = output.lines + DEFAULT_XCODE_PATHS
paths.map(&:strip).uniq.collect do |xcode_path|
Xcode.from_bundle(xcode_path)
end.compact.keep_if(&:valid?)
end
def self.from_bundle(path)
xcode = new(path)
xcode.valid? ? xcode : nil
end
def valid?
is_app = path.end_with?('.app')
has_info = File.exist?(info_path)
return false unless is_app && has_info
bundle_identifier == XCODE_BUNDLE_IDENTIFIER
end
def signed?
if signed.nil?
self.signed = `codesign -dv "#{path}" 2>/dev/null` &&
$CHILD_STATUS.exitstatus == 0
end
signed
end
def restorable?
binary_restorable? || xcodebuild_restorable?
end
def binary_restorable?
File.exist?("#{binary_path}.signed")
end
def xcodebuild_restorable?
File.exist?("#{xcodebuild_path}.signed")
end
def unsign_binary!
unsign!(binary_path)
end
def unsign_xcodebuild!
unsign!(xcodebuild_path)
end
def restore_binary!
restore!(binary_path)
end
def restore_xcodebuild!
restore!(xcodebuild_path)
end
def uuid
defaults_read('DVTPlugInCompatibilityUUID')
end
def to_s
unless signed.nil?
codesign_status = signed ? ' [Signed]' : ' [Unsigned]'
end
"Xcode (#{version})#{codesign_status}: #{path}"
end
def detailed_description
"Xcode (#{version}) [#{uuid}]: #{path}"
end
private
def binary_path
"#{path}/Contents/MacOS/Xcode"
end
def xcodebuild_path
"#{path}/Contents/Developer/usr/bin/xcodebuild"
end
def unsign_path
lib_path = File.expand_path(File.dirname(__FILE__))
"#{lib_path}/bin/unsign"
end
def unsign!(target)
unsigned_target = "#{target}.unsigned"
signed_target = "#{target}.signed"
CLI.chown_if_required(File.dirname(target)) do
`#{unsign_path} "#{target}"` &&
$CHILD_STATUS.exitstatus == 0
File.exist?(unsigned_target) &&
FileUtils.mv(target, signed_target) &&
File.exist?(signed_target) &&
FileUtils.mv(unsigned_target, target)
end
end
def restore!(target)
signed_target = "#{target}.signed"
CLI.chown_if_required(File.dirname(target)) do
File.exist?(signed_target) &&
File.exist?(target) &&
FileUtils.mv(signed_target, target)
end
end
end