# find all lzs files require 'ftools' require 'tempfile' NUM_LINES_BACKWARDS = 2 def files_by_extension( lpshome, extension ) Dir.glob("#{lpshome}/**/*.#{extension}") end # insert an lzxtype event annotation before the # event declaration def annotate_file( f, event ) # find the index of the line with the event declaration current_file_contents = IO.readlines( f ) tf = File.new( f + ".tmp", "w" ) current_file_contents.each_with_index { | l, index | if (l =~ %r{^var #{event} = LzDeclaredEvent}) then # puts "found event #{event} at line #{index} in file #{f}" tf.write( current_file_contents.slice( 0..(index-1) ) ) tf.write( "/** @lzxtype event */\n" ) tf.write( current_file_contents.slice( index..current_file_contents.length ) ) end } end all_lzs_files = files_by_extension("/Users/ben/src/svn/openlaszlo/trunk", "lzs"); files_needing_annotations = []; all_lzs_files.each do | f | line_entries = IO.readlines( f ) line_entries.each_with_index do | l, index | if l =~ %r{var (\w*) = LzDeclaredEvent} then event = $1 found_annotation = false exited_comment = false # search backward until we either enter and exit a comment, # or get to something other than a comment previous_lines = line_entries[ index - NUM_LINES_BACKWARDS, index ] previous_lines.reverse! previous_lines.each_with_index do | prev_line, prev_index | if prev_line =~ %r{lzxtype event} then # we found an annotation puts "found event #{event} at line #{index} and annotation at line #{prev_index}" found_annotation = true break end end if not found_annotation then puts "no annotation for #{event} at line #{index} of file #{f}" files_needing_annotations.push( f ) annotate_file( f, event) end end end end files_needing_annotations.uniq! puts "Annotations needed for... #{files_needing_annotations.length} files:" files_needing_annotations.each do | f | puts "#{f}\n" end