-
Notifications
You must be signed in to change notification settings - Fork 4
/
rakefile
181 lines (160 loc) · 6.52 KB
/
rakefile
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
169
170
171
172
173
174
175
176
177
178
179
180
181
require 'yaml'
# path when build tools is installed from https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=15
MSBUILD1 = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\MSBuild\\Current\\Bin\\"
MSBUILD2 = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\MSBuild\\15.0\\Bin\\"
MSBUILD3 = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\MSBuild\\15.0\\Bin\\" # path when VS is installed
$MSBUILD = MSBUILD1
if !Dir.exist?($MSBUILD)
$MSBUILD = MSBUILD2
if !Dir.exist?($MSBUILD)
$MSBUILD = MSBUILD3
if !Dir.exist?($MSBUILD)
puts "Can't find MsBuild.exe in #{MSBUILD1} or #{MSBUILD2}"
exit 1 # failure
end
end
end
$MSBUILD = $MSBUILD + "msbuild.exe"
PRODUCT_NAME = "FluentAssert"
CLR_TOOLS_VERSION = "v4.8"
DEFAULT_BUILD_NUMBER = "1.1.0"
COMPANY_NAME = "MVBA, P.C."
COPYRIGHT = "MVBA, P.C. (c) 2011-2020"
ARTIFACTS = File.expand_path("dist")
COMPILE_TARGET = "Debug"
TARGET_FRAMEWORK = "net48"
STAGE = File.expand_path("build")
SETTINGS_DIR = "settings"
nunit_cmd = "tools/NUnit/nunit-console.exe"
beginning = Time.now
default_settings = { :artifacts => ARTIFACTS,
:compile_target => COMPILE_TARGET,
:target_framework => TARGET_FRAMEWORK,
:stage => STAGE
}
def read_config(settings_directory, env)
settings = {}
default_settings_file = "#{settings_directory}/#{env}.default.yaml"
if File.exists?(default_settings_file) then
settings = YAML.load_file(default_settings_file)
end
settings_file = "#{settings_directory}/#{env}.yaml"
if File.exists?(settings_file) then
overrides = YAML.load_file(settings_file)
overrides.each do |k,v|
settings[k] = v
end
end
return settings
end
environment = ENV['environment']
settings = read_config(SETTINGS_DIR, environment)
default_settings.each { |key, val| if settings[key].nil? then settings[key] = default_settings[key] end }
puts "Rakefile settings:"
settings.each { |key, val| puts "#{key} => #{val}" }
puts
desc "** Default **"
task :default => [:build, :copy_artifacts, :test, :pack] do
puts "Build Succeeded - time elapsed: #{Time.now - beginning} seconds"
end
desc "Create Directory.Build.props file"
task :assemblyinfo do
build_number = get_build_number
git_hash = get_git_hash
puts "Writing src/Directory.Build.props file..."
File.open('src/Directory.Build.props', 'w') do |file|
file.write "<Project>\n"
file.write " <PropertyGroup>\n"
file.write " <Authors>#{COMPANY_NAME}</Authors>\n"
file.write " <Company>#{COMPANY_NAME}</Company>\n"
file.write " <Description>Fluent assertions express what you want tested instead of how. This project also provides a simple BDD framework that lets you write tests that can be easily understood and verified by non-developers. (git sha for this version: #{git_hash})</Description>\n"
file.write " <Copyright>#{COPYRIGHT}</Copyright>\n"
file.write " <PackageLicenseFile>License.txt</PackageLicenseFile>\n"
file.write " <NeutralLanguage>en-US</NeutralLanguage>\n"
file.write " <RepositoryUrl>https://github.com/mvbalaw/#{PRODUCT_NAME}</RepositoryUrl>\n"
file.write " <RepositoryType>git</RepositoryType>\n"
file.write " <Version>#{build_number}</Version>\n"
file.write " <FileVersion>#{build_number}</FileVersion>\n"
file.write " <ProductName>#{PRODUCT_NAME}</ProductName>\n"
file.write " </PropertyGroup>\n"
file.write "\n"
file.write " <ItemGroup>\n"
file.write " <Content Include=\"..\\..\\License.txt\" Pack=\"true\" Visible=\"false\" PackagePath=\"\" />"
file.write " </ItemGroup>\n"
file.write "</Project>\n"
end
end
desc "Builds the #{PRODUCT_NAME} solution"
task :build => [:clean, :assemblyinfo] do |msb|
puts
puts "Compiling #{PRODUCT_NAME} in #{settings[:compile_target]} mode..."
puts
sh "\"#{$MSBUILD}\" -t:restore /verbosity:quiet /target:#{:Rebuild} /nologo /p:Configuration=#{settings[:compile_target]} src/#{PRODUCT_NAME}.sln"
end
desc "Copy build artifacts to the staging folder"
task :copy_artifacts => [:build] do
puts "Copying files from bin folders to the build folder (#{settings[:stage]})..."
copy_output_files "src/#{PRODUCT_NAME}/bin/#{settings[:compile_target]}/#{settings[:target_framework]}", "*.{dll,pdb,exe}", settings[:stage]
copy_output_files "src/#{PRODUCT_NAME}.Tests/bin/#{settings[:compile_target]}/#{settings[:target_framework]}", "*.{dll,pdb,exe}", settings[:stage]
FileUtils.cp_r "#{PRODUCT_NAME}.xml", "#{settings[:stage]}/#{PRODUCT_NAME}.xml"
end
desc "Prepare the working directory for a new build"
task :clean do
FileUtils.rm_rf settings[:stage]
# work around nasty latency issue where folder still exists for a short while after it is removed
wait_for { !Dir.exists?(settings[:stage]) }
Dir.mkdir settings[:stage]
Dir.mkdir settings[:artifacts] unless Dir.exists?(settings[:artifacts])
end
desc "Run the unit tests"
task :test do
puts "Running the unit tests in the build folder (#{settings[:stage]})..."
nunit_assemblies = "#{settings[:stage]}/#{PRODUCT_NAME}.Tests.dll"
nunit_options = "/framework #{CLR_TOOLS_VERSION}"
sh "#{nunit_cmd} #{nunit_assemblies} #{nunit_options}"
end
desc "Create nuget package"
task :pack do
puts
puts "Creating nuget package for #{PRODUCT_NAME} in artifact folder (#{settings[:artifacts]})..."
puts
sh "\"#{$MSBUILD}\" /p:PackageOutputPath=\"#{settings[:artifacts]}\" -t:pack /verbosity:quiet /nologo src/#{PRODUCT_NAME}/#{PRODUCT_NAME}.csproj"
end
def copy_output_files(fromDir, filePattern, outDir)
Dir.glob(File.join(fromDir, filePattern)){|file|
copy(file, outDir) if File.file?(file)
}
end
def get_build_number
begin
gittag = `git describe --long`.chomp # looks something like v0.1.0-63-g3f10c2e
puts "gittag: #{gittag}"
parts = gittag.split("-")
base_version = parts[0].gsub("v","")
git_build_revision = parts[1]
git_short_hash = parts[2]
puts "base_version: #{base_version}"
puts "git_build_revision: #{git_build_revision}"
puts "git_short_hash: #{git_short_hash}"
build_number = "#{base_version}.#{git_build_revision}"
rescue
build_number = DEFAULT_BUILD_NUMBER
end
end
def get_git_hash
begin
gittag = `git describe --long`.chomp # looks something like v0.1.0-63-g3f10c2e
parts = gittag.split("-")
git_hash = parts[2]
rescue
git_hash = "git unavailable"
end
end
def wait_for(&block)
checks = 0
until block.call || checks >10
sleep 0.5
checks += 1
end
raise 'waitfor timeout expired' if checks > 10
end