コード例 #1
0
        private static JsonResources generateJsonResources(IResxToJsonFormatter formatter, ResourceBundle bundle,
                                                           ResxToJsonConverterOptions options)
        {
            var result = new JsonResources();
            // root resoruce
            IDictionary <string, string> baseValues = bundle.GetValues(null);
            JObject jBaseValues = convertValues(baseValues, options);

            result.BaseResources = formatter.GetJsonResource(jBaseValues, CultureInfo.InvariantCulture, bundle, options);

            // culture specific resources
            foreach (CultureInfo culture in bundle.Cultures)
            {
                if (culture.Equals(CultureInfo.InvariantCulture))
                {
                    continue;
                }

                IDictionary <string, string> values = bundle.GetValues(culture);
                if (options.UseFallbackForMissingTranslation)
                {
                    foreach (var baseValue in baseValues)
                    {
                        if (!values.ContainsKey(baseValue.Key))
                        {
                            values[baseValue.Key] = baseValues[baseValue.Key];
                        }
                    }
                }
                JObject jCultureValues = convertValues(values, options);
                result.LocalizedResources[culture] = formatter.GetJsonResource(jCultureValues, culture, bundle, options);
            }
            return(result);
        }
コード例 #2
0
        public override bool CheckOptions(ResxToJsonConverterOptions options, out string message)
        {
            if (string.IsNullOrEmpty(options.FallbackCulture))
            {
                message = "The parameter fallbackCulture is not specified.";
                return(false);
            }

            message = string.Empty;
            return(true);
        }
コード例 #3
0
        private static JObject convertValues(IDictionary <string, string> values, ResxToJsonConverterOptions options)
        {
            JObject json = new JObject();

            foreach (KeyValuePair <string, string> pair in values)
            {
                if (pair.Key.Contains(options.KeySeparator))
                {
                    JObject curJObject = json;
                    // hierarchy structure
                    string[] fieldNames = pair.Key.Split(options.KeySeparator[0]);
                    for (int i = 0; i < fieldNames.Length; i++)
                    {
                        string fieldName = fieldNames[i];
                        string jKey      = adjustCasing(fieldName, options);

                        if (i == fieldNames.Length - 1)
                        {
                            curJObject[jKey] = pair.Value;
                        }
                        else
                        {
                            var child = curJObject.Property(jKey);
                            if (child == null)
                            {
                                JObject   childObject   = new JObject();
                                JProperty childProperty = new JProperty(jKey, childObject);
                                curJObject.Add(childProperty);

                                curJObject = childObject;
                            }
                            else
                            {
                                JToken value = child.Value;
                                if (value.Type == JTokenType.Object)
                                {
                                    curJObject = (JObject)value;
                                }
                            }
                        }
                    }
                }
                else
                {
                    // flat
                    json[adjustCasing(pair.Key, options)] = pair.Value;
                }
            }
            var s = json.ToString();

            return(json);
        }
コード例 #4
0
        static void checkOptions(ResxToJsonConverterOptions options)
        {
            if (options.Inputs.Count > 0)
            {
                foreach (string input in options.Inputs)
                {
                    string path = input;
                    if (!Path.IsPathRooted(input))
                    {
                        path = Path.Combine(Environment.CurrentDirectory, input);
                    }

                    if (Directory.Exists(path))
                    {
                        if (!string.IsNullOrEmpty(options.FilePattern))
                        {
                            try {
                                var files = Directory.EnumerateFiles(path, options.FilePattern);
                                foreach (string file in files)
                                {
                                    options.InputFiles.Add(file);
                                }
                            } catch (Exception exception) {
                                CrashAndBurn(ExitCode.InvalidInputPath,
                                             "'{0}' is not a valid pattern.", options.FilePattern);
                            }
                        }
                        else
                        {
                            options.InputFolders.Add(path);
                        }
                    }
                    else if (File.Exists(path))
                    {
                        options.InputFiles.Add(path);
                    }
                    else
                    {
                        CrashAndBurn(ExitCode.InvalidInputPath, "input path '{0}' doesn't relate to a file or a directory", path);
                    }
                }
            }
            else
            {
                options.InputFolders.Add(Environment.CurrentDirectory);
            }

            if (String.IsNullOrEmpty(options.OutputFolder) && String.IsNullOrEmpty(options.OutputFile))
            {
                options.OutputFolder = Environment.CurrentDirectory;
            }
        }
コード例 #5
0
        static string stringifyJson(JObject json, ResxToJsonConverterOptions options)
        {
            string text = json.ToString(Formatting.Indented);

            switch (options.OutputFormat)
            {
            case OutputFormat.RequireJs:
                return("define(" + text + ");");

            default:
                return(text);
            }
        }
コード例 #6
0
        private static string adjustCasing(string key, ResxToJsonConverterOptions options)
        {
            switch (options.Casing)
            {
            case JsonCasing.Camel:
                char[] chars = key.ToCharArray();
                chars[0] = Char.ToLower(chars[0]);
                return(new string(chars));

            case JsonCasing.Lower:
                return(key.ToLowerInvariant());
            }
            return(key);
        }
コード例 #7
0
 private static string getOutputPath(ResxToJsonConverterOptions options, string baseDir, string baseFileName, string lang)
 {
     if (false)
     {
         string dirPath = options.OutputFormat == OutputFormat.i18next
             ? Path.Combine(baseDir, lang)
             : baseDir;
         return(Path.Combine(dirPath, baseFileName));
     }
     else
     {
         return(Path.Combine(baseDir,
                             $"{Path.GetFileNameWithoutExtension(baseFileName)}.{lang}{Path.GetExtension(baseFileName)}"));
     }
 }
コード例 #8
0
        private static JsonResources generateJsonResources(ResourceBundle bundle, ResxToJsonConverterOptions options)
        {
            var result = new JsonResources();
            // root resoruce
            IDictionary <string, string> baseValues = bundle.GetValues(null);
            JObject jBaseValues = convertValues(baseValues, options);

            switch (options.OutputFormat)
            {
            case OutputFormat.RequireJs:
                // When dealing with require.js i18n the root resource contains a "root" subnode that contains all
                // of the base translations and then a bunch of nodes like the following for each supported culture:
                //   "en-US" : true
                //   "fr" : true
                //   ...
                var jRoot = new JObject();
                jRoot["root"] = jBaseValues;
                foreach (CultureInfo culture in bundle.Cultures)
                {
                    if (culture.Equals(CultureInfo.InvariantCulture))
                    {
                        continue;
                    }
                    jRoot[culture.Name] = true;
                }
                result.BaseResources = jRoot;
                break;

            default:
                // In the simplest case our output format is plain vanilla json (just a kvp dictionary)
                result.BaseResources = jBaseValues;
                break;
            }

            // culture specific resources
            foreach (CultureInfo culture in bundle.Cultures)
            {
                if (culture.Equals(CultureInfo.InvariantCulture))
                {
                    continue;
                }
                IDictionary <string, string> values = bundle.GetValues(culture);
                JObject jCultureValues = convertValues(values, options);
                result.LocalizedResources[culture.Name] = jCultureValues;
            }
            return(result);
        }
コード例 #9
0
 private static void writeOutput(string outputPath, string jsonText, ResxToJsonConverterOptions options, ConverterLogger logger)
 {
     Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
     if (File.Exists(outputPath))
     {
         var attrs = File.GetAttributes(outputPath);
         if ((attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
         {
             if (options.Overwrite == OverwriteModes.Skip)
             {
                 logger.AddMsg(Severity.Error, "Cannot overwrite {0} file, skipping", outputPath);
                 return;
             }
             // remove read-only attribute
             attrs = ~FileAttributes.ReadOnly & attrs;
             File.SetAttributes(outputPath, attrs);
         }
         // if existing file isn't readonly we just overwrite it
     }
     File.WriteAllText(outputPath, jsonText, Encoding.UTF8);
     logger.AddMsg(Severity.Info, "Created {0} file", outputPath);
 }
コード例 #10
0
        private static JObject convertValues(IDictionary <string, string> values, ResxToJsonConverterOptions options)
        {
            var json = new JObject();

            foreach (KeyValuePair <string, string> pair in values)
            {
                string fieldName = pair.Key;
                switch (options.Casing)
                {
                case JsonCasing.Camel:
                    char[] chars = fieldName.ToCharArray();
                    chars[0]  = Char.ToLower(chars[0]);
                    fieldName = new string(chars);
                    break;

                case JsonCasing.Lower:
                    fieldName = fieldName.ToLowerInvariant();
                    break;
                }
                json[fieldName] = pair.Value;
            }
            return(json);
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: peter-cmw/ResxToJson
        static void checkOptions(ResxToJsonConverterOptions options)
        {
            if (options.Inputs.Count > 0)
            {
                foreach (string input in options.Inputs)
                {
                    string path = input;
                    if (!Path.IsPathRooted(input))
                    {
                        path = Path.Combine(Environment.CurrentDirectory, input);
                    }

                    if (Directory.Exists(path))
                    {
                        options.InputFolders.Add(path);
                    }
                    else if (File.Exists(path))
                    {
                        options.InputFiles.Add(path);
                    }
                    else
                    {
                        CrashAndBurn(ExitCode.InvalidInputPath, "input path '{0}' doesn't relate to a file or a directory", path);
                    }
                }
            }
            else
            {
                options.InputFolders.Add(Environment.CurrentDirectory);
            }

            if (String.IsNullOrEmpty(options.OutputFolder) && String.IsNullOrEmpty(options.OutputFile))
            {
                options.OutputFolder = Environment.CurrentDirectory;
            }
        }
コード例 #12
0
        public override string GetFileContent(JObject json, ResxToJsonConverterOptions options)
        {
            string jsonSerialized = SerializeJsonObject(json);

            string content = $@"""use strict"";

! function(root, factory) {{
	if (""function"" === typeof define && define.amd) {{
		define(function(require) {{
			factory(require(""devextreme/localization""));
		}})
	}} else {{
		if (""object"" === typeof module && module.exports) {{
			factory(require(""devextreme/localization""));
		}} else {{
			factory(DevExpress.localization);
		}}
	}}
}} (this, function(localization) {{
	localization.loadMessages({jsonSerialized});
}}); ";

            return(content);
        }
コード例 #13
0
 public virtual JObject GetJsonResource(JObject jBaseValues, CultureInfo cultureInfo, ResourceBundle bundle, ResxToJsonConverterOptions options)
 {
     return(jBaseValues);
 }
コード例 #14
0
 public virtual string GetLanguageFileName(string baseFileName, CultureInfo cultureInfo, ResxToJsonConverterOptions options)
 {
     return(baseFileName);
 }
コード例 #15
0
 public virtual string GetBaseOutputDirectory(string baseOutputDirectory, CultureInfo cultureInfo, ResxToJsonConverterOptions options)
 {
     if (Equals(cultureInfo, CultureInfo.InvariantCulture))
     {
         return(baseOutputDirectory);
     }
     return(Path.Combine(baseOutputDirectory, cultureInfo.Name));
 }
コード例 #16
0
        private static JsonResources generateJsonResources(ResourceBundle bundle, ResxToJsonConverterOptions options)
        {
            var result = new JsonResources();
            // root resoruce
            IDictionary<string, string> baseValues = bundle.GetValues(null);
            JObject jBaseValues = convertValues(baseValues, options);
            switch (options.OutputFormat)
            {
                case OutputFormat.RequireJs:
                    // When dealing with require.js i18n the root resource contains a "root" subnode that contains all
                    // of the base translations and then a bunch of nodes like the following for each supported culture:
                    //   "en-US" : true
                    //   "fr" : true
                    //   ...
                    var jRoot = new JObject();
                    jRoot["root"] = jBaseValues;
                    foreach (CultureInfo culture in bundle.Cultures)
                    {
                        if (culture.Equals(CultureInfo.InvariantCulture))
                            continue;
                        jRoot[culture.Name] = true;
                    }
                    result.BaseResources = jRoot;
                    break;
                default:
                    // In the simplest case our output format is plain vanilla json (just a kvp dictionary)
                    result.BaseResources = jBaseValues;
                    break;
            }

            // culture specific resources
            foreach (CultureInfo culture in bundle.Cultures)
            {
                if (culture.Equals(CultureInfo.InvariantCulture))
                    continue;
                IDictionary<string, string> values = bundle.GetValues(culture);
                JObject jCultureValues = convertValues(values, options);
                result.LocalizedResources[culture.Name] = jCultureValues;
            }
            return result;
        }
コード例 #17
0
        public override string GetBaseOutputDirectory(string baseOutputDirectory, CultureInfo cultureInfo, ResxToJsonConverterOptions options)
        {
            if (Equals(cultureInfo, CultureInfo.InvariantCulture))
            {
                return(Path.Combine(baseOutputDirectory, options.FallbackCulture));
            }

            return(base.GetBaseOutputDirectory(baseOutputDirectory, cultureInfo, options));
        }
コード例 #18
0
        public override JObject GetJsonResource(JObject jBaseValues, CultureInfo cultureInfo, ResourceBundle bundle, ResxToJsonConverterOptions options)
        {
            if (!Equals(cultureInfo, CultureInfo.InvariantCulture))
            {
                return(jBaseValues);
            }
            // When dealing with require.js i18n the root resource contains a "root" subnode that contains all
            // of the base translations and then a bunch of nodes like the following for each supported culture:
            //   "en-US" : true
            //   "fr" : true
            //   ...
            var jRoot = new JObject();

            jRoot["root"] = jBaseValues;
            foreach (CultureInfo bundleCulture in bundle.Cultures)
            {
                if (bundleCulture.Equals(CultureInfo.InvariantCulture))
                {
                    continue;
                }
                jRoot[cultureInfo.Name] = true;
            }
            return(jRoot);
        }
コード例 #19
0
 public override string GetBaseOutputDirectory(string baseOutputDirectory, CultureInfo cultureInfo, ResxToJsonConverterOptions options)
 {
     return(baseOutputDirectory);
 }
コード例 #20
0
        public static ConverterLogger Convert(ResxToJsonConverterOptions options)
        {
            var logger = new ConverterLogger();

            IDictionary <string, ResourceBundle> bundles = null;

            if (options.InputFiles.Count > 0)
            {
                bundles = ResxHelper.GetResources(options.InputFiles, logger);
            }
            if (options.InputFolders.Count > 0)
            {
                var bundles2 = ResxHelper.GetResources(options.InputFolders, options.Recursive, logger);
                if (bundles == null)
                {
                    bundles = bundles2;
                }
                else
                {
                    // join two bundles collection
                    foreach (var pair in bundles2)
                    {
                        bundles[pair.Key] = pair.Value;
                    }
                }
            }

            if (bundles == null || bundles.Count == 0)
            {
                logger.AddMsg(Severity.Warning, "No resx files were found");
                return(logger);
            }
            logger.AddMsg(Severity.Trace, "Found {0} resx bundles", bundles.Count);
            if (bundles.Count > 1 && !String.IsNullOrEmpty(options.OutputFile))
            {
                // join multiple resx resources into a single js-bundle
                var bundleMerge = new ResourceBundle(Path.GetFileNameWithoutExtension(options.OutputFile));
                foreach (var pair in bundles)
                {
                    bundleMerge.MergeWith(pair.Value);
                }
                logger.AddMsg(Severity.Trace, "As 'outputFile' option was specified all bundles were merged into single bundle '{0}'", bundleMerge.BaseName);
                bundles = new Dictionary <string, ResourceBundle> {
                    { bundleMerge.BaseName, bundleMerge }
                };
            }

            foreach (ResourceBundle bundle in bundles.Values)
            {
                JsonResources jsonResources = generateJsonResources(bundle, options);
                string        baseFileName;
                string        baseDir;
                if (!string.IsNullOrEmpty(options.OutputFile))
                {
                    baseFileName = Path.GetFileName(options.OutputFile);
                    baseDir      = Path.GetDirectoryName(options.OutputFile);
                }
                else
                {
                    baseFileName = bundle.BaseName.ToLowerInvariant() + GetOutputFileExtension(options.OutputFormat);
                    baseDir      = options.OutputFolder;
                }
                if (string.IsNullOrEmpty(baseDir))
                {
                    baseDir = Environment.CurrentDirectory;
                }

                logger.AddMsg(Severity.Trace, "Processing '{0}' bundle (contains {1} resx files)", bundle.BaseName,
                              bundle.Cultures.Count);
                string dirPath = options.OutputFormat == OutputFormat.i18next
                    ? Path.Combine(baseDir, options.FallbackCulture)
                    : baseDir;
                string outputPath = Path.Combine(dirPath, baseFileName);
                string jsonText   = stringifyJson(jsonResources.BaseResources, options);
                writeOutput(outputPath, jsonText, options, logger);

                if (jsonResources.LocalizedResources.Count > 0)
                {
                    foreach (KeyValuePair <string, JObject> pair in jsonResources.LocalizedResources)
                    {
                        dirPath    = Path.Combine(baseDir, pair.Key);
                        outputPath = Path.Combine(dirPath, baseFileName);
                        jsonText   = stringifyJson(pair.Value, options);
                        writeOutput(outputPath, jsonText, options, logger);
                    }
                }
            }

            return(logger);
        }
コード例 #21
0
 private static JObject convertValues(IDictionary<string, string> values, ResxToJsonConverterOptions options)
 {
     var json = new JObject();
     foreach (KeyValuePair<string, string> pair in values)
     {
         string fieldName = pair.Key;
         switch (options.Casing)
         {
             case JsonCasing.Camel:
                 char[] chars = fieldName.ToCharArray();
                 chars[0] = Char.ToLower(chars[0]);
                 fieldName = new string(chars);
                 break;
             case JsonCasing.Lower:
                 fieldName = fieldName.ToLowerInvariant();
                 break;
         }
         json[fieldName] = pair.Value;
     }
     return json;
 }
コード例 #22
0
 static string stringifyJson(JObject json, ResxToJsonConverterOptions options)
 {
     string text = json.ToString(Formatting.Indented);
     switch (options.OutputFormat)
     {
         case OutputFormat.RequireJs:
             return "define(" + text + ");";
         default:
             return text;
     }
 }
コード例 #23
0
 private static void writeOutput(string outputPath, string jsonText, ResxToJsonConverterOptions options, ConverterLogger logger)
 {
     Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
     if (File.Exists(outputPath))
     {
         var attrs = File.GetAttributes(outputPath);
         if ((attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
         {
             if (options.Overwrite == OverwriteModes.Skip)
             {
                 logger.AddMsg(Severity.Error, "Cannot overwrite {0} file, skipping", outputPath);
                 return;
             }
             // remove read-only attribute
             attrs = ~FileAttributes.ReadOnly & attrs;
             File.SetAttributes(outputPath, attrs);
         }
         // if existing file isn't readonly we just overwrite it
     }
     File.WriteAllText(outputPath, jsonText, Encoding.UTF8);
     logger.AddMsg(Severity.Info, "Created {0} file", outputPath);
 }
コード例 #24
0
        public static ConverterLogger Convert(ResxToJsonConverterOptions options)
        {
            var logger = new ConverterLogger();

            IDictionary<string, ResourceBundle> bundles = null;
            if (options.InputFiles.Count > 0)
            {
                bundles = ResxHelper.GetResources(options.InputFiles, logger);
            }
            if (options.InputFolders.Count > 0)
            {
                var bundles2 = ResxHelper.GetResources(options.InputFolders, options.Recursive, logger);
                if (bundles == null )
                {
                    bundles = bundles2;
                }
                else
                {
                    // join two bundles collection
                    foreach (var pair in bundles2)
                    {
                        bundles[pair.Key] = pair.Value;
                    }
                }
            }

            if (bundles == null || bundles.Count == 0)
            {
                logger.AddMsg(Severity.Warning, "No resx files were found");
                return logger;
            }
            logger.AddMsg(Severity.Trace, "Found {0} resx bundles", bundles.Count);
            if (bundles.Count > 1 && !String.IsNullOrEmpty(options.OutputFile))
            {
                // join multiple resx resources into a single js-bundle
                var bundleMerge = new ResourceBundle(Path.GetFileNameWithoutExtension(options.OutputFile));
                foreach (var pair in bundles)
                {
                    bundleMerge.MergeWith(pair.Value);
                }
                logger.AddMsg(Severity.Trace, "As 'outputFile' option was specified all bundles were merged into single bundle '{0}'", bundleMerge.BaseName);
                bundles = new Dictionary<string, ResourceBundle> {{bundleMerge.BaseName, bundleMerge}};
            }

            foreach (ResourceBundle bundle in bundles.Values)
            {
                JsonResources jsonResources = generateJsonResources(bundle, options);
                string baseFileName;
                string baseDir;
                if (!string.IsNullOrEmpty(options.OutputFile))
                {
                    baseFileName = Path.GetFileName(options.OutputFile);
                    baseDir = Path.GetDirectoryName(options.OutputFile);
                }
                else
                {
                    baseFileName = bundle.BaseName.ToLowerInvariant() + GetOutputFileExtension(options.OutputFormat);
                    baseDir = options.OutputFolder;
                }
                if (string.IsNullOrEmpty(baseDir))
                {
                    baseDir = Environment.CurrentDirectory;
                }

                logger.AddMsg(Severity.Trace, "Processing '{0}' bundle (contains {1} resx files)", bundle.BaseName,
                    bundle.Cultures.Count);
                string dirPath = options.OutputFormat == OutputFormat.i18next
                    ? Path.Combine(baseDir, options.FallbackCulture)
                    : baseDir;
                string outputPath = Path.Combine(dirPath, baseFileName);
                string jsonText = stringifyJson(jsonResources.BaseResources, options);
                writeOutput(outputPath, jsonText, options, logger);

                if (jsonResources.LocalizedResources.Count > 0)
                {
                    foreach (KeyValuePair<string, JObject> pair in jsonResources.LocalizedResources)
                    {
                        dirPath = Path.Combine(baseDir, pair.Key);
                        outputPath = Path.Combine(dirPath, baseFileName);
                        jsonText = stringifyJson(pair.Value, options);
                        writeOutput(outputPath, jsonText, options, logger);
                    }
                }
            }

            return logger;
        }
コード例 #25
0
 static string stringifyJson(IResxToJsonFormatter formatter, JObject json, ResxToJsonConverterOptions options)
 {
     return(formatter.GetFileContent(json, options));
 }
コード例 #26
0
 public virtual string GetFileContent(JObject json, ResxToJsonConverterOptions options)
 {
     return(json.ToString(Formatting.Indented));
 }
コード例 #27
0
 public virtual bool CheckOptions(ResxToJsonConverterOptions options, out string message)
 {
     message = string.Empty;
     return(true);
 }
コード例 #28
0
        public override string GetLanguageFileName(string baseFileName, CultureInfo cultureInfo, ResxToJsonConverterOptions options)
        {
            string cultureName = cultureInfo.Name;

            if (Equals(cultureInfo, CultureInfo.InvariantCulture))
            {
                cultureName = options.FallbackCulture;
            }

            string extension = Path.GetExtension(baseFileName);

            baseFileName = Path.ChangeExtension(baseFileName, cultureName + extension);
            return(baseFileName);
        }
コード例 #29
0
 public override string GetFileContent(JObject json, ResxToJsonConverterOptions options)
 {
     return("define(" + base.GetFileContent(json, options) + ");");
 }
コード例 #30
0
        public override JObject GetJsonResource(JObject jBaseValues, CultureInfo cultureInfo, ResourceBundle bundle, ResxToJsonConverterOptions options)
        {
            string cultureName = cultureInfo.Name;

            if (Equals(cultureInfo, CultureInfo.InvariantCulture))
            {
                cultureName = options.FallbackCulture;
            }

            var jRoot = new JObject();

            jRoot[cultureName] = jBaseValues;

            return(jRoot);
        }
コード例 #31
0
        static ResxToJsonConverterOptions getOptions(string[] args)
        {
            var options = new ResxToJsonConverterOptions();

            for (int i = 0; i < args.Length; i++)
            {
                string key = args[i];
                if (key == "-i" || key == "-input")
                {
                    if (args.Length == i + 1)
                    {
                        CrashAndBurn(ExitCode.InputArgumentMissing, "Value for option 'input' is missing");
                    }
                    options.Inputs.Add(args[i + 1]);
                    i++;
                    continue;
                }

                if (key == "-dir" || key == "-outputDir")
                {
                    if (args.Length == i + 1)
                    {
                        CrashAndBurn(ExitCode.InvalidOutputArgument, "Value for option 'outputDir' is missing");
                    }
                    options.OutputFolder = args[i + 1];
                    i++;
                    continue;
                }

                if (key == "-file" || key == "-outputFile")
                {
                    if (args.Length == i + 1)
                    {
                        CrashAndBurn(ExitCode.InvalidOutputArgument, "Value for option 'outputFile' is missing");
                        Console.WriteLine("ERROR: Value for option 'outputFile' is missing");
                        Environment.Exit(-2);
                    }
                    options.OutputFile = args[i + 1];
                    i++;
                    continue;
                }

                if (key == "-format" || key == "-outputFormat")
                {
                    if (args.Length == i + 1)
                    {
                        CrashAndBurn(ExitCode.OutputFormatArgumentMissing, "Value for option 'outputFormat' is missing");
                    }
                    OutputFormat format;
                    if (Enum.TryParse(args[i + 1], true, out format))
                    {
                        options.OutputFormat = format;
                    }
                    i++;
                    continue;
                }

                if (key == "-fallback" || key == "-fallbackCulture")
                {
                    if (args.Length == i + 1)
                    {
                        CrashAndBurn(ExitCode.FallbackArgumentMissing, "Value for option 'fallbackCulture' is missing");
                    }
                    options.FallbackCulture = args[i + 1];
                    i++;
                    continue;
                }

                if (key == "-c" || key == "-case")
                {
                    if (args.Length == i + 1)
                    {
                        CrashAndBurn(ExitCode.CaseArgumentMissing, "Value for option 'case' is missing");
                    }
                    JsonCasing casing;
                    if (Enum.TryParse(args[i + 1], true, out casing))
                    {
                        options.Casing = casing;
                    }
                    i++;
                    continue;
                }
                if (key == "-f" || key == "-force")
                {
                    options.Overwrite = OverwriteModes.Force;
                    continue;
                }
                if (key == "-r" || key == "-recursively")
                {
                    options.Recursive = true;
                    i++;
                    continue;
                }

                if (key == "-outputFileFormat")
                {
                    if (args.Length == i + 1)
                    {
                        CrashAndBurn(ExitCode.CaseArgumentMissing, "Value for option 'outputFileFormat' is missing");
                    }
                    options.OutputFileFormat = args[i + 1];
                    i++;
                    continue;
                }
            }
            return(options);
        }
コード例 #32
0
ファイル: Program.cs プロジェクト: peter-cmw/ResxToJson
        static ResxToJsonConverterOptions getOptions(string[] args)
        {
            var options = new ResxToJsonConverterOptions();
            for (int i = 0; i < args.Length; i++)
            {
                string key = args[i];
                if (key == "-i" || key == "-input")
                {
                    if (args.Length == i + 1)
                    {
                        CrashAndBurn(ExitCode.InputArgumentMissing, "Value for option 'input' is missing");
                    }
                    options.Inputs.Add(args[i + 1]);
                    i++;
                    continue;
                }

                if (key == "-dir" || key == "-outputDir")
                {
                    if (args.Length == i + 1)
                    {
                        CrashAndBurn(ExitCode.InvalidOutputArgument, "Value for option 'outputDir' is missing");
                    }
                    options.OutputFolder = args[i + 1];
                    i++;
                    continue;
                }

                if (key == "-file" || key == "-outputFile")
                {
                    if (args.Length == i + 1)
                    {
                        CrashAndBurn(ExitCode.InvalidOutputArgument, "Value for option 'outputFile' is missing");
                        Console.WriteLine("ERROR: Value for option 'outputFile' is missing");
                        Environment.Exit(-2);
                    }
                    options.OutputFile = args[i + 1];
                    i++;
                    continue;
                }

                if (key == "-format" || key == "-outputFormat")
                {
                    if (args.Length == i + 1)
                    {
                        CrashAndBurn(ExitCode.OutputFormatArgumentMissing, "Value for option 'outputFormat' is missing");
                    }
                    OutputFormat format;
                    if (Enum.TryParse(args[i + 1], true, out format))
                    {
                        options.OutputFormat = format;
                    }
                    i++;
                    continue;
                }

                if (key == "-fallback" || key == "-fallbackCulture")
                {
                    if (args.Length == i + 1)
                    {
                        CrashAndBurn(ExitCode.FallbackArgumentMissing, "Value for option 'fallbackCulture' is missing");
                    }
                    options.FallbackCulture = args[i + 1];
                    i++;
                    continue;
                }

                if (key == "-c" || key == "-case")
                {
                    if (args.Length == i + 1)
                    {
                        CrashAndBurn(ExitCode.CaseArgumentMissing, "Value for option 'case' is missing");
                    }
                    JsonCasing casing;
                    if (Enum.TryParse(args[i + 1], true, out casing))
                    {
                        options.Casing = casing;
                    }
                    i++;
                    continue;
                }
                if (key == "-f" || key == "-force")
                {
                    options.Overwrite = OverwriteModes.Force;
                    continue;
                }
                if (key == "-r" || key == "-recursively")
                {
                    options.Recursive = true;
                    continue;
                }
            }
            return options;
        }