|
| 1 | +properties { |
| 2 | + $projectName = "AliaSQL" |
| 3 | + $version = "1.0.0" |
| 4 | + |
| 5 | + $version = $version + "." + (get-date -format "MMdd") |
| 6 | + $projectConfig = "Release" |
| 7 | + $base_dir = resolve-path . |
| 8 | + $source_dir = "$base_dir\source" |
| 9 | + $unitTestAssembly = "$projectName.UnitTests.dll" |
| 10 | + $nunitPath = "$source_dir\packages\NUnit.Runners.2.6.3\tools\" |
| 11 | + $AliaSQLPath = "$base_dir\lib\AliaSQL\" |
| 12 | + $build_dir = "$base_dir\build" |
| 13 | + $test_dir = "$build_dir\test" |
| 14 | + $testCopyIgnorePath = "_ReSharper" |
| 15 | + $package_dir = "$build_dir\package" |
| 16 | + $package_file = "$build_dir\latestVersion\" + $projectName +"_Package.zip" |
| 17 | + $databaseServer = ".\sqlexpress" |
| 18 | + $databaseScripts = "$source_dir\Database" |
| 19 | + $databaseName = "Demo" |
| 20 | +} |
| 21 | + |
| 22 | +task default -depends Init, UpdateAssemblyInfo, Compile, Test |
| 23 | +task ci -depends Init, UpdateAssemblyInfo, Compile, Test, Package, NugetPack |
| 24 | + |
| 25 | +task Init { |
| 26 | + delete_file $package_file |
| 27 | + delete_directory $build_dir |
| 28 | + create_directory $test_dir |
| 29 | + create_directory $build_dir |
| 30 | +} |
| 31 | + |
| 32 | +task Compile -depends Init { |
| 33 | + exec { & $source_dir\.nuget\nuget.exe restore $source_dir\$projectName.sln } |
| 34 | + msbuild /t:clean /v:q /nologo /p:Configuration=$projectConfig $source_dir\$projectName.sln /p:VisualStudioVersion=12.0 |
| 35 | + |
| 36 | + delete_file $error_dir |
| 37 | + msbuild /t:build /v:q /nologo /p:Configuration=$projectConfig $source_dir\$projectName.sln /p:VisualStudioVersion=12.0 |
| 38 | +} |
| 39 | + |
| 40 | +task Test { |
| 41 | + if (Test-Path ("$nunitPath\nunit-console-x86.exe")){ |
| 42 | + copy_all_assemblies_for_test $test_dir |
| 43 | + if (Test-Path ("$test_dir\$unitTestAssembly")){ |
| 44 | + write-host "Testing $unitTestAssembly" |
| 45 | + exec { |
| 46 | + & $nunitPath\nunit-console-x86.exe $test_dir\$unitTestAssembly /xml $build_dir\UnitTestResult.xml |
| 47 | + } |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + write-host "Cannot run unit tests as $nunitPath\$unitTestAssembly is MISSING" |
| 52 | + } |
| 53 | + } |
| 54 | + else{ |
| 55 | + write-host "Cannot run tests as $nunitPath\nunit.console.clr4.exe is MISSING" |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +task UpdateAssemblyInfo { |
| 60 | + Update-AssemblyInfoFiles $version |
| 61 | +} |
| 62 | + |
| 63 | +task RebuildDatabase { |
| 64 | + exec { & $AliaSQLPath\AliaSQL.exe Rebuild $databaseServer "$databaseName" "$databaseScripts\Scripts"} |
| 65 | +} |
| 66 | + |
| 67 | +task UpdateDatabase { |
| 68 | + try{ |
| 69 | + exec { & $AliaSQLPath\AliaSQL.exe Update $databaseServer "$databaseName" "$databaseScripts\Scripts"} |
| 70 | + } |
| 71 | + catch{ |
| 72 | + write-host "Database does not exist - running rebuild" |
| 73 | + exec { & $AliaSQLPath\AliaSQL.exe Rebuild $databaseServer "$databaseName" "$databaseScripts\Scripts"} |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +task SeedDatabase { |
| 78 | + exec { & $AliaSQLPath\AliaSQL.exe Seed $databaseServer " $databaseName" "$databaseScripts\Scripts"} |
| 79 | +} |
| 80 | + |
| 81 | +task Package -depends Compile { |
| 82 | + write-host "Clean package directory" |
| 83 | + delete_directory $package_dir |
| 84 | + |
| 85 | + write-host "Copy newly compiled version of Database Deployer" |
| 86 | + copy_files "$base_dir\source\AliaSQL.Console\Bin\Release" "$package_dir\AliaSQL" |
| 87 | + |
| 88 | + write-host "Copy in database scripts" |
| 89 | + copy_files "$databaseScripts\scripts" "$package_dir\database\" |
| 90 | + write-host "Copy AliaSQL tool so scripts can be ran" |
| 91 | + copy_files "$AliaSQLPath" "$package_dir\database\" |
| 92 | + write-host "Create batch files to run db updates" |
| 93 | + create-dbdeployscript "Update" "$package_dir\database\_Update-Database.bat" |
| 94 | + create-dbdeployscript "Rebuild" "$package_dir\database\_Rebuild-Database.bat" |
| 95 | + create-dbdeployscript "Baseline" "$package_dir\database\_Baseline-Database.bat" |
| 96 | + create-dbdeployscript "Seed" "$package_dir\database\_Seed-Database.bat" |
| 97 | + write-host "Zip it up" |
| 98 | + zip_directory $package_dir $package_file |
| 99 | +} |
| 100 | + |
| 101 | + task NugetPack -depends Package { |
| 102 | + exec { |
| 103 | + & $base_dir\lib\ilmerge.exe /target:exe /lib:C:\Windows\Microsoft.NET\Framework\v4.0.30319 /targetplatform:v4 /out:$package_dir\AliaSQL\AliaSQL.exe $package_dir\AliaSQL\AliaSQL.console.exe $package_dir\AliaSQL\AliaSQL.core.dll |
| 104 | + } |
| 105 | + exec { |
| 106 | + & $source_dir\.nuget\nuget.exe pack -Version $version -outputdirectory $build_dir $base_dir\nuget\AliaSQL.nuspec |
| 107 | + } |
| 108 | + exec { |
| 109 | + & $source_dir\.nuget\nuget.exe pack -Version $version -outputdirectory $build_dir $base_dir\nuget\AliaSQL.Kickstarter.nuspec |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +function global:zip_directory($directory,$file) { |
| 114 | + write-host "Zipping folder: " $test_assembly |
| 115 | + delete_file $file |
| 116 | + cd $directory |
| 117 | + & "$base_dir\lib\7zip\7za.exe" a -mx=9 -r $file |
| 118 | + cd $base_dir |
| 119 | +} |
| 120 | + |
| 121 | +function global:copy_files($source,$destination,$exclude=@()){ |
| 122 | + create_directory $destination |
| 123 | + Get-ChildItem $source -Recurse -Exclude $exclude -ErrorAction SilentlyContinue | Copy-Item -ErrorAction SilentlyContinue -Destination {Join-Path $destination $_.FullName.Substring($source.length)} |
| 124 | +} |
| 125 | + |
| 126 | +function global:Copy_and_flatten ($source,$filter,$dest) { |
| 127 | + ls $source -filter $filter -r | Where-Object{!$_.FullName.Contains("$testCopyIgnorePath") -and !$_.FullName.Contains("packages") -and !$_.FullName.Contains("build") }| cp -dest $dest -force |
| 128 | +} |
| 129 | + |
| 130 | +function global:copy_all_assemblies_for_test($destination){ |
| 131 | + create_directory $destination |
| 132 | + Copy_and_flatten $source_dir *.exe $destination |
| 133 | + Copy_and_flatten $source_dir *.dll $destination |
| 134 | + Copy_and_flatten $source_dir *.config $destination |
| 135 | + Copy_and_flatten $source_dir *.xml $destination |
| 136 | + Copy_and_flatten $source_dir *.pdb $destination |
| 137 | +} |
| 138 | + |
| 139 | +function global:delete_file($file) { |
| 140 | + if($file) { remove-item $file -force -ErrorAction SilentlyContinue | out-null } |
| 141 | +} |
| 142 | + |
| 143 | +function global:delete_directory($directory_name) |
| 144 | +{ |
| 145 | + rd $directory_name -recurse -force -ErrorAction SilentlyContinue | out-null |
| 146 | +} |
| 147 | + |
| 148 | +function global:delete_files_in_dir($dir) |
| 149 | +{ |
| 150 | + get-childitem $dir -recurse | foreach ($_) {remove-item $_.fullname} |
| 151 | +} |
| 152 | + |
| 153 | +function global:create_directory($directory_name) |
| 154 | +{ |
| 155 | + mkdir $directory_name -ErrorAction SilentlyContinue | out-null |
| 156 | +} |
| 157 | + |
| 158 | +function create-dbdeployscript($verb, $filename) |
| 159 | +{ |
| 160 | +"@echo off |
| 161 | +set /p serverName=""DB Server: "" %=% |
| 162 | +AliaSQL.exe $verb %serverName% $databaseName . |
| 163 | +pause" | out-file $filename -encoding "ASCII" |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | +function Update-AssemblyInfoFiles ([string] $version, [System.Array] $excludes = $null) { |
| 168 | + |
| 169 | +#------------------------------------------------------------------------------- |
| 170 | +# Update version numbers of AssemblyInfo.cs |
| 171 | +# adapted from: http://www.luisrocha.net/2009/11/setting-assembly-version-with-windows.html |
| 172 | +#------------------------------------------------------------------------------- |
| 173 | + |
| 174 | + if ($version -notmatch "[0-9]+(\.([0-9]+|\*)){1,3}") { |
| 175 | + Write-Error "Version number incorrect format: $version" |
| 176 | + } |
| 177 | + |
| 178 | + $versionPattern = 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)' |
| 179 | + $versionAssembly = 'AssemblyVersion("' + $version + '")'; |
| 180 | + $versionFilePattern = 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)' |
| 181 | + $versionAssemblyFile = 'AssemblyFileVersion("' + $version + '")'; |
| 182 | + |
| 183 | + Get-ChildItem -r -filter AssemblyInfo.* | % { |
| 184 | + $filename = $_.fullname |
| 185 | + |
| 186 | + $update_assembly_and_file = $true |
| 187 | + |
| 188 | + # set an exclude flag where only AssemblyFileVersion is set |
| 189 | + if ($excludes -ne $null) |
| 190 | + { $excludes | % { if ($filename -match $_) { $update_assembly_and_file = $false } } } |
| 191 | + |
| 192 | + # see http://stackoverflow.com/questions/3057673/powershell-locking-file |
| 193 | + # I am getting really funky locking issues. |
| 194 | + # The code block below should be: |
| 195 | + # (get-content $filename) | % {$_ -replace $versionPattern, $version } | set-content $filename |
| 196 | + |
| 197 | + $tmp = ($file + ".tmp") |
| 198 | + if (test-path ($tmp)) { remove-item $tmp } |
| 199 | + |
| 200 | + if ($update_assembly_and_file) { |
| 201 | + (get-content $filename) | % {$_ -replace $versionFilePattern, $versionAssemblyFile } | % {$_ -replace $versionPattern, $versionAssembly } > $tmp |
| 202 | + write-host Updating file AssemblyInfo and AssemblyFileInfo: $filename --> $versionAssembly / $versionAssemblyFile |
| 203 | + } else { |
| 204 | + (get-content $filename) | % {$_ -replace $versionFilePattern, $versionAssemblyFile } > $tmp |
| 205 | + write-host Updating file AssemblyInfo only: $filename --> $versionAssemblyFile |
| 206 | + } |
| 207 | + |
| 208 | + if (test-path ($filename)) { remove-item $filename } |
| 209 | + move-item $tmp $filename -force |
| 210 | + |
| 211 | + } |
| 212 | +} |
| 213 | + |
0 commit comments