class Object

Constants

VERSIONING

Public Instance Methods

add_files(stage, what_dlls, nuspec, folder='lib') click to toggle source
# File lib/build.rb, line 34
def add_files stage, what_dlls, nuspec, folder='lib'
  [['net35', 'net-3.5'], ['net40', 'net-4.0'], ['net45', 'net-4.5']].each{|fw|
    takeFrom = File.join(stage, fw[1], what_dlls)
    Dir.glob(takeFrom).each do |f|
      nuspec.file(f.gsub("/", "\\"), "#{folder}\\#{fw[0]}")
    end
  }
end
auto_add_dependencies(project, nuspec) click to toggle source
# File lib/dependency.rb, line 1
def auto_add_dependencies(project, nuspec)
  @Dependencies = Array.new

  each_package_dependency(project) do |package|
    @Dependencies << package
  end

  @Dependencies.each do |dep|
    nuspec.dependency dep.Name, dep.Version
  end
end
auto_update_local_semver() click to toggle source
# File lib/semantic_versioning.rb, line 274
def auto_update_local_semver
  versioning = SemverVersioning.new
  versioning.auto_update_local_semver
end
auto_update_semver(project_name, semver_location, semver_file, semver_dimension) click to toggle source
# File lib/semantic_versioning.rb, line 269
def auto_update_semver project_name, semver_location, semver_file, semver_dimension
  versioning = SemverVersioning.new
  versioning.auto_update_semver project_name, semver_location, semver_file, semver_dimension
end
check_devkit() click to toggle source

devkit version check

# File lib/install.rb, line 94
def check_devkit
  # check if devkit exists
  puts 'Checking devkit version...'
  rubyPath = `where.exe ruby.exe`
  rubyPath['bin\ruby.exe'] = 'devkit'
  rubyPath = rubyPath.sub("\n", '') #doublequotes required for Line break, gotcha
  if (!File.directory?(rubyPath))
    puts 'devkit not found'
    # if not download, install and setup devkit
    puts 'Downloading devkit...'

    sourcePath = 'cdn.rubyinstaller.org'
    versionedExeFile = $selected_version[:devkit]
    filePath = "/archives/devkits/#{versionedExeFile}?direct"
    return false if !download_file(sourcePath, filePath, versionedExeFile)

    puts 'Devkit installation in progress...'
    cmd = "#{versionedExeFile} -y -o\"#{rubyPath}\""  #no space after -o, gotcha
    puts cmd
    return false if !install_file(cmd, versionedExeFile)

    puts 'Setting up devkit...'
    Dir.chdir "#{rubyPath}"
    cmd = 'ruby dk.rb init'
    cmdoutput = `#{cmd}`
    puts cmdoutput
    if !cmdoutput.to_s.include?('Initialization complete!')
      puts 'Error: could not initialize devkit.'
      return false
    end

    cmd = 'ruby dk.rb review'
    cmdoutput = `#{cmd}`
    puts cmdoutput
    if !cmdoutput.to_s.include?('DevKit functionality will be injected')
      puts 'Error: devkit review failed'
      return false
    end

    cmd = 'ruby dk.rb install'
    cmdoutput = `#{cmd}`
    puts cmdoutput
    puts 'Restart console since environment variables are now updated'

    return false
  end

  puts 'devkit ok'
  return true
end
check_gems() click to toggle source

check and install required gems

# File lib/install.rb, line 146
def check_gems
  puts 'Checking required gems...'
  $selected_version[:gems].each {|key, value|
    if !gem_exists(key, "=#{value}")
      begin
        puts "Installing missing gem: #{key}"
        cmd = "gem install --user-install #{key} -v #{value}"
        cmdoutput = `#{cmd}`
        puts cmdoutput
      rescue
        puts 'gem install failed'
        puts $!
        return false  # don't continue if even one gem install fails
      end
    end
  }
  # all gems installed
  return true
end
check_package_version_dependency(package_uri, package, version='IsLatestVersion') click to toggle source

checks nuget dependency

# File lib/build.rb, line 179
def check_package_version_dependency package_uri, package, version='IsLatestVersion'
  # retrieve package version info
  response = Net::HTTP.get_response(URI.parse(package_uri.sub('pkg', package))) # get_response takes a URI object
  package_info = response.body
  xml = Nokogiri::XML package_info
  xml.xpath("//m:properties/d:#{version}").each { |e|
    if e.text.to_s == 'true'
      version = e.parent.xpath('d:Version').text
    end
  }

  # grab all packages.config files
  config_files = Dir.glob '**/packages.config'

  # for each file match version. Return false is check fails
  config_files.each{ |file|
    doc = Nokogiri::XML(File.read file)
    node = doc.at_xpath("//*[@id=\"#{package}\"]")
    if !node.nil?
      config_version = node.attr('version')
      puts "Package: #{package} Latest Version: #{version} File: #{file} File package version: #{config_version}"
      return false unless config_version.to_s == version
    end
  }

  return true
end
check_ruby() click to toggle source

Ruby version check

# File lib/install.rb, line 75
def check_ruby
  puts 'Checking ruby version...'
  # check if expected ruby version exists. If not download and install ruby
  rubyVersion = "#{RUBY_VERSION}"
  localVersion = "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
  expectedVersion1 = '1.9.3-p551'
  expectedVersion2 = '2.0.0-p481'
  if (!rubyVersion.nil? && (localVersion != expectedVersion1 && localVersion != expectedVersion2))
    puts "Uninstall incompatible version of ruby: #{localVersion} and install version: #{expectedVersion1} or #{expectedVersion2}. Then Restart rake. Goto http://rubyinstaller.org/downloads/"
    return false
  end

  $selected_version = $version_map[localVersion]
  puts 'Ruby ok'

  return true
end
clean_build(msb, solution) click to toggle source
# File lib/build.rb, line 100
def clean_build msb, solution
  msb.properties :Configuration => 'Release',
    :Platform => 'Any CPU',
    :VisualStudioVersion => '12.0'
  msb.use :net4
  msb.solution = solution
end
cleantask(props) click to toggle source
# File lib/build.rb, line 67
def cleantask props

  if props.has_key?(:output) && File.directory?(props[:output])
    FileUtils.rm_rf props[:output]
    waitfor { !exists?(props[:output]) }
  end

  if props.has_key?(:artifacts) && File.directory?(props[:artifacts])
    FileUtils.rm_rf props[:artifacts]
    waitfor { !exists?(props[:artifacts]) }
  end

  if props.has_key?(:output)
    Dir.mkdir props[:output]
  end
  if props.has_key?(:artifacts)
    Dir.mkdir props[:artifacts]
  end
end
commit_data() click to toggle source
# File lib/build.rb, line 43
def commit_data
  begin
    commit = `git rev-parse --short HEAD`.chomp()[0,6]
    git_date = `git log -1 --date=iso --pretty=format:%ad`
    commit_date = DateTime.parse( git_date ).strftime("%Y-%m-%d %H%M%S")
  rescue Exception => e
    puts e.inspect
    commit = (ENV['BUILD_VCS_NUMBER'] || "000000")[0,6]
    commit_date = Time.new.strftime("%Y-%m-%d %H%M%S")
  end
  [commit, commit_date]
end
copy_full_directory(fromDir, outDir) click to toggle source
# File lib/build.rb, line 11
def copy_full_directory fromDir, outDir
   FileUtils.cp_r(File.join(fromDir, "."), outDir)
end
copy_output_files(fromDir, filePattern, outDir) click to toggle source
# File lib/build.rb, line 4
def copy_output_files fromDir, filePattern, outDir
  FileUtils.mkdir_p outDir unless exists?(outDir)
  Dir.glob(File.join(fromDir, filePattern)){|file|
    copy(file, outDir) if File.file?(file)
  }
end
copy_runtime_artifacts(package) click to toggle source
# File lib/build.rb, line 207
def copy_runtime_artifacts package

  # grab all packages.config files
  config_files = Dir.glob '**/packages.config'
  config_version = ''

  # find package version
  config_files.each{ |file|
    doc = Nokogiri::XML(File.read file)
    node = doc.at_xpath("//*[@id=\"#{package}\"]")
    if !node.nil?
      config_version = node.attr('version').to_s
      break
    end
  }
  puts config_version

  # copy artifacts
  source = "#{ENV['RuntimePath']}/#{config_version}"
  dest = File.join(File.expand_path('.'), 'RuntimeService')
  puts source
  puts dest
  copy_full_directory source, dest
end
download_file(sourcePath, filePath, localFileLocation) click to toggle source
# File lib/install.rb, line 2
def download_file(sourcePath, filePath, localFileLocation)
  Net::HTTP.start(sourcePath) do |http|
    resp = http.get(filePath)
    f = open(localFileLocation, 'wb')
    begin
      requestPath = "http://#{sourcePath}#{filePath}"
      puts "Downloading #{requestPath}..."
      http.request_get(requestPath) do |resp|
        resp.read_body do |segment|
          f.write(segment)
        end
      end
    rescue
      puts $!
      return false
    ensure
      f.close()
    end
  end
  return true
end
each_package(packages_config) { |attributes, attributes| ... } click to toggle source
# File lib/dependency.rb, line 24
def each_package(packages_config)
  xml = File.read packages_config
  doc = REXML::Document.new xml
  doc.elements.each 'packages/package' do |package|
    if block_given?
      yield package.attributes['id'], package.attributes['version']
    else
      "no package block"
    end
  end
end
each_package_dependency(project) { |dependency id, version| ... } click to toggle source
# File lib/dependency.rb, line 14
def each_package_dependency(project)
  packages_config = File.join File.dirname(project), 'packages.config'
  return [] unless File.exists? packages_config

  each_package packages_config do |id, version|
    yield Dependency.new id, version
  end
end
gem_exists(name, version) click to toggle source
# File lib/install.rb, line 40
def gem_exists(name, version)
  begin
    gem name, version
  rescue Gem::LoadError
    puts "failed to load gem #{name} -v #{version}"
    Gem.clear_paths
    return false
  end
  return true
end
get_commit_hash_and_date() click to toggle source
# File lib/build.rb, line 22
def get_commit_hash_and_date
  begin
    commit = `git log -1 --pretty=format:%H`
    git_date = `git log -1 --date=iso --pretty=format:%ad`
    commit_date = DateTime.parse( git_date ).strftime("%Y-%m-%d %H%M%S")
  rescue
    commit = "git unavailable"
  end

  [commit, commit_date]
end
install_file(cmd, versionedExeFile) click to toggle source
# File lib/install.rb, line 24
def install_file(cmd, versionedExeFile)
  begin
    installerOutput = `#{cmd}`
    puts installerOutput
  rescue
    puts $!
    return false
  ensure
    puts 'Cleaning up...'
    cmd = "#{versionedExeFile}"
    installerOutput = `del #{cmd}`
    puts installerOutput
  end
  return true
end
is_team_city_run() click to toggle source
# File lib/semantic_versioning.rb, line 279
def is_team_city_run
  ENV['BUILD_VCS_NUMBER'] != nil
end
package(rake_files_path, task_list) click to toggle source

Gathers all rake files and runs the task list

# File lib/build.rb, line 233
def package rake_files_path, task_list
  if task_list.nil?
    puts "No task to execute"
  else
    Dir.glob("#{rake_files_path}/*.rb").each {
      |r| load r
    }
    task_list.each { |eachtask|
      Rake::Task["#{eachtask}"].invoke
    }
  end
end
project_outputs(props) click to toggle source
# File lib/build.rb, line 16
def project_outputs props
  props[:projects].map{ |p| "src/#{p}/bin/#{BUILD_CONFIG}/#{p}.dll" }.
    concat( props[:projects].map{ |p| "src/#{p}/bin/#{BUILD_CONFIG}/#{p}.exe" } ).
    find_all{ |path| exists?(path) }
end
restore_nuget(nuget_exe) click to toggle source
# File lib/build.rb, line 87
def restore_nuget nuget_exe
  sh 'powershell', '-Command', "& { Invoke-RestMethod https://www.nuget.org/nuget.exe -OutFile '#{Dir.pwd}/#{nuget_exe}' }"
  sh "#{Dir.pwd}/#{nuget_exe}", 'restore', "src/#{PRODUCT}.sln"
end
restore_project_pkgs(proj) click to toggle source
# File lib/build.rb, line 92
def restore_project_pkgs proj
  msbuild :nuget_restore do |msb|
    msb.use :net4
    msb.targets :RestorePackages
    msb.solution = proj
  end
end
set_framework_version(asm) click to toggle source
# File lib/build.rb, line 156
def set_framework_version asm
  set_version asm, $versionMap[:formal_version], $versionMap[:formal_version], $versionMap[:build_version], 'Framework'
end
set_solution_version(asm) click to toggle source
# File lib/build.rb, line 160
def set_solution_version asm
  set_version asm, $versionMap[:platform_version], $versionMap[:platform_version], $versionMap[:platform_build_version], 'Solution'
end
set_version(asm, version, file_version, assembly_version, output_file, output_file_src='src') click to toggle source
# File lib/build.rb, line 164
def set_version asm, version, file_version, assembly_version, output_file, output_file_src='src'
  # Assembly file config
  asm.product_name = PRODUCT
  asm.description = PRODUCT_DESCRIPTION
  asm.version = version
  asm.file_version = file_version
  asm.custom_attributes :AssemblyInformationalVersion => assembly_version,
    :ComVisibleAttribute => false,
    :CLSCompliantAttribute => true
  asm.copyright = COPYRIGHT
  asm.output_file = "#{output_file_src}/#{output_file}.Version.cs"
  asm.namespaces 'System', 'System.Reflection', 'System.Runtime.InteropServices'
end
teamcity_build_number() click to toggle source

Tell teamcity our decision to set the build.number environment variable

# File lib/build.rb, line 151
def teamcity_build_number
  platform_version  = Time.new.strftime('%y.%-m.%-d') + ".#{(ENV['BUILD_NUMBER'] || '0')}"
  puts "##teamcity[buildNumber '#{platform_version}']"
end
versioning(project=nil, file=nil) click to toggle source
# File lib/build.rb, line 108
def versioning project=nil, file=nil
  if !project.nil? && !file.nil?
    v = SemVer.new
    path = File.join VERSIONING, file
    v.load path
    ver = v
    commitData = commit_data()
    # nuget (not full semver 2.0.0-rc.1 support) see http://nuget.codeplex.com/workitem/1796
    ENV["#{project}_NUGET_VERSION"] = ver.format('%M.%m.%p%s')

    # extensible number w/ git hash
    ENV["#{project}_BUILD_VERSION"] =  ver.format('%M.%m.%p%s') + ".#{commitData[0]}"

    # purely M.m.p format
    sem =  SemVer.new(ver.major, ver.minor,  ver.patch).format '%M.%m.%p'
    build_counter = (ENV['BUILD_COUNTER'] || '0').to_i
    ENV["#{project}_FORMAL_VERSION"] = "#{sem}.#{build_counter}"
  else
    ver = SemVer.find

    commitData = commit_data()

    build_counter = (ENV['BUILD_COUNTER'] || '0').to_i

    # extensible number w/ git hash
    ENV['BUILD_VERSION'] = $versionMap[:build_version] = ver.format('%M.%m.%p%s') + ".#{commitData[0]}"

    # nuget (not full semver 2.0.0-rc.1 support) see http://nuget.codeplex.com/workitem/1796
    ENV['NUGET_VERSION'] = $versionMap[:nuget_version] = ver.format('%M.%m.%p%s')

    ENV['PLATFORM_VERSION'] = $versionMap[:platform_version] = Time.new.strftime('%y.%-m.%-d') + ".#{build_counter}"

    ENV['PLATFORM_BUILD_VERSION'] = $versionMap[:platform_build_version] = Time.new.strftime('%y.%-m.%-d') + ".#{commitData[0]}"

    sem =  SemVer.new(ver.major, ver.minor,  ver.patch).format '%M.%m.%p'

    ENV['FORMAL_VERSION'] = $versionMap[:formal_version] = "#{sem}.#{build_counter}"

    teamcity_build_number
  end
end
waitfor(&block) click to toggle source
# File lib/build.rb, line 56
def waitfor &block
  checks = 0

  until block.call || checks >10
    sleep 0.5
    checks += 1
  end

  raise 'Waitfor timeout expired. Make sure that you aren\'t running something from the build output folders, or that you have browsed to it through Explorer.' if checks > 10
end