예제 #1
0
 private void AddRemoteProvidersFromSources(IList <IRemoteDependencyProvider> remoteProviders, List <PackageSource> effectiveSources)
 {
     foreach (var source in effectiveSources)
     {
         var feed = PackageSourceUtils.CreatePackageFeed(source, NoCache, IgnoreFailedSources, Logger);
         if (feed != null)
         {
             remoteProviders.Add(new RemoteDependencyProvider(feed));
         }
     }
 }
예제 #2
0
        private async Task <bool> RestoreForProject(string projectJsonPath, string rootDirectory, string packagesDirectory)
        {
            var success = true;

            Logger.WriteInformation(string.Format("Restoring packages for {0}", projectJsonPath.Bold()));

            var sw = new Stopwatch();

            sw.Start();

            NuGetProject project;

            if (!ProjectReader.TryReadProject(projectJsonPath, out project))
            {
                throw new Exception("Unable to locate project.json");
            }

            var projectDirectory    = project.ProjectDirectory;
            var projectLockFilePath = Path.Combine(projectDirectory, LockFileFormat.LockFileName);

            var properties = new Dictionary <string, string>
            {
                { "DesignTimeBuild", "true" },
                { "BuildProjectReferences", "false" },
                { "_ResolveReferenceDependencies", "true" },
                { "SolutionDir", rootDirectory + Path.DirectorySeparatorChar }
            };

            var projectCollection = new ProjectCollection();

            foreach (var projectFile in Directory.EnumerateFiles(projectDirectory, "*.csproj"))
            {
                projectCollection.LoadProject(projectFile);
            }

            foreach (var projectFile in Directory.EnumerateFiles(projectDirectory, "*.vbproj"))
            {
                projectCollection.LoadProject(projectFile);
            }

            foreach (var projectFile in Directory.EnumerateFiles(projectDirectory, "*.fsproj"))
            {
                projectCollection.LoadProject(projectFile);
            }

            var context = new RemoteWalkContext();

            var nugetRepository = new NuGetv3LocalRepository(packagesDirectory, checkPackageIdCase: true);

            context.ProjectLibraryProviders.Add(new LocalDependencyProvider(
                                                    new MSBuildDependencyProvider(projectCollection)));

            context.ProjectLibraryProviders.Add(
                new LocalDependencyProvider(
                    new ProjectReferenceDependencyProvider(
                        new ProjectResolver(
                            projectDirectory,
                            rootDirectory))));

            context.LocalLibraryProviders.Add(
                new LocalDependencyProvider(
                    new NuGetDependencyResolver(nugetRepository)));

            var effectiveSources = PackageSourceUtils.GetEffectivePackageSources(SourceProvider,
                                                                                 Sources, FallbackSources);

            AddRemoteProvidersFromSources(context.RemoteLibraryProviders, effectiveSources);

            var remoteWalker = new RemoteDependencyWalker(context);

            var tasks = new List <Task <GraphNode <RemoteResolveResult> > >();

            var msbuildProject = projectCollection.LoadedProjects.FirstOrDefault();

            if (msbuildProject != null)
            {
                var name = msbuildProject.GetPropertyValue("AssemblyName");
                var targetFrameworkMoniker = msbuildProject.GetPropertyValue("TargetFrameworkMoniker");

                // This is so that we have a unique cache per target framework
                tasks.Add(remoteWalker.Walk(name, new NuGetVersion(new Version()), NuGetFramework.Parse(targetFrameworkMoniker)));
            }
            else
            {
                foreach (var framework in project.TargetFrameworks)
                {
                    tasks.Add(remoteWalker.Walk(project.Name, project.Version, framework.FrameworkName));
                }
            }

            var graphs = await Task.WhenAll(tasks);

            Logger.WriteInformation(string.Format("{0}, {1}ms elapsed", "Resolving complete".Green(), sw.ElapsedMilliseconds));

            var libraries    = new HashSet <Library>();
            var installItems = new List <GraphItem <RemoteResolveResult> >();
            var missingItems = new HashSet <LibraryRange>();

            foreach (var g in graphs)
            {
                g.Dump(s => Logger.WriteInformation(s));

                g.ForEach(node =>
                {
                    if (node == null || node.Key == null)
                    {
                        return;
                    }

                    if (node.Item == null || node.Item.Data.Match == null)
                    {
                        if (!node.Key.IsGacOrFrameworkReference &&
                            node.Key.VersionRange != null &&
                            missingItems.Add(node.Key))
                        {
                            Logger.WriteError(string.Format("Unable to locate {0} {1}", node.Key.Name.Red().Bold(), node.Key.VersionRange));
                            success = false;
                        }

                        return;
                    }

                    var isRemote = context.RemoteLibraryProviders.Contains(node.Item.Data.Match.Provider);
                    var isAdded  = installItems.Any(item => item.Data.Match.Library == node.Item.Data.Match.Library);

                    if (!isAdded && isRemote)
                    {
                        installItems.Add(node.Item);
                    }

                    libraries.Add(node.Item.Key);
                });
            }

            await InstallPackages(installItems, packagesDirectory, packageFilter : (library, nupkgSHA) => true);

            if (success)
            {
                Logger.WriteInformation(string.Format("Writing lock file {0}", projectLockFilePath.White().Bold()));
                WriteLockFile(projectLockFilePath, libraries, nugetRepository);
            }

            //if (!ScriptExecutor.Execute(project, "postrestore", getVariable))
            //{
            //    Reports.Error.WriteLine(ScriptExecutor.ErrorMessage);
            //    return false;
            //}

            //if (!ScriptExecutor.Execute(project, "prepare", getVariable))
            //{
            //    Reports.Error.WriteLine(ScriptExecutor.ErrorMessage);
            //    return false;
            //}

            Logger.WriteInformation(string.Format("{0}, {1}ms elapsed", "Restore complete".Green().Bold(), sw.ElapsedMilliseconds));

            //for (int i = 0; i < contexts.Count; i++)
            //{
            //    PrintDependencyGraph(graphs[i], contexts[i].FrameworkName);
            //}

            return(success);
        }