Ejemplo n.º 1
0
        static int Main(string[] args)
        {
            var stopwatch         = Stopwatch.StartNew();
            var settings          = new ExpansionSettings();
            var commandLineParser = GetCommandLineParser(settings);

            try
            {
                if (args.Length == 0)
                {
                    Console.WriteLine("MESS requires at least one argument (the input map file).");
                    Console.WriteLine("");
                    ShowHelp(commandLineParser);
                    return(-1);
                }

                // NOTE: The -repl argument enables a different mode, so required arguments are no longer required,
                //       but the cmd-line parser doesn't support that, so here's a quick hacky workaround:
                if (args.Any(arg => arg == "-repl"))
                {
                    RunMScriptREPL();
                    return(0);
                }

                ConfigFile.ReadSettings(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mess.config"), settings);
                commandLineParser.Parse(args);

                using (var logger = new MultiLogger(new ConsoleLogger(settings.LogLevel), new FileLogger(settings.InputPath + ".mess.log", settings.LogLevel)))
                {
                    logger.Minimal($"MESS v{Assembly.GetExecutingAssembly().GetName().Version}: Macro Entity Substitution System");
                    logger.Minimal("----- BEGIN MESS -----");
                    logger.Minimal($"Command line: {Environment.CommandLine}");
                    logger.Minimal($"Arguments: {string.Join(" ", Environment.GetCommandLineArgs())}");
                    logger.Minimal("");

                    try
                    {
                        ProcessMacroEntities(settings, logger);
                        return(0);
                    }
                    catch (Exception ex)
                    {
                        logger.Error("Failed to process macro entities", ex);
                        var innerException = ex.InnerException;
                        while (innerException != null)
                        {
                            logger.Error("Inner exception:", innerException);
                            innerException = innerException.InnerException;
                        }
                        // TODO: Show more error details here?
                        return(-1);
                    }
                    finally
                    {
                        // TODO: Log a small summary as well? Number of templates/instances/etc?
                        logger.Minimal("");
                        logger.Minimal($"Finished in {stopwatch.ElapsedMilliseconds / 1000f:0.##} seconds.");
                        logger.Minimal("");
                        logger.Minimal("----- END MESS -----");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to read config file or to parse command line arguments: {ex.GetType().Name}: '{ex.Message}'.");
                ShowHelp(commandLineParser);
                return(-1);
            }
        }