예제 #1
0
        public static BundlerModel Parse(string root, string path, bool includeOptionalFiles, bool includeSample)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                return null;
            }

            BundlerModel model = new BundlerModel();
            //Collection<string> files = new Collection<string>();
            Collection<KeyValuePair<string, string>> dictionaries = new Collection<KeyValuePair<string, string>>();

            string content = File.ReadAllText(path, Encoding.UTF8);
            string scriptDirectory = string.Empty;

            string[] lines = content.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            foreach (string line in lines)
            {
                var items = line.Split(':');
                var key = items[0].TrimStart('-').Trim();
                var value = items[1].Trim();

                string defaultLanguage = GetDefaultLanguage(key, value);

                if (!string.IsNullOrWhiteSpace(GetScriptDirectory(key, value)))
                {
                    scriptDirectory = GetScriptDirectory(key, value);
                    scriptDirectory = Path.Combine(root, scriptDirectory);

                    if (!Directory.Exists(scriptDirectory))
                    {
                        scriptDirectory = string.Empty;
                    }
                }

                KeyValuePair<string, string> dictionary = GetDictionary(key, value);
                string outputDirectory = GetOutputDirectory(key, value);

                if (!string.IsNullOrWhiteSpace(defaultLanguage))
                {
                    model.DefaultLanguage = defaultLanguage;
                }

                if (!string.IsNullOrWhiteSpace(dictionary.Key) && !string.IsNullOrWhiteSpace(dictionary.Value))
                {
                    dictionaries.Add(dictionary);
                }

                if (!string.IsNullOrWhiteSpace(outputDirectory))
                {
                    model.OutputDirectory = outputDirectory;
                }
            }

            model.OriginalFileName = Path.GetFileNameWithoutExtension(path).Replace(".sqlbundle", "");
            model.Files = GetScripts(scriptDirectory, includeOptionalFiles, includeSample);
            model.Dictionaries = dictionaries;

            return model;
        }
예제 #2
0
        public static Collection<SQLBundle> Process(string root, BundlerModel model)
        {
            if (model == null)
            {
                return null;
            }

            StringBuilder script = new StringBuilder();
            Collection<SQLBundle> bundles = new Collection<SQLBundle>();

            foreach (string fileName in model.Files)
            {
                if (!string.IsNullOrWhiteSpace(script.ToString()))
                {
                    script.Append(Environment.NewLine);
                }

                Console.WriteLine(@"Adding {0} to bundle", fileName);
                script.Append("-->-->-- ");
                script.Append(Path.GetFullPath(fileName).Replace(root, "").Replace("\\", "/"));
                script.Append(" --<--<--");

                script.Append(Environment.NewLine);

                script.Append(File.ReadAllText(fileName, Encoding.UTF8));
                script.Append(Environment.NewLine);
            }

            if (string.IsNullOrWhiteSpace(script.ToString()))
            {
                return null;
            }

            SQLBundle defaultBundle = new SQLBundle();
            defaultBundle.FileName = GetBundleFileName(model.OutputDirectory, model.OriginalFileName,
                model.DefaultLanguage);
            defaultBundle.Language = model.DefaultLanguage;
            defaultBundle.Script = script.ToString();

            bundles.Add(defaultBundle);

            foreach (var dictionary in model.Dictionaries)
            {
                Console.WriteLine(@"SQL localization dictionary: {0}", dictionary.Value);

                SQLBundle bundle = new SQLBundle();
                string filePath = Path.Combine(root, dictionary.Value);

                bundle.FileName = GetBundleFileName(model.OutputDirectory, model.OriginalFileName, dictionary.Key);
                bundle.Script = script.ToString();

                List<string> lines =
                    File.ReadAllText(filePath, Encoding.UTF8)
                        .Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                        .ToList();
                foreach (string line in lines)
                {
                    string find = line.Split('=')[0].Trim();
                    string replace = line.Split('=')[1].Trim();

                    bundle.Script = bundle.Script.Replace(find, replace);
                }

                bundles.Add(bundle);
            }

            return bundles;
        }