Class: SkotOS::XMLObject

Inherits:
Object
  • Object
show all
Defined in:
lib/dgd-tools/skotos_xml_obj.rb,
lib/dgd-tools/skotos_xml_obj.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pretty, noko_doc: nil) ⇒ XMLObject

Returns a new instance of XMLObject.



23
24
25
26
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 23

def initialize(pretty, noko_doc: nil)
    @pretty = pretty
    @noko_doc = noko_doc
end

Class Attribute Details

.ignore_typesObject

Returns the value of attribute ignore_types.



16
17
18
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 16

def ignore_types
  @ignore_types
end

.ignore_whitespaceObject

Returns the value of attribute ignore_whitespace.



15
16
17
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 15

def ignore_whitespace
  @ignore_whitespace
end

.merry_onlyObject

Returns the value of attribute merry_only.



14
15
16
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 14

def merry_only
  @merry_only
end

Instance Attribute Details

#noko_docObject (readonly)

Returns the value of attribute noko_doc.



21
22
23
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 21

def noko_doc
  @noko_doc
end

#prettyObject (readonly)

Returns the value of attribute pretty.



20
21
22
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 20

def pretty
  @pretty
end

Class Method Details

.diff_between(obj1, obj2, o1_name: "Object 1", o2_name: "Object 2") ⇒ Object



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
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 39

def self.diff_between(obj1, obj2, o1_name: "Object 1", o2_name: "Object 2")
    of1 = Tempfile.new("skotos_xml_diff1_")
    of2 = Tempfile.new("skotos_xml_diff2_")

    begin
        of1.write(obj1.pretty)
        of2.write(obj2.pretty)
        of1.close
        of2.close

        diff_opts = [ "c" ]
        diff_opts += [ "b", "B" ] if self.ignore_whitespace

        # Diff 'fails' if there's a difference between the two files.
        cmd = "diff -#{diff_opts.join("")} #{of1.path} #{of2.path}"
        #puts "Diff command: #{cmd}"
        diff = system_call(cmd, fail_ok: true)
        diff.sub!(of1.path, o1_name)
        diff.sub!(of2.path, o2_name)
    ensure
        of1.unlink
        of2.unlink
    end
    diff
end

.diff_dirs(dir1, dir2) ⇒ Object



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
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 79

def self.diff_dirs(dir1, dir2)
    entries1 = skip_ignored_files(Dir.glob("*", base: dir1).to_a, dir1)
    entries2 = skip_ignored_files(Dir.glob("*", base: dir2).to_a, dir2)

    only_in_1 = entries1 - entries2
    only_in_2 = entries2 - entries1
    in_both = entries1 & entries2

    diff = []
    diff << "Only in first: #{only_in_1.map { |s| dir1 + "/" + s }.join(", ")}" unless only_in_1.empty?
    diff << "Only in second: #{only_in_2.map { |s| dir2 + "/" + s }.join(", ")}" unless only_in_2.empty?

    in_both.each do |file|
        in_1 = File.join dir1, file
        in_2 = File.join dir2, file
        if File.directory?(in_1) ^ File.directory?(in_2)
            diff << "Only a directory in one, not both: #{dir1}/#{file}"
        elsif File.directory?(in_1)
            d = diff_dirs(in_1, in_2)
            diff.concat(d)
        else
            o1 = from_file(in_1)
            o2 = from_file(in_2)
            this_diff = diff_between(o1, o2, o1_name: in_1, o2_name: in_2)
            diff << this_diff unless this_diff.strip == ""
        end
    end
    diff
end

.from_file(filename) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 28

def self.from_file(filename)
    # SkotOS files often have references to undefined namespaces,
    # but we can get Nokogiri to parse it.
    doc = Nokogiri::XML(File.read filename)

    remove_undiffed(doc)

    pretty = doc.to_xml(indent:3)
    SkotOS::XMLObject.new pretty, noko_doc: doc
end

.noko_non_text(nodes) ⇒ Object



171
172
173
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 171

def self.noko_non_text(nodes)
    nodes.select { |n| !n.is_a? Nokogiri::XML::Text }
end

.noko_remove(node) ⇒ Object



154
155
156
157
158
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 154

def self.noko_remove(node)
    nn = node.next
    nn.remove if nn.is_a?(Nokogiri::XML::Text)
    node.remove
end

.noko_remove_non_merry_nodes(root) ⇒ Object



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
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 184

def self.noko_remove_non_merry_nodes(root)
    root.children.each do |node|
        if node.name != "Core:PropertyContainer"
            node.remove
            next
        end

        node.children.each do |node2|
            if node2.name != "Core:PCProperties"
                node2.remove
                next
            end

            node2.children.each do |property_node|
                if property_node.name != "Core:Property" || property_node.attribute("property").value[0..5] != "merry:"
                    property_node.remove
                    next
                end
                # Leave the Merry node alone
            end

            if node2.children.size == 0
                node2.remove
            end
        end
        if node.children.size == 0
            node.remove
        end
    end
end

.noko_single_node(node, name, attrs: {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 160

def self.noko_single_node(node, name, attrs: {})
    choices = noko_with_name_and_attrs(node, name, attrs)
    if choices.size < 1
        nil
    elsif choices.size > 1
        raise "Single-node search returned more than one node! #{name.inspect}, #{attrs.inspect}"
    else
        choices[0]
    end
end

.noko_with_name_and_attrs(node, name, attrs = {}) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 175

def self.noko_with_name_and_attrs(node, name, attrs = {})
    results = node.children.flat_map { |n| noko_with_name_and_attrs(n, name, attrs) }
    if node.name == name &&
        attrs.all? { |k, v| node.attribute(k).value == v }
        results << node
    end
    results
end

.remove_undiffed(doc) ⇒ Object



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
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 109

def self.remove_undiffed(doc)
    if doc.root && doc.root.element?
        ignored_top_elements = ["program", "clone", "owner"]
        ignored_top_elements.each do |attr|
            if doc.root.attribute(attr)
                doc.root.remove_attribute(attr)
            end
        end
    end

    rev = noko_single_node(doc.root, "Core:Property", attrs: { "property" => "revisions" })
    noko_remove(rev) if rev

    list = noko_single_node(doc.root, "Core:Property", attrs: { "property" => "#list#" })
    list.remove if list

    properties = noko_with_name_and_attrs(doc.root, "Core:Property")
    properties.each do |prop_node|
        prop_node.remove if prop_node.attribute("property").value.start_with?("sys:sync")
    end

    if self.merry_only
        # Kill off all the non-Merry nodes
        noko_remove_non_merry_nodes(doc.root)
    end

    if self.ignore_types
        self.ignore_types.each do |ignored_type|
            skipped = noko_with_name_and_attrs(doc.root, ignored_type)
            skipped.each { |n| noko_remove(n) }
        end
    end

    base_combat = noko_single_node(doc.root, "Base:Combat")
    if base_combat
        base_strength = noko_single_node(base_combat, "Base:Strength", attrs: { "value" => "1" })
        base_max_fatigue = noko_single_node(base_combat, "Base:MaxFatigue", attrs: { "value" => "1" })
        if base_strength && base_max_fatigue && noko_non_text(base_combat.children).size == 2
            next_text = base_combat.next
            base_combat.remove
            next_text.remove
        end
    end
end

.skip_ignored_files(list, base_dir) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 65

def self.skip_ignored_files(list, base_dir)
    if self.merry_only
        list.select { |path| File.directory?(base_dir + "/" + path) ||
            path[/.xml$/] || path[/.XML$/] }
    else
        list.select do |path|
            !path[/,v$/] &&  # Ignore files ending in comma-v
                !path[/-backup-\d+-\d+-\d+\.xml/] && # Ignore files ending in -backup-[DATE].xml
                path != ".git" && # Ignore .git directories
                path != "MOVED" # Ignore MOVED - it's a sort of recycle, waiting to be emptied
        end
    end
end

.system_call(cmd, fail_ok: false) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/dgd-tools/skotos_xml_obj.rb', line 215

def self.system_call(cmd, fail_ok: false)
    f = Tempfile.new("system_call_xml_diff_")
    begin
        system(cmd, out: f)
        unless fail_ok || $?.success?
            f.rewind
            out = f.read
            raise "Error running command: #{cmd.inspect}!\n\nOutput:\n#{out}\n\n"
        end
        f.rewind
        return f.read
    ensure
        f.close
        f.unlink
    end
end