public void AddChildElementTest()
        {
            var     manifest = new XmlDocument();
            XmlNode docNode  = manifest.CreateXmlDeclaration("1.0", "UTF-8", null);

            manifest.AppendChild(docNode);

            var element = XmlElementExtensions.AddChildElement(manifest, "package", new List <XmlNodeAttribute> {
                new XmlNodeAttribute("xmlns", @"http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd")
            });
            var metadata = XmlElementExtensions.AddChildElement(manifest, element, "metadata");

            XmlElementExtensions.AddChildElement(manifest, metadata, "id", "123");
            XmlElementExtensions.AddChildElement(manifest, metadata, "version", "0.1");
            XmlElementExtensions.AddChildElement(manifest, metadata, "authors", "AM");
            XmlElementExtensions.AddChildElement(manifest, metadata, "owners", "AM");
            XmlElementExtensions.AddChildElement(manifest, metadata, "licenseUrl", "http://example.com");
            XmlElementExtensions.AddChildElement(manifest, metadata, "projectUrl", "http://example.com");
            XmlElementExtensions.AddChildElement(manifest, metadata, "requireLicenseAcceptance", "false");
            XmlElementExtensions.AddChildElement(manifest, metadata, "description", "The test deployment package, built on " + DateTime.Now.ToShortDateString());
            XmlElementExtensions.AddChildElement(manifest, metadata, "releaseNotes", "");

            manifest.Save("test.nuspec");

            Assert.IsTrue(File.Exists("test.nuspec"));
            Assert.IsNotNull(XmlElementExtensions.ElementAnyNamespace(manifest, "package"));
        }
Esempio n. 2
0
        private void UpdatePackageIdWithAppendValue(XmlDocument nuSpec)
        {
            if (StringHelper.IsNullOrWhiteSpace(AppendToPackageId))
            {
                return;
            }

            var package = XmlElementExtensions.ElementAnyNamespace(nuSpec, "package");

            if (package == null)
            {
                throw new Exception("The NuSpec file does not contain a <package> XML element. The NuSpec file appears to be invalid.");
            }

            var metadata = XmlElementExtensions.ElementAnyNamespace(package, "metadata");

            if (metadata == null)
            {
                throw new Exception("The NuSpec file does not contain a <metadata> XML element. The NuSpec file appears to be invalid.");
            }

            var packageId = XmlElementExtensions.ElementAnyNamespace(metadata, "id");

            if (packageId == null)
            {
                throw new Exception("The NuSpec file does not contain a <id> XML element. The NuSpec file appears to be invalid.");
            }

            packageId.InnerText = string.Format("{0}.{1}", packageId.InnerText, AppendToPackageId.Trim());
        }
        public void ElementAtAnyNamespaceShouldReturnFirstNodeByName2()
        {
            var doc = new XmlDocument();

            doc.LoadXml(xml2);

            var result = XmlElementExtensions.ElementAnyNamespace(doc, "package");

            Assert.AreEqual("package", result.Name);
        }
Esempio n. 4
0
        private static bool SpecAlreadyHasFiles(XmlDocument nuSpec)
        {
            var package = XmlElementExtensions.ElementAnyNamespace(nuSpec, "package");

            if (package == null)
            {
                throw new Exception("The NuSpec file does not contain a <package> XML element. The NuSpec file appears to be invalid.");
            }

            var files = XmlElementExtensions.ElementAnyNamespace(package, "files");

            return(files != null && files.HasChildNodes);
        }
Esempio n. 5
0
        private void AddReleaseNotes(XmlDocument nuSpec)
        {
            if (StringHelper.IsNullOrWhiteSpace(ReleaseNotesFile))
            {
                return;
            }

            ReleaseNotesFile = fileSystem.GetFullPath(ReleaseNotesFile);

            if (!fileSystem.FileExists(ReleaseNotesFile))
            {
                LogWarning("OCT901", string.Format("The release notes file: {0} does not exist or could not be found. Release notes will not be added to the package.", ReleaseNotesFile));
                return;
            }

            LogMessage("Adding release notes from file: " + ReleaseNotesFile);

            var notes = fileSystem.ReadFile(ReleaseNotesFile);

            var package = XmlElementExtensions.ElementAnyNamespace(nuSpec, "package");

            if (package == null)
            {
                throw new Exception("The NuSpec file does not contain a <package> XML element. The NuSpec file appears to be invalid.");
            }

            var metadata = XmlElementExtensions.ElementAnyNamespace(package, "metadata");

            if (metadata == null)
            {
                throw new Exception("The NuSpec file does not contain a <metadata> XML element. The NuSpec file appears to be invalid.");
            }

            var releaseNotes = XmlElementExtensions.ElementAnyNamespace(metadata, "releaseNotes");

            if (releaseNotes == null)
            {
                XmlElementExtensions.AddChildElement(nuSpec, metadata, "releaseNotes", notes);
            }
            else
            {
                releaseNotes.InnerText = notes;
            }
        }
Esempio n. 6
0
        private void AddFiles(XmlDocument nuSpec, IEnumerable <ITaskItem> sourceFiles, string sourceBaseDirectory, string targetDirectory = "", string relativeTo = "")
        {
            var package = XmlElementExtensions.ElementAnyNamespace(nuSpec, "package");

            if (package == null)
            {
                throw new Exception("The NuSpec file does not contain a <package> XML element. The NuSpec file appears to be invalid.");
            }


            var files = XmlElementExtensions.ElementAnyNamespace(package, "files") ??
                        XmlElementExtensions.AddChildElement(nuSpec, package, "files");

            if (!StringHelper.IsNullOrWhiteSpace(relativeTo) && Path.IsPathRooted(relativeTo))
            {
                relativeTo = fileSystem.GetPathRelativeTo(relativeTo, sourceBaseDirectory);
            }

            foreach (var sourceFile in sourceFiles)
            {
                var destinationPath = sourceFile.ItemSpec;
                var link            = sourceFile.GetMetadata("Link");
                if (!StringHelper.IsNullOrWhiteSpace(link))
                {
                    destinationPath = link;
                }

                if (!Path.IsPathRooted(destinationPath))
                {
                    destinationPath = fileSystem.GetFullPath(Path.Combine(sourceBaseDirectory, destinationPath));
                }

                if (Path.IsPathRooted(destinationPath))
                {
                    destinationPath = fileSystem.GetPathRelativeTo(destinationPath, sourceBaseDirectory);
                }

                if (!StringHelper.IsNullOrWhiteSpace(relativeTo))
                {
                    if (destinationPath.StartsWith(relativeTo, StringComparison.OrdinalIgnoreCase))
                    {
                        destinationPath = destinationPath.Substring(relativeTo.Length);
                    }
                }

                destinationPath = Path.Combine(targetDirectory, destinationPath);

                var sourceFilePath = Path.Combine(sourceBaseDirectory, sourceFile.ItemSpec);

                sourceFilePath = Path.GetFullPath(sourceFilePath);

                if (!fileSystem.FileExists(sourceFilePath))
                {
                    LogMessage("The source file '" + sourceFilePath + "' does not exist, so it will not be included in the package", MessageImportance.High);
                    continue;
                }

                if (seenBefore.Contains(sourceFilePath))
                {
                    continue;
                }

                seenBefore.Add(sourceFilePath, true);

                var fileName = Path.GetFileName(destinationPath);
                if (string.Equals(fileName, "app.config", StringComparison.OrdinalIgnoreCase))
                {
                    if (fileSystem.FileExists(AppConfigFile))
                    {
                        var configFileName = Path.GetFileName(AppConfigFile);
                        destinationPath = Path.GetDirectoryName(destinationPath);
                        destinationPath = Path.Combine(destinationPath, configFileName);


                        XmlElementExtensions.AddChildElement(nuSpec, files, "file", new List <XmlNodeAttribute> {
                            new XmlNodeAttribute {
                                Name = "src", Value = AppConfigFile
                            },
                            new XmlNodeAttribute {
                                Name = "target", Value = destinationPath
                            }
                        });

                        LogMessage("Added file: " + destinationPath, MessageImportance.Normal);
                    }
                    continue;
                }

                if (new[] { "Deploy.ps1", "DeployFailed.ps1", "PreDeploy.ps1", "PostDeploy.ps1" }.Any(f => string.Equals(f, fileName, StringComparison.OrdinalIgnoreCase)))
                {
                    var isNonRoot = destinationPath.Contains('\\') || destinationPath.Contains('/');
                    if (isNonRoot && !IgnoreNonRootScripts)
                    {
                        LogWarning("OCTNONROOT", "As of Octopus Deploy 2.4, PowerShell scripts that are not at the root of the package will not be executed. The script '" + destinationPath + "' lives in a subdirectory, so it will not be executed. If you want Octopus to execute this script, move it to the root of your project. If you don't want it to be executed, you can ignore this warning, or suppress it by setting the MSBuild property OctoPackIgnoreNonRootScripts=true");
                    }
                }

                var isTypeScript = string.Equals(Path.GetExtension(sourceFilePath), ".ts", StringComparison.OrdinalIgnoreCase);
                if (isTypeScript)
                {
                    if (IncludeTypeScriptSourceFiles)
                    {
                        XmlElementExtensions.AddChildElement(nuSpec, files, "file", new List <XmlNodeAttribute> {
                            new XmlNodeAttribute {
                                Name = "src", Value = sourceFilePath
                            },
                            new XmlNodeAttribute {
                                Name = "target", Value = destinationPath
                            }
                        });

                        LogMessage("Added file: " + destinationPath, MessageImportance.Normal);
                    }

                    var changedSource      = Path.ChangeExtension(sourceFilePath, ".js");
                    var changedDestination = Path.ChangeExtension(destinationPath, ".js");
                    if (fileSystem.FileExists(changedSource))
                    {
                        XmlElementExtensions.AddChildElement(nuSpec, files, "file", new List <XmlNodeAttribute> {
                            new XmlNodeAttribute {
                                Name = "src", Value = changedSource
                            },
                            new XmlNodeAttribute {
                                Name = "target", Value = changedDestination
                            }
                        });

                        LogMessage("Added file: " + changedDestination, MessageImportance.Normal);
                    }
                }
                else
                {
                    XmlElementExtensions.AddChildElement(nuSpec, files, "file", new List <XmlNodeAttribute> {
                        new XmlNodeAttribute {
                            Name = "src", Value = sourceFilePath
                        },
                        new XmlNodeAttribute {
                            Name = "target", Value = destinationPath
                        }
                    });

                    LogMessage("Added file: " + destinationPath, MessageImportance.Normal);
                }
            }
        }