public void RemoveTemplateSource(TemplateSource sourceToRemove)
        {
            List <TemplateSource> existingTemplates = null;

            if (File.Exists(SourcesFilePath))
            {
                try {
                    existingTemplates = GetTemplateSources();
                }
                catch (Exception ex) {
                    Console.WriteLine(string.Format("Unable to read the settings file from [{0}]. Overwriting with new source only. Error: {1}", SourcesFilePath, ex.ToString()));
                }
            }

            if (existingTemplates == null)
            {
                existingTemplates = new List <TemplateSource>();
            }

            if (existingTemplates.Contains(sourceToRemove))
            {
                existingTemplates.Remove(sourceToRemove);
                SaveTemplates(existingTemplates);
            }
            else
            {
                Console.WriteLine("Source to remove was not found in sources");
            }
        }
예제 #2
0
        public override bool Equals(object obj)
        {
            TemplateSource other = obj as TemplateSource;

            if (other == null)
            {
                return(false);
            }
            if (Type != other.Type)
            {
                return(false);
            }

            if (SourceFolder != null && !SourceFolder.Equals(other.SourceFolder))
            {
                return(false);
            }

            if (GitUrl != null && !GitUrl.Equals(other.GitUrl))
            {
                return(false);
            }

            if (PackageVersion != null && !PackageVersion.Equals(other.PackageVersion))
            {
                return(false);
            }

            if (PackageName != null && !PackageName.Equals(other.PackageName))
            {
                return(false);
            }

            return(true);
        }
        public void AddTemplateSource(TemplateSource sourceToAdd)
        {
            List <TemplateSource> existingTemplates = null;

            if (File.Exists(SourcesFilePath))
            {
                try {
                    existingTemplates = GetTemplateSources();
                }
                catch (Exception ex) {
                    Console.WriteLine(string.Format("Unable to read the settings file from [{0}]. Overwriting with new source only. Error: {1}", SourcesFilePath, ex.ToString()));
                }
            }

            List <TemplateSource> sources = new List <TemplateSource>();

            sources.Add(sourceToAdd);

            // add back existing templates, and don't add any duplicates
            if (existingTemplates != null)
            {
                foreach (var template in existingTemplates)
                {
                    if (template.Equals(sourceToAdd))
                    {
                        continue;
                    }
                    sources.Add(template);
                }
            }

            SaveTemplates(sources);
        }
예제 #4
0
        public static void OldMain(string[] args)
        {
            try {
                var pkgtemplate = GetTemplateFromPackage("DotnetSampleConsoleApp", "1.0.0", "microsoft.dotnet.console.rc2");
                if (pkgtemplate != null)
                {
                    string targetfolder = @"c:\temp\dn-waffle\frompkg";
                    if (!string.IsNullOrWhiteSpace(targetfolder) && Directory.Exists(targetfolder))
                    {
                        Directory.Delete(targetfolder, true);
                    }
                    var tcreator = new TemplateCreator();
                    tcreator.CreateProject(pkgtemplate, targetfolder, "MyNewConsoleProject", null);
                }

                var template = CreateTemplate();
                var result   = Newtonsoft.Json.JsonConvert.SerializeObject(template, Newtonsoft.Json.Formatting.Indented);
                Console.WriteLine(result);

                string destFolder = @"c:\temp\dn-waffle\webapi";
                if (!string.IsNullOrWhiteSpace(destFolder) && Directory.Exists(destFolder))
                {
                    Directory.Delete(destFolder, true);
                }
                var creator = new TemplateCreator();
                creator.CreateProject(template, destFolder, "MyNewProject", null);

                string giturl       = @"https://github.com/sayedihashimi/dotnet-waffle.git";
                string gitbranch    = "master";
                string templatename = "microsoft.dotnet.console.rc2";
                template   = GetTemplateFromGit(giturl, gitbranch, templatename);
                destFolder = @"C:\temp\dn-waffle\gitconsole";
                if (!string.IsNullOrWhiteSpace(destFolder) && Directory.Exists(destFolder))
                {
                    Directory.Delete(destFolder, true);
                }
                creator.CreateProject(template, destFolder, "MyNewConsoleProject", null);

                var settingsFile = @"C:\temp\dn-waffle\settings.json";
                var manager      = new TemplateSourceManager(settingsFile);
                var sourceFolder = @"C:\Data\mycode\dotnet-waffle\samples";
                manager.AddTemplateSource(TemplateSource.NewFolderSource(sourceFolder));

                destFolder = @"c:\temp\dn-waffle\console";
                if (!string.IsNullOrWhiteSpace(destFolder) && Directory.Exists(destFolder))
                {
                    Directory.Delete(destFolder, true);
                }
                template = Template.BuildFromFile(@"C:\Users\sayedha\Documents\Visual Studio 2015\Projects\SamplesForDotnetWaffle\src\SampleConsoleApp\waffle.json");
                creator.CreateProject(template, destFolder, "MyNewConsoleApp", null);
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }
예제 #5
0
 public Template(string name, TemplateType type, string sourceFolder)
 {
     if (type != TemplateType.ProjectTemplate)
     {
         throw new NotImplementedException();
     }
     Name   = name;
     Type   = type;
     Source = TemplateSource.NewFolderSource(sourceFolder);
 }
예제 #6
0
        public static Template BuildFromFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("Template file not found", filePath);
            }

            var template = JsonConvert.DeserializeObject <Template>(File.ReadAllText(filePath));

            template.TemplateFilePath = filePath;
            if (template.Source == null)
            {
                template.Source = TemplateSource.NewFolderSource(new FileInfo(filePath).DirectoryName);
            }

            return(template);
        }
        public IEnumerable <Template> GetTemplateFromSource(TemplateSource source)
        {
            switch (source.Type)
            {
            case SourceType.Folder:
                return(GetTemplatesFromFolder(source.SourceFolder, true));

            case SourceType.Package:
                return(GetTemplatesFromPackage(source.PackageName, source.PackageVersion));

            case SourceType.Git:
                return(GetTemplatesFromGit(source.GitUrl.AbsoluteUri, source.GitBranch));

            default:
                throw new InvalidOperationException(string.Format("Unknown source type [{0}]", source.Type));
            }
        }
예제 #8
0
        public int Run(string[] args)
        {
            //var templateManager = new TemplateSourceManager(new TemplateHelper.GetSourcesFilePath());
            var templateMan = new TemplateManager(new TemplateSourceManager(Helper.GetSourcesFilePath()));

            templateMan.SourceManager.UpdateRemoteTemplateSources(templateMan.SourceManager.GetTemplateSources());

            var app = new CommandLineApplication();

            app.Name        = "dotnet-waffle";
            app.Description = "The dotnet-waffle command is used to create .NET Core projects using templates.";

            app.HelpOption("-?|-h|--help");

            app.Command("list", command => {
                command.Description = "prints the installed templates";
                command.HelpOption("-?|-h|--help");
                command.OnExecute(() => {
                    // get and display the templates
                    // var sources = sourceManager.GetTemplateSources();
                    var installedTemplates = templateMan.GetInstalledTemplates();
                    PrintTemplates(installedTemplates);

                    return(0);
                });
            });

            app.Command("listsource", command => {
                command.Description = "shows the template sources that have been added";
                command.OnExecute(() => {
                    var sources = templateMan.SourceManager.GetTemplateSources();
                    PrintSources(sources);
                    return(0);
                });
            });

            app.Command("removesource", command => {
                command.Description = "Removes a source for templates";
                command.HelpOption("-?|-h|--help");

                var pkgOption    = command.Option("-p|--package <packagename>", "Name of the NuGet package that contains templates to install", CommandOptionType.SingleValue);
                var pkgVerOption = command.Option("-v|--version <version>", "Version of the NuGet package", CommandOptionType.SingleValue);

                var gitOption       = command.Option("-g|--giturl <giturl>", "URL for the git repo which contains templates to install", CommandOptionType.SingleValue);
                var gitBranchOption = command.Option("-b|--gitbranchname <branchname>", "Name of the branch for the git repo", CommandOptionType.SingleValue);

                var pathOption = command.Option("-f|--folder <folder-or-file-path>", "Path to the folder to add templates from", CommandOptionType.SingleValue);
                command.OnExecute(() => {
                    if (pathOption.HasValue() && !string.IsNullOrWhiteSpace(pathOption.Value()))
                    {
                        string path = pathOption.Value();
                        if (string.IsNullOrWhiteSpace(path))
                        {
                            Console.WriteLine("path is empty");
                            command.ShowHelp();
                            return(-1);
                        }
                        templateMan.SourceManager.RemoveTemplateSource(TemplateSource.NewFolderSource(path));
                        PrintSources(templateMan.SourceManager.GetTemplateSources());
                        PrintTemplates(templateMan.GetInstalledTemplates());
                    }
                    else if (pkgOption.HasValue() && !string.IsNullOrWhiteSpace(pkgOption.Value()))
                    {
                        string pkgName    = pkgOption.Value();
                        string pkgVersion = pkgVerOption.Value();

                        if (string.IsNullOrWhiteSpace(pkgName) || string.IsNullOrWhiteSpace(pkgVersion))
                        {
                            Console.WriteLine("package name or version missing");
                            command.ShowHelp();
                            return(-1);
                        }

                        templateMan.SourceManager.RemoveTemplateSource(TemplateSource.NewNuGetSource(pkgName, pkgVersion));
                        PrintSources(templateMan.SourceManager.GetTemplateSources());
                        PrintTemplates(templateMan.GetInstalledTemplates());

                        return(0);
                    }
                    else if (gitOption.HasValue() && !string.IsNullOrWhiteSpace(gitOption.Value()))
                    {
                        string gitUrl    = gitOption.Value();
                        string gitBranch = gitBranchOption.Value();

                        if (string.IsNullOrWhiteSpace(gitUrl) || string.IsNullOrWhiteSpace(gitBranch))
                        {
                            Console.WriteLine("git url or branch missing");
                            command.ShowHelp();
                            return(-1);
                        }

                        templateMan.SourceManager.RemoveTemplateSource(TemplateSource.NewGitSource(new Uri(gitUrl), gitBranch));
                        PrintSources(templateMan.SourceManager.GetTemplateSources());
                        PrintTemplates(templateMan.GetInstalledTemplates());

                        return(0);
                    }
                    return(0);
                });
            });

            app.Command("addsource", command => {
                command.Description = "Used to add a template source";
                command.HelpOption("-?|-h|--help");

                var pkgOption    = command.Option("-p|--package <packagename>", "Name of the NuGet package that contains templates to install", CommandOptionType.SingleValue);
                var pkgVerOption = command.Option("-v|--version <version>", "Version of the NuGet package", CommandOptionType.SingleValue);

                var gitOption       = command.Option("-g|--giturl <giturl>", "URL for the git repo which contains templates to install", CommandOptionType.SingleValue);
                var gitBranchOption = command.Option("-b|--gitbranchname <branchname>", "Name of the branch for the git repo", CommandOptionType.SingleValue);

                var pathOption = command.Option("-f|--folder <folder-or-file-path>", "Path to the folder to add templates from", CommandOptionType.SingleValue);

                command.OnExecute(() => {
                    if (pathOption.HasValue() && !string.IsNullOrWhiteSpace(pathOption.Value()))
                    {
                        string path = pathOption.Value();
                        if (string.IsNullOrWhiteSpace(path))
                        {
                            Console.WriteLine("path is empty");
                            command.ShowHelp();
                            return(-1);
                        }

                        templateMan.SourceManager.AddTemplateSource(TemplateSource.NewFolderSource(path));
                        PrintSources(templateMan.SourceManager.GetTemplateSources());
                        PrintTemplates(templateMan.GetInstalledTemplates());

                        Console.Write("folder selected [{0}]", path);
                        return(0);
                    }
                    else if (pkgOption.HasValue() && !string.IsNullOrWhiteSpace(pkgOption.Value()))
                    {
                        string pkgName    = pkgOption.Value();
                        string pkgVersion = pkgVerOption.Value();

                        if (string.IsNullOrWhiteSpace(pkgName) || string.IsNullOrWhiteSpace(pkgVersion))
                        {
                            Console.WriteLine("package name or version missing");
                            command.ShowHelp();
                            return(-1);
                        }

                        templateMan.SourceManager.AddTemplateSource(TemplateSource.NewNuGetSource(pkgName, pkgVersion));
                        PrintSources(templateMan.SourceManager.GetTemplateSources());
                        PrintTemplates(templateMan.GetInstalledTemplates());

                        return(0);
                    }
                    else if (gitOption.HasValue() && !string.IsNullOrWhiteSpace(gitOption.Value()))
                    {
                        string gitUrl    = gitOption.Value();
                        string gitBranch = gitBranchOption.Value();

                        if (string.IsNullOrWhiteSpace(gitUrl) || string.IsNullOrWhiteSpace(gitBranch))
                        {
                            Console.WriteLine("git url or branch missing");
                            command.ShowHelp();
                            return(-1);
                        }

                        templateMan.SourceManager.AddTemplateSource(TemplateSource.NewGitSource(new Uri(gitUrl), gitBranch));
                        PrintSources(templateMan.SourceManager.GetTemplateSources());
                        PrintTemplates(templateMan.GetInstalledTemplates());

                        return(0);
                    }
                    else
                    {
                        command.ShowHelp();
                        return(-1);
                    }
                    // check for git
                    // check for package
                    // check for folder/file

                    command.ShowHelp();
                    return(0);
                });
            });

            app.Command("updatesources", command => {
                command.Description = "Updates local template content for git template sources";
                command.HelpOption("-?|-h|--help");

                command.OnExecute(() => {
                    templateMan.SourceManager.UpdateRemoteTemplateSources(templateMan.SourceManager.GetTemplateSources());
                    return(0);
                });
            });

            var templateOption = app.Option("-t|--template <template>", "Template name used for creation", CommandOptionType.SingleValue);
            var nameOption     = app.Option("-n|--name <name>", "The name of the new project", CommandOptionType.SingleValue);
            var destPathOption = app.Option("-d|--dest <destpath>", "The location to create the project, current working dir is used by default", CommandOptionType.SingleValue);

            app.OnExecute(() => {
                var templateName  = templateOption.Value();
                Template template = GetOrPromptForTemplate(templateMan, templateName);

                if (template == null)
                {
                    Console.WriteLine("template not found");
                    return(-1);
                }

                var nameValue = nameOption.Value();
                if (string.IsNullOrWhiteSpace(nameValue))
                {
                    nameValue = PromptForName();
                }

                var destPath = destPathOption.Value();
                if (string.IsNullOrWhiteSpace(destPath))
                {
                    // destPath = Directory.GetCurrentDirectory();
                    destPath = PromptForDest();
                }

                if (!Path.IsPathRooted(destPath))
                {
                    destPath = Path.Combine(Directory.GetCurrentDirectory(), destPath);
                }

                new TemplateCreator().CreateProject(template, destPath, nameValue, null);

                return(0);
            });

            return(app.Execute(args));
        }