2012-12-26 64 views
2

我正在嘗試使用NuGet核心創建一個NuGet包作爲構建過程的一部分。構建過程創建特定的框架版本,3.5,4.0和4.5。我如何爲每個程序集指定框架版本?創建NuGet包時,如何爲程序集指定框架版本?

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); 
} 

回答

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); 
    } 
相關問題