Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby -w
- begin; require 'rubygems'; rescue LoadError; end
- require 'appscript'; include Appscript
- #################################################################################
- # #
- # AutoImplementationFromInterface.rb #
- # #
- # author: Craig Williams cwilliams <at> allancraig <dot> net #
- # created: 2010-03-07 #
- # updated by: Alfred R. Baudisch me <at> ialfred <dot> com #
- # #
- #################################################################################
- # #
- # This program is free software: you can redistribute it and/or modify #
- # it under the terms of the GNU General Public License as published by #
- # the Free Software Foundation, either version 3 of the License, or #
- # (at your option) any later version. #
- # #
- # This program is distributed in the hope that it will be useful, #
- # but WITHOUT ANY WARRANTY; without even the implied warranty of #
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
- # GNU General Public License for more details. #
- # #
- # You should have received a copy of the GNU General Public License #
- # along with this program. If not, see <http://www.gnu.org/licenses/>;. #
- # #
- #################################################################################
- #==============================================================
- # Preferences
- #==============================================================
- SLEEP_TIME = 0.0
- Xcode = app('/Developer/Applications/Xcode.app')
- @use_nonatomic = true
- @position_prop_after_closing_bracket = false
- OS_VERSION = `osascript -e "tell (system info) to get system version"`
- #==============================================================
- # Methods
- #==============================================================
- def find_doc_with_save(file_path)
- docs = Xcode.text_documents.get
- docs.each do |doc|
- path = doc.get.file_name.get
- if file_path == path
- doc.save
- return doc
- end
- end
- return false
- end
- def replace_document_contents(file_path, new_contents, close_file = false)
- Xcode.open(file_path)
- sleep SLEEP_TIME
- basename = File.basename(file_path)
- doc_name = Xcode.text_documents[1].name.get
- if doc_name == basename
- Xcode.text_documents[1].contents.set(new_contents)
- Xcode.text_documents[1].save
- Xcode.text_documents[1].close if close_file
- else
- print "This file '#{doc_name}' is not '#{basename}'"
- return false
- end
- end
- def replace_contents_leopard(file_path, new_contents)
- Xcode.open(file_path)
- sleep SLEEP_TIME
- if doc = find_doc_with_save(file_path)
- doc.contents.set(new_contents)
- return true
- end
- return false
- end
- #==============================================================
- # Checking for .m file existence
- #==============================================================
- # Save the current document Snow Leopard
- Xcode.text_documents[1].save
- path = "%%%{PBXFilePath}%%%"
- doc = find_doc_with_save(path)
- # Continue only if we are starting in the .h file
- if File.extname(path) != '.h'
- print "This is not a header file"
- exit
- end
- parent = File.dirname(path)
- file_name = File.basename(path, '.*')
- mPath = parent + '/' + file_name + '.m'
- # you could check for .mm ext here also
- if !File.exists?(mPath)
- print "File does not exist: #{path}"
- exit
- end
- #==============================================================
- # Converting selection to @property, @synthensize and dealloc
- #==============================================================
- # copy/retain/assign
- implementation = ''
- selection = STDIN.read
- if selection == ''
- find_doc_with_save(path)
- exit
- end
- selection.each do |line|
- # Selection lines must end with ;
- implementation << line[0..-2] << " {\n\t\n}\n"
- end
- #==============================================================
- # Reading and updating the .m file contents
- #==============================================================
- content_added, go_next = false
- mFileContents = IO.read(mPath)
- updatedFileContents = ''
- mFileContents.split("\n").each do |line|
- if line =~ /^@implementation/
- go_next = true
- end
- if !content_added && go_next && line =~ /^[^@synthesize]/
- updatedFileContents << implementation << "\n" << line
- content_added = true
- else
- updatedFileContents << line
- updatedFileContents << "\n"
- end
- end
- #==============================================================
- # Update the Xcode .h and .m files with new content
- #==============================================================
- if OS_VERSION.to_f < 10.6
- exit if replace_contents_leopard(mPath, updatedFileContents.chomp) == false
- sleep SLEEP_TIME
- replace_contents_leopard(path, updatedHFileContents.chomp)
- find_doc_with_save(mPath)
- find_doc_with_save(path)
- else
- exit if replace_document_contents(mPath, updatedFileContents.chomp, true) == false
- sleep SLEEP_TIME
- replace_document_contents(path, updatedHFileContents.chomp)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement