创建NuGet包时,如何为程序集指定框架版本?

时间:2012-12-26 19:24:49

标签: c# nuget nuget-package

我正在尝试使用NuGet核心创建一个NuGet包作为构建过程的一部分。构建过程创建特定的框架版本,3.5,4.0和4.5。如何为每个程序集指定框架版本?

public void Create(Package package)
{
    if (String.IsNullOrEmpty(package.Id))
        throw new ArgumentException("package.Id is required", "package");

    if (String.IsNullOrEmpty(package.Version))
        throw new ArgumentException("package.Version is required", "package");

    if (String.IsNullOrEmpty(package.Authors))
        throw new ArgumentException("package.Authors is required", "package");

    if (String.IsNullOrEmpty(package.Description))
        throw new ArgumentException("package.Description is required", "package");

    if (package.Files == null || package.Files.Count == 0)
        throw new ArgumentException("package.Files cannot be empty", "package");

    ManifestMetadata metadata = new ManifestMetadata();
    metadata.Id = package.Id;
    metadata.Version = package.Version;
    metadata.Title = package.Title;
    metadata.Authors = package.Authors;
    metadata.Owners = package.Authors;
    metadata.IconUrl = package.IconUrl;
    metadata.ProjectUrl = package.ProjectUrl;
    metadata.RequireLicenseAcceptance = true;
    metadata.LicenseUrl = package.LicenseUrl;
    metadata.Summary = package.Summary;
    metadata.Description = package.Description;
    metadata.Copyright = package.Copyright;
    metadata.Language = "en-US";
    metadata.Tags = package.Tags;
    metadata.ReleaseNotes = package.ReleaseNotes;

    List<ManifestFile> manifestFiles = new List<ManifestFile>();

    foreach (var file in package.Files)
    {
        ManifestFile manifestFile = new ManifestFile();
        manifestFile.Source = Path.GetFileName(file);
        manifestFile.Target = @"lib\" + Path.GetFileName(file);
        manifestFiles.Add(manifestFile);
    }

    PackageBuilder builder = new PackageBuilder();
    builder.PopulateFiles(ExtractPath(package.Files[0]), manifestFiles);
    builder.Populate(metadata);

    using (FileStream stream = File.Open(package.PackagePath, FileMode.OpenOrCreate))
    {
        builder.Save(stream);
    }
}

private static string ExtractPath(string fullPath)
{
    if (fullPath.Length == 0)
        throw new ArgumentNullException("fullPath");

    //Account for already in form of path
    if (String.IsNullOrEmpty(Path.GetFileName(fullPath))
        || String.IsNullOrEmpty(Path.GetExtension(fullPath)))
    {
        return fullPath;
    }

    return Path.GetDirectoryName(fullPath);
}

1 个答案:

答案 0 :(得分:2)

按照惯例,在定义目标时,您使用根目录,然后使用框架版本。概述here

因此,在nuspec文件中,您会看到.NET 4.5的这个:

  <files>
    <file src="bin\Release\MyAssembly.dll" target="lib\net45" />
  </files>

因此,在定义目标时,只需添加您要定位的.NET版本的约定:

var netVersion45 = "\net45";
foreach (var file in package.Files)
    {
        ManifestFile manifestFile = new ManifestFile();
        manifestFile.Source = Path.GetFileName(file);
        manifestFile.Target = @"lib\" + netVersion45;
        manifestFiles.Add(manifestFile);
    }