class Albacore::Project

A project encapsulates the properties from a xxproj file.

Attributes

ext[R]
proj_filename[R]
proj_filename_noext[R]
proj_path_base[R]
proj_xml_node[R]

Public Class Methods

new(proj_path) click to toggle source
# File lib/albacore/project.rb, line 87
def initialize proj_path
  raise ArgumentError, 'project path does not exist' unless File.exists? proj_path.to_s
  proj_path                       = proj_path.to_s unless proj_path.is_a? String
  @proj_xml_node                  = Nokogiri.XML(open(proj_path))
  @proj_path_base, @proj_filename = File.split proj_path
  @ext = File.extname @proj_filename
  @proj_filename_noext = File.basename @proj_filename, ext
  sanity_checks
end

Private Class Methods

find_ref(proj_xml, pkg_id) click to toggle source

find the node of pkg_id

# File lib/albacore/project.rb, line 498
def self.find_ref proj_xml, pkg_id
  @proj_xml.css("Project ItemGroup Reference[@Include*='#{pkg_id},']").first
end

Public Instance Methods

asmname() click to toggle source

get the assembly name specified in the project file

# File lib/albacore/project.rb, line 128
def asmname
  read_property('AssemblyName') || proj_filename_noext
end
assembly_info_path() click to toggle source

Get AssemblyInfo path @return string or project base path if path not found

# File lib/albacore/project.rb, line 383
def assembly_info_path
  result=@proj_xml_node.css("Compile[Include*='AssemblyInfo']").first #
  p     = if result.nil?
    @proj_path_base
  else
    File.expand_path(File.join(@proj_path_base, '/', Albacore::Paths.normalise_slashes(result.attributes["Include"].value)))
  end
  p
end
authors() click to toggle source

gets any authors from the project file

# File lib/albacore/project.rb, line 143
def authors
  read_property 'Authors'
end
debug_type(conf) click to toggle source
# File lib/albacore/project.rb, line 197
def debug_type conf
  dt = read_property "DebugType", conf
end
declared_packages() click to toggle source
# File lib/albacore/project.rb, line 309
def declared_packages
  return nuget_packages || paket_packages || []
end
declared_projects() click to toggle source
# File lib/albacore/project.rb, line 313
def declared_projects
  @proj_xml_node.css("ProjectReference").collect do |proj_ref|
    debug do
      ref_name = proj_ref.css("Name").inner_text
      "found project reference: #{name} => #{ref_name} [albacore: project]"
    end
    Project.new(File.join(@proj_path_base, Albacore::Paths.normalise_slashes(proj_ref['Include'])))
  end
end
default_assembly_version() click to toggle source

Reads assembly version information Returns 1.0.0.0 if AssemblyVersion is not found @return string

# File lib/albacore/project.rb, line 396
def default_assembly_version
  begin
    info= File.read(assembly_info_path)
    v   = info.each_line
              .select { |l| !(l.start_with?('//')||l.start_with?('/*')) && l.include?('AssemblyVersion(') }.first
    reg = /"(.*?)"/
    reg.match(v).captures.first
  rescue
    '1.0.0.0'
  end
end
default_platform() click to toggle source
# File lib/albacore/project.rb, line 193
def default_platform
  @proj_xml_node.css('Project PropertyGroup Platform').first.inner_text || 'AnyCPU'
end
description() click to toggle source
# File lib/albacore/project.rb, line 147
def description
  read_property 'Description'
end
faulty_refs() click to toggle source
# File lib/albacore/project.rb, line 289
def faulty_refs
  find_refs.to_a.keep_if { |r| r.children.css("HintPath").empty? }
end
find_packages() click to toggle source

Find all packages that have been declared and can be found in ./src/packages. This is mostly useful if you have that repository structure. returns enumerable Package

# File lib/albacore/project.rb, line 342
def find_packages
  declared_packages.collect do |package|
    guess = ::Albacore::PackageRepo.new(%w|./packages ./src/packages|).find_latest package.id
    debug "#{name}: guess: #{guess} [albacore: project]"
    guess
  end
end
find_refs() click to toggle source

find the NodeList reference list

# File lib/albacore/project.rb, line 284
def find_refs
  # should always be there
  @proj_xml_node.css("Project Reference")
end
guid() click to toggle source

Get the project GUID without '{' or '}' characters.

# File lib/albacore/project.rb, line 98
def guid
  guid_raw.gsub /[\{\}]/, ''
end
guid_raw() click to toggle source

Get the project GUID as it is in the project file.

# File lib/albacore/project.rb, line 103
def guid_raw
  read_property 'ProjectGuid'
end
has_faulty_refs?() click to toggle source
# File lib/albacore/project.rb, line 293
def has_faulty_refs?
  faulty_refs.any?
end
has_packages_config?() click to toggle source
# File lib/albacore/project.rb, line 297
def has_packages_config?
  File.exists? package_config
end
has_paket_deps?() click to toggle source
# File lib/albacore/project.rb, line 301
def has_paket_deps?
  File.exists? paket_deps
end
has_paket_refs?() click to toggle source
# File lib/albacore/project.rb, line 305
def has_paket_refs?
  File.exists? paket_refs
end
id() click to toggle source

Get the project id specified in the project file. Defaults to name.

# File lib/albacore/project.rb, line 108
def id
  (read_property 'Id') || name
end
included_files() click to toggle source

returns a list of the files included in the project

# File lib/albacore/project.rb, line 324
def included_files
  ['Compile', 'Content', 'EmbeddedResource', 'None'].map { |item_name|
    proj_xml_node.xpath("/x:Project/x:ItemGroup/x:#{item_name}",
                        'x' => "http://schemas.microsoft.com/developer/msbuild/2003").collect { |f|
      debug "#{name}: #included_files looking at '#{f}' [albacore: project]"
      link = f.elements.select { |el| el.name == 'Link' }.map { |el| el.content }.first
      OpenStruct.new(
          :item_name => item_name.downcase,
          :link      => link,
          :include   => f['Include']
      )
    }
  }.flatten
end
license() click to toggle source

the license that the project has defined in the metadata in the xxproj file.

# File lib/albacore/project.rb, line 152
def license
  read_property 'License'
end
name() click to toggle source

Get the project name specified in the project file. This is the same as the title of the nuspec and, if Id is not specified, also the id of the nuspec.

# File lib/albacore/project.rb, line 115
def name
  read_property('Name') || asmname || proj_filename_noext
end
Also aliased as: title
namespace() click to toggle source

Get the root namespace of the project

# File lib/albacore/project.rb, line 133
def namespace
  read_property 'RootNamespace'
end
netcore?() click to toggle source

The project is a .Net Core project.

# File lib/albacore/project.rb, line 123
def netcore?
  ! @proj_xml_node.css('Project').attr('Sdk').nil?
end
output_dll(conf, fw) click to toggle source

Gets the relative location (to the project base path) of the dll that it will output

# File lib/albacore/project.rb, line 279
def output_dll conf, fw
  output_paths(conf, fw).keep_if { |o| o.library? }.first
end
output_file_ext() click to toggle source

“.exe”?, “.dll”?

# File lib/albacore/project.rb, line 184
def output_file_ext
  case output_type
  when OutputArtifact::LIBRARY
    ".dll"
  when OutputArtifact::EXECUTABLE
    ".exe"
  end
end
output_type() click to toggle source

OutputArtifact::LIBRARY OutputArtifact::EXECUTABLE

# File lib/albacore/project.rb, line 171
def output_type
  ot = read_property 'OutputType'
  case ot
  when 'Library'
    OutputArtifact::LIBRARY
  when 'Exe'
    OutputArtifact::EXECUTABLE
  else
    ot
  end
end
outputs(conf, fw) click to toggle source

Returns OutputArtifact[] or throws an error

# File lib/albacore/project.rb, line 243
def outputs conf, fw
  os = try_outputs(conf, fw)
  if os.empty?
    raise(ConfigurationNotFoundError, "could not find configuration '#{conf}'")
  else
    os
  end
end
package_config() click to toggle source

get the full path of 'packages.config'

# File lib/albacore/project.rb, line 362
def package_config
  File.join @proj_path_base, 'packages.config'
end
paket_deps() click to toggle source

Get the full path of 'paket.dependencies'

# File lib/albacore/project.rb, line 367
def paket_deps
  File.join @proj_path_base, 'paket.dependencies'
end
paket_refs() click to toggle source

Get the full path of 'paket.references'

# File lib/albacore/project.rb, line 372
def paket_refs
  File.join @proj_path_base, 'paket.references'
end
path() click to toggle source

get the path of the project file

# File lib/albacore/project.rb, line 351
def path
  File.join @proj_path_base, @proj_filename
end
save(output = nil) click to toggle source

save the xml

# File lib/albacore/project.rb, line 356
def save(output = nil)
  output = path unless output
  File.open(output, 'w') { |f| @proj_xml_node.write_xml_to f }
end
symbols?(conf='Debug', platform='AnyCPU') click to toggle source
# File lib/albacore/project.rb, line 165
def symbols? conf='Debug', platform='AnyCPU'
  read_property('DebugSymbols', conf) == 'true'
end
target_framework() click to toggle source

the target .NET Framework / .NET version

# File lib/albacore/project.rb, line 202
def target_framework
  read = read_property('TargetFrameworkVersion')
  case read
  when 'v3.5'
    'net35'
  when 'v3.5.1'
    'net351'
  when 'v4.0'
    'net40'
  when 'v4.5'
    'net45'
  when 'v4.5.1'
    'net451'
  when 'v4.6'
    'net46'
  when 'v4.6.1'
    'net461'
  when 'v4.6.2'
    'net462'
  when 'v5.0'
    'net50'
  when 'v5.0.1'
    'net501'
  else
    read
  end
end
target_frameworks() click to toggle source

Gets the target frameworks as specified by .Net Core syntax

# File lib/albacore/project.rb, line 231
def target_frameworks
  if netcore?
    tfw = @proj_xml_node.css('Project PropertyGroup TargetFramework').inner_text
    tfws = @proj_xml_node.css('Project PropertyGroup TargetFrameworks').inner_text
    nfws = if tfw.nil? || tfw == '' then tfws else tfw end
    fws = nfws.split(';')
  else
    [ target_framework ]
  end
end
title()

The same as name

Alias for: name
to_s() click to toggle source

Gets the path of the project file

# File lib/albacore/project.rb, line 377
def to_s
  path
end
try_outputs(conf, fw) click to toggle source
# File lib/albacore/project.rb, line 252
def try_outputs conf, fw
  outputs = []
  if netcore? then
    outputs << OutputArtifact.new("bin/#{conf}/#{fw}/#{asmname}#{output_file_ext}", output_type)
    outputs << OutputArtifact.new("bin/#{conf}/#{fw}/#{asmname}.xml", OutputArtifact::XMLDOC) if xmldoc?
  else
    path = read_property 'OutputPath', conf, default_platform
    if path != ''
      full_path = Albacore::Paths.join(path, "#{asmname}#{output_file_ext}").to_s
      outputs << OutputArtifact.new(full_path, output_type)
    end

    if xmldoc? conf, default_platform
      xml_full_path = read_property 'DocumentationFile', conf
      outputs << OutputArtifact.new(xml_full_path, OutputArtifact::XMLDOC)
    end

    if symbols? conf, default_platform
      pdb_full_path = Albacore::Paths.join(path, "#{asmname}.pdb").to_s
      outputs << OutputArtifact.new(pdb_full_path, OutputArtifact::SYMBOLS)
    end
  end
  outputs
end
version() click to toggle source

gets the version from the project file

# File lib/albacore/project.rb, line 138
def version
  read_property 'Version'
end
xmldoc?(conf='Debug', platform='AnyCPU') click to toggle source
# File lib/albacore/project.rb, line 156
def xmldoc? conf='Debug', platform='AnyCPU'
  if netcore?
    gdf = read_property('GenerateDocumentationFile')
    !gdf.nil? && gdf == 'true'
  else
    ! read_property('DocumentationFile', conf, platform).nil?
  end
end

Private Instance Methods

all_paket_deps() click to toggle source
# File lib/albacore/project.rb, line 424
def all_paket_deps
  return @all_paket_deps if @all_paket_deps

  path = if File.exists?('paket.lock') then
      'paket.lock'
    else
      File.join(@proj_path_base, "paket.lock")
    end

  arr = File.open(path, 'r') do |io|
    lines = io.readlines.map(&:chomp)
    Albacore::Paket.parse_paket_lock(lines)
      .map { |depid, dep|
        [ depid,
          target_frameworks.map { |fw|
            dep2 = OpenStruct.new dep
            dep2[:target_framework] = fw
            dep2
          }
        ]
      }
  end

  @all_paket_deps = Hash[arr]
end
nuget_packages() click to toggle source
# File lib/albacore/project.rb, line 411
def nuget_packages
  return nil unless has_packages_config?
  doc = Nokogiri.XML(open(package_config))
  doc.xpath("//packages/package").collect { |p|
    OpenStruct.new(:id               => p[:id],
                   :version          => p[:version],
                   :target_framework => p[:targetFramework],
                   :semver           => Albacore::SemVer.parse(p[:version], '%M.%m.%p', false)
    )
  }

end
paket_package_by_id!(id, arr, ref_type) click to toggle source
# File lib/albacore/project.rb, line 476
def paket_package_by_id! id, arr, ref_type
  pkgs = all_paket_deps[id]
  if ! pkgs.nil? && pkgs.length > 0
    debug { "found #{ref_type} package '#{id}' [project: paket_packages]" }
    arr.concat(pkgs)
  else
    warn { "found #{ref_type} package '#{id}' not in paket.lock [project: paket_packages]" }
  end
end
paket_packages() click to toggle source
# File lib/albacore/project.rb, line 450
def paket_packages
  return nil unless has_paket_deps? || has_paket_refs?
  info { "extracting paket dependencies from '#{to_s}' and 'paket.{dependencies,references}' in its folder [project: paket_package]" }
  all_refs = []

  if has_paket_refs?
    File.open paket_refs, 'r' do |io|
      lines = io.readlines.map(&:chomp)
      lines.compact.each do |line|
        paket_package_by_id! line, all_refs, 'referenced'
      end
    end
  end

  if has_paket_deps?
    File.open paket_deps, 'r' do |io|
      lines = io.readlines.map(&:chomp)
      Albacore::Paket.parse_dependencies_file(lines).each do |line|
        paket_package_by_id! line, all_refs, 'dependent'
      end
    end
  end

  all_refs.uniq
end
read_property(prop_name, conf='Debug', platform='AnyCPU') click to toggle source
# File lib/albacore/project.rb, line 490
def read_property prop_name, conf='Debug', platform='AnyCPU'
  specific = @proj_xml_node.css("Project PropertyGroup[Condition*='#{conf}|#{platform}'] #{prop_name}")
  chosen = if specific.empty? then @proj_xml_node.css("Project PropertyGroup #{prop_name}") else specific end
  first = chosen.first
  if first.nil? then nil else first.inner_text end
end
sanity_checks() click to toggle source
# File lib/albacore/project.rb, line 486
def sanity_checks
  warn { "project '#{@proj_filename}' has no name" } unless name
end