コード例 #1
0
        public LibraryDescription GetDescription(LibraryRange libraryRange, NuGetFramework targetFramework)
        {
            if (libraryRange.IsGacOrFrameworkReference)
            {
                return(null);
            }

            var project = _projectCollection.LoadedProjects.FirstOrDefault(p => string.Equals(p.GetPropertyValue("ProjectGuid"), libraryRange.Name));

            if (project == null)
            {
                return(null);
            }

            var projectInstance = project.CreateProjectInstance();

            var projectReferences = projectInstance.GetItems("ProjectReference")
                                    .Select(p => p.GetMetadataValue("FullPath"))
                                    .ToList();

            var dependencies = new List <LibraryDependency>();

            foreach (var projectReference in projectReferences)
            {
                var referencedProject         = _projectCollection.LoadProject(projectReference);
                var referencedProjectInstance = referencedProject.CreateProjectInstance();
                dependencies.Add(new LibraryDependency
                {
                    LibraryRange = new LibraryRange
                    {
                        Name         = referencedProjectInstance.GetPropertyValue("ProjectGuid"),
                        VersionRange = new NuGetVersionRange(new NuGetVersion(new Version(1, 0)))
                    },
                });
            }

            NuGetProject nugetProject;
            var          projectDirectory = Path.GetDirectoryName(project.ProjectFileLocation.File);

            if (ProjectReader.TryReadProject(projectDirectory, out nugetProject))
            {
                // Grab dependencies from here too
                var targetFrameworkInfo = nugetProject.GetTargetFramework(targetFramework);

                dependencies.AddRange(nugetProject.Dependencies);
                dependencies.AddRange(targetFrameworkInfo.Dependencies);
            }

            return(new LibraryDescription
            {
                LibraryRange = libraryRange,
                Identity = new Library
                {
                    Name = libraryRange.Name,
                    Version = new NuGetVersion(new Version(1, 0)) // TODO: Make up something better
                },
                Path = project.ProjectFileLocation.File,
                Dependencies = dependencies
            });
        }
コード例 #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);
        }