Class: Imp::UnusedSel

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/objcthin.rb

Instance Method Summary collapse

Instance Method Details

#check_file_type(path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/objcthin.rb', line 57

def check_file_type(path)
  pathname = Pathname.new(path)
  unless pathname.exist?
    raise "#{path} not exit!"
  end

  cmd = "/usr/bin/file -b #{path}"
  output = `#{cmd}`

  unless output.include?('Mach-O')
    raise 'input file not mach-o file type'
  end
  puts Rainbow('will begin process...').green
  pathname
end

#find_impl_methods(path) ⇒ Object



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
# File 'lib/objcthin.rb', line 73

def find_impl_methods(path)
  apple_protocols = [
      'tableView:canEditRowAtIndexPath:',
      'commitEditingStyle:forRowAtIndexPath:',
      'tableView:viewForHeaderInSection:',
      'tableView:cellForRowAtIndexPath:',
      'tableView:canPerformAction:forRowAtIndexPath:withSender:',
      'tableView:performAction:forRowAtIndexPath:withSender:',
      'tableView:accessoryButtonTappedForRowWithIndexPath:',
      'tableView:willDisplayCell:forRowAtIndexPath:',
      'tableView:commitEditingStyle:forRowAtIndexPath:',
      'tableView:didEndDisplayingCell:forRowAtIndexPath:',
      'tableView:didEndDisplayingHeaderView:forSection:',
      'tableView:heightForFooterInSection:',
      'tableView:shouldHighlightRowAtIndexPath:',
      'tableView:shouldShowMenuForRowAtIndexPath:',
      'tableView:viewForFooterInSection:',
      'tableView:willDisplayHeaderView:forSection:',
      'tableView:willSelectRowAtIndexPath:',
      'willMoveToSuperview:',
      'scrollViewDidEndScrollingAnimation:',
      'scrollViewDidZoom',
      'scrollViewWillEndDragging:withVelocity:targetContentOffset:',
      'searchBarTextDidEndEditing:',
      'searchBar:selectedScopeButtonIndexDidChange:',
      'shouldInvalidateLayoutForBoundsChange:',
      'textFieldShouldReturn:',
      'numberOfSectionsInTableView:',
      'actionSheet:willDismissWithButtonIndex:',
      'gestureRecognizer:shouldReceiveTouch:',
      'gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:',
      'gestureRecognizer:shouldReceiveTouch:',
      'imagePickerController:didFinishPickingMediaWithInfo:',
      'imagePickerControllerDidCancel:',
      'animateTransition:',
      'animationControllerForDismissedController:',
      'animationControllerForPresentedController:presentingController:sourceController:',
      'navigationController:animationControllerForOperation:fromViewController:toViewController:',
      'navigationController:interactionControllerForAnimationController:',
      'alertView:didDismissWithButtonIndex:',
      'URLSession:didBecomeInvalidWithError:',
      'setDownloadTaskDidResumeBlock:',
      'tabBarController:didSelectViewController:',
      'tabBarController:shouldSelectViewController:',
      'applicationDidReceiveMemoryWarning:',
      'application:didRegisterForRemoteNotificationsWithDeviceToken:',
      'application:didFailToRegisterForRemoteNotificationsWithError:',
      'application:didReceiveRemoteNotification:fetchCompletionHandler:',
      'application:didRegisterUserNotificationSettings:',
      'application:performActionForShortcutItem:completionHandler:',
      'application:continueUserActivity:restorationHandler:'].freeze

  # imp -[class sel]

  sub_patten = /[+|-]\[.+\s(.+)\]/
  patten = /\s*imp\s*(#{sub_patten})/
  sel_set_patten = /set[A-Z].*:$/
  sel_get_patten = /is[A-Z].*/

  output = `/usr/bin/otool -oV #{path}`

  imp = {}

  output.each_line do |line|
    patten.match(line) do |m|
      sub = sub_patten.match(m[0]) do |subm|
        class_and_sel = subm[0]
        sel = subm[1]

        next if sel.start_with?('.')
        next if apple_protocols.include?(sel)
        next if sel_set_patten.match?(sel)
        next if sel_get_patten.match?(sel)

        if imp.has_key?(sel)
          imp[sel] << class_and_sel
        else
          imp[sel] = [class_and_sel]
        end
      end
    end
  end

  imp.sort
end

#find_unused_sel(path, prefix) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/objcthin.rb', line 33

def find_unused_sel(path, prefix)
  check_file_type(path)
  all_sels = find_impl_methods(path)
  used_sel = reference_selectors(path)

  unused_sel = []

  all_sels.each do |sel,class_and_sels|
    unless used_sel.include?(sel)
      unused_sel += class_and_sels
    end
  end

  puts Rainbow('below selector is unused:').red
  if prefix
    unused_sel.select! do |classname_selector|
      current_prefix = classname_selector.byteslice(2, prefix.length)
      current_prefix == prefix
    end
  end

  puts unused_sel
end

#reference_selectors(path) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/objcthin.rb', line 159

def reference_selectors(path)
  patten = /__TEXT:__objc_methname:(.+)/
  output = `/usr/bin/otool -v -s __DATA __objc_selrefs #{path}`

  sels = []
  output.each_line do |line|
    patten.match(line) do |m|
      sels << m[1]
    end
  end

  sels
end