Beispiel #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("SrcMake version " + Version.String + " by Michael Furlong.");

            if (args.Length < MinArgs)
            {
                Console.WriteLine("Invalid argument count.");
                Console.WriteLine(Help.Usage);
                return;
            }

            string funcstr = args[FuncArg].Trim().ToLower();

            if (funcstr == "set")
            {
                if (args.Length != 3)
                {
                    Console.WriteLine("Invalid argument count.");
                    Console.WriteLine(Help.Usage);
                    return;
                }

                if (!SetSetting(args[SettArg], args[ValArg]))
                {
                    Console.WriteLine("Unabke to set setting.");
                    Console.WriteLine(Help.Usage);
                    Console.WriteLine(Help.Settings);
                    return;
                }

                return;
            }
            else if (funcstr == "reset")
            {
                if (args.Length != 2)
                {
                    Console.WriteLine("Invalid argument count.");
                    Console.WriteLine(Help.Usage);
                    return;
                }

                if (!ResetSetting(args[SettArg]))
                {
                    Console.WriteLine("Unabke to reset setting.");
                    Console.WriteLine(Help.Usage);
                    Console.WriteLine(Help.Settings);
                    return;
                }

                return;
            }

            string langstr = args[LangArg].ToLower(),
                   filestr = args[FileArg].ToLower(),
                   name    = args[NameArg];

            GeneratorInfo info = new GeneratorInfo();

            if (!File.Exists(SettingsPath))
            {
                info.Settings.SaveToFile(SettingsPath);
            }
            else if (!info.Settings.LoadFromFile(SettingsPath))
            {
                Console.WriteLine("Unable to load user settings. Defaults will be used.");
            }

            // Language flag
            {
                Language lang = (Language)(-1);

                if (langstr == "c#")
                {
                    lang = Language.CSharp;
                }
                else
                {
                    for (Language l = 0; (int)l < Enum.GetNames(typeof(Language)).Length; l++)
                    {
                        if (langstr == Enum.GetName(typeof(Language), l).ToLower())
                        {
                            lang = l;
                            break;
                        }
                    }

                    if ((int)lang == -1)
                    {
                        Console.WriteLine("Invalid language argument.");
                        Console.WriteLine(Help.Language);
                        return;
                    }
                }

                info.Language = lang;
            }
            // FileType flag
            {
                FileType file = (FileType)(-1);

                for (FileType f = 0; (int)f < Enum.GetNames(typeof(FileType)).Length; f++)
                {
                    if (filestr == Enum.GetName(typeof(FileType), f).ToLower())
                    {
                        file = f;
                        break;
                    }
                }

                if ((int)file == -1)
                {
                    Console.WriteLine("Invalid file type argument.");
                    Console.WriteLine(Help.FileType);
                    return;
                }

                info.FileType = file;
            }

            if (!Flags.IsValidName(name))
            {
                Console.WriteLine("Invalid name argument.");
                Console.WriteLine(Help.Naming);
                return;
            }

            info.Name = name;

            if (args.Length > MinGenArgs)
            {
                for (int i = MinGenArgs + 1; i < args.Length; i++)
                {
                    info.Args.Add(StringTools.RemoveUnquotedWhitespace(args[i]));
                }
            }

            if (!info.IsValid())
            {
                Console.WriteLine("Incompatible or invalid arguments given.");
                Console.WriteLine(Help.Arguments);
                return;
            }

            if (!Generate(info))
            {
                Console.WriteLine("Generating failed. Try running as admin and ensuring no files are locked.");
                return;
            }
        }
Beispiel #2
0
        /// <summary>
        ///   Loads settings from the file at the given path.
        /// </summary>
        /// <param name="path">
        ///   The path of the file to load.
        /// </param>
        /// <returns>
        ///   True if settings were loaded successfully, otherwise false.
        /// </returns>
        public bool LoadFromFile(string path)
        {
            List <string> lines = new List <string>();

            {
                try
                {
                    string[] ls = File.ReadAllLines(path);

                    foreach (string l in ls)
                    {
                        string line = StringTools.RemoveUnquotedWhitespace(l);

                        if (line.Length == 0 || line[0] == ';' || line[0] == '#')
                        {
                            continue;
                        }

                        lines.Add(line);
                    }
                }
                catch
                {
                    return(false);
                }
            }

            if (lines[0].ToLower() != "[user]")
            {
                Console.WriteLine("Settings file does not start with [User] header.");
                return(false);
            }

            for (int i = 1; i < lines.Count; i++)
            {
                if (lines[i].Length >= 7 && lines[i].Substring(0, 7).ToLower() == "author=")
                {
                    if (lines[i].Length == 7)
                    {
                        Author = string.Empty;
                    }
                    else
                    {
                        Author = lines[i].Substring(7);

                        if (Author.Length > 2 && Author[0] == '"' && Author[Author.Length - 1] == '"')
                        {
                            Author = Author.Substring(1, Author.Length - 2);
                        }
                    }
                }
                else if (lines[i].Length >= 6 && lines[i].Substring(0, 6).ToLower() == "email=")
                {
                    if (lines[i].Length == 6)
                    {
                        Email = string.Empty;
                    }
                    else
                    {
                        Email = lines[i].Substring(6);

                        if (Email.Length > 2 && Email[0] == '"' && Email[Email.Length - 1] == '"')
                        {
                            Email = Email.Substring(1, Email.Length - 2);
                        }
                    }
                }
                else if (lines[i].Length >= 8 && lines[i].Substring(0, 8).ToLower() == "lineend=")
                {
                    if (lines[i].Length == 8)
                    {
                        LineEnd = string.Empty;
                    }
                    else
                    {
                        LineEnd = lines[i].Substring(8);

                        if (LineEnd.Length > 2 && LineEnd[0] == '"' && LineEnd[LineEnd.Length - 1] == '"')
                        {
                            LineEnd = LineEnd.Substring(1, LineEnd.Length - 2);
                        }
                    }
                }
            }

            return(true);
        }