visual_studio_files 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/visual_studio_files.rb +141 -0
  2. metadata +48 -0
@@ -0,0 +1,141 @@
1
+ require 'rexml/document'
2
+ require 'fileutils'
3
+
4
+ module VisualStudioFiles
5
+ class CsProj
6
+ attr_reader :content
7
+ def initialize(content)
8
+ @content = content
9
+ @xmldoc = REXML::Document.new(@content)
10
+ @xmlns = {"x"=>"http://schemas.microsoft.com/developer/msbuild/2003"};
11
+ end
12
+
13
+ def element_types
14
+ ['Compile','Content','EmbeddedResource','None', 'Page']
15
+ end
16
+ def files()
17
+ files=[]
18
+ element_types.each { |elementType|
19
+ REXML::XPath.each(@xmldoc,"/x:Project/x:ItemGroup/x:#{elementType}", @xmlns) { |file|
20
+ links = file.elements.select{ |el| el.name == 'Link' }.map { |e| e.get_text.value }
21
+ depend_upon = file.elements.select { |el| el.name == 'DependentUpon' }.map { |e| e.get_text.value }
22
+ generator = file.elements.select { |el| el.name == 'Generator' }.map { |e| e.get_text.value }
23
+ sub_type = file.elements.select { |el| el.name == 'SubType' }.map { |e| e.get_text.value }
24
+ files.push(FileReference.new({
25
+ :file=>file.attributes['Include'],
26
+ :type=>elementType,
27
+ :link=> links.first,
28
+ :dependent_upon=>depend_upon.first,
29
+ :generator => generator.first,
30
+ :sub_type => sub_type.first
31
+ }))
32
+ }
33
+ }
34
+ return files
35
+ end
36
+ def add(ref)
37
+
38
+ ref = FileReference.new(ref) if !ref.is_a? FileReference
39
+
40
+ last = REXML::XPath.match(@xmldoc,"/x:Project/x:ItemGroup/", @xmlns).last
41
+ el = last.add_element(ref.type,{'Include'=>ref.file})
42
+ if ref.link
43
+ el.add_element('Link').add_text(ref.link)
44
+ end
45
+ if ref.dependent_upon
46
+ el.add_element('DependentUpon').add_text(ref.dependent_upon)
47
+ end
48
+ if ref.generator
49
+ el.add_element('Generator').add_text(ref.generator)
50
+ end
51
+ if ref.sub_type
52
+ el.add_element('SubType').add_text(ref.sub_type)
53
+ end
54
+ end
55
+ def clear_links()
56
+ element_types.each { |elementType|
57
+ REXML::XPath.each(@xmldoc,"/x:Project/x:ItemGroup/x:#{elementType}", @xmlns) { |file|
58
+ links = file.elements.select{ |el| el.name == 'Link' }
59
+ if (links.any?)
60
+ file.parent.delete_element(file)
61
+ end
62
+ }
63
+ }
64
+ end
65
+ def to_s
66
+ return @xmldoc.to_s
67
+ end
68
+ def write output
69
+ formatter = REXML::Formatters::Pretty.new(2)
70
+ formatter.compact = true # This is the magic line that does what you need!
71
+ formatter.write(@xmldoc, output)
72
+ end
73
+ end
74
+
75
+ class FileReference
76
+ attr_reader :file, :downcase_and_path_replaced, :type, :link, :dependent_upon, :generator, :sub_type
77
+ def initialize opts
78
+ opts.each do |key,value|
79
+ self[key]=value
80
+ end
81
+ end
82
+ def downcase_and_path_replaced
83
+ @file.downcase.gsub(/\//,'\\')
84
+ end
85
+ def ==(other)
86
+ other.downcase_and_path_replaced == @downcase_and_path_replaced
87
+ end
88
+ def []=(idx,value)
89
+ case idx
90
+ when :file
91
+ @file = value
92
+ when :type
93
+ @type = value
94
+ when :link
95
+ @link = value
96
+ when :dependent_upon
97
+ @dependent_upon = value
98
+ when :sub_type
99
+ @sub_type = value
100
+ when :generator
101
+ @generator = value
102
+ end
103
+ end
104
+ def link?
105
+ @link!=nil && !@link.empty?
106
+ end
107
+ def none?
108
+ @type=="None"
109
+ end
110
+ def compile?
111
+ @type=="Compile"
112
+ end
113
+ def content?
114
+ @type=="Content"
115
+ end
116
+ def embedded_resource?
117
+ @type=="EmbeddedResource"
118
+ end
119
+ def page?
120
+ @type=="Page"
121
+ end
122
+ alias_method :eql?, :==
123
+ def hash
124
+ @downcase_and_path_replaced.hash
125
+ end
126
+ def to_hash
127
+ return {
128
+ :file=>@file,
129
+ :type=>@type,
130
+ :link=>@link,
131
+ :dependent_upon=>@dependent_upon,
132
+ :sub_type=>@sub_type,
133
+ :generator=>@generator
134
+ }
135
+ end
136
+ def to_s
137
+ "#{@file} #{@type} #{@link}"
138
+ end
139
+ end
140
+
141
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: visual_studio_files
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - wallymathieu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-29 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: This library is intended to help when manipulating such things as the
15
+ files included in a visual studio project. Or verify some condition related to the
16
+ files in the project.
17
+ email:
18
+ - gewalli@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/visual_studio_files.rb
24
+ homepage: https://github.com/wallymathieu/visual_studio_files
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: 1.9.2
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.24
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: A simple library to manipulate visual studio files
48
+ test_files: []