Beispiel #1
0
        /// <summary>
        /// Flushes current Data. If the file is closed, it means write data to disk; otherwise corresponding file buffer is modified.
        /// </summary>
        public void Flush()
        {
            if (!dataChangedInBatchMode && IsInBatchMode)
            {
                return;
            }
            string path = InternalProjectItem.GetFullPath();

            if (RDTManager.IsFileOpen(path))
            {
                VLDocumentViewsManager.SaveDataToBuffer(data, path); // modify in-memory buffer of this file
            }
            else
            {
                // write data to disk
                ResXResourceWriter writer = null;
                try {
                    writer          = new ResXResourceWriter(path);
                    writer.BasePath = Path.GetDirectoryName(path);

                    foreach (var pair in data)
                    {
                        writer.AddResource(pair.Value);
                    }
                    writer.Generate();
                } finally {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                }
            }

            // regenerate designer item
            if (DesignerItem != null)
            {
                RDTManager.SilentlyModifyFile(DesignerItem.GetFullPath(), (string p) => {
                    RunCustomTool();
                });
            }
        }
Beispiel #2
0
        /// <summary>
        /// Attempts to obtain class and namespace name of the designer file (if it exists)
        /// </summary>
        /// <param name="neutralItems">List of relevant culture-neutral ResX files</param>
        public void ResolveNamespaceClass(List <ResXProjectItem> neutralItems)
        {
            if (neutralItems == null)
            {
                throw new ArgumentNullException("neutralItems");
            }
            Class     = null;
            Namespace = null;

            // if this file is culture-specific it's designer file is empty - we need to find designer item if corresponding culture-neutral file
            if (IsCultureSpecific())
            {
                DesignerItem = GetNeutralDesignerItem(neutralItems);
            }

            if (DesignerItem != null)    // designer file is set
            {
                if (!File.Exists(DesignerItem.GetFullPath()))
                {
                    RunCustomTool();
                }

                // select first namespace in the designer file
                CodeElement nmspcElemet = null;
                bool        fileOpened;
                var         rootCodeElements = DesignerItem.GetCodeModel(true, false, out fileOpened).CodeElements;

                foreach (CodeElement element in rootCodeElements)
                {
                    if (element.Kind == vsCMElement.vsCMElementNamespace)
                    {
                        Namespace   = element.FullName;
                        nmspcElemet = element;
                        break;
                    }
                }

                var nestedCodeElements = nmspcElemet == null ? rootCodeElements : nmspcElemet.Children;
                // select first class/module in the namespace
                if (nestedCodeElements != null)
                {
                    foreach (CodeElement child in nestedCodeElements)
                    {
                        if (child.Kind == vsCMElement.vsCMElementClass || child.Kind == vsCMElement.vsCMElementModule)
                        {
                            Class = child.Name;
                            break;
                        }
                    }
                }

                if (Namespace == null && DesignerLanguage == LANGUAGE.VB)
                {
                    Namespace = InternalProjectItem.ContainingProject.GetRootNamespace();
                }
            }
            else     // designer file doesn't exist

            // if ResX files is contained in ASP .NET website project, designer files are implicit
            {
                if (InternalProjectItem.ContainingProject != null && InternalProjectItem.ContainingProject.Kind.ToUpper() == StringConstants.WebSiteProject)
                {
                    string relative = InternalProjectItem.GetRelativeURL();

                    // file must be located in App_GlobalResources folder
                    if (!string.IsNullOrEmpty(relative) && relative.StartsWith(StringConstants.GlobalWebSiteResourcesFolder))
                    {
                        Class = Path.GetFileNameWithoutExtension(InternalProjectItem.GetFullPath()); // take file name as a class name
                        if (IsCultureSpecific())                                                     // strip culture-specific info
                        {
                            Class = GetCultureNeutralName();
                            Class = Class.Substring(0, Class.IndexOf('.'));
                        }
                        Namespace = StringConstants.GlobalWebSiteResourcesNamespace; // namespace is hard-coded as "Resources"
                    }
                    else
                    {
                        Class     = null;
                        Namespace = null;
                    }
                }
            }
        }