public override bool Execute() { PackageIdentity[] latestPackages = NuGetPackages .Select(item => { using (var reader = new PackageArchiveReader(item.GetMetadata("FullPath"))) { return(reader.GetIdentity()); } }) .GroupBy(identity => identity.Id) .Select(g => g.OrderBy(id => id.Version).Last()) .OrderBy(id => id.Id) .ToArray(); var additionalAssets = (AdditionalAssetDirs ?? new string[0]) .Where(Directory.Exists) .Where(dir => Directory.GetDirectories(dir).Count() > 0) .Select(dir => new { Name = new DirectoryInfo(dir).Name + "Version", Version = new DirectoryInfo(Directory.EnumerateDirectories(dir).OrderBy(s => s).Last()).Name }).ToArray(); Directory.CreateDirectory(Path.GetDirectoryName(OutputPath)); using (var outStream = File.Open(OutputPath, FileMode.Create)) using (var sw = new StreamWriter(outStream, new UTF8Encoding(false))) { sw.WriteLine(@"<?xml version=""1.0"" encoding=""utf-8""?>"); sw.WriteLine(@"<Project ToolsVersion=""14.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"); sw.WriteLine(@" <PropertyGroup>"); foreach (PackageIdentity packageIdentity in latestPackages) { string propertyName = GetPropertyName(packageIdentity.Id); sw.WriteLine($" <{propertyName}>{packageIdentity.Version}</{propertyName}>"); } foreach (var extraProp in ExtraProperties ?? Enumerable.Empty <ITaskItem>()) { string propertyName = extraProp.GetMetadata("Identity"); sw.WriteLine($" <{propertyName}>{extraProp.GetMetadata("Version")}</{propertyName}>"); } foreach (var additionalAsset in additionalAssets) { sw.WriteLine($" <{additionalAsset.Name}>{additionalAsset.Version}</{additionalAsset.Name}>"); } sw.WriteLine(@" </PropertyGroup>"); if (IncludeCreationTimeProperty) { sw.WriteLine(@" <PropertyGroup>"); sw.WriteLine($@" <{CreationTimePropertyName}>{DateTime.UtcNow.Ticks}</{CreationTimePropertyName}>"); sw.WriteLine(@" </PropertyGroup>"); } sw.WriteLine(@"</Project>"); } return(true); }
public override bool Execute() { PackageIdentity[] latestPackages = NuGetPackages .Select(item => { using (var reader = new PackageArchiveReader(item.GetMetadata("FullPath"))) { return(reader.GetIdentity()); } }) .GroupBy(identity => identity.Id) .Select(g => g.OrderBy(id => id.Version).Last()) .OrderBy(id => id.Id) .ToArray(); Directory.CreateDirectory(Path.GetDirectoryName(OutputPath)); var invalidElementNameCharRegex = new Regex(@"(^|[^A-Za-z0-9])(?<FirstPartChar>.)"); using (var outStream = File.Open(OutputPath, FileMode.Create)) using (var sw = new StreamWriter(outStream, new UTF8Encoding(false))) { sw.WriteLine(@"<?xml version=""1.0"" encoding=""utf-8""?>"); sw.WriteLine(@"<Project ToolsVersion=""14.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"); sw.WriteLine(@" <PropertyGroup>"); foreach (PackageIdentity packageIdentity in latestPackages) { string formattedId = invalidElementNameCharRegex.Replace( packageIdentity.Id, match => match.Groups?["FirstPartChar"].Value.ToUpperInvariant() ?? string.Empty); string propertyName = $"{formattedId}PackageVersion"; sw.WriteLine($" <{propertyName}>{packageIdentity.Version}</{propertyName}>"); } sw.WriteLine(@" </PropertyGroup>"); sw.WriteLine(@"</Project>"); } return(true); }
public override bool Execute() { PackageIdentity[] latestPackages = NuGetPackages .Select(item => { using (var reader = new PackageArchiveReader(item.GetMetadata("FullPath"))) { return(reader.GetIdentity()); } }) .Concat(ExtraPackageIdentities) .GroupBy(identity => identity.Id) .Select(g => g.OrderBy(id => id.Version).Last()) .OrderBy(id => id.Id) .ToArray(); Directory.CreateDirectory(Path.GetDirectoryName(OutputPath)); using (var outStream = File.Open(OutputPath, FileMode.Create)) using (var sw = new StreamWriter(outStream, new UTF8Encoding(false))) { sw.WriteLine(@"<?xml version=""1.0"" encoding=""utf-8""?>"); sw.WriteLine(@"<Project ToolsVersion=""14.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"); sw.WriteLine(@" <PropertyGroup>"); foreach (PackageIdentity packageIdentity in latestPackages) { string propertyName = GetPropertyName(packageIdentity.Id); sw.WriteLine($" <{propertyName}>{packageIdentity.Version}</{propertyName}>"); } sw.WriteLine(@" </PropertyGroup>"); sw.WriteLine(@"</Project>"); } return(true); }