From 8da3419f6d4e14affdf505f366c6ceaaa4d7f4db Mon Sep 17 00:00:00 2001 From: Joel Marty Date: Mon, 16 Sep 2013 17:20:00 +0200 Subject: [PATCH] initial commit --- svn_markup.rb | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 svn_markup.rb diff --git a/svn_markup.rb b/svn_markup.rb new file mode 100755 index 0000000..60d88ef --- /dev/null +++ b/svn_markup.rb @@ -0,0 +1,52 @@ +#!/usr/bin/env ruby +# coding: utf-8 + +XML_MARKUP = "" +SVN_MARKUP = "" +REV_PAT = /\$Revision\$/ +SEARCH_PAT = /(|<\?xml.*\?>)/mi + +usage = "usage: \n svn_markup.rb \"/path/to/templates\"" + +# just checking script was called with path argument +unless ARGV.length >= 1 + puts "Wrong number of arguments" + puts usage + exit +end + +path = ARGV[0] + +# is the given path real? +if not File.directory? path + puts "path is invalid" + exit +end + +dir = Dir.new path + +dir.each do |filename| + #building absolute path, is there a better way to do this? + absolute_path = %{#{dir.path}#{filename}} + unless File.directory? File.new absolute_path + begin + content = File.read absolute_path + unless REV_PAT.match content + if SEARCH_PAT.match content + replace = content.gsub(SEARCH_PAT) {|match| "#{match}\n#{SVN_MARKUP}"} + message = "File #{filename} was tagged below header" + else + replace = "#{XML_MARKUP}\n#{SVN_MARKUP}\n#{content}" + message = "File #{filename} was tagged with new header" + end + File.open(absolute_path, 'w') { |file| file.write(replace) } + puts message + else + puts "#{filename} is already tagged" + end + rescue Exception => e + puts e.message + puts e.backtrace.inspect + end + end +end