Example #1
0
        // ensure that the resource manager class exists, or that it's coplete
        public static void EnsureResourceManager(Project project = null)
        {
            var managerFile = Resourcer.GetResourcesManagerPath();

            if (File.Exists(managerFile)) // if the file exists, ensure it has the required methods
            {
                var source = File.ReadAllText(managerFile, Encoding.UTF8);

                var rc1 = source.Contains(resCheck1);
                var rc2 = source.Contains(resCheck2);
                var rc3 = source.Contains(resCheck3);
                if (!rc1 || !rc2 || !rc3) // seems like it doesn't have everything!
                {
                    var bracketRegex = new Regex(Settings.ResourcesManagerName + @"\s*{");
                    var match        = bracketRegex.Match(source); // the class may exist

                    var newFile = new StringBuilder();
                    if (match.Success) // there's a match, append only the missing code to the existing class
                    {
                        var bracket = match.Index + match.Length;
                        newFile.Append(source.Substring(0, bracket));

                        if (!rc1)
                        {
                            newFile.AppendLine(resPart1);
                        }
                        if (!rc2)
                        {
                            newFile.AppendLine(resPart2);
                        }
                        if (!rc3)
                        {
                            newFile.AppendLine(resPart3);
                        }

                        newFile.Append(source.Substring(bracket));
                    }
                    else // no match (no resourcemanager class), append the entire code
                    {
                        if (!source.Contains(resManagerUsing))
                        {
                            newFile.AppendLine(resManagerUsing);
                        }

                        newFile.AppendLine(source);
                        newFile.AppendLine(getResManagerSource());
                    }

                    File.WriteAllText(managerFile, newFile.ToString());
                }
            }
            else // file doesn't exist, just create it
            {
                createResourceManagerFile(managerFile);
            }

            if (project == null)
            {
                project = VsUtils.GetCurrentProject();
            }
            VsUtils.AddFileIfUnexisting(project, managerFile);
        }
Example #2
0
        // ensure that the App.xaml.cs contains SelectCulture method
        public static void EnsureAppXamlCs(Project project = null)
        {
            if (project == null)
            {
                project = VsUtils.GetCurrentProject();
            }

            var appXamlCs = VsUtils.GetFullPath(project, "App.xaml.cs");

            if (string.IsNullOrEmpty(appXamlCs))
            {
                return;                                  // should never happen...
            }
            var source = File.ReadAllText(appXamlCs, Encoding.UTF8);

            var startRegex = new Regex(@"class\s*App\s*:\s*Application\s*{");
            var start      = startRegex.Match(source);

            if (!start.Success)
            {
                return;                 // should never happen either!...
            }
            var startIdx = start.Index + start.Length;

            if (!source.Contains(selectCultureMethodCheck)) // make sure to add the method
            {
                var sb = new StringBuilder();

                foreach (var rusing in requiredAppUsings)
                {
                    if (!source.Contains(rusing))
                    {
                        sb.AppendLine(rusing);
                    }
                }

                appendAfterStart(sb, startIdx, ref source, selectCultureMethod);
                File.WriteAllText(appXamlCs, source);
            }

            if (Settings.SetLocaleOnStartup && !source.Contains(selectCultureMethodCall))
            {
                var constructorMatch = new Regex(@"public\s+App\s*\(\)\s*{");
                var constructor      = constructorMatch.Match(source);

                var sb = new StringBuilder();
                if (constructor.Success) // append only the method call
                {
                    var constructorIdx = constructor.Index + constructor.Length;

                    // indentation = 3 indent levels * 4 spaces each
                    appendAfterStart(sb, constructorIdx, ref source,
                                     new string(' ', 3 * 4) + selectCultureMethodCall);

                    File.WriteAllText(appXamlCs, source);
                }
                else // append a whole new constructor
                {
                    appendAfterStart(sb, startIdx, ref source, selectCultureConstructor);
                    File.WriteAllText(appXamlCs, source);
                }
            }
            else if (source.Contains(selectCultureMethodCall))
            {
                if (source.Contains(selectCultureMethodCall))
                {
                    source = source.Replace(selectCultureMethodCall, string.Empty);
                    File.WriteAllText(appXamlCs, source);
                }
            }
            if (!source.Contains(initializeComponent))
            {
                var constructorMatch = new Regex(@"public\s+App\s*\(\)\s*{");
                var constructor      = constructorMatch.Match(source);

                var sb = new StringBuilder();
                if (constructor.Success) // append only the method call
                {
                    var constructorIdx = constructor.Index + constructor.Length;

                    // indentation = 3 indent levels * 4 spaces each
                    appendAfterStart(sb, constructorIdx, ref source,
                                     new string(' ', 3 * 4) + initializeComponent);

                    File.WriteAllText(appXamlCs, source);
                }
                else // append a whole new constructor
                {
                    appendAfterStart(sb, startIdx, ref source, selectCultureConstructor);
                    File.WriteAllText(appXamlCs, source);
                }
            }
        }
Example #3
0
 // get the resources folder path
 public static string GetResourcesFolderPath()
 {
     return(Path.Combine(VsUtils.GetCurrentProjectPath(), Settings.ResourcesFolderName));
 }