forked from NancyFx/Nancy
-
Notifications
You must be signed in to change notification settings - Fork 9
/
customassemblyinfo.rb
168 lines (132 loc) · 4.28 KB
/
customassemblyinfo.rb
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
98
99
100
101
102
103
104
105
106
107
108
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
require 'albacore'
# Forked from Albacore to add AssemblyInformationalVersion support
class CustomAssemblyInfo
include Albacore::Task
include Configuration::AssemblyInfo
attr_accessor :input_file, :output_file, :language,
:version, :title, :description, :custom_attributes,
:copyright, :com_visible, :com_guid, :company_name, :product_name,
:file_version, :trademark, :lang_engine, :assembly_informational_version
attr_array :namespaces, :custom_data
attr_hash :custom_attributes
def initialize
@namespaces = []
@custom_data = []
super()
update_attributes assemblyinfo.to_hash
end
def use(file)
@input_file = @output_file = file
end
def execute
unless check_lang_engine then
@lang_engine = from_language
end
write_assemblyinfo @output_file, @input_file
end
def write_assemblyinfo(assemblyinfo_file, input_file)
valid = check_output_file assemblyinfo_file
return if !valid
input_data = read_input_file input_file
asm_data = build_assembly_info_data input_data
@logger.info "Generating Assembly Info File At: " + File.expand_path(assemblyinfo_file)
File.open(assemblyinfo_file, 'w') do |f|
asm_data.each do |line|
f.puts line
end
end
end
def read_input_file(file)
data = []
return data if file.nil?
File.open(file, 'r') do |f|
f.each_line do |line|
data << line.strip
end
end
data
end
def check_output_file(file)
return true if file
fail_with_message 'output_file cannot be nil'
false
end
def from_language
({
"F#" => lambda { FSharpEngine.new },
"C#" => lambda { CSharpEngine.new },
"C++.Net" => lambda { CppCliEngine.new },
"VB.Net" => lambda { VbNetEngine.new }
}[@language] || lambda { CSharpEngine.new }).call
end
def check_lang_engine
!@lang_engine.nil?
end
def build_assembly_info_data(data)
# data < []
# requires: @lang_engine.nil? == false
if data.empty?
data = build_header
end
data = build_using_statements(data) + data
build_attribute(data, "AssemblyTitle", @title)
build_attribute(data, "AssemblyDescription", @description)
build_attribute(data, "AssemblyCompany", @company_name)
build_attribute(data, "AssemblyProduct", @product_name)
build_attribute(data, "AssemblyCopyright", @copyright)
build_attribute(data, "AssemblyTrademark", @trademark)
build_attribute(data, "ComVisible", @com_visible)
build_attribute(data, "Guid", @com_guid)
build_attribute(data, "AssemblyVersion", @version)
build_attribute(data, "AssemblyFileVersion", @file_version)
build_attribute(data, "AssemblyInformationalVersion", @assembly_informational_version)
data << ""
if @custom_attributes != nil
build_custom_attributes(data)
data << ""
end
@custom_data.each do |cdata|
data << cdata unless data.include? cdata
end
data.concat build_footer
chomp data
end
def chomp(ary)
non_empty_rindex = ary.rindex {|line| !line.empty? } || 0
ary.slice(0..non_empty_rindex)
end
def build_header
@lang_engine.respond_to?(:before) ? [@lang_engine.before()] : []
end
def build_footer
@lang_engine.respond_to?(:after) ? [@lang_engine.after()] : []
end
def build_attribute(data, attr_name, attr_data, allow_empty_args = false)
if !allow_empty_args and attr_data.nil? then return end
attr_value = @lang_engine.build_attribute(attr_name, attr_data)
attr_re = @lang_engine.build_attribute_re(attr_name)
result = nil
@logger.debug "Build Assembly Info Attribute: " + attr_value
data.each do |line|
break unless result.nil?
result = line.sub! attr_re, attr_value
end
data << attr_value if result.nil?
end
def build_using_statements(data)
@namespaces = [] if @namespaces.nil?
@namespaces << "System.Reflection"
@namespaces << "System.Runtime.InteropServices"
@namespaces.uniq!
ns = []
@namespaces.each do |n|
ns << @lang_engine.build_using_statement(n) unless data.index { |l| l.match n }
end
ns
end
def build_custom_attributes(data)
@custom_attributes.each do |key, value|
build_attribute(data, key, value, true)
end
end
end