From 17f653857dfa6f8920c2cef6b82ff29480ae439f Mon Sep 17 00:00:00 2001 From: Tomasz Heimowski Date: Tue, 1 Nov 2016 12:36:07 +0100 Subject: [PATCH] convert - try to read PackageReferences of new MsBuild - references #1977 --- src/Paket.Core/NugetConvert.fs | 41 +++++++++++++++++++++++++------- src/Paket.Core/Paket.Core.fsproj | 2 +- src/Paket.Core/ProjectFile.fs | 8 +++++++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/Paket.Core/NugetConvert.fs b/src/Paket.Core/NugetConvert.fs index 3f5b30c1ff..9bd4d0daef 100644 --- a/src/Paket.Core/NugetConvert.fs +++ b/src/Paket.Core/NugetConvert.fs @@ -138,7 +138,7 @@ type NugetEnv = { RootDirectory : DirectoryInfo NuGetConfig : NugetConfig NuGetConfigFiles : list - NuGetProjectFiles : list + NuGetProjectFiles : list NuGetTargets : option NuGetExe : option } @@ -179,14 +179,19 @@ module NugetEnv = |> ok with _ -> fail (NugetPackagesConfigParseError file) + let readPackages (projectFile : ProjectFile) : Result, DomainMessage> = + let path = Path.Combine(Path.GetDirectoryName(projectFile.FileName), Constants.PackagesConfigFile) + if File.Exists path then + path |> FileInfo |> readSingle |> lift Some + else + Result.Succeed None + |> lift (fun configFile -> (projectFile,configFile)) + let projectFiles = ProjectFile.FindAllProjects rootDirectory.FullName projectFiles - |> Array.map (fun p -> p, Path.Combine(Path.GetDirectoryName(p.FileName), Constants.PackagesConfigFile)) - |> Array.filter (fun (p,packages) -> File.Exists packages) - |> Array.map (fun (p,packages) -> readSingle(FileInfo(packages)) |> lift (fun packages -> (p,packages))) + |> Array.map readPackages |> collect - let read (rootDirectory : DirectoryInfo) = trial { let configs = FindAllFiles(rootDirectory.FullName, "nuget.config") |> Array.toList let targets = FindAllFiles(rootDirectory.FullName, "nuget.targets") |> Array.tryHead @@ -229,7 +234,13 @@ let createDependenciesFileR (rootDirectory : DirectoryInfo) nugetEnv mode = let allVersionsGroupped = nugetEnv.NuGetProjectFiles - |> List.collect (fun (_,c) -> c.Packages) + |> List.collect (fun (pf,c) -> + c + |> Option.map (fun x -> x.Packages) + |> Option.toList + |> List.concat + |> List.append (ProjectFile.dotNetCorePackages pf)) + |> List.map (fun x -> tracefn "%A" x; x) |> List.groupBy (fun p -> p.Id) let findDistinctPackages selector = @@ -321,13 +332,22 @@ let convertDependenciesConfigToReferencesFile projectFileName dependencies = let convertProjects nugetEnv = [for project,packagesConfig in nugetEnv.NuGetProjectFiles do - let packagesAndIds = packagesConfig.Packages |> List.map (fun p -> p.Id, p.Version) + let packagesAndIds = + packagesConfig + |> Option.map (fun x -> x.Packages) + |> Option.toList + |> List.concat + |> List.map (fun p -> p.Id, p.Version) project.ReplaceNuGetPackagesFile() project.RemoveNuGetTargetsEntries() project.RemoveNugetAnalysers(packagesAndIds) project.RemoveImportAndTargetEntries(packagesAndIds) project.RemoveNuGetPackageImportStamp() - yield project, convertPackagesConfigToReferencesFile project.FileName packagesConfig] + match packagesConfig with + | Some packagesConfig -> + yield project, convertPackagesConfigToReferencesFile project.FileName packagesConfig + | None -> + ()] let createPaketEnv rootDirectory nugetEnv credsMirationMode = trial { let! depFile = createDependenciesFileR rootDirectory nugetEnv credsMirationMode @@ -373,7 +393,10 @@ let replaceNuGetWithPaket initAutoRestore installAfter fromBootstrapper result = fi.Delete() result.NuGetEnv.NuGetConfigFiles |> List.iter remove - result.NuGetEnv.NuGetProjectFiles |> List.map (fun (_,n) -> n.File) |> List.iter remove + result.NuGetEnv.NuGetProjectFiles + |> List.map (fun (_,n) -> n |> Option.map (fun x -> x.File)) + |> List.choose id + |> List.iter remove result.NuGetEnv.NuGetTargets |> Option.iter remove result.NuGetEnv.NuGetExe |> Option.iter diff --git a/src/Paket.Core/Paket.Core.fsproj b/src/Paket.Core/Paket.Core.fsproj index 1f9be4544b..1e3a9694bb 100644 --- a/src/Paket.Core/Paket.Core.fsproj +++ b/src/Paket.Core/Paket.Core.fsproj @@ -117,6 +117,7 @@ + @@ -126,7 +127,6 @@ - diff --git a/src/Paket.Core/ProjectFile.fs b/src/Paket.Core/ProjectFile.fs index 3d3f077842..5fa829b40a 100644 --- a/src/Paket.Core/ProjectFile.fs +++ b/src/Paket.Core/ProjectFile.fs @@ -1333,6 +1333,14 @@ module ProjectFile = tryNextPlat platforms [] + let dotNetCorePackages (projectFile: ProjectFile) = + projectFile.ProjectNode + |> getDescendants "PackageReference" + |> List.map (fun node -> + {Paket.PackagesConfigFile.NugetPackage.Id = node |> getAttribute "Include" |> Option.get + Paket.PackagesConfigFile.Version = node |> getNode "Version" |> Option.get |> (fun n -> Paket.SemVer.Parse n.InnerText) + Paket.PackagesConfigFile.TargetFramework = None }) + type ProjectFile with member this.GetPropertyWithDefaults propertyName defaultProperties = ProjectFile.getPropertyWithDefaults propertyName defaultProperties this