private static void CreateFileElements(XmlDocument document, NuGetPackSettings settings,
                                               XmlNamespaceManager namespaceManager)
        {
            if (settings.Files != null && settings.Files.Count > 0)
            {
                var filesPath = string.Format(CultureInfo.InvariantCulture,
                                              "//*[local-name()='package']/*[local-name()='files']");
                var filesElement = document.SelectSingleNode(filesPath, namespaceManager);
                if (filesElement == null)
                {
                    var package = GetPackageElement(document);
                    filesElement = document.CreateAndAppendElement(package, "files");
                }

                filesElement.RemoveAll();
                foreach (var file in settings.Files)
                {
                    if (file.Source != null)
                    {
                        var fileElement = document.CreateAndAppendElement(filesElement, "file");
                        fileElement.AddAttributeIfSpecified(file.Source, "src");
                        fileElement.AddAttributeIfSpecified(file.Exclude, "exclude");
                        fileElement.AddAttributeIfSpecified(file.Target, "target");
                    }
                }
            }
        }
Пример #2
0
        public FilePath Process(FilePath nuspecFilePath, NuGetPackSettings settings)
        {
            // Make the nuspec file path absolute.
            nuspecFilePath = nuspecFilePath.MakeAbsolute(_environment);

            // Make sure the nuspec file exist.
            var nuspecFile = _fileSystem.GetFile(nuspecFilePath);

            if (!nuspecFile.Exists)
            {
                const string format = "Could not find nuspec file '{0}'.";
                throw new CakeException(string.Format(CultureInfo.InvariantCulture, format, nuspecFilePath.FullPath));
            }

            // Load the content of the nuspec file.
            _log.Debug("Parsing nuspec...");
            var xml = LoadNuspecXml(nuspecFile);

            // Process the XML.
            _log.Debug("Transforming nuspec...");
            NuspecTransformer.Transform(xml, settings);

            // Return the file of the new nuspec.
            _log.Debug("Writing temporary nuspec...");
            return(SaveNuspecXml(nuspecFilePath, xml));
        }
Пример #3
0
        private FilePath SaveNuspecXml(FilePath nuspecFilePath, NuGetPackSettings settings, XmlDocument document)
        {
            // Get the new nuspec path.
            var filename = nuspecFilePath.GetFilename();

            filename = filename.ChangeExtension("temp.nuspec");
            var outputDirectory = GetOutputDirectory(nuspecFilePath, settings);
            var resultPath      = outputDirectory.GetFilePath(filename).MakeAbsolute(_environment);

            // Make sure the new nuspec file does not exist.
            var nuspecFile = _fileSystem.GetFile(resultPath);

            if (nuspecFile.Exists)
            {
                const string format = "Could not create the nuspec file '{0}' since it already exist.";
                throw new CakeException(string.Format(CultureInfo.InvariantCulture, format, resultPath.FullPath));
            }

            // Now create the file.
            using (var stream = nuspecFile.OpenWrite())
            {
                document.Save(stream);
            }

            // Return the new path.
            return(nuspecFile.Path);
        }
 private static void CreateReleaseNotes(XmlDocument document, NuGetPackSettings settings,
                                        XmlNamespaceManager namespaceManager)
 {
     if (settings.ReleaseNotes != null && settings.ReleaseNotes.Count > 0)
     {
         var node = FindOrCreateElement(document, namespaceManager, "releaseNotes");
         node.AppendChild(document.CreateCDataSection(ToMultiLineString(settings.ReleaseNotes)));
     }
 }
 private static void CreateMinClientVersion(XmlDocument document, NuGetPackSettings settings,
                                            XmlNamespaceManager namespaceManager)
 {
     if (settings.MinClientVersion != null)
     {
         var metadataNode = document.SelectSingleNode("//*[local-name()='metadata']");
         metadataNode.AddAttributeIfSpecified(settings.MinClientVersion, "minClientVersion");
     }
 }
        private static void AddSimpleTypesToXmlDocument(XmlDocument document, NuGetPackSettings settings,
                                                        XmlNamespaceManager namespaceManager)
        {
            var mappings = CreateMappings(settings);

            CreateSimpleTypeMetadataFromMappings(document, settings, namespaceManager, mappings);
            CreateReleaseNotes(document, settings, namespaceManager);
            CreateMinClientVersion(document, settings, namespaceManager);
        }
Пример #7
0
        private FilePath ProcessXml(FilePath nuspecFilePath, NuGetPackSettings settings, XmlDocument xml)
        {
            // Process the XML.
            _log.Debug("Transforming nuspec...");
            NuspecTransformer.Transform(xml, settings);

            // Return the file of the new nuspec.
            _log.Debug("Writing temporary nuspec...");
            return(SaveNuspecXml(nuspecFilePath, xml));
        }
        public static void Transform(XmlDocument document, NuGetPackSettings settings)
        {
            // Create the namespace manager.
            var namespaceManager = new XmlNamespaceManager(document.NameTable);

            namespaceManager.AddNamespace("nu", NuSpecXsd);

            AddSimpleTypesToXmlDocument(document, settings, namespaceManager);
            AddComplexTypesToXmlDocument(document, settings, namespaceManager);
        }
Пример #9
0
        public FilePath Process(NuGetPackSettings settings)
        {
            var nuspecFilePath = settings.OutputDirectory
                                 .CombineWithFilePath(string.Concat(settings.Id, ".nuspec"))
                                 .MakeAbsolute(_environment);

            var xml = LoadEmptyNuSpec();

            return(ProcessXml(nuspecFilePath, settings, xml));
        }
 private static void CreateLicenseElement(XmlDocument document, NuGetPackSettings settings,
                                          XmlNamespaceManager namespaceManager)
 {
     if (settings.License?.Type != null)
     {
         var licenseNode = FindOrCreateElement(document, namespaceManager, "license");
         licenseNode.AddAttributeIfSpecified(settings.License.Type, "type");
         licenseNode.AddAttributeIfSpecified(settings.License.Version, "version");
         licenseNode.InnerText = settings.License.Value;
     }
 }
 private static void AddComplexTypesToXmlDocument(XmlDocument document, NuGetPackSettings settings,
                                                  XmlNamespaceManager namespaceManager)
 {
     CreateRepositoryElement(document, settings, namespaceManager);
     CreateLicenseElement(document, settings, namespaceManager);
     CreatePackageTypeElements(document, settings, namespaceManager);
     CreateDependencyElements(document, settings, namespaceManager);
     CreateFrameworkAssemblyElements(document, settings, namespaceManager);
     CreateReferenceElements(document, settings, namespaceManager);
     CreateContentFileElements(document, settings, namespaceManager);
     CreateFileElements(document, settings, namespaceManager);
 }
 private static void CreateRepositoryElement(XmlDocument document, NuGetPackSettings settings,
                                             XmlNamespaceManager namespaceManager)
 {
     if (settings.Repository != null)
     {
         var repositoryNode = FindOrCreateElement(document, namespaceManager, "repository");
         repositoryNode.AddAttributeIfSpecified(settings.Repository.Type, "type");
         repositoryNode.AddAttributeIfSpecified(settings.Repository.Url, "url");
         repositoryNode.AddAttributeIfSpecified(settings.Repository.Commit, "commit");
         repositoryNode.AddAttributeIfSpecified(settings.Repository.Branch, "branch");
     }
 }
Пример #13
0
        public static void Transform(XmlDocument document, NuGetPackSettings settings)
        {
            // Create the namespace manager.
            var namespaceManager = new XmlNamespaceManager(document.NameTable);

            namespaceManager.AddNamespace("nu", NuSpecXsd);

            foreach (var elementName in _mappings.Keys)
            {
                var content = _mappings[elementName](settings);
                if (content != null)
                {
                    // Replace the node content.
                    var node = FindOrCreateElement(document, namespaceManager, elementName);

                    if (cdataElements.Contains(elementName))
                    {
                        node.AppendChild(document.CreateCDataSection(content));
                    }
                    else
                    {
                        node.InnerText = content;
                    }
                }
            }

            if (settings.Files != null && settings.Files.Count > 0)
            {
                var filesPath    = string.Format(CultureInfo.InvariantCulture, "//*[local-name()='package']//*[local-name()='files']");
                var filesElement = document.SelectSingleNode(filesPath, namespaceManager);
                if (filesElement == null)
                {
                    // Get the package element.
                    var package = GetPackageElement(document);
                    filesElement = document.CreateAndAppendElement(package, "files");
                }

                // Add the files
                filesElement.RemoveAll();
                foreach (var file in settings.Files)
                {
                    var fileElement = document.CreateAndAppendElement(filesElement, "file");
                    fileElement.AddAttributeIfSpecified(file.Source, "src");
                    fileElement.AddAttributeIfSpecified(file.Exclude, "exclude");
                    fileElement.AddAttributeIfSpecified(file.Target, "target");
                }
            }
        }
        private static void CreatePackageTypeElements(XmlDocument document, NuGetPackSettings settings,
                                                      XmlNamespaceManager namespaceManager)
        {
            if (settings.PackageTypes != null && settings.PackageTypes.Count > 0)
            {
                var packageTypesElement = FindOrCreateElement(document, namespaceManager, "packageTypes");

                packageTypesElement.RemoveAll();
                foreach (var packageType in settings.PackageTypes)
                {
                    if (packageType.Name != null)
                    {
                        var fileElement = document.CreateAndAppendElement(packageTypesElement, "packageType");
                        fileElement.AddAttributeIfSpecified(packageType.Name, "name");
                        fileElement.AddAttributeIfSpecified(packageType.Version, "version");
                    }
                }
            }
        }
Пример #15
0
        public static void Transform(XmlDocument document, NuGetPackSettings settings)
        {
            // Create the namespace manager.
            var namespaceManager = new XmlNamespaceManager(document.NameTable);

            namespaceManager.AddNamespace("nu", "http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd");

            // Iterate through all mappings.
            foreach (var elementName in _mappings.Keys)
            {
                var content = _mappings[elementName](settings);
                if (content != null)
                {
                    // Replace the node content.
                    var node = FindOrCreateElement(document, namespaceManager, elementName);
                    node.InnerText = content;
                }
            }
        }
        private static void CreateFrameworkAssemblyElements(XmlDocument document, NuGetPackSettings settings,
                                                            XmlNamespaceManager namespaceManager)
        {
            if (settings.FrameworkAssemblies != null && settings.FrameworkAssemblies.Count > 0)
            {
                var frameWorkAssembliesElement = FindOrCreateElement(document, namespaceManager, "frameworkAssemblies");

                frameWorkAssembliesElement.RemoveAll();
                foreach (var frameworkAssembly in settings.FrameworkAssemblies)
                {
                    if (frameworkAssembly.AssemblyName != null)
                    {
                        var fileElement = document.CreateAndAppendElement(frameWorkAssembliesElement, "frameworkAssembly");
                        fileElement.AddAttributeIfSpecified(frameworkAssembly.AssemblyName, "assemblyName");
                        fileElement.AddAttributeIfSpecified(frameworkAssembly.TargetFramework, "targetFramework");
                    }
                }
            }
        }
        private static void CreateContentFileElements(XmlDocument document, NuGetPackSettings settings,
                                                      XmlNamespaceManager namespaceManager)
        {
            if (settings.ContentFiles != null && settings.ContentFiles.Count > 0)
            {
                var contentFilesElement = FindOrCreateElement(document, namespaceManager, "contentFiles");

                contentFilesElement.RemoveAll();
                foreach (var contentFile in settings.ContentFiles)
                {
                    if (contentFile.Include != null)
                    {
                        var fileElement = document.CreateAndAppendElement(contentFilesElement, "files");
                        fileElement.AddAttributeIfSpecified(contentFile.Include, "include");
                        fileElement.AddAttributeIfSpecified(contentFile.Exclude, "exclude");
                        fileElement.AddAttributeIfSpecified(contentFile.BuildAction, "buildAction");
                        fileElement.AddAttributeIfSpecified(ToString(contentFile.CopyToOutput), "copyToOutput");
                        fileElement.AddAttributeIfSpecified(ToString(contentFile.Flatten), "flatten");
                    }
                }
            }
        }
        private static void CreateDependencyElements(XmlDocument document, NuGetPackSettings settings,
                                                     XmlNamespaceManager namespaceManager)
        {
            if (settings.Dependencies != null && settings.Dependencies.Count > 0)
            {
                var dependenciesElement = FindOrCreateElement(document, namespaceManager, "dependencies");

                dependenciesElement.RemoveAll();
                if (settings.Dependencies.All(c => string.IsNullOrEmpty(c.TargetFramework)))
                {
                    foreach (var dependency in settings.Dependencies)
                    {
                        AddDependency(document, dependenciesElement, dependency);
                    }
                }
                else
                {
                    foreach (var targetFrameworkDependencies in settings.Dependencies.GroupBy(x => x.TargetFramework))
                    {
                        CreateDependencyGroup(document, dependenciesElement, targetFrameworkDependencies);
                    }
                }
            }
        }
        private static void CreateReferenceElements(XmlDocument document, NuGetPackSettings settings,
                                                    XmlNamespaceManager namespaceManager)
        {
            if (settings.References != null && settings.References.Count > 0)
            {
                var referencesElement = FindOrCreateElement(document, namespaceManager, "references");

                referencesElement.RemoveAll();
                if (settings.References.All(c => string.IsNullOrEmpty(c.TargetFramework)))
                {
                    foreach (var reference in settings.References)
                    {
                        AddReference(document, referencesElement, reference);
                    }
                }
                else
                {
                    foreach (var targetFrameworkReferences in settings.References.GroupBy(x => x.TargetFramework))
                    {
                        CreateReferenceGroup(document, referencesElement, targetFrameworkReferences);
                    }
                }
            }
        }
Пример #20
0
        public static void Transform(XmlDocument document, NuGetPackSettings settings)
        {
            // Create the namespace manager.
            var namespaceManager = new XmlNamespaceManager(document.NameTable);

            namespaceManager.AddNamespace("nu", NuSpecXsd);

            foreach (var elementName in _mappings.Keys)
            {
                var content = _mappings[elementName](settings);
                if (content != null)
                {
                    // Replace the node content.
                    var node = FindOrCreateElement(document, namespaceManager, elementName);

                    if (_cdataElements.Contains(elementName))
                    {
                        node.AppendChild(document.CreateCDataSection(content));
                    }
                    else
                    {
                        node.InnerText = content;
                    }
                }
            }

            if (settings.Repository != null)
            {
                var repositoryNode = FindOrCreateElement(document, namespaceManager, "repository");
                repositoryNode.AddAttributeIfSpecified(settings.Repository.Type, "type");
                repositoryNode.AddAttributeIfSpecified(settings.Repository.Url, "url");
                repositoryNode.AddAttributeIfSpecified(settings.Repository.Commit, "commit");
                repositoryNode.AddAttributeIfSpecified(settings.Repository.Branch, "branch");
            }

            if (settings.Files != null && settings.Files.Count > 0)
            {
                var filesPath    = string.Format(CultureInfo.InvariantCulture, "//*[local-name()='package']//*[local-name()='files']");
                var filesElement = document.SelectSingleNode(filesPath, namespaceManager);
                if (filesElement == null)
                {
                    // Get the package element.
                    var package = GetPackageElement(document);
                    filesElement = document.CreateAndAppendElement(package, "files");
                }

                // Add the files
                filesElement.RemoveAll();
                foreach (var file in settings.Files)
                {
                    var fileElement = document.CreateAndAppendElement(filesElement, "file");
                    fileElement.AddAttributeIfSpecified(file.Source, "src");
                    fileElement.AddAttributeIfSpecified(file.Exclude, "exclude");
                    fileElement.AddAttributeIfSpecified(file.Target, "target");
                }
            }

            if (settings.Dependencies != null && settings.Dependencies.Count > 0)
            {
                var dependenciesElement = FindOrCreateElement(document, namespaceManager, "dependencies");

                // Add the files
                dependenciesElement.RemoveAll();
                if (settings.Dependencies.All(c => string.IsNullOrEmpty(c.TargetFramework)))
                {
                    foreach (var dependency in settings.Dependencies)
                    {
                        var fileElement = document.CreateAndAppendElement(dependenciesElement, "dependency");
                        fileElement.AddAttributeIfSpecified(dependency.Id, "id");
                        fileElement.AddAttributeIfSpecified(dependency.Version, "version");
                    }
                }
                else
                {
                    foreach (var targetFrameworkDependencies in settings.Dependencies.GroupBy(x => x.TargetFramework))
                    {
                        var groupElement = document.CreateAndAppendElement(dependenciesElement, "group");
                        if (!string.IsNullOrEmpty(targetFrameworkDependencies.Key))
                        {
                            groupElement.AddAttributeIfSpecified(targetFrameworkDependencies.Key, "targetFramework");
                        }
                        foreach (var dependency in targetFrameworkDependencies)
                        {
                            var fileElement = document.CreateAndAppendElement(groupElement, "dependency");
                            fileElement.AddAttributeIfSpecified(dependency.Id, "id");
                            fileElement.AddAttributeIfSpecified(dependency.Version, "version");
                        }
                    }
                }
            }
        }
 private static void CreateSimpleTypeMetadataFromMappings(XmlDocument document, NuGetPackSettings settings,
                                                          XmlNamespaceManager namespaceManager, IDictionary <string, Func <NuGetPackSettings, string> > mappings)
 {
     mappings.ToList().ForEach(x =>
     {
         var value = x.Value(settings);
         if (value != null)
         {
             var node       = FindOrCreateElement(document, namespaceManager, x.Key);
             node.InnerText = value;
         }
     });
 }
 private static IDictionary <string, Func <NuGetPackSettings, string> > CreateMappings(NuGetPackSettings settings)
 {
     return(new Dictionary <string, Func <NuGetPackSettings, string> >
     {
         { "id", x => ToString(settings.Id) },
         { "version", x => ToString(settings.Version) },
         { "title", x => ToString(settings.Title) },
         { "authors", x => ToCommaSeparatedString(settings.Authors) },
         { "owners", x => ToCommaSeparatedString(settings.Owners) },
         { "licenseUrl", x => ToString(settings.LicenseUrl) },
         { "projectUrl", x => ToString(settings.ProjectUrl) },
         { "icon", x => ToString(settings.Icon) },
         { "iconUrl", x => ToString(settings.IconUrl) },
         { "requireLicenseAcceptance", x => ToString(settings.RequireLicenseAcceptance) },
         { "developmentDependency", x => ToString(settings.DevelopmentDependency) },
         { "description", x => ToString(settings.Description) },
         { "summary", x => ToString(settings.Summary) },
         { "copyright", x => ToString(settings.Copyright) },
         { "language", x => ToString(settings.Language) },
         { "tags", x => ToSpaceSeparatedString(settings.Tags) },
         { "serviceable", x => ToString(settings.Serviceable) }
     });
 }
Пример #23
0
 private DirectoryPath GetOutputDirectory(FilePath nuspecFilePath, NuGetPackSettings settings)
 {
     return(settings.OutputDirectory != null
         ? settings.OutputDirectory.MakeAbsolute(_environment)
         : nuspecFilePath.GetDirectory().MakeAbsolute(_environment));
 }