class Albacore::NugetModel::Package

the nuget package element writer

Attributes

files[RW]

the files is something enumerable that corresponds to the file elements inside '//package/files'.

metadata[RW]

the metadata corresponds to the metadata element of the nuspec

Public Class Methods

from_xml(xml) click to toggle source

read the nuget specification from a nuspec file

# File lib/albacore/nuget_model.rb, line 439
def self.from_xml xml
  ns = { ng: 'http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd' }
  parser = Nokogiri::XML(xml)
  meta = Metadata.from_xml(parser.xpath('.//ng:metadata', ns))
  files = parser.
    xpath('.//ng:files', ns).
    children.
    reject { |n| n.text? or n['src'].nil? }.
    collect { |n| FileItem.new n['src'], n['target'], n['exclude'] }
  Package.new meta, files
end
from_xxproj(proj, *opts) click to toggle source

Read the nuget specification from a xxproj instance (e.g. csproj, fsproj) Options:

- symbols
 Specifies the version to use for constructing the nuspec's lib folder
- known_projects
- configuration
- project_dependencies
 Specifies whether to follow the project dependencies. See nuget_model_spec.rb
 for examples of usage of this property.
- nuget_dependencies
# File lib/albacore/nuget_model.rb, line 467
def self.from_xxproj proj, *opts
  opts = Map.options(opts || {}).
    apply({
      symbols:                false,
      known_projects:         Set.new,
      configuration:          'Debug',
      project_dependencies:   true,
      verify_files:           false,
      nuget_dependencies:     true,
      framework_dependencies: true,
      metadata:               Metadata.new })

  trace { "#from_xxproj proj: '#{proj}' opts: #{opts} [nuget model: package]" }

  version = opts.get :version
  package = Package.new(opts.get(:metadata))
  package.metadata.id      = proj.id if proj.id
  package.metadata.title   = proj.name if proj.name
  package.metadata.version = version if version
  package.metadata.authors = proj.authors if proj.authors

  if opts.get :nuget_dependencies
    trace "adding nuget dependencies for id #{proj.id} [nuget model: package]"
    # add declared packages as dependencies
    proj.declared_packages.each do |p|
      # p is a Package
      debug "adding package dependency: #{proj.id} => #{p.id} at #{p.version}, fw #{p.target_framework} [nuget model: package]"
      package.metadata.add_dependency p.id, p.version, p.target_framework, group=true
    end
  end

  if opts.get :project_dependencies
    # add declared projects as dependencies
    proj.
      declared_projects.
      keep_if { |p| opts.get(:known_projects).include? p.id }.
      each do |p|
      # p is a Project
      debug "adding project dependency: #{proj.id} => #{p.id} at #{version}, fw #{p.target_frameworks.inspect} [nuget model: package]"
      p.target_frameworks.each do |fw|
        package.metadata.add_dependency p.id, version, fw, group=true
      end
    end
  end

  fd = opts.get :framework_dependencies
  if fd && fd.respond_to?(:each)
    fd.each { |n, p|
      package.metadata.add_framework_dependency p.id, p.version, p.target_framework, p.group
    }
  end

  debug "including files for frameworks #{proj.target_frameworks.inspect}"
  proj.target_frameworks.each do |fw|
    conf = opts.get(:configuration)

    proj.outputs(conf, fw).each do |output|
      lib_filepath = %W|lib #{fw}|.join(Albacore::Paths.separator)
      debug "adding output file #{output.path} => #{lib_filepath} [nuget model: package]"
      package.add_file output.path, lib_filepath
    end

    if opts.get :sources
      compile_files = proj.included_files.keep_if { |f| f.item_name == "compile" }

      debug "add compiled files: #{compile_files} [nuget model: package]"
      compile_files.each do |f|
        target = %W[src #{Albacore::Paths.normalise_slashes(f.include)}].join(Albacore::Paths.separator)
        package.add_file f.include, target
      end
    end
  end

  if opts.get :verify_files
    package.files.each do |file|
      file_path = File.expand_path file.src, proj.proj_path_base
      unless File.exists? file_path
        info "while building nuspec for proj id: #{proj.id}, file: #{file_path} => #{file.target} not found, removing from nuspec [nuget model: package]"
        package.remove_file file.src
        trace { "files: #{package.files.map { |f| f.src }.inspect} [nuget model: package]" }
      end
    end
  end

  package
end
from_xxproj_file(file, *opts) click to toggle source

read the nuget specification from a xxproj file (e.g. csproj, fsproj)

# File lib/albacore/nuget_model.rb, line 452
def self.from_xxproj_file file, *opts
  proj = Albacore::Project.new file
  from_xxproj proj, *opts
end
get_output_path(proj, opts) click to toggle source
# File lib/albacore/nuget_model.rb, line 554
def self.get_output_path proj, opts
  try = proj.try_output_path(opts.get(:configuration))
  return try if try
  warn 'using fallback output path'
  proj.fallback_output_path
end
new(metadata = nil, files = nil) click to toggle source

creates a new nuspec package instance

# File lib/albacore/nuget_model.rb, line 348
def initialize metadata = nil, files = nil
  @metadata = metadata || Metadata.new
  @files = files || []
end

Public Instance Methods

add_file(src, target, exclude = nil) click to toggle source

add a file to the instance

# File lib/albacore/nuget_model.rb, line 354
def add_file src, target, exclude = nil
  @files << FileItem.new(src, target, exclude)
  self
end
merge_with(other) click to toggle source

creates a new Package/Metadata by overriding data in this instance with data from passed instance

# File lib/albacore/nuget_model.rb, line 415
def merge_with other
  m_next = @metadata.merge_with other.metadata
  files_res = {}

  # my files
  @files.each { |f| files_res[f.src] = f }

  # overrides
  other.files.each { |f| files_res[f.src] = f }

  # result
  f_next = files_res.collect { |k, v| v }

  Package.new m_next, f_next
end
remove_file(src) click to toggle source

remove the file denoted by src

# File lib/albacore/nuget_model.rb, line 360
def remove_file src
  src = src.src if src.respond_to? :src # if passed an OpenStruct e.g.
  trace { "remove_file: removing file '#{src}' [nuget model: package]" }
  @files = @files.reject { |f| f.src == src }
end
to_s() click to toggle source
# File lib/albacore/nuget_model.rb, line 431
def to_s
  "NugetModel::Package(files: #{@files.map(&:to_s)}, metadata: #{ @metadata.to_s })"
end
to_template() click to toggle source
# File lib/albacore/nuget_model.rb, line 373
def to_template
  lines = @metadata.to_template

  unless @files.empty?
    lines << 'files'
    @files.each do |file|
      lines << "  #{file.src} ==> #{file.target}" unless file.exclude
      lines << "  !#{file.src}" if file.exclude
    end
  end

  lines
end
to_xml() click to toggle source

gets the current package as a xml node

# File lib/albacore/nuget_model.rb, line 409
def to_xml
  to_xml_builder.to_xml
end
to_xml_builder() click to toggle source

gets the current package as a xml builder

# File lib/albacore/nuget_model.rb, line 388
def to_xml_builder
  md = Nokogiri::XML(@metadata.to_xml).at_css('metadata').to_xml
  Nokogiri::XML::Builder.new(encoding: 'utf-8') do |x|
    x.package(xmlns: 'http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd') {
      x << md
      unless @files.empty?
        x.files {
          @files.each do |f|
            if f.exclude
              x.file src: f.src, target: f.target, exclude: f.exclude
            else
              x.file src: f.src, target: f.target
            end
          end
        }
      end
    }
  end
end
with_metadata() { |metadata| ... } click to toggle source

do something with the metadata. returns the self Package instance

# File lib/albacore/nuget_model.rb, line 368
def with_metadata &block
  yield @metadata if block_given?
  self
end