Class: VisualStudioFiles::CsProj

Inherits:
Object
  • Object
show all
Defined in:
lib/visual_studio_files.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ CsProj

Returns a new instance of CsProj.



7
8
9
10
11
# File 'lib/visual_studio_files.rb', line 7

def initialize(content)
  @content = content
  @xmldoc = REXML::Document.new(@content)
  @xmlns = {"x"=>"http://schemas.microsoft.com/developer/msbuild/2003"};
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



6
7
8
# File 'lib/visual_studio_files.rb', line 6

def content
  @content
end

Instance Method Details

#add(ref) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/visual_studio_files.rb', line 43

def add(ref)
  
  ref = FileReference.new(ref) if !ref.is_a? FileReference

  last = REXML::XPath.match(@xmldoc,"/x:Project/x:ItemGroup/", @xmlns).last
  el = last.add_element(ref.type,{'Include'=>ref.file})
  if ref.link
    el.add_element('Link').add_text(ref.link)
  end
  if ref.dependent_upon
    el.add_element('DependentUpon').add_text(ref.dependent_upon)
  end
  if ref.generator
    el.add_element('Generator').add_text(ref.generator)
  end
  if ref.sub_type
    el.add_element('SubType').add_text(ref.sub_type)
  end
  if ref.copy_to_output_directory
    el.add_element('CopyToOutputDirectory').add_text(ref.copy_to_output_directory)
  end
end


65
66
67
68
69
70
71
72
73
74
# File 'lib/visual_studio_files.rb', line 65

def clear_links()
  element_types.each { |elementType|
      REXML::XPath.each(@xmldoc,"/x:Project/x:ItemGroup/x:#{elementType}", @xmlns) { |file|
        links = file.elements.select{ |el| el.name == 'Link' }
        if (links.any?)
          file.parent.delete_element(file)
        end
      }
  }
end

#element_typesObject



13
14
15
# File 'lib/visual_studio_files.rb', line 13

def element_types
  ['Compile','Content','EmbeddedResource','None', 'Page']
end

#filesObject



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
# File 'lib/visual_studio_files.rb', line 16

def files()
  files=[]
  element_types.each { |elementType|
      REXML::XPath.each(@xmldoc,"/x:Project/x:ItemGroup/x:#{elementType}", @xmlns) { |file|
        links = file.elements.select{ |el| el.name == 'Link' }.map { |e| e.get_text.value }
        depend_upon = file.elements.select { |el| el.name == 'DependentUpon'  }.map { |e| e.get_text.value }
        generator = file.elements.select { |el| el.name == 'Generator' }.map { |e| e.get_text.value }
        sub_type =  file.elements.select { |el| el.name == 'SubType' }.map { |e| e.get_text.value }
        copy_to_output_directory = file.elements.select { |el| el.name == 'CopyToOutputDirectory' }.map { |e| e.get_text.value }
        hash = {
          :file=>file.attributes['Include'], 
          :type=>elementType, 
          :link=> links.first,
          :dependent_upon=>depend_upon.first,
          :sub_type => sub_type.first
        }
        if generator!=nil
          hash[:generator] = generator.first
        end
        if copy_to_output_directory!=nil
          hash[:copy_to_output_directory] = copy_to_output_directory.first
        end
        files.push(FileReference.new(hash))
      }
  }
  return files
end

#to_sObject



75
76
77
# File 'lib/visual_studio_files.rb', line 75

def to_s
  return @xmldoc.to_s
end

#write(output) ⇒ Object



78
79
80
81
82
# File 'lib/visual_studio_files.rb', line 78

def write output
  formatter = REXML::Formatters::Pretty.new(2)
  formatter.compact = true # This is the magic line that does what you need!
  formatter.write(@xmldoc, output)
end