Guest User

Untitled

a guest
Mar 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'ftools'
  3. require 'optparse'
  4. require 'find'
  5.  
  6. class PhpGhettoDoc
  7.  
  8. Class_Doc = <<-EOF
  9.  
  10. /**
  11. * Undocumented class.
  12. *
  13. * @todo document me
  14. * @package unknown
  15. * @author unknown
  16. **/
  17. EOF
  18.  
  19. Function_Doc = <<-EOF
  20.  
  21. /**
  22. * Undocumented function.
  23. *
  24. * @todo document me
  25. * @return void
  26. * @author unknown
  27. **/
  28. EOF
  29.  
  30. Const_Doc = <<-EOF
  31.  
  32. /**
  33. * Undocumented constant.
  34. * @todo document me
  35. **/
  36. EOF
  37.  
  38.  
  39. def initialize(file, backup = true)
  40. @backup = backup
  41. @word_matcher = /^(\s*)(class|const|function|public|interface|final|protected|private|static)+ (\w+) (\w+)/
  42. @doc_matcher = /^\s*(\*|\/)+/
  43.  
  44. @doc_blocks = {:class => Class_Doc,
  45. :function => Function_Doc,
  46. :const => Const_Doc,
  47. :public => Function_Doc,
  48. :protected => Function_Doc,
  49. :private => Function_Doc,
  50. :abstract => Class_Doc,
  51. :final => Class_Doc,
  52. :interface => Class_Doc}
  53.  
  54. @file = file
  55. @dirty_lines = File.open(file, 'r+').readlines
  56. @modified = false
  57. end
  58.  
  59. def parse_lines
  60. @clean_lines = Array.new
  61.  
  62. @dirty_lines.each do |line|
  63. if (line.match @word_matcher)
  64. indent = $1
  65. type = (Array.new($2.intern, $3.intern, $4.intern).include? :function) ? :function : nil
  66.  
  67. unless (@clean_lines.last =~ @doc_matcher || !type)
  68. doc_block = @doc_blocks[type]
  69. @clean_lines.push(doc_block.split("\n").map! { |l| l = indent + l }.join("\n") + "\n");
  70. @modified = true
  71. end
  72. end
  73. @clean_lines.push line
  74. end
  75. end
  76.  
  77. def dump
  78. @clean_lines.each do |line|
  79. puts line
  80. end
  81. end
  82.  
  83. def save
  84. if @modified
  85. if @backup
  86. File.move(@file, @file + '.bak.' + Time.now.strftime("%Y%m%d%H%M%S"))
  87. end
  88. f = File.new(@file, "w")
  89. @clean_lines.each { |line| f.write(line) }
  90. f.close
  91. end
  92. @modified
  93. end
  94. end
  95.  
  96. if $0 == __FILE__
  97. options = { :save => false, :backup => false, :recurse => false }
  98. usage = "usage: #{__FILE__} [options] <file|directory>"
  99. OptionParser.new do |opts|
  100. opts.banner = usage
  101. opts.on('-s', '--save', "Saves the parsed file to disk. (Default: false)") do |s|
  102. options[:save] = s
  103. end
  104. opts.on('-b', '--[no-]backup', 'Create backups. (Default: false)') do |b|
  105. options[:backup] = b
  106. end
  107. opts.on('-r', '--recurse', 'Recurse subdirectories. (Default: false)') do |r|
  108. options[:recurse] = r
  109. end
  110. end.parse!
  111.  
  112. unless ARGV.length >= 1
  113. puts usage
  114. exit
  115. end
  116.  
  117. modified = 0
  118. files = Array.new
  119. if File.directory?(ARGV[0])
  120. Find.find(ARGV[0]) do |path|
  121. if (FileTest.directory?(path))
  122. if (File.basename(path)[0] == ?. && path != '.')
  123. Find.prune # Skip .foo directories
  124. elsif (! options[:recurse] && ARGV[0] != path)
  125. Find.prune # Skip recurse
  126. else
  127. next
  128. end
  129. else
  130. if path =~ /.php$/
  131. files.push path
  132. end
  133. end
  134. end
  135. else
  136. files.push ARGV[0]
  137. end
  138.  
  139. files.each do |file|
  140. p = PhpGhettoDoc.new(file, options[:backup])
  141. p.parse_lines
  142. if options[:save]
  143. modified += (p.save) ? 1 : 0
  144. else
  145. p.dump
  146. end
  147. end
  148.  
  149. puts "Processed #{files.size} files"
  150. puts "Modified #{modified} files"
  151. end
Add Comment
Please, Sign In to add comment