From 364323a7cc1e186a28bebbcefccedf7b423a9b2d Mon Sep 17 00:00:00 2001 From: Steffen Forkmann Date: Mon, 24 Oct 2016 13:19:01 +0200 Subject: [PATCH] Create a restore task --- src/Paket.Core/Environment.fs | 2 +- src/Paket.Core/InstallProcess.fs | 21 +------ src/Paket.Core/LockFile.fs | 3 +- src/Paket.Core/RestoreProcess.fs | 43 +++++++++++++++ src/Paket.Core/UpdateProcess.fs | 4 +- src/Paket/Paket.fsproj | 11 ++-- src/Paket/PlatformTask.fs | 38 ------------- src/PaketRestoreTask/PaketRestoreTask.csproj | 40 ++++++++++++++ src/PaketRestoreTask/Program.cs | 58 ++++++++++++++++++++ 9 files changed, 151 insertions(+), 69 deletions(-) delete mode 100644 src/Paket/PlatformTask.fs create mode 100644 src/PaketRestoreTask/PaketRestoreTask.csproj create mode 100644 src/PaketRestoreTask/Program.cs diff --git a/src/Paket.Core/Environment.fs b/src/Paket.Core/Environment.fs index 20c6e13f66..51e72f9478 100644 --- a/src/Paket.Core/Environment.fs +++ b/src/Paket.Core/Environment.fs @@ -46,7 +46,7 @@ module PaketEnv = with _ -> fail (LockFileParseError fi) - let! projects = InstallProcess.findAllReferencesFiles(directory.FullName) + let! projects = RestoreProcess.findAllReferencesFiles(directory.FullName) return create directory dependenciesFile lockFile projects } diff --git a/src/Paket.Core/InstallProcess.fs b/src/Paket.Core/InstallProcess.fs index 26c8b43766..432763b7ce 100644 --- a/src/Paket.Core/InstallProcess.fs +++ b/src/Paket.Core/InstallProcess.fs @@ -294,25 +294,6 @@ let private applyBindingRedirects isFirstGroup createNewBindingFiles redirects c applyBindingRedirectsToFolder isFirstGroup createNewBindingFiles cleanBindingRedirects root allKnownLibs bindingRedirects -let findAllReferencesFiles root = - let findRefFile (p:ProjectFile) = - match p.FindReferencesFile() with - | Some fileName -> - try - ok <| (p, ReferencesFile.FromFile fileName) - with _ -> - fail <| ReferencesFileParseError (FileInfo fileName) - | None -> - let fileName = - let fi = FileInfo(p.FileName) - Path.Combine(fi.Directory.FullName,Constants.ReferencesFile) - - ok <| (p, ReferencesFile.New fileName) - - ProjectFile.FindAllProjects root - |> Array.map findRefFile - |> collect - /// Installs all packages from the lock file. let InstallIntoProjects(options : InstallerOptions, forceTouch, dependenciesFile, lockFile : LockFile, projectsAndReferences : (ProjectFile * ReferencesFile) list, updatedGroups) = let packagesToInstall = @@ -492,5 +473,5 @@ let InstallIntoProjects(options : InstallerOptions, forceTouch, dependenciesFile /// Installs all packages from the lock file. let Install(options : InstallerOptions, forceTouch, dependenciesFile, lockFile : LockFile, updatedGroups) = let root = FileInfo(lockFile.FileName).Directory.FullName - let projects = findAllReferencesFiles root |> returnOrFail + let projects = RestoreProcess.findAllReferencesFiles root |> returnOrFail InstallIntoProjects(options, forceTouch, dependenciesFile, lockFile, projects, updatedGroups) diff --git a/src/Paket.Core/LockFile.fs b/src/Paket.Core/LockFile.fs index b0ad69a2c5..01e0b75977 100644 --- a/src/Paket.Core/LockFile.fs +++ b/src/Paket.Core/LockFile.fs @@ -709,7 +709,6 @@ type LockFile(fileName:string,groups: Map) = let usedPackages = Dictionary<_,_>() match referencesFile.Groups |> Map.tryFind groupName with | Some g -> - for p in g.NugetPackages do let k = groupName,p.Name if usedPackages.ContainsKey k then @@ -717,7 +716,7 @@ type LockFile(fileName:string,groups: Map) = usedPackages.Add(k,p) g.NugetPackages - |> List.iter (fun package -> + |> List.iter (fun package -> try for d in this.GetAllDependenciesOf(groupName,package.Name,referencesFile.FileName) do let k = groupName,d diff --git a/src/Paket.Core/RestoreProcess.fs b/src/Paket.Core/RestoreProcess.fs index df64bbcc68..388339c893 100644 --- a/src/Paket.Core/RestoreProcess.fs +++ b/src/Paket.Core/RestoreProcess.fs @@ -9,6 +9,7 @@ open Paket.PackageResolver open Paket.PackageSources open FSharp.Polyfill open System +open Chessie.ErrorHandling // Find packages which would be affected by a restore, i.e. not extracted yet or with the wrong version let FindPackagesNotExtractedYet(dependenciesFileName) = @@ -119,6 +120,25 @@ let internal computePackageHull groupName (lockFile : LockFile) (referencesFileN |> Seq.map (fun p -> (snd p.Key))) |> Seq.concat +let findAllReferencesFiles root = + let findRefFile (p:ProjectFile) = + match p.FindReferencesFile() with + | Some fileName -> + try + ok <| (p, ReferencesFile.FromFile fileName) + with _ -> + fail <| ReferencesFileParseError (FileInfo fileName) + | None -> + let fileName = + let fi = FileInfo(p.FileName) + Path.Combine(fi.Directory.FullName,Constants.ReferencesFile) + + ok <| (p, ReferencesFile.New fileName) + + ProjectFile.FindAllProjects root + |> Array.map findRefFile + |> collect + let Restore(dependenciesFileName,force,group,referencesFileNames,ignoreChecks,failOnChecks) = let lockFileName = DependenciesFile.FindLockfile dependenciesFileName let localFileName = DependenciesFile.FindLocalfile dependenciesFileName @@ -178,4 +198,27 @@ let Restore(dependenciesFileName,force,group,referencesFileNames,ignoreChecks,fa |> Async.RunSynchronously |> ignore + // .NET Core restore + let root = FileInfo(lockFile.FileName).Directory.FullName + let projects = findAllReferencesFiles root |> returnOrFail + let resolved = lockFile.GetGroupedResolution() + for projectFile,referencesFile in projects do + let list = System.Collections.Generic.List<_>() + let fi = FileInfo projectFile.FileName + let newFileName = FileInfo(Path.Combine(fi.Directory.FullName,"obj",fi.Name + ".references")) + if not newFileName.Directory.Exists then + newFileName.Directory.Create() + + for kv in groups do + let hull = lockFile.GetPackageHull(kv.Key,referencesFile) + + for package in hull do + let _,packageName = package.Key + list.Add(packageName.ToString() + "," + resolved.[package.Key].Version.ToString()) + + let output = String.Join(Environment.NewLine,list) + if not newFileName.Exists || File.ReadAllText(newFileName.FullName) <> output then + File.WriteAllLines(newFileName.FullName,list) + tracefn "Created %s" newFileName.FullName + GarbageCollection.CleanUp(root, dependenciesFile, lockFile) diff --git a/src/Paket.Core/UpdateProcess.fs b/src/Paket.Core/UpdateProcess.fs index 64bfa06fad..e82d442ca4 100644 --- a/src/Paket.Core/UpdateProcess.fs +++ b/src/Paket.Core/UpdateProcess.fs @@ -158,7 +158,7 @@ let detectProjectFrameworksForDependenciesFile (dependenciesFile:DependenciesFil let root = Path.GetDirectoryName dependenciesFile.FileName let groups = let targetFrameworks = lazy ( - InstallProcess.findAllReferencesFiles root |> returnOrFail + RestoreProcess.findAllReferencesFiles root |> returnOrFail |> List.map (fun (p,_) -> match p.GetTargetFramework() with | Some fw -> Requirements.FrameworkRestriction.Exactly fw @@ -215,7 +215,7 @@ let SmartInstall(dependenciesFile, updateMode, options : UpdaterOptions) = let lockFile,hasChanged,updatedGroups = SelectiveUpdate(dependenciesFile, updateMode, options.Common.SemVerUpdateMode, options.Common.Force) let root = Path.GetDirectoryName dependenciesFile.FileName - let projectsAndReferences = InstallProcess.findAllReferencesFiles root |> returnOrFail + let projectsAndReferences = RestoreProcess.findAllReferencesFiles root |> returnOrFail if not options.NoInstall then let forceTouch = hasChanged && options.Common.TouchAffectedRefs diff --git a/src/Paket/Paket.fsproj b/src/Paket/Paket.fsproj index 728b66be22..60d7c36048 100644 --- a/src/Paket/Paket.fsproj +++ b/src/Paket/Paket.fsproj @@ -22,7 +22,7 @@ full false false - ..\..\bin + D:\code\coretest\.paket DEBUG;TRACE 3 @@ -30,8 +30,8 @@ Project paket.exe Project - convert-from-nuget - C:\Git\Third Party\Paket\integrationtests\scenarios\i001922-convert-nuget-with-analyzers\before\ + restore + D:\code\coretest\.paket pdbonly @@ -53,8 +53,8 @@ D:\code\PaketKopie update -f D:\code\Paket\integrationtests\scenarios\i001117-aws\temp - update - D:\temp\repo3 + restore + D:\code\coretest 11 @@ -85,7 +85,6 @@ - diff --git a/src/Paket/PlatformTask.fs b/src/Paket/PlatformTask.fs deleted file mode 100644 index 497e03a21b..0000000000 --- a/src/Paket/PlatformTask.fs +++ /dev/null @@ -1,38 +0,0 @@ -// Keep this file for compatibility issues -namespace MSBuild.Tasks - -open System -open System.IO -open Microsoft.Build.Utilities -open Microsoft.Build.Framework -open Paket -open Paket.Domain -open Paket.Requirements - -type CopyRuntimeDependencies() = - inherit Task() - - let mutable outputPath = "" - let mutable targetFramework = "" - let mutable projectFile = "" - let mutable projectsWithRuntimeLibs = "" - - [] - member this.OutputPath - with get() = outputPath - and set(v) = outputPath <- v - - [] - member this.ProjectsWithRuntimeLibs - with get() = projectsWithRuntimeLibs - and set(v) = projectsWithRuntimeLibs <- v - - member this.ProjectFile - with get() = projectFile - and set(v) = projectFile <- v - - member this.TargetFramework - with get() = targetFramework - and set(v) = targetFramework <- v - - override this.Execute() = true diff --git a/src/PaketRestoreTask/PaketRestoreTask.csproj b/src/PaketRestoreTask/PaketRestoreTask.csproj new file mode 100644 index 0000000000..c8c63e8abb --- /dev/null +++ b/src/PaketRestoreTask/PaketRestoreTask.csproj @@ -0,0 +1,40 @@ + + + + + library + netcoreapp1.0 + + + + + + + + + + 1.0.1 + + + 1.0.0-alpha-20161019-1 + All + + + + 0.1.0-preview-00038-160914 + All + + + + 0.1.0-preview-00038-160914 + All + + + + 0.1.0-preview-00038-160914 + All + + + + + diff --git a/src/PaketRestoreTask/Program.cs b/src/PaketRestoreTask/Program.cs new file mode 100644 index 0000000000..abc698999c --- /dev/null +++ b/src/PaketRestoreTask/Program.cs @@ -0,0 +1,58 @@ +using System; +using System.IO; +using System.Text; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Paket.Build.Tasks +{ + public class PaketRestoreTask : Task + { + + /// + /// Full path to the msbuild project. + /// + [Required] + public string ProjectUniqueName { get; set; } + + [Required] + public ITaskItem[] PackageReferences { get; set; } + + /// + /// Target frameworks to apply this for. If empty this applies to all. + /// + public string TargetFrameworks { get; set; } + + /// + /// Output items + /// + [Output] + public ITaskItem[] NewPackageReferences { get; set; } + + public override bool Execute() + { + var fileInfo = new FileInfo(ProjectUniqueName); + var fileName = Path.Combine(fileInfo.Directory.FullName, "obj", fileInfo.Name + ".references"); + var lines = System.IO.File.ReadAllLines(fileName); + var list = new System.Collections.Generic.List(); + list.AddRange(PackageReferences); + char[] delimiterChars = { ','}; + + foreach (var line in lines) + { + var splitted = line.Split(delimiterChars); + if(splitted.Length < 2) + break; + ITaskItem dependency = new TaskItem(splitted[0].Trim()); + dependency.SetMetadata("Version", splitted[1].Trim()); + list.Add(dependency); + } + + this.NewPackageReferences = list.ToArray(); + + return true; + } + } +} \ No newline at end of file