Class: Stepmod::Utils::StepmodFileAnnotator

Inherits:
Object
  • Object
show all
Defined in:
lib/stepmod/utils/stepmod_file_annotator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(express_file:, resource_docs_cache_file:, stepmod_dir: nil) ⇒ StepmodFileAnnotator

Returns a new instance of StepmodFileAnnotator.

Parameters:

  • express_file (String) —

    path to the exp file needed to annotate

  • resource_docs_cache_file (String) —

    output of ./stepmod-build-resource-docs-cache



12
13
14
15
16
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 12

def initialize(express_file:, resource_docs_cache_file:, stepmod_dir: nil)
  @express_file = express_file
  @resource_docs_cache_file = resource_docs_cache_file
  @stepmod_dir = stepmod_dir || Dir.pwd
end

Instance Attribute Details

#express_file ⇒ Object (readonly)

Returns the value of attribute express_file.



8
9
10
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 8

def express_file
  @express_file
end

#resource_docs_cache_file ⇒ Object (readonly)

Returns the value of attribute resource_docs_cache_file.



8
9
10
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 8

def resource_docs_cache_file
  @resource_docs_cache_file
end

#stepmod_dir ⇒ Object (readonly)

Returns the value of attribute stepmod_dir.



8
9
10
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 8

def stepmod_dir
  @stepmod_dir
end

Instance Method Details

#call ⇒ Object



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
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 18

def call
  match = File.basename(express_file).match('^(arm|mim|bom)\.exp$')
  descriptions_base = match ? "#{match.captures[0]}_descriptions.xml" : 'descriptions.xml'
  descriptions_file = File.join(File.dirname(express_file), descriptions_base)
  output_express = File.read(express_file)
  resource_docs_cache = JSON.parse(File.read(resource_docs_cache_file))

  if File.exists?(descriptions_file)
    descriptions = Nokogiri::XML(File.read(descriptions_file)).root
    descriptions.xpath('ext_description').each do |description|
      unless description.text.strip.empty? then
        Dir.chdir(File.dirname(descriptions_file)) do
          wrapper = "<ext_descriptions>#{description.to_s}</ext_descriptions>"
          output_express << "\n" + Stepmod::Utils::SmrlDescriptionConverter.convert(wrapper)
        end
      else
        # remark is empty, fallback to resource_docs_cache
        resource_docs_dir = resource_docs_cache[description['linkend']]
        if resource_docs_dir
          resource_docs_file = File.join(stepmod_dir, 'data/resource_docs', resource_docs_dir, 'resource.xml')
          resource_docs = Nokogiri::XML(File.read(resource_docs_file)).root
          schema = resource_docs.xpath("schema[@name='#{description['linkend']}']")

          Dir.chdir(File.dirname(descriptions_file)) do
            wrapper = "<resource>#{schema.to_s}</resource>"
            output_express << "\n" + Stepmod::Utils::SmrlResourceConverter.convert(wrapper)
          end
        end
      end
    end
  end

  output_express
end