Пример #1
0
        public static void NewTemplate(string projectName, string path, string type, string author, string[] directories, string description,
                                       string id, string[] tags, string version, string[] dependsOn, string license)
        {
            XmlTemplate projectTemplate = new XmlTemplate();

            XmlWriterSettings settings = new XmlWriterSettings
            {
                Indent      = true,
                IndentChars = "\t"
            };

            using (XmlWriter writer = XmlWriter.Create(path, settings))
            {
                writer.WriteStartElement(projectTemplate.XmlRootAttributeValue);
                writer.WriteElementString(projectTemplate.ProjectName, projectName);
                writer.WriteElementString(projectTemplate.Directories, string.Join(",", directories));
                writer.WriteElementString(projectTemplate.Type, type);
                writer.WriteElementString(projectTemplate.Dependencies, string.Join(",", dependsOn));
                writer.WriteElementString(projectTemplate.License, license);

                // This creates the metadata section which is used for creating module manifest
                writer.WriteStartElement(projectTemplate.Metadata);
                writer.WriteElementString(projectTemplate.Author, author);
                writer.WriteElementString(projectTemplate.Path, $"{Path.GetDirectoryName(path)}\\{projectName}.psd1");
                writer.WriteElementString(projectTemplate.RootModule, $"{projectName}.psm1");
                writer.WriteElementString(projectTemplate.Description, description);
                writer.WriteElementString(projectTemplate.Guid, id);
                writer.WriteElementString(projectTemplate.Tags, string.Join(",", tags));
                writer.WriteElementString(projectTemplate.ModuleVersion, version);
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.Flush();
            }
        }
Пример #2
0
        public static PoshTemplate DeserializeTemplate(string path)
        {
            try
            {
                XmlTemplate template = new XmlTemplate();

                XmlSerializer xmlSerializer = new XmlSerializer(typeof(PoshTemplate), new XmlRootAttribute(template.XmlRootAttributeValue));

                StringReader stringReader = new StringReader(File.ReadAllText(path));

                PoshTemplate poshTemplate = (PoshTemplate)xmlSerializer.Deserialize(stringReader);

                return(poshTemplate);
            }

            catch
            {
                return(null);
            }
        }
Пример #3
0
        private static void CreateManifest(PoshTemplate template, string path)
        {
            XmlTemplate projectTemplate = new XmlTemplate();

            string[] dependencies = null;

            if (!string.IsNullOrEmpty(template.Dependencies))
            {
                dependencies = template.Dependencies.Split(',');
            }

            // Creating Module Manifest
            PowerShell ps = PowerShell.Create().AddCommand("New-ModuleManifest")
                            .AddParameter(projectTemplate.Author, template.Metadata.Author)
                            .AddParameter(projectTemplate.Description, template.Metadata.Description)
                            .AddParameter(projectTemplate.Guid, template.Metadata.Guid)
                            .AddParameter(projectTemplate.ModuleVersion, template.Metadata.ModuleVersion)
                            .AddParameter(projectTemplate.Path, $"{path}\\{template.ProjectName}.psd1")
                            .AddParameter(projectTemplate.Tags, template.Metadata.Tags.Split(','))
                            .AddParameter(projectTemplate.RequiredModules, dependencies)
                            .AddParameter(projectTemplate.RootModule, template.Metadata.RootModule);

            ps.Invoke();
        }
        protected override void ProcessRecord()
        {
            if ((File.Exists(TemplatePath)))
            {
                if (!ProjectTemplate.TemplateNameValidator(TemplatePath))
                {
                    ProjectTemplate.InvalidFileName();
                }

                else
                {
                    if (ProjectTemplate.TestTemplate(TemplatePath))
                    {
                        var         template    = ProjectTemplate.GetTemplate(TemplatePath);
                        XmlTemplate xmlTemplate = new XmlTemplate();
                        bool        _isManifest = template.Metadata.Path.Contains(".psd1");
                        string      guid        = template.Metadata.Guid.ToString();

                        if (string.IsNullOrEmpty(template.ProjectName))
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"<{xmlTemplate.ProjectName}> is empty");
                            _errorCount += 1;
                        }

                        if (string.IsNullOrEmpty(template.Directories))
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"<{xmlTemplate.Directories}> are empty");
                            _errorCount += 1;
                        }

                        if (string.IsNullOrEmpty(template.Type))
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"<{xmlTemplate.Type}> is empty");
                            _errorCount += 1;
                        }

                        if (!string.IsNullOrEmpty(template.Type))
                        {
                            if (template.Type != "Script" & template.Type != "Module" & template.Type != "Binary")
                            {
                                ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"<{xmlTemplate.Type}> invalid project type; Accepted values are 'Script', 'Module' and 'Binary'");
                                _errorCount += 1;
                            }
                        }

                        if (string.IsNullOrEmpty(template.Metadata.Author))
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"<{xmlTemplate.Author}> is empty");
                            _errorCount += 1;
                        }

                        if (string.IsNullOrEmpty(template.Metadata.Description))
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"<{xmlTemplate.Description}> is empty");
                            _errorCount += 1;
                        }

                        if (string.IsNullOrEmpty(template.Metadata.Path))
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"<{xmlTemplate.Path}> is empty");
                            _errorCount += 1;
                        }

                        if (!(_isManifest))
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"Invalid path: '{template.Metadata.Path}'");
                            _errorCount += 1;
                        }

                        if (string.IsNullOrEmpty(guid))
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"<{xmlTemplate.Guid}> is empty");
                            _errorCount += 1;
                        }

                        if (!string.IsNullOrEmpty(guid))
                        {
                            if (!ProjectTemplate.IsGuid(template.Metadata.Guid))
                            {
                                ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), "Invalid Guid");
                                _errorCount += 1;
                            }
                        }

                        if (string.IsNullOrEmpty(template.Metadata.ModuleVersion))
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"<{xmlTemplate.ModuleVersion}> is empty");
                            _errorCount += 1;
                        }

                        if (string.IsNullOrEmpty(template.Metadata.RootModule))
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"<{xmlTemplate.RootModule}> is empty");
                            _errorCount += 1;
                        }

                        if (!template.Metadata.RootModule.Contains(".psm1"))
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"Invalid root module name: '{template.Metadata.RootModule}'");
                            _errorCount += 1;
                        }

                        if (string.IsNullOrEmpty(template.Metadata.Tags))
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"<{xmlTemplate.Tags}> are empty");
                            _errorCount += 1;
                        }

                        if (_errorCount == 0)
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("info"), $"Error Count: {_errorCount}");
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("info"), "Valid Template");
                        }

                        else
                        {
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"Error Count: {_errorCount}");
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), $"Template validation failed");
                        }
                    }

                    else
                    {
                        ProjectTemplate.InvalidTemplate();
                    }
                }
            }

            else
            {
                ProjectTemplate.FileNotFound();
            }
        }