コード例 #1
0
        protected override void ProcessRecord()
        {
            if (MyInvocation.BoundParameters.ContainsKey("FilePath") & (!ProjectTemplate.TemplateNameValidator(FilePath)))
            {
                ProjectTemplate.InvalidFileName();
            }

            else
            {
                if (!(MyInvocation.BoundParameters.ContainsKey("FilePath")))
                {
                    FilePath = Directory.GetCurrentDirectory() + $"\\PoshProjectTemplate.xml";
                }

                if (!(MyInvocation.BoundParameters.ContainsKey("Author")))
                {
                    if (!string.IsNullOrEmpty(ProjectTemplate.GetUserName()))
                    {
                        Author = ProjectTemplate.GetUserName();
                    }

                    else
                    {
                        Author = Environment.UserName;
                    }
                }

                if (!(MyInvocation.BoundParameters.ContainsKey("Directories")))
                {
                    if (ProjectType == "Script")
                    {
                        Directories = new string[] { $"{ProjectName}.tests.ps1" };
                    }

                    else if (ProjectType == "Module")
                    {
                        Directories = new string[]
                        {
                            "Classes", "Private", "Public", "docs", "en-US", "Tests"
                        };
                    }

                    else
                    {
                        Directories = new string[]
                        {
                            "Output", ProjectName, "src", "docs", "en-US", "Tests"
                        };
                    }
                }

                if (!(MyInvocation.BoundParameters.ContainsKey("Description")))
                {
                    Description = $"Module for {ProjectName}";
                }

                if (!(MyInvocation.BoundParameters.ContainsKey("Tags")))
                {
                    Tags = new string[]
                    {
                        "PowerShell", $"{ProjectName}Module", ProjectName
                    };
                }

                ProjectTemplate.NewTemplate(ProjectName, FilePath, ProjectType, Author, Directories, Description, Guid.ToString(), Tags, Version, DependsOn, License);
            }
        }
コード例 #2
0
        protected override void ProcessRecord()
        {
            if (ParameterSetName == "Path")
            {
                if (!(File.Exists(TemplatePath)))
                {
                    ProjectTemplate.FileNotFound();
                }

                else if (!ProjectTemplate.TemplateNameValidator(TemplatePath))
                {
                    ProjectTemplate.InvalidFileName();
                }

                else
                {
                    if (ProjectTemplate.TestTemplate(TemplatePath))
                    {
                        if (ProjectTemplate.ValidateTemplate(TemplatePath) == 0)
                        {
                            PoshTemplate template = ProjectTemplate.GetTemplate(TemplatePath);

                            if (InstallDependencies.IsPresent)
                            {
                                ProjectTemplate.CreateProject(template, InstallDependencies.IsPresent);
                            }

                            else
                            {
                                ProjectTemplate.CreateProject(template);
                            }
                        }

                        else
                        {
                            ProjectTemplate.InvalidTemplate();
                            ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), "Run Test-PoshTemplate cmdlet to validate the template");
                        }
                    }

                    else
                    {
                        ProjectTemplate.InvalidTemplate();
                        ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), "Run Test-PoshTemplate cmdlet to validate the template");
                    }
                }
            }

            else if (ParameterSetName == "Custom")
            {
                ProjectTemplate.InstallDependencies(TemplateObject);
            }

            else
            {
                if (TemplateObject.GetType() != typeof(PoshTemplate))
                {
                    ProjectTemplate.InvalidTemplate();
                }

                else
                {
                    if (ProjectTemplate.ValidateTemplateObject(TemplateObject) == 0)
                    {
                        if (InstallDependencies.IsPresent)
                        {
                            ProjectTemplate.CreateProject(TemplateObject, InstallDependencies.IsPresent);
                        }

                        else
                        {
                            ProjectTemplate.CreateProject(TemplateObject);
                        }
                    }

                    else
                    {
                        ProjectTemplate.InvalidTemplate();
                        ProjectTemplate.WriteMessage(ProjectTemplate.GetSign("err"), "Run Test-PoshTemplate cmdlet to validate the template");
                    }
                }
            }
        }
コード例 #3
0
        // function for validating the template object
        public static int ValidateTemplateObject(PoshTemplate templateObject)
        {
            ProjectTemplate projectTemplate = new ProjectTemplate();

            bool   _isManifest = templateObject.Metadata.Path.Contains(".psd1");
            string guid        = templateObject.Metadata.Guid.ToString();

            if (string.IsNullOrEmpty(templateObject.ProjectName))
            {
                projectTemplate._errorCount += 1;
            }

            if (string.IsNullOrEmpty(templateObject.Directories))
            {
                projectTemplate._errorCount += 1;
            }

            if (string.IsNullOrEmpty(templateObject.Type))
            {
                projectTemplate._errorCount += 1;
            }

            if (!string.IsNullOrEmpty(templateObject.Type))
            {
                if (templateObject.Type != "Script" & templateObject.Type != "Module" & templateObject.Type != "Binary")
                {
                    projectTemplate._errorCount += 1;
                }
            }

            if (string.IsNullOrEmpty(templateObject.Metadata.Author))
            {
                projectTemplate._errorCount += 1;
            }

            if (string.IsNullOrEmpty(templateObject.Metadata.Description))
            {
                projectTemplate._errorCount += 1;
            }

            if (string.IsNullOrEmpty(templateObject.Metadata.Path))
            {
                projectTemplate._errorCount += 1;
            }

            if (!(_isManifest))
            {
                projectTemplate._errorCount += 1;
            }

            if (string.IsNullOrEmpty(guid))
            {
                projectTemplate._errorCount += 1;
            }

            if (!string.IsNullOrEmpty(guid))
            {
                if (!IsGuid(templateObject.Metadata.Guid))
                {
                    projectTemplate._errorCount += 1;
                }
            }

            if (string.IsNullOrEmpty(templateObject.Metadata.ModuleVersion))
            {
                projectTemplate._errorCount += 1;
            }

            if (string.IsNullOrEmpty(templateObject.Metadata.RootModule))
            {
                projectTemplate._errorCount += 1;
            }

            if (!templateObject.Metadata.RootModule.Contains(".psm1"))
            {
                projectTemplate._errorCount += 1;
            }

            if (string.IsNullOrEmpty(templateObject.Metadata.Tags))
            {
                projectTemplate._errorCount += 1;
            }

            return(projectTemplate._errorCount);
        }
コード例 #4
0
        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();
            }
        }