示例#1
0
        public static void InstallDependencies(PoshTemplate template)
        {
            // Installing dependencies
            if (!string.IsNullOrEmpty(template.Dependencies))
            {
                string[] dependencies = template.Dependencies.Split(',');

                foreach (string module in dependencies)
                {
                    if (!string.IsNullOrWhiteSpace(module))
                    {
                        WriteMessage(GetSign("info"), $"Installing {module}");

                        if (IsAlreadyInstalled(module))
                        {
                            WriteMessage(GetSign("info"), $"Already installed: {module}");
                        }

                        else if (!FindModule(module))
                        {
                            WriteMessage(GetSign("err"), $"Couldn't find the module {module} in PSGallery");
                        }

                        else
                        {
                            try
                            {
                                PowerShell ps = PowerShell.Create().AddCommand("Install-Module")
                                                .AddParameter("Name", module)
                                                .AddParameter("Scope", "CurrentUser")
                                                .AddParameter("Force", true)
                                                .AddParameter("AllowClobber", true);

                                ps.Invoke();

                                WriteMessage(GetSign("info"), $"Successfully installed: {module}");
                            }

                            catch
                            {
                                WriteMessage(GetSign("err"), $"Installation failed: {module}");
                            }
                        }
                    }

                    else
                    {
                        WriteMessage(GetSign("err"), $"Invalid module name");
                    }
                }
            }

            else
            {
                WriteMessage(GetSign("info"), "No dependencies found");
            }
        }
示例#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();
        }
示例#4
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);
        }
示例#5
0
        public static void CreateProject(PoshTemplate template, bool installDependencies = false)
        {
            var projectPath = template.Metadata.Path.Replace(".psd1", "");

            // Template contents class contains the path to sample files that will be copied to the project folder
            TemplateContents contents     = new TemplateContents();
            string           _path        = contents.AssemblyRootPath;
            string           _currentPath = $"{_path}{contents.ContentsPath}";
            string           modulePath   = $"{_currentPath}{contents.ModulePath}";
            string           classPath    = $"{_currentPath}{contents.ClassPath}";
            string           testsPath    = $"{_currentPath}{contents.TestsPath}";
            string           mitPath      = $"{_currentPath}{contents.MITPath}";
            string           apachePath   = $"{_currentPath}{contents.ApachePath}";
            string           aboutMD      = $"{_currentPath}{contents.AboutMDPath}";
            string           aboutText    = $"{_currentPath}{contents.AboutTextPath}";

            if (Directory.Exists(projectPath))
            {
                WriteMessage(GetSign("err"), $"Project '{projectPath}' already exists");
            }

            else
            {
                string sign = GetSign("info");

                // Creating Project
                WriteMessage(sign, "Creating Project");

                // Creating Project Directory
                WriteMessage(sign, "Creating Project Directory");
                Directory.CreateDirectory(projectPath);

                // Creating manifest
                WriteMessage(sign, "Creating Module Manifest");
                CreateManifest(template, projectPath);

                // Creating module file
                WriteMessage(sign, "Creating Root Module");
                File.WriteAllText($"{projectPath}\\{template.Metadata.RootModule}", ReadContents(modulePath));

                // Creating README
                WriteMessage(sign, "Creating README.md");
                File.WriteAllText($"{projectPath}\\README.md", $"# {template.ProjectName}");

                // Adding License
                if (!string.IsNullOrEmpty(template.License))
                {
                    WriteMessage(sign, $"Adding License");

                    if (template.License == "MIT")
                    {
                        File.WriteAllText($"{projectPath}\\LICENSE", ReadContents(mitPath, "@AuthorName", template.Metadata.Author));
                    }

                    else if (template.License == "Apache")
                    {
                        File.WriteAllText($"{projectPath}\\LICENSE", ReadContents(apachePath, "@AuthorName", template.Metadata.Author));
                    }
                }

                if (template.Type == "Script")
                {
                    // Creating Directories (in this case it will be a .tests file)
                    WriteMessage(sign, "Creating Pester Tests File");
                    File.WriteAllText($"{projectPath}\\{template.Directories}", ReadContents(testsPath));
                }

                else
                {
                    // Creating Directories
                    WriteMessage(sign, "Creating Project Directories");
                    string[] directories = template.Directories.Split(',');

                    foreach (string dir in directories)
                    {
                        WriteMessage(sign, $"Creating {dir}");
                        Directory.CreateDirectory($"{projectPath}\\{dir}");

                        if (dir == "Classes" || dir == "Class")
                        {
                            File.WriteAllText($"{projectPath}\\{dir}\\{template.ProjectName}.Class.ps1", ReadContents(classPath));
                        }

                        else if (dir == "Public")
                        {
                            File.WriteAllText($"{projectPath}\\{dir}\\{template.ProjectName}.ps1", ReadContents(modulePath));
                        }

                        else if (dir == "Tests" || dir == "Test" || dir == "tests" || dir == "test")
                        {
                            File.WriteAllText($"{projectPath}\\{dir}\\{template.ProjectName}.Tests.ps1", ReadContents(testsPath));
                        }

                        else if (dir == "docs" || dir == "doc" || dir == "documents")
                        {
                            File.WriteAllText($"{projectPath}\\{dir}\\about_Module.help.md", ReadContents(aboutMD, "@ProjectName", template.ProjectName));
                        }

                        else if (dir == "en-US")
                        {
                            File.WriteAllText($"{projectPath}\\{dir}\\about_Module.help.txt", ReadContents(aboutText, "@ProjectName", template.ProjectName));
                        }
                    }
                }

                if (installDependencies)
                {
                    // Installing Dependencies
                    WriteMessage(sign, "Installing Dependencies");
                    InstallDependencies(template);
                }
            }
        }
示例#6
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");
                    }
                }
            }
        }