Beispiel #1
0
        // update a string key to a new key name
        public static void UpdateStringKey(string oldKey, string newKey)
        {
            Ensurer.EnsureEverything();

            updateStringKey(oldKey, newKey, string.Empty);
            foreach (var locale in GetStringsResLocales())
            {
                updateStringKey(oldKey, newKey, locale);
            }

            var oldCs   = GetStringCall(oldKey, false);
            var oldXaml = GetStringCall(oldKey, true);
            var newCs   = GetStringCall(newKey, false);
            var newXaml = GetStringCall(newKey, true);

            foreach (var file in VsUtils.GetProjectFiles(VsUtils.GetCurrentProject()))
            {
                if (file.EndsWith(extensionCs))
                {
                    updateSource(file, oldCs, newCs);
                }

                else if (file.EndsWith(extensionXaml))
                {
                    updateSource(file, oldXaml, newXaml);
                }
            }
        }
Beispiel #2
0
 public static void RenameResFolder(string oldName, string newName)
 {
     if (!VsUtils.RenameFolder(VsUtils.GetCurrentProject(), oldName, newName))
     {
         MessageBox.Show("The resources folder could not be renamed. It will have to be renamed manually",
                         "Rename failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
        // executed before the menu is open
        void menuCommand_BeforeQueryStatus(object sender, EventArgs e)
        {
            if (sender == null)
            {
                return;
            }

            // get the menu that fired the event
            var menuCommand = (OleMenuCommand)sender;

            menuCommand.Visible = menuCommand.Enabled = VsUtils.GetCurrentProject() != null;
        }
Beispiel #4
0
        // create the resources file in the desired path
        public static void CreateXamlRes(string path, bool openInEditor)
        {
            StringsXMLEditor.CreateDocument(path);
            VsUtils.AddFileIfUnexisting(VsUtils.GetCurrentProject(), path);
            Ensurer.EnsureAppXaml();

            if (openInEditor)
            {
                VsUtils.OpenInEditor(path);
            }

            Ensurer.EnsureEverything();
        }
Beispiel #5
0
        // add all the .xaml dictionaries to the App.xaml resources
        public static void EnsureAppXaml(Project project = null)
        {
            try
            {
                if (project == null)
                {
                    project = VsUtils.GetCurrentProject();
                }

                var appXaml = VsUtils.GetFullPath(project, "App.xaml");
                if (string.IsNullOrEmpty(appXaml))
                {
                    return;                                // should never happen...
                }
                var doc = XDocument.Load(appXaml);

                var appResources       = getNode(doc.Root, "Application.Resources");
                var resDictionary      = getNode(appResources, "ResourceDictionary");
                var mergedDictionaries = getNode(resDictionary, "ResourceDictionary.MergedDictionaries");

                var resFolder = Resourcer.GetResourcesFolderPath();
                if (Directory.Exists(resFolder))
                {
                    // clear all resources
                    mergedDictionaries.RemoveAll();

                    // add them again
                    foreach (var file in VsUtils.GetProjectItemsInFolder(project, resFolder))
                    {
                        // perhaps it's not a dictionary (.xaml) file
                        if (!file.Name.EndsWith(".xaml"))
                        {
                            continue;
                        }
                        // perhaps it is a folder
                        if (!File.Exists(file.Properties.Item("FullPath").Value.ToString()))
                        {
                            continue;
                        }

                        mergedDictionaries.Add(new XElement(
                                                   mergedDictionaries.Name.Namespace + "ResourceDictionary",
                                                   new XAttribute("Source", Settings.ResourcesFolderName + "/" + file.Name)
                                                   ));
                    }
                }

                StringsXMLEditor.SaveDocument(doc, appXaml);
            }
            catch { /* nobody likes errors (which shouldn't happen) :( */ }
        }
Beispiel #6
0
        // delete a strings resource provided a locale
        public static void DeleteStrings(string locale = null)
        {
            var resFile = Path.Combine(GetResourcesFolderPath(), GetStringsResName(locale));

            if (File.Exists(resFile))
            {
                try
                {
                    VsUtils.DeleteFile(VsUtils.GetCurrentProject(), resFile);
                    Ensurer.EnsureAppXaml();
                }
                catch { }
            }
        }
Beispiel #7
0
        // ensure the strings.xml file exists
        public static void EnsureStringResources(Project project = null)
        {
            if (project == null)
            {
                project = VsUtils.GetCurrentProject();
            }
            var resFile = Resourcer.GetResourcePath(Resourcer.GetStringsResName());

            if (!File.Exists(resFile)) // if the file doesn't exist, create it
            {
                StringsXMLEditor.CreateDocument(resFile);
            }

            VsUtils.AddFileIfUnexisting(VsUtils.GetCurrentProject(), resFile);
        }
Beispiel #8
0
        public static void EnsureResourcesFolder(Project project = null)
        {
            if (project == null)
            {
                project = VsUtils.GetCurrentProject();
            }
            var resFolder = Resourcer.GetResourcesFolderPath();

            if (!Directory.Exists(resFolder)) // if the directory doesn't exist, create it
            {
                Directory.CreateDirectory(resFolder);
            }

            VsUtils.AddDirectoryIfUnexisting(project, resFolder);
        }
Beispiel #9
0
        // are the current settings the old ones (i.e. we reset the settings)?
        void checkChanges(bool currentOld = false)
        {
            // no loaded project, no need to check changes
            if (VsUtils.GetCurrentProject() == null)
            {
                return;
            }

            Settings.UseStaticResourceXAML = !useDynamicXamlCB.Checked;

            // if the settings changed and they're not empty, update them
            if (Settings.SetLocaleOnStartup != selectLocaleStartCB.Checked)
            {
                Ensurer.EnsureAppXamlCs();
            }

            if (Settings.ResourcesFolderName != resFolderNameTB.Text &&
                resFolderNameTB.Text.Length > 0)
            {
                if (currentOld) // change the order depending on which are the old settings
                {
                    Ensurer.RenameResFolder(resFolderNameTB.Text, Settings.ResourcesFolderName);
                }
                else
                {
                    Ensurer.RenameResFolder(Settings.ResourcesFolderName, resFolderNameTB.Text);
                }
            }

            if (Settings.ResourcesManagerName != resManNameTB.Text &&
                resManNameTB.Text.Length > 0)
            {
                if (currentOld)
                {
                    Ensurer.RenameResManager(resManNameTB.Text, Settings.ResourcesManagerName);
                }
                else
                {
                    Ensurer.RenameResManager(Settings.ResourcesManagerName, resManNameTB.Text);
                }
            }
        }
Beispiel #10
0
        public static void ShowSingle()
        {
            var p = VsUtils.GetCurrentProject();

            if (p == null) // either warning box no open project or show settings directly (prompting a warning perhaps)
            {
                SettingsForm.ShowSingle("Could not open the strings resource manager due to no project is currently open");
                return;
            }
            if (form == null)
            {
                form             = new ManageStringsForm();
                form.FormClosed += (s, e) => form = null;
                form.Show();
            }
            else
            {
                form.Activate();
            }
        }
Beispiel #11
0
        // TODO use Code.cs and CodeLine.cs

        #endregion

        #region Everything

        // ensure everything and save the project
        public static void EnsureEverything()
        {
            var project = VsUtils.GetCurrentProject();

            // omit these checks, perform them manually
            //EnsureResourcesFolder(project);
            //EnsureStringResources(project);

            // only if res dir contains resources
            if (Resourcer.AnyResource())
            {
                EnsureResourceManager(project);
                EnsureAppXaml(project);
            }

            // only if there are translations added
            if (Resourcer.AnyLocale())
            {
                EnsureAppXamlCs(project);
            }

            VsUtils.SaveProject(VsUtils.GetCurrentProject());
        }
Beispiel #12
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);
                }
            }
        }
Beispiel #13
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);
        }