Advertisement
Guest User

XCode implementation from interface

a guest
Aug 23rd, 2010
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.65 KB | None | 0 0
  1. #!/usr/bin/env ruby -w
  2.  
  3. begin; require 'rubygems'; rescue LoadError; end
  4. require 'appscript'; include Appscript
  5.  
  6. #################################################################################
  7. #                                                                               #
  8. #     AutoImplementationFromInterface.rb                                        #
  9. #                                                                               #
  10. #     author:   Craig Williams  cwilliams <at> allancraig <dot> net             #
  11. #     created:  2010-03-07                                                      #
  12. #     updated by: Alfred R. Baudisch       me <at> ialfred <dot> com            #
  13. #                                                                               #
  14. #################################################################################
  15. #                                                                               #
  16. #     This program is free software: you can redistribute it and/or modify      #
  17. #     it under the terms of the GNU General Public License as published by      #
  18. #     the Free Software Foundation, either version 3 of the License, or         #
  19. #     (at your option) any later version.                                       #
  20. #                                                                               #
  21. #     This program is distributed in the hope that it will be useful,           #
  22. #     but WITHOUT ANY WARRANTY; without even the implied warranty of            #
  23. #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             #
  24. #     GNU General Public License for more details.                              #
  25. #                                                                               #
  26. #     You should have received a copy of the GNU General Public License         #
  27. #     along with this program.  If not, see <http://www.gnu.org/licenses/>;.     #
  28. #                                                                               #
  29. #################################################################################
  30.  
  31.  
  32. #==============================================================
  33. # Preferences
  34. #==============================================================
  35. SLEEP_TIME = 0.0
  36. Xcode = app('/Developer/Applications/Xcode.app')
  37. @use_nonatomic = true
  38. @position_prop_after_closing_bracket = false
  39.  
  40. OS_VERSION = `osascript -e "tell (system info) to get system version"`
  41.  
  42. #==============================================================
  43. # Methods
  44. #==============================================================
  45. def find_doc_with_save(file_path)
  46. docs = Xcode.text_documents.get
  47. docs.each do |doc|
  48. path = doc.get.file_name.get
  49. if file_path == path
  50. doc.save
  51. return doc
  52. end
  53. end
  54. return false
  55. end
  56.  
  57. def replace_document_contents(file_path, new_contents, close_file = false)
  58. Xcode.open(file_path)
  59. sleep SLEEP_TIME
  60. basename = File.basename(file_path)
  61. doc_name = Xcode.text_documents[1].name.get
  62. if doc_name == basename
  63. Xcode.text_documents[1].contents.set(new_contents)
  64. Xcode.text_documents[1].save
  65. Xcode.text_documents[1].close if close_file
  66. else
  67. print "This file '#{doc_name}' is not '#{basename}'"
  68. return false
  69. end
  70. end
  71.  
  72. def replace_contents_leopard(file_path, new_contents)
  73. Xcode.open(file_path)
  74. sleep SLEEP_TIME
  75. if doc = find_doc_with_save(file_path)
  76. doc.contents.set(new_contents)
  77. return true
  78. end
  79. return false
  80. end
  81.  
  82. #==============================================================
  83. # Checking for .m file existence
  84. #==============================================================
  85. # Save the current document Snow Leopard
  86.  
  87. Xcode.text_documents[1].save
  88.  
  89. path = "%%%{PBXFilePath}%%%"
  90. doc = find_doc_with_save(path)
  91.  
  92. # Continue only if we are starting in the .h file
  93. if File.extname(path) != '.h'
  94. print "This is not a header file"
  95. exit
  96. end
  97.  
  98. parent    = File.dirname(path)
  99. file_name = File.basename(path, '.*')
  100. mPath     = parent + '/' + file_name + '.m'
  101.  
  102. # you could check for .mm ext here also
  103. if !File.exists?(mPath)
  104. print "File does not exist: #{path}"
  105. exit
  106. end
  107.  
  108.  
  109. #==============================================================
  110. # Converting selection to @property, @synthensize and dealloc
  111. #==============================================================
  112. # copy/retain/assign
  113.  
  114. implementation = ''
  115.  
  116. selection = STDIN.read
  117. if selection == ''
  118. find_doc_with_save(path)
  119. exit
  120. end
  121.  
  122. selection.each do |line|
  123. # Selection lines must end with ;
  124. implementation << line[0..-2] << " {\n\t\n}\n"
  125. end
  126.  
  127.  
  128. #==============================================================
  129. # Reading and updating the .m file contents
  130. #==============================================================
  131. content_added, go_next = false
  132.  
  133. mFileContents = IO.read(mPath)
  134. updatedFileContents = ''
  135. mFileContents.split("\n").each do |line|
  136.   if line =~ /^@implementation/
  137.     go_next = true
  138.   end
  139.  
  140.   if !content_added && go_next && line =~ /^[^@synthesize]/
  141.     updatedFileContents << implementation << "\n" << line
  142.     content_added = true
  143.   else
  144.     updatedFileContents << line
  145.     updatedFileContents << "\n"
  146.   end
  147. end
  148.  
  149.  
  150. #==============================================================
  151. # Update the Xcode .h and .m files with new content
  152. #==============================================================
  153. if OS_VERSION.to_f < 10.6
  154. exit if replace_contents_leopard(mPath, updatedFileContents.chomp) == false
  155. sleep SLEEP_TIME
  156. replace_contents_leopard(path, updatedHFileContents.chomp)
  157. find_doc_with_save(mPath)
  158. find_doc_with_save(path)
  159. else
  160. exit if replace_document_contents(mPath, updatedFileContents.chomp, true) == false
  161. sleep SLEEP_TIME
  162. replace_document_contents(path, updatedHFileContents.chomp)
  163. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement