<commit_before>require 'albacore'
require 'fileutils'
require 'semver'

CLR_TOOLS_VERSION = 'v4.0.30319'
ARTIFACTS_PATH = "build"
PROJECT_NAME = "WcfClientProxyGenerator"

$config = ENV['config'] || 'Debug'
$nuget_api_key = ENV['nuget_api_key']

task :default => :compile

desc 'Generate the VersionInfo.cs class'
assemblyinfo :version => [:versioning] do |asm|
  git_data = commit_data()
  revision_hash = git_data[0]
  revision_date = git_data[1]
    
  asm.version = FORMAL_VERSION
  asm.file_version = FORMAL_VERSION
  asm.product_name = PROJECT_NAME
  asm.description = "Git commit hash: #{revision_hash} - #{revision_date}"
  asm.custom_attributes :AssemblyInformationalVersion => "#{BUILD_VERSION}"
  asm.output_file = "source/VersionInfo.cs"
  asm.namespaces 'System', 'System.Reflection'  
end

desc 'Compile the project'
task :compile do
  Rake::Task['build:net45'].invoke()
end

namespace :build do
  task :all => [:net45] do
  end

  msbuild :net45 => :version do |msb|
    msb.properties :configuration => $config, :Framework => 'NET45'
    msb.targets [:clean, :build]
    msb.solution = "source/#{PROJECT_NAME}.sln"
  end    
end

desc 'Run tests'
nunit :test => :compile do |nunit|
  include FileUtils
  mkpath ARTIFACTS_PATH unless Dir.exists? ARTIFACTS_PATH
  
  nunit.command = nunit_path
  nunit.assemblies "source/#{PROJECT_NAME}.Tests/bin/#{$config}/#{PROJECT_NAME}.Tests.dll"
  #nunit.options '/xml=nunit-console-output.xml'
  
  nunit.options = "/framework=#{CLR_TOOLS_VERSION}", '/noshadow', '/nologo', '/labels', "\"/xml=#{File.join(ARTIFACTS_PATH, "nunit-test-results.xml")}\""
end

desc 'Builds release package'
task :package => 'build:all' do
  include FileUtils
  
  assemble_path = File.join(ARTIFACTS_PATH, "assemble")
  
  mkpath ARTIFACTS_PATH unless Dir.exists? ARTIFACTS_PATH
  rm_rf Dir.glob(File.join(ARTIFACTS_PATH, "**/*.zip"))
  rm_rf assemble_path if Dir.exists? assemble_path
    
  mkpath assemble_path unless Dir.exists? assemble_path 
  rm_rf Dir.glob(File.join(assemble_path, "**/*"))
    
  cp_r Dir.glob("source/#{PROJECT_NAME}/bin/#{$config}/**"), assemble_path, :verbose => true
  rm Dir.glob("#{assemble_path}/log.*")
     
  zip_directory(assemble_path, File.join(ARTIFACTS_PATH, "#{PROJECT_NAME}-#{BUILD_VERSION}.zip"))
  rm_rf assemble_path if Dir.exists? assemble_path
end

namespace :nuget do
  desc 'Creates the nuspec file'
  nuspec :spec => :version do |nuspec|
    mkpath ARTIFACTS_PATH unless Dir.exists? ARTIFACTS_PATH
    
    nuspec.id = PROJECT_NAME
    nuspec.version = ENV['NUGET_VERSION']
    nuspec.authors = "j.weber"
    nuspec.description = "Utility to generate fault tolerant and highly configurable client proxies for WCF services based on WCF ServiceContracts. Supports making async calls using non async-ready ServiceContracts."
    nuspec.projectUrl = "https://github.com/jweber/WcfClientProxyGenerator"
    nuspec.title = PROJECT_NAME
    nuspec.tags = "wcf service client proxy dynamic async"
    nuspec.file "..\\source\\#{PROJECT_NAME}\\bin\\#{$config}\\#{PROJECT_NAME}.dll", 'lib\net45'
    nuspec.file "..\\source\\#{PROJECT_NAME}\\bin\\#{$config}\\#{PROJECT_NAME}.xml", 'lib\net45'

    nuspec.working_directory = 'build'
    nuspec.output_file = "#{PROJECT_NAME}.nuspec"
  end
  
  nugetpack :pack => ['build:all', :spec] do |nuget|
    nuget.command = nuget_path
    nuget.nuspec = "build\\#{PROJECT_NAME}.nuspec"
    nuget.base_folder = 'build'
    nuget.output = 'build'
  end
  
  nugetpush :push => [:pack] do |nuget|
    raise "No NuGet API key was defined" unless $nuget_api_key
  
    nuget.command = nuget_path
    nuget.package = "build\\#{PROJECT_NAME}.#{ENV['NUGET_VERSION']}.nupkg"
    nuget.create_only = false
    nuget.apikey = $nuget_api_key
    nuget.create_only = false
  end
end

desc 'Builds version environment variables'
task :versioning do
  ver = SemVer.find
  revision = (ENV['BUILD_NUMBER'] || ver.patch).to_i
  var = SemVer.new(ver.major, ver.minor, revision, ver.special)
  
  ENV['BUILD_VERSION'] = BUILD_VERSION = ver.format("%M.%m.%p.%s") + ".#{commit_data()[0]}"
  ENV['NUGET_VERSION'] = NUGET_VERSION = ver.format("%M.%m.%p-%s")
  ENV['FORMAL_VERSION'] = FORMAL_VERSION = "#{ SemVer.new(ver.major, ver.minor, revision).format "%M.%m.%p"}"
  puts "##teamcity[buildNumber '#{BUILD_VERSION}']"  
end

def nunit_path()
  File.join(Dir.glob(File.join('lib', 'nuget_packages', "nunit.runners.*")).sort.last, "tools", "nunit-console.exe")
end

def nuget_path()
  File.join(Dir.glob(File.join('lib', 'nuget_packages', "nuget.commandline.*")).sort.last, "tools", "nuget.exe")
end

def zip_directory(assemble_path, output_path)
  zip = ZipDirectory.new<commit_msg>Fixing issue with generating version number in the rakefile<commit_after>require 'albacore'
require 'fileutils'
require 'semver'

CLR_TOOLS_VERSION = 'v4.0.30319'
ARTIFACTS_PATH = "build"
PROJECT_NAME = "WcfClientProxyGenerator"

$config = ENV['config'] || 'Debug'
$nuget_api_key = ENV['nuget_api_key']

task :default => :compile

desc 'Generate the VersionInfo.cs class'
assemblyinfo :version => [:versioning] do |asm|
  git_data = commit_data()
  revision_hash = git_data[0]
  revision_date = git_data[1]
    
  asm.version = FORMAL_VERSION
  asm.file_version = FORMAL_VERSION
  asm.product_name = PROJECT_NAME
  asm.description = "Git commit hash: #{revision_hash} - #{revision_date}"
  asm.custom_attributes :AssemblyInformationalVersion => "#{BUILD_VERSION}"
  asm.output_file = "source/VersionInfo.cs"
  asm.namespaces 'System', 'System.Reflection'  
end

desc 'Compile the project'
task :compile do
  Rake::Task['build:net45'].invoke()
end

namespace :build do
  task :all => [:net45] do
  end

  msbuild :net45 => :version do |msb|
    msb.properties :configuration => $config, :Framework => 'NET45'
    msb.targets [:clean, :build]
    msb.solution = "source/#{PROJECT_NAME}.sln"
  end    
end

desc 'Run tests'
nunit :test => :compile do |nunit|
  include FileUtils
  mkpath ARTIFACTS_PATH unless Dir.exists? ARTIFACTS_PATH
  
  nunit.command = nunit_path
  nunit.assemblies "source/#{PROJECT_NAME}.Tests/bin/#{$config}/#{PROJECT_NAME}.Tests.dll"
  #nunit.options '/xml=nunit-console-output.xml'
  
  nunit.options = "/framework=#{CLR_TOOLS_VERSION}", '/noshadow', '/nologo', '/labels', "\"/xml=#{File.join(ARTIFACTS_PATH, "nunit-test-results.xml")}\""
end

desc 'Builds release package'
task :package => 'build:all' do
  include FileUtils
  
  assemble_path = File.join(ARTIFACTS_PATH, "assemble")
  
  mkpath ARTIFACTS_PATH unless Dir.exists? ARTIFACTS_PATH
  rm_rf Dir.glob(File.join(ARTIFACTS_PATH, "**/*.zip"))
  rm_rf assemble_path if Dir.exists? assemble_path
    
  mkpath assemble_path unless Dir.exists? assemble_path 
  rm_rf Dir.glob(File.join(assemble_path, "**/*"))
    
  cp_r Dir.glob("source/#{PROJECT_NAME}/bin/#{$config}/**"), assemble_path, :verbose => true
  rm Dir.glob("#{assemble_path}/log.*")
     
  zip_directory(assemble_path, File.join(ARTIFACTS_PATH, "#{PROJECT_NAME}-#{BUILD_VERSION}.zip"))
  rm_rf assemble_path if Dir.exists? assemble_path
end

namespace :nuget do
  desc 'Creates the nuspec file'
  nuspec :spec => :version do |nuspec|
    mkpath ARTIFACTS_PATH unless Dir.exists? ARTIFACTS_PATH
    
    nuspec.id = PROJECT_NAME
    nuspec.version = ENV['NUGET_VERSION']
    nuspec.authors = "j.weber"
    nuspec.description = "Utility to generate fault tolerant and highly configurable client proxies for WCF services based on WCF ServiceContracts. Supports making async calls using non async-ready ServiceContracts."
    nuspec.projectUrl = "https://github.com/jweber/WcfClientProxyGenerator"
    nuspec.title = PROJECT_NAME
    nuspec.tags = "wcf service client proxy dynamic async"
    nuspec.file "..\\source\\#{PROJECT_NAME}\\bin\\#{$config}\\#{PROJECT_NAME}.dll", 'lib\net45'
    nuspec.file "..\\source\\#{PROJECT_NAME}\\bin\\#{$config}\\#{PROJECT_NAME}.xml", 'lib\net45'

    nuspec.working_directory = 'build'
    nuspec.output_file = "#{PROJECT_NAME}.nuspec"
  end
  
  nugetpack :pack => ['build:all', :spec] do |nuget|
    nuget.command = nuget_path
    nuget.nuspec = "build\\#{PROJECT_NAME}.nuspec"
    nuget.base_folder = 'build'
    nuget.output = 'build'
  end
  
  nugetpush :push => [:pack] do |nuget|
    raise "No NuGet API key was defined" unless $nuget_api_key
  
    nuget.command = nuget_path
    nuget.package = "build\\#{PROJECT_NAME}.#{ENV['NUGET_VERSION']}.nupkg"
    nuget.create_only = false
    nuget.apikey = $nuget_api_key
    nuget.create_only = false
  end
end

desc 'Builds version environment variables'
task :versioning do
  ver = SemVer.find
  revision = (ENV['BUILD_NUMBER'] || ver.patch).to_i
  #ver = SemVer.new(ver.major, ver.minor, revision, ver.special)
  
  if ver.special != ''
    ENV['BUILD_VERSION'] = BUILD_VERSION = ver.format("%M.%m.%p.%s") + ".#{commit_data()[0]}"
    ENV['NUGET_VERSION'] = NUGET_VERSION = ver.format("%M.%m.%p-%s")
  else
    ENV['BUILD_VERSION'] = BUILD_VERSION = ver.format("%M.%m.%p") + ".#{commit_data()[0]}"
    ENV['NUGET_VERSION'] = NUGET_VERSION = ver.format("%M.%m.%p")  
  end
  
  ENV['FORMAL_VERSION'] = FORMAL_VERSION = "#{ SemVer.new(ver.major, ver.minor, revision).format "%M.%m.%p"}"
  puts "##teamcity[buildNumber '#{BUILD_VERSION}']"  
end

def nunit_path()
  File.join(Dir.glob(File.join('lib', 'nuget_packages', "nunit.runners.*")).sort.last, "tools", "nunit-console.exe")
end

def nuget_path()
  File.join(Dir.glob(File.join('lib', 'nuget_packages', "nuget.commandline.*")).sort.last, "tools", "nuget.exe")
end

def zip_directory(assemble_path, output_path)
  zip = ZipDirectory.new
{'fraction_non_alphanumeric': 0.15160987808690216, 'fraction_numerical': 0.004688965301656768, 'mean_word_length': 4.465831435079727, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5468145', 'n_tokens_mistral': 3606, 'n_tokens_neox': 3496, 'n_words': 694}