1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
#!/usr/bin/env ruby
#
# Convert Plugins from the old format to the new format.
# Andrew Horton
#
# Use like this
# for i in `ls my-plugins/*.rb`
# do
# echo $i; ./convert-plugin.rb $i
# done
def gem_available?(gemname)
Gem::Specification.find_by_name(gemname) ? true : false
rescue LoadError
return false
end
def has_program?(program)
ENV['PATH'].split(File::PATH_SEPARATOR).any? do |directory|
File.executable?(File.join(directory, program.to_s))
end
end
exit 1 if %w|colorize fileutils |.reject( &method(:gem_available?) ).map {|x| puts "Gem missing. Install with: gem install #{x}" }.any?
abort("Editor missing. Install nano.") unless has_program?("nano")
require 'pp'
require 'colorize'
require 'tempfile'
require 'fileutils'
EDITOR="nano -Y ruby"
#Authors = [
# 'someone', # initial plugin
# 'someone else', # added XYZ functionality
# 'another' # refactored ABC
#]
##
# Version 0.2 # 2016-04-23 # Andrew Horton
# Moved patterns from passive function to matches[]
##
if ARGV.empty?
puts(<<-EOT)
Usage: #{$PROGRAM_NAME} plugin.rb
Migrate plugins from the v0.4 to v0.5 format.
DESCRIPTION:
This auto-converts the v0.4 plugin to the new v0.5 format.
You will be prompted to approve the convesion result before it saves.
If the plugin needs further changes it will open in nano for editing.
USAGE HINT:
To convert a folder of plugins you can use this in a loop, e.g.
for i in `ls my-plugins/*.rb` do echo $i; ./convert-plugin.rb $i; done
EOT
exit
end
FNAME = ARGV.first #"movable_type.rb"
abort "File #{FNAME} does not exist." unless File.exist? FNAME
# check if file already converted
OLDFILE = File.read FNAME
if OLDFILE =~ /^\s*authors /
abort("#{FNAME} was already converted.")
end
# clear screen
clear_code = %x{clear}
puts clear_code
# backup plugin first
BACKUP_FNAME = FNAME + ".bak"
puts "[+] Backup #{FNAME} to #{BACKUP_FNAME}".colorize(:color=>:blue, :background => :black)
FileUtils.cp(FNAME, BACKUP_FNAME)
puts "[+] Converting #{FNAME} to v0.5".colorize(:color=>:blue, :background => :black)
# show me the Versions block
puts "BEFORE".colorize(:color=>:black, :background => :yellow)
cmd = "cat #{FNAME} | egrep \"^\s*author \""
puts `#{cmd}`.strip.yellow
cmd = "cat #{FNAME} | egrep -B 9999 \"^Plugin.define\" | egrep -A 999 '# https://morningstarsecurity.com/research/whatweb' | sed '1d;$d'"
OLDCOMMENTS = `#{cmd}`
puts OLDCOMMENTS.yellow
# Convert the current author "" line into an authors array
# I started this with sed
cmd = %q{sed 's/^[ ]*author "\([^"]*"\)\(.*$\)/authors \[ "\1,\2\n]/g'} + " #{FNAME}"
# puts cmd
s = `#{cmd}`
#puts s
puts "AFTER".colorize(:color=>:black, :background => :green)
# show the first author
FIRSTAUTHOR = s.scan(/^[\s]*authors.*/).first
FIRSTAUTHOR.sub!('[',"[\n ")
puts FIRSTAUTHOR.green
# convert Version string
#sed 's/\# \(Version 0.[0-9]\) \# \([0-9\-]*\) \# \(.*$\)/\n"\3", # \1 \2/g'
# next lines until ^##$
versions = []
# get the Version 0.x until a line with ^##
res = s.scan(/^(#\s+Version[^\n]+)(.*?)(^##)/m)
res.each_with_index do | thisversion, i |
# discard the \n and ##
x = thisversion.delete_if {|x| x =~/(^##\s*?$)|(#\s*?\n)/ }
v, date, author, comment = nil
thisversion.each_with_index do | line, i |
# first
if i == 0
# puts "# first - #{line}"
# Version 0.2 # 2016-04-23 # Andrew Horton
if ret = line.scan(/^#\s+Version\s+([0-9.]+)\s+#\s+([0-9\-]+)[\s#]+(.*)/i).first
v = ret[0]
date = ret[1]
author = ret[2]
author.delete! '"'
author.strip!
author = nil if author.strip.empty?
elsif ret = line.scan(/^#\s*?Version\s*([0-9.]+)\s*#\s*([0-9\-]+)/i).first
# just the version and date
#puts "just the version and date"
v = ret[0]
date = ret[1]
#statements
elsif ret = line.scan(/^#\s*Version\s*([0-9.]+)\s*/i).first
# just the version
#puts "just the version"
v = ret[0]
else
puts "what's this? #{line}"
end
else
# remaining lines in this version
#puts "comment [#{i}]:"
# pp line
line.gsub!(/^\n/,'')
line.gsub!(/#\s*/,'')
line.gsub!("\n",'. ')
#puts " - #{line}"
if comment
comment += line
else
comment = line
end
end
end
# FIX duplicate authors
# MOVE output out of this loop
if v
newversion = {}
if author
#outblock = "\"#{author}\","
newversion[:author] = author
end
# has to be v
if v
#outblock += " " if author
#outblock += "# v#{v}"
newversion[:v] = v
end
if date
#outblock += " # #{date}"
newversion[:date] = date
end
if comment
#outblock += " # #{comment}"
newversion[:comment] = comment
end
versions << newversion
#puts outblock.green
else
puts "# FAIL! Didn't find version"
pp res
end
# pp x
end
# put versions in correct order
versions.reverse!
authors_seen = [FIRSTAUTHOR.scan(/"([^"]*)"/).flatten.first]
#pp authors_seen
outblock = []
versions.each do |thisversion|
# MOVE output out of this loop
if thisversion[:v]
thisoutblock = ""
if thisversion[:author]
# check for duplicate authors
if authors_seen.include? thisversion[:author]
# prefix with hash
thisoutblock = "# #{thisversion[:author]},"
else
thisoutblock = "\"#{thisversion[:author]}\","
authors_seen << thisversion[:author]
end
end
# has to be v
if thisversion[:v]
thisoutblock += " " if thisversion[:author]
thisoutblock += "# v#{thisversion[:v]}"
end
if thisversion[:date]
thisoutblock += " # #{thisversion[:date]}"
end
if thisversion[:comment]
thisoutblock += " # #{thisversion[:comment]}"
end
thisoutblock = " " + thisoutblock # add indent
outblock << thisoutblock
else
puts "# FAIL! Didn't find version"
pp res.red
end
end
# add closing ]
outblock << "]"
outblock = outblock.join("\n")
puts outblock.green
# build new file
newfile = OLDFILE
# add back ##
# wipe old commentss
newfile[OLDCOMMENTS] = ""
unless newfile.include? "##\nPlugin.define"
newfile["Plugin.define"] = "##\nPlugin.define"
end
unless newfile.include? "##\n# This file"
newfile["# This file"] = "##\n# This file" unless not newfile.include? "# This file"
end
# substitude first author line and add other authors
newfile.gsub!(/^[\s]*author.*/, FIRSTAUTHOR + "\n" + outblock)
response = ""
while response != "y" and response != "n"
# is it OK?
puts "Is it OK? y or n. (y to continue, or n to edit in nano)"
response = STDIN.gets.strip
end
if response == "y"
# replace the file
file = Tempfile.new("whatweb-convert-plugin")
file.write(newfile)
file.close
FileUtils.mv(file.path, FNAME)
else
# edit file to fix
# add comment block to top
file = Tempfile.new("whatweb-convert-plugin")
file.write(OLDCOMMENTS + "\n" + newfile)
file.close
cmd = "#{EDITOR} #{file.path}"
puts cmd
ret = system(cmd)
puts "Save it now? y or n"
response = STDIN.gets.strip
if response == "y"
FileUtils.mv(file.path, FNAME)
end
end
puts "NEW".colorize(:color=>:black, :background => :cyan)
puts File.read(FNAME).cyan
puts "[+] Migrated plugin saved to #{FNAME}. Original plugin backup in #{BACKUP_FNAME}".colorize(:color=>:blue, :background => :black)
puts "<press any key>"
STDIN.gets
|