5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2016 Xamarin Inc (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Generic;
32 using System.Resources;
33 using System.ComponentModel.Design;
40 public bool ShowHelp { get; set; }
41 public string OutputFile { get; set; }
44 public static int Main (string[] args)
46 var options = new CmdOptions ();
48 var p = new OptionSet () {
49 { "o|out=", "Specifies output file name",
50 v => options.OutputFile = v },
51 { "h|help", "Display available options",
52 v => options.ShowHelp = v != null },
57 extra = p.Parse (args);
58 } catch (OptionException e) {
59 Console.WriteLine (e.Message);
60 Console.WriteLine ("Try 'resx2sr -help' for more information.");
64 if (options.ShowHelp) {
69 if (extra.Count != 1) {
74 var resxStrings = new List<Tuple<string, string, string>> ();
75 if (!LoadStrings (resxStrings, extra))
78 GenerateFile (resxStrings, options);
83 static void ShowHelp (OptionSet p)
85 Console.WriteLine ("Usage: resx2sr [options] input-files");
86 Console.WriteLine ("Generates C# file with string constants from resource file");
88 Console.WriteLine ("Options:");
89 p.WriteOptionDescriptions (Console.Out);
92 static void GenerateFile (List<Tuple<string, string, string>> txtStrings, CmdOptions options)
94 // var outputFile = options.OutputFile ?? "SR.cs";
96 using (var str = options.OutputFile == null ? Console.Out : new StreamWriter (options.OutputFile)) {
98 str.WriteLine ("// This file was generated by resx2sr tool");
102 str.WriteLine ("partial class SR");
104 foreach (var entry in txtStrings) {
105 str.Write ($"\tpublic const string {entry.Item1} = \"{ToCSharpString (entry.Item2)}\";");
107 if (!string.IsNullOrEmpty (entry.Item3))
108 str.Write (" // {entry.Item3}");
117 static string ToCSharpString (string str)
119 str = str.Replace ("\n", "\\n");
121 return str.Replace ("\\", "\\\\").Replace ("\"", "\\\"");
124 static bool LoadStrings (List<Tuple<string, string, string>> resourcesStrings, List<string> files)
126 var keys = new Dictionary<string, string> ();
127 foreach (var fileName in files) {
128 if (!File.Exists (fileName)) {
129 Console.Error.WriteLine ($"Error reading resource file '{fileName}'");
133 var rr = new ResXResourceReader (fileName);
134 rr.UseResXDataNodes = true;
135 var dict = rr.GetEnumerator ();
136 while (dict.MoveNext ()) {
137 var node = (ResXDataNode)dict.Value;
139 resourcesStrings.Add (Tuple.Create (node.Name, (string) node.GetValue ((ITypeResolutionService)null), node.Comment));