-
Notifications
You must be signed in to change notification settings - Fork 0
/
svn_markup.rb
executable file
·57 lines (50 loc) · 1.76 KB
/
svn_markup.rb
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
#!/usr/bin/env ruby
# coding: utf-8
#
# This script adds SVN keyword tags Revision and Date to XML/HTML files, just below the XML header or the DOCTYPE declaration.
# Doctype because that's how our "embedded" html pages need to be.
# If one of both headers is not detected, it will assume it's XML and add it.
# It assumes the directory argument contains XML files only and will NOT filter
XML_MARKUP = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
SVN_MARKUP = "<!--\n$Revision$\n$Date$\n-->"
REV_PAT = /\$Revision\$/
SEARCH_PAT = /(<!DOCTYPE(.*?)\">|<\?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