Ejemplo n.º 1
0
        public ProjectFile(string csprojFilePath)
        {
            var fileContent = File.ReadAllText(csprojFilePath);

            lineEndings = fileContent.Contains("\r\n") ? "\r\n" : "\n";
            FilePath    = csprojFilePath;
            Document    = XmlDocumentHelper.Create(fileContent);
        }
Ejemplo n.º 2
0
        public ProjectFile(string csprojFilePath)
        {
            var fileContent = File.ReadAllText(csprojFilePath);

            log = LogManager.GetLogger(typeof(ProjectFile));

            LineEndings = fileContent.Contains("\r\n") ? "\r\n" : "\n";
            FilePath    = csprojFilePath;
            Document    = XmlDocumentHelper.Create(fileContent);
            newFormat   = !string.IsNullOrEmpty(Document.DocumentElement?.GetAttribute("Sdk"));
        }
Ejemplo n.º 3
0
        public XmlDocument CreateCsProjWithNugetReferences(List <Dep> deps, bool preRelease)
        {
            if (!newFormat)
            {
                throw new Exception("Only new csproj format supported");
            }
            var fileContent    = File.ReadAllText(FilePath);
            var patchedProjDoc = XmlDocumentHelper.Create(fileContent);
            var genPackageNode = patchedProjDoc.SelectSingleNode("//PropertyGroup/GeneratePackageOnBuild");

            genPackageNode?.ParentNode?.RemoveChild(genPackageNode);
            var itemGroup = patchedProjDoc.CreateElement("ItemGroup");

            if (patchedProjDoc.DocumentElement == null)
            {
                throw new Exception("DocumentElement is null at csproj");
            }
            patchedProjDoc.DocumentElement?.AppendChild(itemGroup);

            var nugetRunCommand = NuGetHelper.GetNugetRunCommand();

            foreach (var dep in deps)
            {
                var refNodes = patchedProjDoc.SelectNodes("//Reference");
                if (refNodes != null)
                {
                    var node = refNodes.Cast <XmlNode>().FirstOrDefault(
                        x =>
                    {
                        var moduleName = x.Attributes?["Include"]?.Value;
                        return(moduleName != null && moduleName.Equals(dep.Name, StringComparison.InvariantCultureIgnoreCase));
                    });
                    node?.ParentNode?.RemoveChild(node);
                }

                var refElement  = patchedProjDoc.CreateElement("PackageReference");
                var includeAttr = patchedProjDoc.CreateAttribute("Include");
                includeAttr.Value = dep.Name;
                refElement.Attributes.Append(includeAttr);
                var packageVersion = NuGetHelper.GetNugetPackageVersion(dep.Name, nugetRunCommand, preRelease);
                if (!string.IsNullOrEmpty(packageVersion))
                {
                    var versionAttr = patchedProjDoc.CreateAttribute("Version");
                    versionAttr.Value = packageVersion;
                    refElement.Attributes.Append(versionAttr);
                }

                itemGroup.AppendChild(refElement);
            }

            return(patchedProjDoc);
        }
Ejemplo n.º 4
0
        public RulesetFile(string rulesetFilePath)
        {
            string fileContent;

            if (File.Exists(rulesetFilePath))
            {
                fileContent = File.ReadAllText(rulesetFilePath);
            }
            else
            {
                var name = Path.GetFileNameWithoutExtension(rulesetFilePath);
                fileContent =
                    $@"<?xml version=""1.0"" encoding=""utf-8""?>
<RuleSet Name=""{name}"" Description=""Ruleset for project {name}.csproj"" ToolsVersion=""10.0"">
</RuleSet>";
            }

            lineEndings = fileContent.Contains("\r\n") ? "\r\n" : "\n";
            FilePath    = rulesetFilePath;
            Document    = XmlDocumentHelper.Create(fileContent);
        }