Esempio n. 1
0
        public void GetFallbackPackageFolders_MultipleFoldersFromNuGetConfig()
        {
            // Arrange
            var config = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""d"" value=""C:\Temp\d"" />
        <add key=""b"" value=""C:\Temp\b"" />
        <add key=""c"" value=""C:\Temp\c"" />
    </fallbackPackageFolders>
</configuration>";

            var nugetConfigPath = "NuGet.Config";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);
                Settings settings = new Settings(mockBaseDirectory);

                // Act
                var paths = SettingsUtility.GetFallbackPackageFolders(settings).ToArray();

                // Assert
                Assert.Equal(3, paths.Length);
                Assert.Equal("d", GetFileName(paths[0]));
                Assert.Equal("b", GetFileName(paths[1]));
                Assert.Equal("c", GetFileName(paths[2]));
            }
        }
Esempio n. 2
0
        public void GetFallbackPackageFolders_RelativePathChild()
        {
            // Arrange
            var config = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""shared"" value=""test"" />
    </fallbackPackageFolders>
</configuration>";

            var nugetConfigPath = "NuGet.Config";

            using (var mockBaseDirectory = TestDirectory.Create())
            {
                var testFolder = Path.Combine(mockBaseDirectory, "test");


                ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);
                Settings settings = new Settings(mockBaseDirectory);

                // Act
                var paths = SettingsUtility.GetFallbackPackageFolders(settings);

                // Assert
                Assert.Equal(testFolder, paths.Single());
            }
        }
        private async Task <RestoreTargetGraph?> RestoreProjectAsync(IEnumerable <NuGetReference> packages, IEnumerable <TargetFrameworkMoniker> tfms, CancellationToken token)
        {
            var tfmInfo = tfms.Select(tfm => new TargetFrameworkInformation {
                FrameworkName = NuGetFramework.Parse(tfm.ToFullString())
            }).ToList();

            // Create a project in a unique and temporary directory
            var path = Path.Combine(Path.GetTempPath(), "dotnet-ua", "restores", Guid.NewGuid().ToString(), "project.txt");

            var spec = new PackageSpec(tfmInfo)
            {
                Dependencies = packages.Select(i => new LibraryDependency
                {
                    LibraryRange = new LibraryRange(i.Name, new VersionRange(i.GetNuGetVersion()), LibraryDependencyTarget.Package),
                }).ToList(),
                RestoreMetadata = new()
                {
                    ProjectPath              = path,
                    ProjectName              = Path.GetFileNameWithoutExtension(path),
                    ProjectStyle             = ProjectStyle.PackageReference,
                    ProjectUniqueName        = path,
                    OutputPath               = Path.GetTempPath(),
                    OriginalTargetFrameworks = tfms.Select(tfm => tfm.ToFullString()).ToArray(),
                    ConfigFilePaths          = _settings.GetConfigFilePaths(),
                    PackagesPath             = SettingsUtility.GetGlobalPackagesFolder(_settings),
                    Sources         = _packageSources.ToList(),
                    FallbackFolders = SettingsUtility.GetFallbackPackageFolders(_settings).ToList(),
                },
                FilePath = path,
                Name     = Path.GetFileNameWithoutExtension(path),
            };

            var dependencyGraphSpec = new DependencyGraphSpec();

            dependencyGraphSpec.AddProject(spec);
            dependencyGraphSpec.AddRestore(spec.RestoreMetadata.ProjectUniqueName);

            var requestProvider = new DependencyGraphSpecRequestProvider(new RestoreCommandProvidersCache(), dependencyGraphSpec);

            var restoreArgs = new RestoreArgs
            {
                AllowNoOp             = true,
                CacheContext          = _context,
                CachingSourceProvider = new CachingSourceProvider(new PackageSourceProvider(_settings)),
                Log = new NuGetLogger(_logger),
            };

            // Create requests from the arguments
            var requests = await requestProvider.CreateRequests(restoreArgs).ConfigureAwait(false);

            // Restore the package without generating extra files
            var result = await RestoreRunner.RunWithoutCommit(requests, restoreArgs).ConfigureAwait(false);

            if (result.Count == 0)
            {
                return(null);
            }

            return(result[0].Result.RestoreGraphs.FirstOrDefault());
        }
Esempio n. 4
0
        public void GetFallbackPackageFolders_WithNullSettings_Throws()
        {
            var ex = Record.Exception(() => SettingsUtility.GetFallbackPackageFolders(settings: null));

            ex.Should().NotBeNull();
            ex.Should().BeOfType <ArgumentNullException>();
        }
Esempio n. 5
0
        public void GetFallbackPackageFolders_MultipleFoldersFromMultipleNuGetConfigs()
        {
            // Arrange
            var configA = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""a"" value=""C:\Temp\a"" />
        <add key=""b"" value=""C:\Temp\b"" />
    </fallbackPackageFolders>
</configuration>";

            var configB = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""c"" value=""C:\Temp\c"" />
        <add key=""d"" value=""C:\Temp\d"" />
    </fallbackPackageFolders>
</configuration>";

            var configC = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""x"" value=""C:\Temp\x"" />
        <add key=""y"" value=""C:\Temp\y"" />
    </fallbackPackageFolders>
</configuration>";

            var nugetConfigPath = "NuGet.Config";

            using (var machineWide = TestDirectory.Create())
                using (var mockBaseDirectory = TestDirectory.Create())
                {
                    var subFolder = Path.Combine(mockBaseDirectory, "sub");

                    ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, subFolder, configA);
                    ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, configB);
                    ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, machineWide, configC);

                    var machineWiderFolderSettings = new Settings(machineWide);
                    var machineWideSettings        = new TestMachineWideSettings(machineWiderFolderSettings);

                    var settings = Settings.LoadDefaultSettings(
                        subFolder,
                        configFileName: null,
                        machineWideSettings: machineWideSettings);

                    // Act
                    var paths = SettingsUtility.GetFallbackPackageFolders(settings).ToArray();

                    // Assert
                    Assert.Equal(6, paths.Length);
                    Assert.Equal("a", GetFileName(paths[0]));
                    Assert.Equal("b", GetFileName(paths[1]));
                    Assert.Equal("c", GetFileName(paths[2]));
                    Assert.Equal("d", GetFileName(paths[3]));
                    Assert.Equal("x", GetFileName(paths[4]));
                    Assert.Equal("y", GetFileName(paths[5]));
                }
        }
Esempio n. 6
0
        public void GetFallbackPackageFolders_DefaultHasNoFallbackFolders()
        {
            // Arrange & Act
            var paths = SettingsUtility.GetFallbackPackageFolders(new NullSettings());

            // Assert
            Assert.Equal(0, paths.Count);
        }
Esempio n. 7
0
        public void GetFallbackPackageFolders_MultipleFoldersFromMultipleNuGetConfigs()
        {
            // Arrange
            var configA = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""a"" value=""C:\Temp\a"" />
        <add key=""b"" value=""C:\Temp\b"" />
    </fallbackPackageFolders>
</configuration>";

            var configB = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""c"" value=""C:\Temp\c"" />
        <add key=""d"" value=""C:\Temp\d"" />
    </fallbackPackageFolders>
</configuration>";

            var configC = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""x"" value=""C:\Temp\x"" />
        <add key=""y"" value=""C:\Temp\y"" />
    </fallbackPackageFolders>
</configuration>";

            var nugetConfigPath = "NuGet.Config";

            using (var machineWide = TestDirectory.Create())
                using (var mockBaseDirectory = TestDirectory.Create())
                {
                    var subFolder = Path.Combine(mockBaseDirectory, "sub");

                    ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, subFolder, configA);
                    ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, configB);
                    ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, machineWide, configC);

                    var machineWiderFolderSettings = new Settings(machineWide);
                    var machineWideSettings        = new TestMachineWideSettings(machineWiderFolderSettings);

                    var settings = Settings.LoadDefaultSettings(
                        subFolder,
                        configFileName: null,
                        machineWideSettings: machineWideSettings);

                    // Act
                    var actual = SettingsUtility
                                 .GetFallbackPackageFolders(settings)
                                 .Select(GetFileName);

                    var expected = new[] { "a", "b", "c", "d", "x", "y" };

                    // Ignore any extra folders on the machine
                    var actualFiltered = Enumerable.Intersect(actual, expected);
                    Assert.Equal(expected, actualFiltered);
                }
        }
Esempio n. 8
0
        public void GetFallbackPackageFolders_ClearTag()
        {
            // Arrange
            var configA = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <clear />
        <add key=""a"" value=""C:\Temp\a"" />
        <add key=""b"" value=""C:\Temp\b"" />
    </fallbackPackageFolders>
</configuration>";

            var configB = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""c"" value=""C:\Temp\c"" />
        <add key=""d"" value=""C:\Temp\d"" />
    </fallbackPackageFolders>
</configuration>";

            var configC = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""x"" value=""C:\Temp\x"" />
        <add key=""y"" value=""C:\Temp\y"" />
    </fallbackPackageFolders>
</configuration>";

            var nugetConfigPath = "NuGet.Config";

            using (var machineWide = TestDirectory.Create())
                using (var mockBaseDirectory = TestDirectory.Create())
                {
                    var subFolder = Path.Combine(mockBaseDirectory, "sub");

                    SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, subFolder, configA);
                    SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, configB);
                    SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, machineWide, configC);

                    var settings = Settings.LoadSettingsGivenConfigPaths(new[] {
                        Path.Combine(subFolder, nugetConfigPath),
                        Path.Combine(mockBaseDirectory, nugetConfigPath),
                        Path.Combine(machineWide, nugetConfigPath)
                    });

                    // Act
                    var paths = SettingsUtility.GetFallbackPackageFolders(settings).ToArray();

                    // Assert
                    Assert.Equal(2, paths.Length);
                    Assert.Equal("a", GetFileName(paths[0]));
                    Assert.Equal("b", GetFileName(paths[1]));
                }
        }
        /// <summary>
        /// Returns a PackageReference spec.
        /// </summary>
        /// <param name="settings">Settings to be used for the restore metadata.</param>
        /// <param name="projectName">Project name</param>
        /// <param name="rootPath">Root path, normally solution root. The project is gonna be "located" at rootPath/projectName/projectName.csproj </param>
        /// <param name="framework">framework</param>
        /// <returns>Returns a PackageReference spec with all details similar to what a spec after the post processing before restore would look like.
        /// The RestoreMetadata has the settings, sources etc set based on the ISettings provided.
        /// </returns>
        public static PackageSpec GetPackageSpec(ISettings settings, string projectName, string rootPath = @"C:\", string framework = "net5.0")
        {
            var packageSpec = GetPackageSpec(projectName, rootPath, framework);

            packageSpec.RestoreMetadata.ConfigFilePaths = settings.GetConfigFilePaths();
            packageSpec.RestoreMetadata.Sources         = SettingsUtility.GetEnabledSources(settings).ToList();
            packageSpec.RestoreMetadata.FallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings).ToList();
            packageSpec.RestoreMetadata.PackagesPath    = SettingsUtility.GetGlobalPackagesFolder(settings);

            return(packageSpec);
        }
Esempio n. 10
0
        public static IList <string> GetFallbackFolders(string projectFullPath, ISettings settings, IEnumerable <string> fallbackFolders)
        {
            if (ShouldReadFromSettings(fallbackFolders))
            {
                fallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings);
            }
            else
            {
                fallbackFolders = HandleClear(fallbackFolders);
            }

            return(fallbackFolders.Select(e => UriUtility.GetAbsolutePathFromFile(projectFullPath, e)).ToList());
        }
Esempio n. 11
0
 static ProjectRestoreMetadata CreateRestoreMetadata(PackageSpec packageSpec, IDotNetProject project, ISettings settings)
 {
     return(new ProjectRestoreMetadata {
         ConfigFilePaths = SettingsUtility.GetConfigFilePaths(settings).ToList(),
         FallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings).ToList(),
         PackagesPath = SettingsUtility.GetGlobalPackagesFolder(settings),
         ProjectStyle = ProjectStyle.PackageReference,
         ProjectPath = project.FileName,
         ProjectName = packageSpec.Name,
         ProjectUniqueName = project.FileName,
         ProjectWideWarningProperties = GetWarningProperties(project),
         OutputPath = project.BaseIntermediateOutputPath,
         OriginalTargetFrameworks = GetOriginalTargetFrameworks(project).ToList(),
         Sources = SettingsUtility.GetEnabledSources(settings).ToList()
     });
 }
Esempio n. 12
0
        /// <summary>
        /// Gets the package fallback folders for a project.
        /// </summary>
        /// <param name="startupDirectory">The start-up directory of the tool.</param>
        /// <param name="projectDirectory">The full path to the directory of the project.</param>
        /// <param name="fallbackFolders">A <see cref="T:string[]" /> containing the fallback folders for the project.</param>
        /// <param name="fallbackFoldersOverride">A <see cref="T:string[]" /> containing overrides for the fallback folders for the project.</param>
        /// <param name="additionalProjectFallbackFolders">An <see cref="IEnumerable{String}" /> containing additional fallback folders for the project.</param>
        /// <param name="additionalProjectFallbackFoldersExcludes">An <see cref="IEnumerable{String}" /> containing fallback folders to exclude.</param>
        /// <param name="settings">An <see cref="ISettings" /> object containing settings for the project.</param>
        /// <returns>A <see cref="T:string[]" /> containing the package fallback folders for the project.</returns>
        public static string[] GetFallbackFolders(string startupDirectory, string projectDirectory, string[] fallbackFolders, string[] fallbackFoldersOverride, IEnumerable <string> additionalProjectFallbackFolders, IEnumerable <string> additionalProjectFallbackFoldersExcludes, ISettings settings)
        {
            // Fallback folders
            var currentFallbackFolders = RestoreSettingsUtils.GetValue(
                () => fallbackFoldersOverride?.Select(e => UriUtility.GetAbsolutePath(startupDirectory, e)).ToArray(),
                () => MSBuildRestoreUtility.ContainsClearKeyword(fallbackFolders) ? Array.Empty <string>() : null,
                () => fallbackFolders?.Select(e => UriUtility.GetAbsolutePath(projectDirectory, e)).ToArray(),
                () => SettingsUtility.GetFallbackPackageFolders(settings).ToArray());

            // Append additional fallback folders after removing excluded folders
            var filteredAdditionalProjectFallbackFolders = MSBuildRestoreUtility.AggregateSources(
                values: additionalProjectFallbackFolders,
                excludeValues: additionalProjectFallbackFoldersExcludes);

            return(AppendItems(projectDirectory, currentFallbackFolders, filteredAdditionalProjectFallbackFolders));
        }
Esempio n. 13
0
        public static SimpleTestProjectContext CreateProject(string projectName,
                                                             SimpleTestPathContext pathContext,
                                                             string projectFrameworks)
        {
            var settings = Settings.LoadDefaultSettings(Path.GetDirectoryName(pathContext.NuGetConfig), Path.GetFileName(pathContext.NuGetConfig), null);
            var project  = SimpleTestProjectContext.CreateNETCoreWithSDK(
                projectName: projectName,
                solutionRoot: pathContext.SolutionRoot,
                frameworks: MSBuildStringUtility.Split(projectFrameworks));

            project.FallbackFolders      = (IList <string>)SettingsUtility.GetFallbackPackageFolders(settings);
            project.GlobalPackagesFolder = SettingsUtility.GetGlobalPackagesFolder(settings);
            var packageSourceProvider = new PackageSourceProvider(settings);

            project.Sources = packageSourceProvider.LoadPackageSources();

            project.Save();
            return(project);
        }
Esempio n. 14
0
        static IList <string> GetFallbackPackageFolders(ISettings settings, IDotNetProject project)
        {
            var folders = new List <string> ();

            AddFolders(folders, project, "RestoreFallbackFolders");

            if (folders.Any())
            {
                HandleClear(folders);
            }
            else
            {
                folders = SettingsUtility.GetFallbackPackageFolders(settings).ToList();
            }

            AddFolders(folders, project, "RestoreAdditionalProjectFallbackFolders");

            return(folders);
        }
Esempio n. 15
0
        private static List <string> GetFallbackFolders(ISettings settings, PackageSpec project)
        {
            IEnumerable <string> fallbackFolders = project.RestoreMetadata.FallbackFolders;

            if (ShouldReadFromSettings(fallbackFolders))
            {
                fallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings);
            }
            else
            {
                fallbackFolders = HandleClear(fallbackFolders);
            }

            // Resolve relative paths
            return(fallbackFolders.Select(e =>
                                          UriUtility.GetAbsolutePathFromFile(
                                              sourceFile: project.RestoreMetadata.ProjectPath,
                                              path: e))
                   .ToList());
        }
        private IList <string> GetFallbackFolders(ISettings settings, bool shouldThrow = true)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            var fallbackFolders = MSBuildStringUtility.Split(_vsProjectAdapter.RestoreFallbackFolders).AsEnumerable();

            if (ShouldReadFromSettings(fallbackFolders))
            {
                fallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings);
            }
            else
            {
                fallbackFolders = VSRestoreSettingsUtilities.HandleClear(fallbackFolders);
            }

            // Add additional fallback folders
            fallbackFolders = fallbackFolders.Concat(MSBuildStringUtility.Split(_vsProjectAdapter.RestoreAdditionalProjectFallbackFolders));

            return(fallbackFolders.Select(e => UriUtility.GetAbsolutePathFromFile(_projectFullPath, e)).ToList());
        }
Esempio n. 17
0
        public static IList <string> GetFallbackFolders(ISettings settings, PackageSpec project)
        {
            var results                   = ProcessEntriesWithAdditional(project.RestoreMetadata.FallbackFolders);
            var fallbackFolders           = results.Item1;
            var additionalFallbackFolders = results.Item2;


            var processedFallbackFolders = (
                ShouldReadFromSettings(fallbackFolders) ?
                SettingsUtility.GetFallbackPackageFolders(settings) :
                HandleClear(fallbackFolders))
                                           .Concat(additionalFallbackFolders);


            // Resolve relative paths
            return(processedFallbackFolders.Select(e =>
                                                   UriUtility.GetAbsolutePathFromFile(
                                                       sourceFile: project.RestoreMetadata.ProjectPath,
                                                       path: e))
                   .ToList());
        }
Esempio n. 18
0
        public void GetFallbackPackageFolders_SingleFolderFromNuGetConfig()
        {
            // Arrange
            var config = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <fallbackPackageFolders>
        <add key=""shared"" value=""a"" />
    </fallbackPackageFolders>
</configuration>";

            var nugetConfigPath = "NuGet.Config";

            using (var mockBaseDirectory = TestFileSystemUtility.CreateRandomTestFolder())
            {
                ConfigurationFileTestUtility.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config);
                Settings settings = new Settings(mockBaseDirectory);

                // Act
                var paths = SettingsUtility.GetFallbackPackageFolders(settings);

                // Assert
                Assert.Equal(Path.Combine(mockBaseDirectory, "a"), paths.Single());
            }
        }
Esempio n. 19
0
        public override async Task <IReadOnlyList <PackageSpec> > GetPackageSpecsAsync(DependencyGraphCacheContext context)
        {
            PackageSpec packageSpec = null;

            if (context == null || !context.PackageSpecCache.TryGetValue(MSBuildProjectPath, out packageSpec))
            {
                packageSpec = JsonPackageSpecReader.GetPackageSpec(ProjectName, JsonConfigPath);
                if (packageSpec == null)
                {
                    throw new InvalidOperationException(
                              string.Format(Strings.ProjectNotLoaded_RestoreFailed, ProjectName));
                }
                var metadata = new ProjectRestoreMetadata();
                packageSpec.RestoreMetadata = metadata;

                metadata.ProjectStyle = ProjectStyle.ProjectJson;
                metadata.OutputPath   = await GetBaseIntermediatePathAsync();

                metadata.ProjectPath       = MSBuildProjectPath;
                metadata.ProjectJsonPath   = packageSpec.FilePath;
                metadata.ProjectName       = packageSpec.Name;
                metadata.ProjectUniqueName = MSBuildProjectPath;

                // Reload the target framework from csproj and update the target framework in packageSpec for restore
                await UpdateInternalTargetFrameworkAsync();

                if (TryGetInternalFramework(out var internalTargetFramework))
                {
                    // Ensure the project json has only one target framework
                    if (packageSpec.TargetFrameworks != null && packageSpec.TargetFrameworks.Count == 1)
                    {
                        var replaceTargetFramework = new TargetFrameworkInformation();
                        replaceTargetFramework.FrameworkName = internalTargetFramework as NuGetFramework;
                        packageSpec.TargetFrameworks[0]      = replaceTargetFramework;
                    }
                }

                var references = (await ProjectServices
                                  .ReferencesReader
                                  .GetProjectReferencesAsync(context.Logger, CancellationToken.None))
                                 .ToList();

                if (references != null && references.Count > 0)
                {
                    // Add msbuild reference groups for each TFM in the project
                    foreach (var framework in packageSpec.TargetFrameworks.Select(e => e.FrameworkName))
                    {
                        metadata.TargetFrameworks.Add(new ProjectRestoreMetadataFrameworkInfo(framework));
                    }

                    foreach (var reference in references)
                    {
                        // This reference applies to all frameworks
                        // Include/exclude flags may be applied later when merged with project.json
                        // Add the reference for all TFM groups, there are no conditional project
                        // references in UWP. There should also be just one TFM.
                        foreach (var frameworkInfo in metadata.TargetFrameworks)
                        {
                            frameworkInfo.ProjectReferences.Add(reference);
                        }
                    }
                }
                // Write restore settings to the package spec.
                // For project.json these properties may not come from the project file.
                var settings = context?.Settings ?? NullSettings.Instance;
                packageSpec.RestoreMetadata.PackagesPath    = SettingsUtility.GetGlobalPackagesFolder(settings);
                packageSpec.RestoreMetadata.Sources         = SettingsUtility.GetEnabledSources(settings).AsList();
                packageSpec.RestoreMetadata.FallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings).AsList();
                packageSpec.RestoreMetadata.ConfigFilePaths = SettingsUtility.GetConfigFilePaths(settings).AsList();

                context?.PackageSpecCache.Add(MSBuildProjectPath, packageSpec);
            }

            return(new[] { packageSpec });
        }
Esempio n. 20
0
        private RestoreTargetGraph GetRestoreTargetGraph(List <PackageReference> packages, string projectPath, List <NuGetFramework> targetFrameworks, SourceCacheContext sourceCacheContext)
        {
            // The package spec details what packages to restore
            PackageSpec packageSpec = new PackageSpec(targetFrameworks.Select(i => new TargetFrameworkInformation
            {
                FrameworkName = i
            }).ToList())
            {
                //Dependencies = new List<LibraryDependency>
                //{
                //    new LibraryDependency
                //    {
                //        LibraryRange = new LibraryRange(id, new VersionRange(NuGetVersion.Parse(version)), LibraryDependencyTarget.Package),
                //        SuppressParent = LibraryIncludeFlags.All,
                //        AutoReferenced = true,
                //        IncludeType = LibraryIncludeFlags.None,
                //        Type = LibraryDependencyType.Build
                //    }
                //},
                Dependencies = packages.Select(i => new LibraryDependency
                {
                    LibraryRange = new LibraryRange(i.PackageId, new VersionRange(i.PackageVersion), LibraryDependencyTarget.Package),
                    //SuppressParent = LibraryIncludeFlags.All,
                    //AutoReferenced = true,
                    //IncludeType = LibraryIncludeFlags.None,
                    //Type = LibraryDependencyType.
                }).ToList(),
                RestoreMetadata = new ProjectRestoreMetadata
                {
                    ProjectPath              = projectPath,
                    ProjectName              = Path.GetFileNameWithoutExtension(projectPath),
                    ProjectStyle             = ProjectStyle.PackageReference,
                    ProjectUniqueName        = projectPath,
                    OutputPath               = Path.GetTempPath(),
                    OriginalTargetFrameworks = targetFrameworks.Select(i => i.ToString()).ToList(),
                    ConfigFilePaths          = SettingsUtility.GetConfigFilePaths(_nugetSettings).ToList(),
                    PackagesPath             = SettingsUtility.GetGlobalPackagesFolder(_nugetSettings),
                    Sources         = SettingsUtility.GetEnabledSources(_nugetSettings).ToList(),
                    FallbackFolders = SettingsUtility.GetFallbackPackageFolders(_nugetSettings).ToList()
                },
                FilePath = projectPath,
                Name     = Path.GetFileNameWithoutExtension(projectPath),
            };

            DependencyGraphSpec dependencyGraphSpec = new DependencyGraphSpec();

            dependencyGraphSpec.AddProject(packageSpec);

            dependencyGraphSpec.AddRestore(packageSpec.RestoreMetadata.ProjectUniqueName);

            IPreLoadedRestoreRequestProvider requestProvider = new DependencyGraphSpecRequestProvider(new RestoreCommandProvidersCache(), dependencyGraphSpec);

            RestoreArgs restoreArgs = new RestoreArgs
            {
                AllowNoOp             = true,
                CacheContext          = sourceCacheContext,
                CachingSourceProvider = new CachingSourceProvider(new PackageSourceProvider(_nugetSettings)),
                Log = NullLogger.Instance,
            };

            // Create requests from the arguments
            IReadOnlyList <RestoreSummaryRequest> requests = requestProvider.CreateRequests(restoreArgs).Result;

            // Restore the package without generating extra files
            RestoreResultPair restoreResult = RestoreRunner.RunWithoutCommit(requests, restoreArgs).Result.FirstOrDefault();

            RestoreTargetGraph restoreTargetGraph = restoreResult?.Result.RestoreGraphs.FirstOrDefault();

            return(restoreTargetGraph);
        }
Esempio n. 21
0
        public static (RestoreRequest, RestoreResult) Restore(ILogger logger, NuGetFramework nugetFramework, string runtimeIdentifier, string packageName, VersionRange versionRange, string settingsRoot = null)
        {
            var settings = NuGet.Configuration.Settings.LoadDefaultSettings(settingsRoot);

            var assemblies = new List <string>();

            var projectPath = Path.Combine("StrideNugetResolver.json");
            var spec        = new PackageSpec()
            {
                // Make sure this package never collides with a dependency
                Name         = Path.GetFileNameWithoutExtension(projectPath),
                FilePath     = projectPath,
                Dependencies = new List <LibraryDependency>()
                {
                    new LibraryDependency
                    {
                        LibraryRange = new LibraryRange(packageName, versionRange, LibraryDependencyTarget.Package),
                    }
                },
                TargetFrameworks =
                {
                    new TargetFrameworkInformation
                    {
                        FrameworkName = nugetFramework,
                    }
                },
                RestoreMetadata = new ProjectRestoreMetadata
                {
                    ProjectPath              = projectPath,
                    ProjectName              = Path.GetFileNameWithoutExtension(projectPath),
                    ProjectStyle             = ProjectStyle.PackageReference,
                    ProjectUniqueName        = projectPath,
                    OutputPath               = Path.Combine(Path.GetTempPath(), $"StrideNugetResolver-{packageName}-{versionRange.MinVersion.ToString()}-{nugetFramework.GetShortFolderName()}-{runtimeIdentifier}"),
                    OriginalTargetFrameworks = new[] { nugetFramework.GetShortFolderName() },
                    ConfigFilePaths          = settings.GetConfigFilePaths(),
                    PackagesPath             = SettingsUtility.GetGlobalPackagesFolder(settings),
                    Sources         = SettingsUtility.GetEnabledSources(settings).ToList(),
                    FallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings).ToList()
                },
                RuntimeGraph = new RuntimeGraph(new[] { new RuntimeDescription(runtimeIdentifier) })
            };

            using (var context = new SourceCacheContext())
            {
                context.IgnoreFailedSources = true;

                var dependencyGraphSpec = new DependencyGraphSpec();
                dependencyGraphSpec.AddProject(spec);
                dependencyGraphSpec.AddRestore(spec.RestoreMetadata.ProjectUniqueName);

                IPreLoadedRestoreRequestProvider requestProvider = new DependencyGraphSpecRequestProvider(new RestoreCommandProvidersCache(), dependencyGraphSpec);

                var restoreArgs = new RestoreArgs
                {
                    AllowNoOp             = true,
                    CacheContext          = context,
                    CachingSourceProvider = new CachingSourceProvider(new PackageSourceProvider(settings)),
                    Log = logger,
                };

                // Create requests from the arguments
                var requests = requestProvider.CreateRequests(restoreArgs).Result;

                // Restore the packages
                for (int tryCount = 0; tryCount < 2; ++tryCount)
                {
                    try
                    {
                        var results = RestoreRunner.RunWithoutCommit(requests, restoreArgs).Result;

                        // Commit results so that noop cache works next time
                        foreach (var result in results)
                        {
                            result.Result.CommitAsync(logger, CancellationToken.None).Wait();
                        }
                        var mainResult = results.First();
                        return(mainResult.SummaryRequest.Request, mainResult.Result);
                    }
                    catch (Exception e) when(e is UnauthorizedAccessException || e is IOException || ((e is AggregateException ae) && ae.InnerExceptions.Any(e2 => e2 is UnauthorizedAccessException || e2 is IOException)))
                    {
                        // If we have an unauthorized access exception, it means assemblies are locked by running Stride process
                        // During first try, kill some known harmless processes, and try again
                        if (tryCount == 1)
                        {
                            throw;
                        }

                        foreach (var process in new[] { "Stride.ConnectionRouter" }.SelectMany(Process.GetProcessesByName))
                        {
                            try
                            {
                                if (process.Id != Process.GetCurrentProcess().Id)
                                {
                                    process.Kill();
                                    process.WaitForExit();
                                }
                            }
                            catch { }
                        }
                    }
                }

                Debug.Fail("Unreachable code");
                return(default);
Esempio n. 22
0
        public override bool Execute()
        {
            // Log Inputs
            var log = new MSBuildLogger(Log);

            log.LogDebug($"(in) ProjectUniqueName '{ProjectUniqueName}'");
            if (RestoreSources != null)
            {
                log.LogDebug($"(in) RestoreSources '{string.Join(";", RestoreSources.Select(p => p))}'");
            }
            if (RestorePackagesPath != null)
            {
                log.LogDebug($"(in) RestorePackagesPath '{RestorePackagesPath}'");
            }
            if (RestoreFallbackFolders != null)
            {
                log.LogDebug($"(in) RestoreFallbackFolders '{string.Join(";", RestoreFallbackFolders.Select(p => p))}'");
            }
            if (RestoreConfigFile != null)
            {
                log.LogDebug($"(in) RestoreConfigFile '{RestoreConfigFile}'");
            }

            if (RestoreSolutionDirectory != null)
            {
                log.LogDebug($"(in) RestoreSolutionDirectory '{RestoreSolutionDirectory}'");
            }

            if (RestoreAdditionalProjectSources != null)
            {
                log.LogDebug($"(in) RestoreAdditionalProjectSources '{RestoreAdditionalProjectSources}'");
            }

            if (RestoreAdditionalProjectFallbackFolders != null)
            {
                log.LogDebug($"(in) RestoreAdditionalProjectFallbackFolders '{RestoreAdditionalProjectFallbackFolders}'");
            }

            try
            {
                var settings = RestoreSettingsUtils.ReadSettings(RestoreSolutionDirectory, Path.GetDirectoryName(ProjectUniqueName), RestoreConfigFile, _machineWideSettings);

                OutputPackagesPath = RestoreSettingsUtils.GetPackagesPath(ProjectUniqueName, settings, RestorePackagesPath);

                if (RestoreSources == null)
                {
                    var packageSourceProvider      = new PackageSourceProvider(settings);
                    var packageSourcesFromProvider = packageSourceProvider.LoadPackageSources();
                    OutputSources = packageSourcesFromProvider.Select(e => e.Source).ToArray();
                }
                else if (MSBuildRestoreUtility.ContainsClearKeyword(RestoreSources))
                {
                    if (MSBuildRestoreUtility.LogErrorForClearIfInvalid(RestoreSources, ProjectUniqueName, log))
                    {
                        // Fail due to invalid combination
                        return(false);
                    }

                    OutputSources = new string[] { };
                }
                else
                {
                    // Relative -> Absolute paths
                    OutputSources = RestoreSources.Select(e => UriUtility.GetAbsolutePathFromFile(ProjectUniqueName, e)).ToArray();
                }

                // Append additional sources
                OutputSources = AppendItems(OutputSources, RestoreAdditionalProjectSources);

                if (RestoreFallbackFolders == null)
                {
                    OutputFallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings).ToArray();
                }
                else if (MSBuildRestoreUtility.ContainsClearKeyword(RestoreFallbackFolders))
                {
                    if (MSBuildRestoreUtility.LogErrorForClearIfInvalid(RestoreFallbackFolders, ProjectUniqueName, log))
                    {
                        // Fail due to invalid combination
                        return(false);
                    }

                    OutputFallbackFolders = new string[] { };
                }
                else
                {
                    // Relative -> Absolute paths
                    OutputFallbackFolders = RestoreFallbackFolders.Select(e => UriUtility.GetAbsolutePathFromFile(ProjectUniqueName, e)).ToArray();
                }

                // Append additional fallback folders
                OutputFallbackFolders = AppendItems(OutputFallbackFolders, RestoreAdditionalProjectFallbackFolders);

                OutputConfigFilePaths = SettingsUtility.GetConfigFilePaths(settings).ToArray();
            }
            catch (Exception ex)
            {
                // Log exceptions with error codes if they exist.
                ExceptionUtilities.LogException(ex, log);
                return(false);
            }

            // Log Outputs
            log.LogDebug($"(out) OutputPackagesPath '{OutputPackagesPath}'");
            log.LogDebug($"(out) OutputSources '{string.Join(";", OutputSources.Select(p => p))}'");
            log.LogDebug($"(out) OutputFallbackFolders '{string.Join(";", OutputFallbackFolders.Select(p => p))}'");
            log.LogDebug($"(out) OutputConfigFilePaths '{string.Join(";", OutputConfigFilePaths.Select(p => p))}'");

            return(true);
        }
        public async Task GetPackageSpecsAsync_ReadSettingsWithFullPaths(string restorePackagesPath, string sources, string fallbackFolders)
        {
            // Arrange
            using (var testDirectory = TestDirectory.Create())
            {
                var projectAdapter = CreateProjectAdapter(testDirectory);
                Mock.Get(projectAdapter)
                .SetupGet(x => x.RestorePackagesPath)
                .Returns(restorePackagesPath);

                Mock.Get(projectAdapter)
                .SetupGet(x => x.RestoreSources)
                .Returns(sources);

                Mock.Get(projectAdapter)
                .SetupGet(x => x.RestoreFallbackFolders)
                .Returns(fallbackFolders);

                var projectServices = new TestProjectSystemServices();

                var testProject = new LegacyPackageReferenceProject(
                    projectAdapter,
                    Guid.NewGuid().ToString(),
                    projectServices,
                    _threadingService);

                var settings = NullSettings.Instance;
                var testDependencyGraphCacheContext = new DependencyGraphCacheContext(NullLogger.Instance, settings);

                await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

                // Act
                var packageSpecs = await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext);

                // Assert
                Assert.NotNull(packageSpecs);
                var actualRestoreSpec = packageSpecs.Single();
                SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec);

                // Assert packagespath
                Assert.Equal(restorePackagesPath != null ? restorePackagesPath : SettingsUtility.GetGlobalPackagesFolder(settings), actualRestoreSpec.RestoreMetadata.PackagesPath);

                // assert sources
                var specSources     = actualRestoreSpec.RestoreMetadata.Sources.Select(e => e.Source);
                var expectedSources = sources != null?MSBuildStringUtility.Split(sources) : SettingsUtility.GetEnabledSources(settings).Select(e => e.Source);

                Assert.True(Enumerable.SequenceEqual(expectedSources.OrderBy(t => t), specSources.OrderBy(t => t)));

                // assert fallbackfolders
                var specFallback    = actualRestoreSpec.RestoreMetadata.FallbackFolders;
                var expectedFolders = fallbackFolders != null?MSBuildStringUtility.Split(fallbackFolders) : SettingsUtility.GetFallbackPackageFolders(settings);

                Assert.True(Enumerable.SequenceEqual(expectedFolders.OrderBy(t => t), specFallback.OrderBy(t => t)));

                // Verify
                Mock.Get(projectAdapter)
                .Verify(x => x.RestorePackagesPath, Times.Once);
                Mock.Get(projectAdapter)
                .Verify(x => x.RestoreSources, Times.Once);
                Mock.Get(projectAdapter)
                .Verify(x => x.RestoreFallbackFolders, Times.Once);
            }
        }
        public NuGetSourceRepositoryProvider(ISettings settings, ICakeConfiguration config, PackageReference package, string packagesRoot)
        {
            if (settings is null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (config is null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            if (package is null)
            {
                throw new ArgumentNullException(nameof(package));
            }

            // Create the default v3 resource provider
            _resourceProviders = new List <Lazy <INuGetResourceProvider> >();
            _resourceProviders.AddRange(Repository.Provider.GetCoreV3());

            // Add repositories
            var sourceComparer = new NuGetSourceRepositoryComparer();

            _repositories        = new HashSet <SourceRepository>(sourceComparer);
            _primaryRepositories = new HashSet <SourceRepository>(sourceComparer);
            _localRepositories   = new HashSet <SourceRepository>(sourceComparer);
            _localRepositories.Add(CreateRepository(packagesRoot));
            _localRepositories.Add(CreateRepository(SettingsUtility.GetGlobalPackagesFolder(settings)));
            _localRepositories.AddRange(SettingsUtility.GetFallbackPackageFolders(settings).Select(CreateRepository));

            var packageSources = new PackageSourceProvider(settings).LoadPackageSources().ToList();

            if (package.Address != null)
            {
                var repository = GetOrCreateRepository(package.Address.AbsoluteUri);

                // Sources specified in directive is always primary.
                _repositories.Add(repository);
                _primaryRepositories.Add(repository);
            }

            var nugetSources = config.GetValue(Constants.NuGet.Source);

            if (!string.IsNullOrEmpty(nugetSources))
            {
                foreach (var nugetSource in nugetSources.Split(';'))
                {
                    if (!string.IsNullOrWhiteSpace(nugetSource))
                    {
                        var repository = GetOrCreateRepository(nugetSource);
                        _repositories.Add(repository);

                        // If source is not specified in directive, add it as primary source.
                        if (package.Address == null)
                        {
                            _primaryRepositories.Add(repository);
                        }
                    }
                }
            }
            else
            {
                // Only add sources added via NuGet.config if nuget_source configuration value is not specified.
                foreach (var source in packageSources)
                {
                    if (source.IsEnabled)
                    {
                        var repository = CreateRepository(source);
                        _repositories.Add(repository);

                        // If source is not specified in directive, add it as primary source.
                        if (package.Address == null)
                        {
                            _primaryRepositories.Add(repository);
                        }
                    }
                }
            }

            SourceRepository GetOrCreateRepository(string source)
            {
                var packageSource = packageSources
                                    .Where(p => p.IsEnabled)
                                    .FirstOrDefault(p => p.Source.Equals(source, StringComparison.OrdinalIgnoreCase));

                return(packageSource == null?
                       CreateRepository(source) :
                           CreateRepository(packageSource));
            }
        }
Esempio n. 25
0
        /// <summary>
        /// Fetch, if not already downloaded, and install the package represented by
        /// (<paramref name="packageId"/>, <paramref name="version"/>).
        /// </summary>
        /// <remarks>It is safe to call it concurrently be cause we operations are done using the FileLock.</remarks>
        /// <param name="packageId">Name of package to install.</param>
        /// <param name="version">Version of package to install.</param>
        public async Task <NugetLocalPackage> InstallPackage(string packageId, PackageVersion version, IEnumerable <string> targetFrameworks, ProgressReport progress)
        {
            using (GetLocalRepositoryLock())
            {
                currentProgressReport = progress;
                try
                {
                    var identity = new PackageIdentity(packageId, version.ToNuGetVersion());

                    var resolutionContext = new ResolutionContext(
                        DependencyBehavior.Lowest,
                        true,
                        true,
                        VersionConstraints.None);

                    var repositories = PackageSources.Select(sourceRepositoryProvider.CreateRepository).ToArray();

                    var projectContext = new EmptyNuGetProjectContext()
                    {
                        ActionType = NuGetActionType.Install,
                        PackageExtractionContext = new PackageExtractionContext(PackageSaveMode.Defaultv3, XmlDocFileSaveMode.Skip, null, NativeLogger),
                    };

                    ActivityCorrelationId.StartNew();

                    {
                        var installPath = SettingsUtility.GetGlobalPackagesFolder(settings);

                        // In case it's a package without any TFM (i.e. Visual Studio plugin), we still need to specify one
                        if (!targetFrameworks.Any())
                        {
                            targetFrameworks = new string[] { "net6.0" }
                        }
                        ;

                        // Old version expects to be installed in GamePackages
                        if (packageId == "Xenko" && version < new PackageVersion(3, 0, 0, 0) && oldRootDirectory != null)
                        {
                            installPath = oldRootDirectory;
                        }

                        var projectPath = Path.Combine("StrideLauncher.json");
                        var spec        = new PackageSpec()
                        {
                            Name         = Path.GetFileNameWithoutExtension(projectPath), // make sure this package never collides with a dependency
                            FilePath     = projectPath,
                            Dependencies = new List <LibraryDependency>()
                            {
                                new LibraryDependency
                                {
                                    LibraryRange = new LibraryRange(packageId, new VersionRange(version.ToNuGetVersion()), LibraryDependencyTarget.Package),
                                }
                            },
                            RestoreMetadata = new ProjectRestoreMetadata
                            {
                                ProjectPath              = projectPath,
                                ProjectName              = Path.GetFileNameWithoutExtension(projectPath),
                                ProjectStyle             = ProjectStyle.PackageReference,
                                ProjectUniqueName        = projectPath,
                                OutputPath               = Path.Combine(Path.GetTempPath(), $"StrideLauncher-{packageId}-{version.ToString()}"),
                                OriginalTargetFrameworks = targetFrameworks.ToList(),
                                ConfigFilePaths          = settings.GetConfigFilePaths(),
                                PackagesPath             = installPath,
                                Sources         = SettingsUtility.GetEnabledSources(settings).ToList(),
                                FallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings).ToList()
                            },
                        };
                        foreach (var targetFramework in targetFrameworks)
                        {
                            spec.TargetFrameworks.Add(new TargetFrameworkInformation {
                                FrameworkName = NuGetFramework.Parse(targetFramework)
                            });
                        }

                        using (var context = new SourceCacheContext {
                            MaxAge = DateTimeOffset.UtcNow
                        })
                        {
                            context.IgnoreFailedSources = true;

                            var dependencyGraphSpec = new DependencyGraphSpec();

                            dependencyGraphSpec.AddProject(spec);

                            dependencyGraphSpec.AddRestore(spec.RestoreMetadata.ProjectUniqueName);

                            IPreLoadedRestoreRequestProvider requestProvider = new DependencyGraphSpecRequestProvider(new RestoreCommandProvidersCache(), dependencyGraphSpec);

                            var restoreArgs = new RestoreArgs
                            {
                                AllowNoOp             = true,
                                CacheContext          = context,
                                CachingSourceProvider = new CachingSourceProvider(new PackageSourceProvider(settings)),
                                Log = NativeLogger,
                            };

                            // Create requests from the arguments
                            var requests = requestProvider.CreateRequests(restoreArgs).Result;

                            foreach (var request in requests)
                            {
                                // Limit concurrency to avoid timeout
                                request.Request.MaxDegreeOfConcurrency = 4;

                                var command = new RestoreCommand(request.Request);

                                // Act
                                var result = await command.ExecuteAsync();

                                if (!result.Success)
                                {
                                    throw new InvalidOperationException($"Could not restore package {packageId}");
                                }
                                foreach (var install in result.RestoreGraphs.Last().Install)
                                {
                                    var package = result.LockFile.Libraries.FirstOrDefault(x => x.Name == install.Library.Name && x.Version == install.Library.Version);
                                    if (package != null)
                                    {
                                        var packagePath = Path.Combine(installPath, package.Path);
                                        OnPackageInstalled(this, new PackageOperationEventArgs(new PackageName(install.Library.Name, install.Library.Version.ToPackageVersion()), packagePath));
                                    }
                                }
                            }
                        }

                        if (packageId == "Xenko" && version < new PackageVersion(3, 0, 0, 0))
                        {
                            UpdateTargetsHelper();
                        }
                    }

                    // Load the recently installed package
                    var installedPackages = GetPackagesInstalled(new[] { packageId });
                    return(installedPackages.FirstOrDefault(p => p.Version == version));
                }
                finally
                {
                    currentProgressReport = null;
                }
            }
        }
Esempio n. 26
0
        public static async Task <(RestoreRequest, RestoreResult)> Restore(ILogger logger, string packageName, VersionRange versionRange)
        {
            var settings = NuGet.Configuration.Settings.LoadDefaultSettings(null);
            var packageSourceProvider = new PackageSourceProvider(settings);
            var installPath           = SettingsUtility.GetGlobalPackagesFolder(settings);
            var assemblies            = new List <string>();

            var projectPath = Path.Combine("XenkoNugetResolver.json");
            var spec        = new PackageSpec()
            {
                Name         = Path.GetFileNameWithoutExtension(projectPath), // make sure this package never collides with a dependency
                FilePath     = projectPath,
                Dependencies = new List <LibraryDependency>()
                {
                    new LibraryDependency
                    {
                        LibraryRange = new LibraryRange(packageName, versionRange, LibraryDependencyTarget.Package),
                    }
                },
                TargetFrameworks =
                {
                    new TargetFrameworkInformation
                    {
                        FrameworkName = NuGetFramework.Parse("net48"),
                    }
                },
                RestoreMetadata = new ProjectRestoreMetadata
                {
                    ProjectPath              = projectPath,
                    ProjectName              = Path.GetFileNameWithoutExtension(projectPath),
                    ProjectStyle             = ProjectStyle.PackageReference,
                    ProjectUniqueName        = projectPath,
                    OutputPath               = Path.Combine(Path.GetTempPath(), $"XenkoNugetResolver-{packageName}-{versionRange.MinVersion.ToString()}"),
                    OriginalTargetFrameworks = new[] { "net48" },
                    ConfigFilePaths          = settings.GetConfigFilePaths(),
                    PackagesPath             = SettingsUtility.GetGlobalPackagesFolder(settings),
                    Sources         = SettingsUtility.GetEnabledSources(settings).ToList(),
                    FallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings).ToList()
                },
            };

            // remove all remote sources, so we don't have to worry about connectivity issues
            // we are only restoring local dev packages anyway
            for (int i = 0; i < spec.RestoreMetadata.Sources.Count; i++)
            {
                var s = spec.RestoreMetadata.Sources[i];
                if (s.IsLocal == false)
                {
                    spec.RestoreMetadata.Sources.RemoveAt(i);
                    i--;
                }
            }

            using (var context = new SourceCacheContext())
            {
                context.IgnoreFailedSources = true;

                var dependencyGraphSpec = new DependencyGraphSpec();

                dependencyGraphSpec.AddProject(spec);

                dependencyGraphSpec.AddRestore(spec.RestoreMetadata.ProjectUniqueName);

                IPreLoadedRestoreRequestProvider requestProvider = new DependencyGraphSpecRequestProvider(new RestoreCommandProvidersCache(), dependencyGraphSpec);

                var restoreArgs = new RestoreArgs
                {
                    AllowNoOp             = true,
                    CacheContext          = context,
                    CachingSourceProvider = new CachingSourceProvider(new PackageSourceProvider(settings)),
                    Log = logger,
                };

                // Create requests from the arguments
                var requests = requestProvider.CreateRequests(restoreArgs).Result;

                // Restore the packages
                for (int tryCount = 0; tryCount < 2; ++tryCount)
                {
                    try
                    {
                        var results = await RestoreRunner.RunWithoutCommit(requests, restoreArgs);

                        // Commit results so that noop cache works next time
                        foreach (var result in results)
                        {
                            await result.Result.CommitAsync(logger, CancellationToken.None);
                        }
                        var mainResult = results.First();
                        return(mainResult.SummaryRequest.Request, mainResult.Result);
                    }
                    catch (Exception e) when(e is UnauthorizedAccessException || e is IOException)
                    {
                        // If we have an unauthorized access exception, it means assemblies are locked by running Xenko process
                        // During first try, kill some known harmless processes, and try again
                        if (tryCount == 1)
                        {
                            throw;
                        }

                        foreach (var process in new[] { "Xenko.ConnectionRouter" }.SelectMany(Process.GetProcessesByName))
                        {
                            try
                            {
                                if (process.Id != Process.GetCurrentProcess().Id)
                                {
                                    process.Kill();
                                    process.WaitForExit();
                                }
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }

                throw new InvalidOperationException("Unreachable code");
            }
        }
Esempio n. 27
0
 public IReadOnlyList <string> GetEffectiveFallbackPackageFolders(ISettings settings)
 {
     return(SettingsUtility.GetFallbackPackageFolders(settings));
 }
        public override bool Execute()
        {
            var log = new MSBuildLogger(Log);

            // Log Inputs
            BuildTasksUtility.LogInputParam(log, nameof(ProjectUniqueName), ProjectUniqueName);
            BuildTasksUtility.LogInputParam(log, nameof(RestoreSources), RestoreSources);
            BuildTasksUtility.LogInputParam(log, nameof(RestorePackagesPath), RestorePackagesPath);
            BuildTasksUtility.LogInputParam(log, nameof(RestoreFallbackFolders), RestoreFallbackFolders);
            BuildTasksUtility.LogInputParam(log, nameof(RestoreConfigFile), RestoreConfigFile);
            BuildTasksUtility.LogInputParam(log, nameof(RestoreSolutionDirectory), RestoreSolutionDirectory);
            BuildTasksUtility.LogInputParam(log, nameof(RestorePackagesPathOverride), RestorePackagesPathOverride);
            BuildTasksUtility.LogInputParam(log, nameof(RestoreSourcesOverride), RestoreSourcesOverride);
            BuildTasksUtility.LogInputParam(log, nameof(RestoreFallbackFoldersOverride), RestoreFallbackFoldersOverride);
            BuildTasksUtility.LogInputParam(log, nameof(MSBuildStartupDirectory), MSBuildStartupDirectory);

            try
            {
                // Validate inputs
                if (RestoreSourcesOverride == null &&
                    MSBuildRestoreUtility.LogErrorForClearIfInvalid(RestoreSources, ProjectUniqueName, log))
                {
                    // Fail due to invalid source combination
                    return(false);
                }

                if (RestoreFallbackFoldersOverride == null &&
                    MSBuildRestoreUtility.LogErrorForClearIfInvalid(RestoreFallbackFolders, ProjectUniqueName, log))
                {
                    // Fail due to invalid fallback combination
                    return(false);
                }

                // Settings
                // Find the absolute path of nuget.config, this should only be set on the command line. Setting the path in project files
                // is something that could happen, but it is not supported.
                var absoluteConfigFilePath = GetGlobalAbsolutePath(RestoreConfigFile);
                var settings = RestoreSettingsUtils.ReadSettings(RestoreSolutionDirectory, Path.GetDirectoryName(ProjectUniqueName), absoluteConfigFilePath, _machineWideSettings);
                OutputConfigFilePaths = SettingsUtility.GetConfigFilePaths(settings).ToArray();

                // PackagesPath
                OutputPackagesPath = RestoreSettingsUtils.GetValue(
                    () => GetGlobalAbsolutePath(RestorePackagesPathOverride),
                    () => string.IsNullOrEmpty(RestorePackagesPath) ? null : UriUtility.GetAbsolutePathFromFile(ProjectUniqueName, RestorePackagesPath),
                    () => SettingsUtility.GetGlobalPackagesFolder(settings));

                // Sources
                var currentSources = RestoreSettingsUtils.GetValue(
                    () => RestoreSourcesOverride?.Select(MSBuildRestoreUtility.FixSourcePath).Select(e => GetGlobalAbsolutePath(e)).ToArray(),
                    () => MSBuildRestoreUtility.ContainsClearKeyword(RestoreSources) ? new string[0] : null,
                    () => RestoreSources?.Select(MSBuildRestoreUtility.FixSourcePath).Select(e => UriUtility.GetAbsolutePathFromFile(ProjectUniqueName, e)).ToArray(),
                    () => (new PackageSourceProvider(settings)).LoadPackageSources().Where(e => e.IsEnabled).Select(e => e.Source).ToArray());

                // Append additional sources
                // Escape strings to avoid xplat path issues with msbuild.
                var additionalProjectSources = MSBuildRestoreUtility.AggregateSources(
                    values: GetPropertyValues(RestoreSettingsPerFramework, "RestoreAdditionalProjectSources"),
                    excludeValues: Enumerable.Empty <string>())
                                               .Select(MSBuildRestoreUtility.FixSourcePath)
                                               .ToArray();

                OutputSources = AppendItems(currentSources, additionalProjectSources);

                // Fallback folders
                var currentFallbackFolders = RestoreSettingsUtils.GetValue(
                    () => RestoreFallbackFoldersOverride?.Select(e => GetGlobalAbsolutePath(e)).ToArray(),
                    () => MSBuildRestoreUtility.ContainsClearKeyword(RestoreFallbackFolders) ? new string[0] : null,
                    () => RestoreFallbackFolders?.Select(e => UriUtility.GetAbsolutePathFromFile(ProjectUniqueName, e)).ToArray(),
                    () => SettingsUtility.GetFallbackPackageFolders(settings).ToArray());

                // Append additional fallback folders after removing excluded folders
                var additionalProjectFallbackFolders = MSBuildRestoreUtility.AggregateSources(
                    values: GetPropertyValues(RestoreSettingsPerFramework, "RestoreAdditionalProjectFallbackFolders"),
                    excludeValues: GetPropertyValues(RestoreSettingsPerFramework, "RestoreAdditionalProjectFallbackFoldersExcludes"))
                                                       .ToArray();

                OutputFallbackFolders = AppendItems(currentFallbackFolders, additionalProjectFallbackFolders);
            }
            catch (Exception ex)
            {
                // Log exceptions with error codes if they exist.
                ExceptionUtilities.LogException(ex, log);
                return(false);
            }

            // Log Outputs
            BuildTasksUtility.LogOutputParam(log, nameof(OutputPackagesPath), OutputPackagesPath);
            BuildTasksUtility.LogOutputParam(log, nameof(OutputSources), OutputSources);
            BuildTasksUtility.LogOutputParam(log, nameof(OutputFallbackFolders), OutputFallbackFolders);
            BuildTasksUtility.LogOutputParam(log, nameof(OutputConfigFilePaths), OutputConfigFilePaths);

            return(true);
        }
        /// <summary>
        /// Restores a package by querying, downloading, and unzipping it without generating any other files (like project.assets.json).
        /// </summary>
        /// <param name="projectPath">The full path to the project.</param>
        /// <param name="id">The ID of the package.</param>
        /// <param name="version">The version of the package.</param>
        /// <param name="settings">The NuGet settings to use.</param>
        /// <param name="logger">An <see cref="ILogger"/> to use for logging.</param>
        /// <returns></returns>
        public static Task <IReadOnlyList <RestoreResultPair> > RunWithoutCommit(string projectPath, string id, string version, ISettings settings, ILogger logger)
        {
            using (SourceCacheContext sourceCacheContext = new SourceCacheContext
            {
                IgnoreFailedSources = true,
            })
            {
                // The package spec details what packages to restore
                PackageSpec packageSpec = new PackageSpec(TargetFrameworks.Select(i => new TargetFrameworkInformation
                {
                    FrameworkName = i,
                }).ToList())
                {
                    Dependencies = new List <LibraryDependency>
                    {
                        new LibraryDependency
                        {
                            LibraryRange   = new LibraryRange(id, new VersionRange(NuGetVersion.Parse(version)), LibraryDependencyTarget.Package),
                            SuppressParent = LibraryIncludeFlags.All,
                            AutoReferenced = true,
                            IncludeType    = LibraryIncludeFlags.None,
                            Type           = LibraryDependencyType.Build
                        }
                    },
                    RestoreMetadata = new ProjectRestoreMetadata
                    {
                        ProjectPath              = projectPath,
                        ProjectName              = Path.GetFileNameWithoutExtension(projectPath),
                        ProjectStyle             = ProjectStyle.PackageReference,
                        ProjectUniqueName        = projectPath,
                        OutputPath               = Path.GetTempPath(),
                        OriginalTargetFrameworks = TargetFrameworks.Select(i => i.ToString()).ToList(),
                        ConfigFilePaths          = SettingsUtility.GetConfigFilePaths(settings).ToList(),
                        PackagesPath             = SettingsUtility.GetGlobalPackagesFolder(settings),
                        Sources         = SettingsUtility.GetEnabledSources(settings).ToList(),
                        FallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings).ToList()
                    },
                    FilePath = projectPath,
                    Name     = Path.GetFileNameWithoutExtension(projectPath),
                };

                DependencyGraphSpec dependencyGraphSpec = new DependencyGraphSpec();

                dependencyGraphSpec.AddProject(packageSpec);

                dependencyGraphSpec.AddRestore(packageSpec.RestoreMetadata.ProjectUniqueName);

                IPreLoadedRestoreRequestProvider requestProvider = new DependencyGraphSpecRequestProvider(new RestoreCommandProvidersCache(), dependencyGraphSpec);

                RestoreArgs restoreArgs = new RestoreArgs
                {
                    AllowNoOp             = true,
                    CacheContext          = sourceCacheContext,
                    CachingSourceProvider = new CachingSourceProvider(new PackageSourceProvider(settings)),
                    Log = logger,
                };

                // Create requests from the arguments
                IReadOnlyList <RestoreSummaryRequest> requests = requestProvider.CreateRequests(restoreArgs).Result;

                // Restore the package without generating extra files
                return(RestoreRunner.RunWithoutCommit(requests, restoreArgs));
            }
        }
        /// <summary>
        /// Restores a package by querying, downloading, and unzipping it without generating any other files (like project.assets.json).
        /// </summary>
        /// <param name="libraryIdentity">The <see cref="LibraryIdentity"/> of the package.</param>
        /// <param name="settings">The NuGet settings to use.</param>
        /// <param name="logger">An <see cref="ILogger"/> to use for logging.</param>
        /// <returns></returns>
        public static Task <IReadOnlyList <RestoreResultPair> > RunWithoutCommit(LibraryIdentity libraryIdentity, ISettings settings, ILogger logger)
        {
            using (var sourceCacheContext = new SourceCacheContext
            {
                IgnoreFailedSources = true,
            })
            {
                var projectDirectory = Path.Combine(NuGetEnvironment.GetFolderPath(NuGetFolderPath.Temp), Guid.NewGuid().ToString("N"));

                var projectName = Guid.NewGuid().ToString("N");

                var projectFullPath = Path.Combine(projectDirectory, $"{projectName}.proj");

                // Iterate through TargetFrameworks to generate Lists required for packageSpec
                var frameworks = new List <TargetFrameworkInformation>(TargetFrameworks.Count);
                var originalTargetFrameworks = new List <string>(TargetFrameworks.Count);
                foreach (var tf in TargetFrameworks)
                {
                    frameworks.Add(new TargetFrameworkInformation
                    {
                        FrameworkName = tf
                    });

                    originalTargetFrameworks.Add(tf.ToString());
                }

                // The package spec details what packages to restore
                var packageSpec = new PackageSpec(frameworks)
                {
                    Dependencies = new List <LibraryDependency>
                    {
                        new LibraryDependency
                        {
                            LibraryRange = new LibraryRange(
                                libraryIdentity.Name,
                                new VersionRange(
                                    minVersion: libraryIdentity.Version,
                                    includeMinVersion: true,
                                    maxVersion: libraryIdentity.Version,
                                    includeMaxVersion: true),
                                LibraryDependencyTarget.Package),
                            SuppressParent = LibraryIncludeFlags.All,
                            AutoReferenced = true,
                            IncludeType    = LibraryIncludeFlags.None,
                        }
                    },
                    RestoreMetadata = new ProjectRestoreMetadata
                    {
                        ProjectPath              = projectFullPath,
                        ProjectName              = projectName,
                        ProjectStyle             = ProjectStyle.PackageReference,
                        ProjectUniqueName        = projectFullPath,
                        OutputPath               = projectDirectory,
                        OriginalTargetFrameworks = originalTargetFrameworks,
                        ConfigFilePaths          = settings.GetConfigFilePaths(),
                        PackagesPath             = SettingsUtility.GetGlobalPackagesFolder(settings),
                        Sources         = SettingsUtility.GetEnabledSources(settings).AsList(),
                        FallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings).ToList()
                    },
                    FilePath = projectFullPath,
                    Name     = projectName,
                };

                var dependencyGraphSpec = new DependencyGraphSpec();

                dependencyGraphSpec.AddProject(packageSpec);

                dependencyGraphSpec.AddRestore(packageSpec.RestoreMetadata.ProjectUniqueName);

                IPreLoadedRestoreRequestProvider requestProvider = new DependencyGraphSpecRequestProvider(new RestoreCommandProvidersCache(), dependencyGraphSpec);

                var restoreArgs = new RestoreArgs
                {
                    AllowNoOp    = false,
                    CacheContext = sourceCacheContext,
#pragma warning disable CS0618 // Type or member is obsolete
                    CachingSourceProvider = new CachingSourceProvider(new PackageSourceProvider(settings, enablePackageSourcesChangedEvent: false)),
#pragma warning restore CS0618 // Type or member is obsolete
                    Log = logger,
                };

                // Create requests from the arguments
                var requests = requestProvider.CreateRequests(restoreArgs).Result;

                // Restore the package without generating extra files
                return(RestoreRunner.RunWithoutCommit(requests, restoreArgs));
            }
        }