Пример #1
0
    static void Main(string[] args)
    {
        try
        {
            string recipePath = args[0];

            var macros = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            // add all environment variables to the dictionary
            IDictionary env = Environment.GetEnvironmentVariables();
            foreach (string key in env.Keys) macros.Add(key, (string)env[key]);

            // parse command line arguments and add to dictionary
            for (int i = 1; i < args.Length; i++)
            {
                string[] nv = args[i].Split('=');
                if (nv.Length == 2) macros[nv[0]] = nv[1];
                else throw new Exception("Invalid argument: " + args[i]);
            }
            var program = new Recipe();
            program.Write = true;
            program.SetRootPath(recipePath);
            program.ClearHandlers();
            program.Message += PrintLine;

            using (var sr = new LineReader(File.OpenText(recipePath), name: recipePath))
            {
                program.Run(sr, macros, string.Empty);
            }
        }
        catch (ApplicationException)
        {
            //exit
        }
        catch (Exception ex)
        {
            Console.WriteLine("Usage: recipe.exe recipe-file key1=val1 key2=val2...");
            Console.WriteLine(ex.Message);
        }
    }
Пример #2
0
        private void InitializeRecipe()
        {
            try
            {
                ConfigUtils.RefreshAppSettings();
                string recipePath = ConfigUtils.GetString("Path", @"~prog\Main.rcp");
                recipePath = ExpandPath(recipePath);
                _LogLevel = ConfigUtils.GetValue("LogLevel", 1);

                var macros = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                // add all environment variables to the dictionary
                IDictionary env = Environment.GetEnvironmentVariables();
                foreach (string key in env.Keys) macros.Add(key, (string)env[key]);

                // parse config file arguments and add to dictionary
                string[] args = ConfigUtils.GetArray("Macros", "");
                var sb = new StringBuilder();
                if (args != null)
                {
                    foreach (string t in args)
                    {
                        string[] nv = t.Split('=');
                        if (nv.Length == 2)
                        {
                            string key = nv[0].Trim();
                            string value = ExpandPath(nv[1].Trim());
                            macros[key] = value;
                            sb.Append(key).Append('=').Append(value).Append('|');
                        }
                        else throw new Exception("Invalid argument: " + t);
                    }
                }
                Log(0, "Macros from configuration: [{0}]", sb.ToString().TrimEnd('|'));
                _RecipeExec = new Recipe { Write = true };
                _RecipeExec.ClearHandlers();
                _RecipeExec.Message += HandleMessage;
                _RecipeExec.SetRootPath(recipePath);

                using (LineReader sr = new LineReader(File.OpenText(recipePath), name: recipePath))
                {
                    Log(0, "Running: [{0}] LogLevel: {1}", recipePath, _LogLevel);
                    _RecipeExec.Run(sr, macros, string.Empty);
                }
            }
            catch (ApplicationException)
            {
                Log(0, "Recipe Exit {0}");
            }
            catch (Exception ex)
            {
                Log(1, "{0}", ex);
            }
        }