public void AdditionalImportsAreAdded_WithOverrideImportFalse()
        {
            // Arrange
            var path     = "obj/test.props";
            var depsFile = DependencyVersionsFile.Create(addOverrideImport: false, additionalImports: new[] { path });

            depsFile.Save(_tempFile);

            // Act
            var project = ProjectRootElement.Open(_tempFile);

            _output.WriteLine(File.ReadAllText(_tempFile));

            // Assert
            var import = Assert.Single(project.Imports);

            Assert.Equal(path, import.Project);
        }
        public void SetIsCaseInsensitive()
        {
            var depsFile = DependencyVersionsFile.Create(addOverrideImport: true);

            depsFile.Set("XunitRunnerVisualStudioVersion", "2.3.0");
            depsFile.Set("XunitRunnerVisualstudioVersion", "2.4.0");
            depsFile.Save(_tempFile);

            var project = ProjectRootElement.Open(_tempFile);

            _output.WriteLine(File.ReadAllText(_tempFile));

            var versions = Assert.Single(project.PropertyGroups, p => !string.IsNullOrEmpty(p.Label));
            var prop     = Assert.Single(versions.Properties);

            Assert.Equal("XunitRunnerVisualStudioVersion", prop.Name);
            Assert.Equal("2.4.0", prop.Value);
        }
        public void ItSortsVariablesAlphabetically()
        {
            var depsFile = DependencyVersionsFile.Create(addOverrideImport: true);

            depsFile.Set("XyzPackageVersion", "123");
            depsFile.Set("AbcPackageVersion", "456");
            depsFile.Save(_tempFile);

            var project = ProjectRootElement.Open(_tempFile);

            _output.WriteLine(File.ReadAllText(_tempFile));

            var versions = Assert.Single(project.PropertyGroups, p => !string.IsNullOrEmpty(p.Label));

            Assert.Collection(versions.Properties,
                              v => Assert.Equal("AbcPackageVersion", v.Name),
                              v => Assert.Equal("XyzPackageVersion", v.Name));
        }
        public void AdditionalImportsAreAdded_WithOverrideImportTrue()
        {
            // Arrange
            var path     = "obj/external.props";
            var depsFile = DependencyVersionsFile.Create(addOverrideImport: true, additionalImports: new[] { path });

            depsFile.Save(_tempFile);

            // Act
            var project = ProjectRootElement.Open(_tempFile);

            _output.WriteLine(File.ReadAllText(_tempFile));

            // Assert
            Assert.Collection(
                project.Imports,
                import => Assert.Equal(path, import.Project),
                import => Assert.Equal("$(DotNetPackageVersionPropsPath)", import.Project));
        }
Example #5
0
        public override bool Execute()
        {
            OutputPath = OutputPath.Replace('\\', '/');
            Directory.CreateDirectory(Path.GetDirectoryName(OutputPath));

            DependencyVersionsFile depsFile;

            if (File.Exists(OutputPath))
            {
                if (!DependencyVersionsFile.TryLoad(OutputPath, out depsFile))
                {
                    depsFile = DependencyVersionsFile.Create(AddOverrideImport, AdditionalImports);
                    Log.LogWarning($"Could not load the existing deps file from {OutputPath}. This file will be overwritten.");
                }
            }
            else
            {
                depsFile = DependencyVersionsFile.Create(AddOverrideImport, AdditionalImports);
            }

            var varNames = new HashSet <string>();

            foreach (var pkg in Packages)
            {
                var packageVersion = pkg.GetMetadata("Version");

                if (string.IsNullOrEmpty(packageVersion))
                {
                    Log.LogError("Package {0} is missing the Version metadata", pkg.ItemSpec);
                    continue;
                }

                string packageVarName;
                if (!string.IsNullOrEmpty(pkg.GetMetadata("VariableName")))
                {
                    packageVarName = pkg.GetMetadata("VariableName");
                    if (!packageVarName.EndsWith("Version", StringComparison.Ordinal))
                    {
                        Log.LogError("VariableName for {0} must end in 'Version'", pkg.ItemSpec);
                        continue;
                    }
                }
                else
                {
                    packageVarName = DependencyVersionsFile.GetVariableName(pkg.ItemSpec);
                }

                if (varNames.Contains(packageVarName))
                {
                    Log.LogError("Multiple packages would produce {0} in the generated dependencies.props file. Set VariableName to differentiate the packages manually", packageVarName);
                    continue;
                }

                var item = depsFile.Update(packageVarName, packageVersion);
                if (!SuppressVariableLabels)
                {
                    item.SetLabel(pkg.ItemSpec);
                }
            }

            depsFile.Save(OutputPath);
            Log.LogMessage(MessageImportance.Normal, $"Generated {OutputPath}");
            return(!Log.HasLoggedErrors);
        }