Exemple #1
0
        private bool Initialize()
        {
            bool ephemeralHiveFlag = _app.RemainingArguments.Any(x => x == "--debug:ephemeral-hive");

            if (ephemeralHiveFlag)
            {
                EnvironmentSettings.Host.VirtualizeDirectory(_paths.User.BaseDir);
            }

            bool reinitFlag = _app.RemainingArguments.Any(x => x == "--debug:reinit");

            if (reinitFlag)
            {
                _paths.Delete(_paths.User.FirstRunCookie);
            }

            // Note: this leaves things in a weird state. Might be related to the localized caches.
            // not sure, need to look into it.
            if (reinitFlag || _app.RemainingArguments.Any(x => x == "--debug:reset-config"))
            {
                _paths.Delete(_paths.User.AliasesFile);
                _paths.Delete(_paths.User.SettingsFile);
                _templateCache.DeleteAllLocaleCacheFiles();
                return(false);
            }

            if (!_paths.Exists(_paths.User.BaseDir) || !_paths.Exists(_paths.User.FirstRunCookie))
            {
                if (!IsQuietFlagSpecified)
                {
                    Reporter.Output.WriteLine(LocalizableStrings.GettingReady);
                }

                ConfigureEnvironment();
                _paths.WriteAllText(_paths.User.FirstRunCookie, "");
            }

            if (_app.RemainingArguments.Any(x => x == "--debug:showconfig"))
            {
                ShowConfig();
                return(false);
            }

            return(true);
        }
Exemple #2
0
        public static int Main(string[] args)
        {
            CommandLineApplication app = new CommandLineApplication(false)
            {
                Name     = "dotnet new3",
                FullName = "Template Instantiation Commands for .NET Core CLI."
            };

            CommandArgument template        = app.Argument("template", "The template to instantiate.");
            CommandOption   listOnly        = app.Option("-l|--list", "Lists templates with containing the specified name.", CommandOptionType.NoValue);
            CommandOption   name            = app.Option("-n|--name", "The name for the output. If no name is specified, the name of the current directory is used.", CommandOptionType.SingleValue);
            CommandOption   dir             = app.Option("-d|--dir", "Indicates whether to display create a directory for the generated content.", CommandOptionType.NoValue);
            CommandOption   alias           = app.Option("-a|--alias", "Creates an alias for the specified template", CommandOptionType.SingleValue);
            CommandOption   parametersFiles = app.Option("-x|--extra-args", "Adds a parameters file.", CommandOptionType.MultipleValue);
            CommandOption   install         = app.Option("-i|--install", "Installs a source or a template pack.", CommandOptionType.MultipleValue);
            CommandOption   help            = app.Option("-h|--help", "Indicates whether to display the help for the template's parameters instead of creating it.", CommandOptionType.NoValue);

            CommandOption quiet           = app.Option("--quiet", "Doesn't output any status information.", CommandOptionType.NoValue);
            CommandOption skipUpdateCheck = app.Option("--skip-update-check", "Don't check for updates.", CommandOptionType.NoValue);
            CommandOption update          = app.Option("--update", "Update matching templates.", CommandOptionType.NoValue);

            CommandOption localeOption = app.Option("--locale", "The locale to use", CommandOptionType.SingleValue);

            app.OnExecute(async() =>
            {
                //TODO: properly determine the locale, pass it to the host constructor
                // specifying on the command line is probaby not the ultimate answer.
                // Regardless, the locale should get set before anything else happens.
                string locale = localeOption.HasValue() ? localeOption.Value() : "en_US";
                EngineEnvironmentSettings.Host = new DefaultTemplateEngineHost(locale);

                bool reinitFlag = app.RemainingArguments.Any(x => x == "--debug:reinit");

                if (reinitFlag)
                {
                    Paths.User.FirstRunCookie.Delete();
                }

                // Note: this leaves things in a weird state. Might be related to the localized caches.
                // not sure, need to look into it.
                if (reinitFlag || app.RemainingArguments.Any(x => x == "--debug:reset-config"))
                {
                    Paths.User.AliasesFile.Delete();
                    Paths.User.SettingsFile.Delete();
                    TemplateCache.DeleteAllLocaleCacheFiles();
                    return(0);
                }

                if (!Paths.User.BaseDir.Exists() || !Paths.User.FirstRunCookie.Exists())
                {
                    if (!quiet.HasValue())
                    {
                        Reporter.Output.WriteLine("Getting things ready for first use...");
                    }

                    ConfigureEnvironment();
                    Paths.User.FirstRunCookie.WriteAllText("");
                }

                if (app.RemainingArguments.Any(x => x == "--debug:showconfig"))
                {
                    ShowConfig();
                    return(0);
                }

                if (install.HasValue())
                {
                    InstallPackage(install.Values, quiet.HasValue());
                    return(0);
                }

                if (update.HasValue())
                {
                    //return PerformUpdateAsync(template.Value, quiet.HasValue(), source);
                }

                if (listOnly.HasValue())
                {
                    ListTemplates(template);
                    return(0);
                }

                IReadOnlyDictionary <string, string> parameters;

                try
                {
                    parameters = app.ParseExtraArgs(parametersFiles);
                }
                catch (Exception ex)
                {
                    Reporter.Error.WriteLine(ex.Message.Red().Bold());
                    app.ShowHelp();
                    return(-1);
                }

                if (string.IsNullOrWhiteSpace(template.Value) && help.HasValue())
                {
                    app.ShowHelp();
                    return(0);
                }

                if (help.HasValue())
                {
                    return(DisplayHelp(template));
                }

                string aliasName    = alias.HasValue() ? alias.Value() : null;
                string fallbackName = new DirectoryInfo(Directory.GetCurrentDirectory()).Name;

                if (await TemplateCreator.Instantiate(template.Value ?? "", name.Value(), fallbackName, dir.HasValue(), aliasName, parameters, skipUpdateCheck.HasValue()) == -1)
                {
                    ListTemplates(template);
                    return(-1);
                }

                return(0);
            });

            int result;

            try
            {
                using (Timing.Over("Execute"))
                {
                    result = app.Execute(args);
                }
            }
            catch (Exception ex)
            {
                AggregateException ax = ex as AggregateException;

                while (ax != null && ax.InnerExceptions.Count == 1)
                {
                    ex = ax.InnerException;
                    ax = ex as AggregateException;
                }

                Reporter.Error.WriteLine(ex.Message.Bold().Red());

                while (ex.InnerException != null)
                {
                    ex = ex.InnerException;
                    ax = ex as AggregateException;

                    while (ax != null && ax.InnerExceptions.Count == 1)
                    {
                        ex = ax.InnerException;
                        ax = ex as AggregateException;
                    }

                    Reporter.Error.WriteLine(ex.Message.Bold().Red());
                }

                Reporter.Error.WriteLine(ex.StackTrace.Bold().Red());
                result = 1;
            }

            return(result);
        }