-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathautovalidate
More file actions
executable file
·98 lines (81 loc) · 2.12 KB
/
Copy pathautovalidate
File metadata and controls
executable file
·98 lines (81 loc) · 2.12 KB
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
#!/usr/bin/env ruby
#
# xmlvwatch - stoopid program to "watch" an xml document and its xsd,
# changing screen color as the document is validated/invalidated.
# Basically, this is a poor man's autospec.
require 'appscript'
require 'libxml'
require 'ruby-debug'
require 'sparky'
include Appscript
class XmlValidateWatcher
attr_reader :xml, :xsd, :errors, :xmlstat, :xsdstat
@@use_sparky = 0
def initialize(xml, xsd)
@xml, @xsd = xml, xsd
raise "Could not open XML file" unless File.exist?(xml)
raise "Could not open XSD file" unless File.exist?(xsd)
iterm = Appscript::app('iTerm')
@term = iterm.current_terminal.sessions.get.find {|sess| sess.get == iterm.current_terminal.current_session.get }
@sparky = Sparky.new if use_sparky?
end
def self.use_sparky!
@@use_sparky = true
end
def self.no_sparky!
@@use_sparky = false
end
def use_sparky?
@@use_sparky
end
def header
puts "\033[2J\033[H"
puts "autovalidate #{@xml} #{@xsd}"
puts "Last validated: #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}"
puts
end
def red
@term.background_color.set [32768, 0, 0]
@sparky.reset && @sparky.failed if use_sparky?
end
def green
@term.background_color.set [0, 32768, 0]
@sparky.reset && @sparky.pass if use_sparky?
end
def validate
@sparky.reset && @sparky.run if use_sparky?
header
result = false
begin
document = LibXML::XML::Document.file(@xml)
schema = LibXML::XML::Schema.new(@xsd)
result = document.validate_schema(schema)
rescue
nil
end
result ? green : red
end
def changed?
@newxmlstat, @newxsdstat = File.mtime(@xml), File.mtime(@xsd)
c = @xmlstat != @newxmlstat || @xsdstat != @newxsdstat
@xmlstat, @xsdstat = @newxmlstat, @newxsdstat
c
end
def run
while true
validate if changed?
sleep 0.1
end
end
end
if $0 == __FILE__
args = ARGV.dup
if args.include?("--sparky")
XmlValidateWatcher.use_sparky!
args.delete("--sparky")
else
XmlValidateWatcher.no_sparky!
end
watcher = XmlValidateWatcher.new(args[0], args[1])
watcher.run
end