private static void AssertParse(string source, string expectedClassName)
        {
            ExtractedClassName className = CSharpParserUtilities.GetFirstClassNameFullyQualified
                                           (
                StreamHelpers.StringToStream(source)
                                           );

            Assert.Equal(expectedClassName, className.Name);
        }
Esempio n. 2
0
        /*
         * Method:  AssertParse
         *
         * Parse 'source' as VB source code and get the first class name fully-qualified
         * with namespace information. That class name must match the expected class name.
         */
        private static void AssertParse(string source, string expectedClassName)
        {
            source = source.Replace("&qt", "\"");

            ExtractedClassName className = VisualBasicParserUtilities.GetFirstClassNameFullyQualified
                                           (
                StreamHelpers.StringToStream(source)
                                           );

            Assert.Equal(expectedClassName, className.Name);
        }
Esempio n. 3
0
        /// <summary>
        /// Utility function for creating a X#-style manifest name from
        /// a resource name. Note that this function is inspired by the similar C# function
        /// </summary>
        /// <param name="fileName">The file name of the dependent (usually a .resx)</param>
        /// <param name="linkFileName">The file name of the dependent (usually a .resx)</param>
        /// <param name="rootNamespace">The root namespace (usually from the project file). May be null</param>
        /// <param name="prependCultureAsDirectory">should the culture name be prepended to the manifest name as a path</param>
        /// <param name="dependentUponFileName">The file name of the parent of this dependency (usually a .cs file). May be null</param>
        /// <param name="culture">The override culture of this resource, if any</param>
        /// <param name="binaryStream">File contents binary stream, may be null</param>
        /// <param name="log">Task's TaskLoggingHelper, for logging warnings or errors</param>
        /// <returns>Returns the manifest name</returns>
        internal static string CreateManifestNameImpl
        (
            string fileName,
            string linkFileName,
            bool prependCultureAsDirectory,
            string rootNamespace,
            string dependentUponFileName,
            string culture,
            Stream binaryStream,
            TaskLoggingHelper log
        )
        {
            string embeddedFileName = linkFileName;

            // Use the link file name if there is one, otherwise, fall back to file name.

            if ((embeddedFileName == null) || (embeddedFileName.Length == 0))
            {
                embeddedFileName = fileName;
            }
            Culture.ItemCultureInfo info = Culture.GetItemCultureInfo(embeddedFileName, dependentUponFileName);
            // If the item has a culture override, respect that.
            if (!string.IsNullOrEmpty(culture))
            {
                info.culture = culture;
            }
            StringBuilder manifestName = new StringBuilder();

            if (binaryStream != null)
            {
                // Resource depends on a form. Now, get the form's class name fully
                // qualified with a namespace.

                ExtractedClassName result = CreateXSharpManifestResourceName.GetFirstClassNameFullyQualified(fileName, binaryStream, log);
                if (result.IsInsideConditionalBlock && log != null)
                {
                    log.LogWarningWithCodeFromResources("CreateManifestResourceName.DefinitionFoundWithinConditionalDirective", dependentUponFileName, embeddedFileName);
                }
                if ((result.Name != null) && (result.Name.Length > 0))
                {
                    manifestName.Append(result.Name);
                    if ((info.culture != null) && (info.culture.Length > 0))
                    {
                        manifestName.Append(".").Append(info.culture);
                    }
                }
            }
            // If there's no manifest name at this point, then fall back to using the
            // RootNamespace+Filename_with_slashes_converted_to_dots

            if (manifestName.Length == 0)
            {
                // If Rootnamespace was null, then it wasn't set from the project resourceFile.
                // Empty namespaces are allowed.
                if ((rootNamespace != null) && (rootNamespace.Length > 0))
                {
                    manifestName.Append(rootNamespace).Append(".");
                }
                // Replace spaces in the directory name with underscores. Needed for compatibility with Everett.
                // Note that spaces in the file name itself are preserved.

                string everettCompatibleDirectoryName = CreateManifestResourceName.MakeValidEverettIdentifier(Path.GetDirectoryName(info.cultureNeutralFilename));
                // only strip extension for .resx and .restext files
                string sourceExtension = Path.GetExtension(info.cultureNeutralFilename);
                if (
                    (0 == String.Compare(sourceExtension, ".resx", StringComparison.OrdinalIgnoreCase))
                    ||
                    (0 == String.Compare(sourceExtension, ".restext", StringComparison.OrdinalIgnoreCase))
                    ||
                    (0 == String.Compare(sourceExtension, ".resources", StringComparison.OrdinalIgnoreCase))
                    )
                {
                    manifestName.Append(Path.Combine(everettCompatibleDirectoryName, Path.GetFileNameWithoutExtension(info.cultureNeutralFilename)));
                    // Replace all '\' with '.'

                    manifestName.Replace(Path.DirectorySeparatorChar, '.');
                    manifestName.Replace(Path.AltDirectorySeparatorChar, '.');
                    // Append the culture if there is one.
                    if ((info.culture != null) && (info.culture.Length > 0))
                    {
                        manifestName.Append(".").Append(info.culture);
                    }
                    // If the original extension was .resources, add it back
                    if (string.Equals(sourceExtension, ".resources", StringComparison.OrdinalIgnoreCase))
                    {
                        manifestName.Append(sourceExtension);
                    }
                }
                else
                {
                    manifestName.Append(Path.Combine(everettCompatibleDirectoryName, Path.GetFileName(info.cultureNeutralFilename)));
                    // Replace all '\' with '.'
                    manifestName.Replace(Path.DirectorySeparatorChar, '.');
                    manifestName.Replace(Path.AltDirectorySeparatorChar, '.');
                    // Prepend the culture as a subdirectory if there is one.
                    if (prependCultureAsDirectory)
                    {
                        if (info.culture != null && info.culture.Length > 0)
                        {
                            manifestName.Insert(0, Path.DirectorySeparatorChar);
                            manifestName.Insert(0, info.culture);
                        }
                    }
                }
            }
            return(manifestName.ToString());
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName">The file name of the dependent (usually a .resx)</param>
        /// <param name="binaryStream">File contents binary stream, may be null</param>
        /// <param name="log">Task's TaskLoggingHelper, for logging warnings or errors</param>
        /// <returns></returns>
        private static ExtractedClassName GetFirstClassNameFullyQualified(string fileName, Stream binaryStream, TaskLoggingHelper log)
        {
            Match              m = null;
            string             currentNamespace = "";
            ExtractedClassName name             = new ExtractedClassName();
            int          conditionalDepth       = 0;
            StreamReader reader     = new StreamReader(binaryStream, true); // let the reader determine the encoding
            var          namespaces = new Stack <string>();
            var          contents   = reader.ReadToEnd();

            if (contents.Contains("/*"))
            {
                contents = stripMultiLineComments(contents);
            }
            var lines = contents.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            foreach (string l in lines)
            {
                var line = l;
                if (line.Contains("//"))
                {
                    // find the line comment mark. Make sure that we do not check inside string literals
                    line = stripComments(line);
                }
                // Does the line contain "CLASS"
                m = classRx.Match(line);
                if (m.Success)
                {
                    name.Name = currentNamespace + m.Groups[1].Value;
                    name.IsInsideConditionalBlock = conditionalDepth > 0;
                    return(name);
                }
                // Does the line contain "BEGIN NAMESPACE"
                m = namespaceBeginRx.Match(line);
                if (m.Success)
                {
                    namespaces.Push(currentNamespace);
                    var ns = m.Groups[1].Value + ".";
                    if (ns.StartsWith("global::", StringComparison.OrdinalIgnoreCase))
                    {
                        ns = ns.Substring(8);
                    }
                    currentNamespace = currentNamespace + ns;
                }
                // Does the line contain "END NAMESPACE"
                else if (namespaceEndRx.Match(line).Success)
                {
                    if (namespaces.Count > 0)
                    {
                        currentNamespace = namespaces.Pop();
                    }
                    else
                    {
                        object[] messageArgs = new object[] { fileName };
                        log.LogError("CreateXSharpManifestResourceName: found 'END NAMESPACE' with no matching 'BEGIN NAMESPACE' in '{0}'", messageArgs);
                    }
                }
                // Does the line contain "#IFDEF"
                else if (ifdefRx.Match(line).Success)
                {
                    conditionalDepth++;
                }
                // Does the line contain "#ENDIF"
                else if (endifRx.Match(line).Success)
                {
                    conditionalDepth--;
                }
            }
            return(name);
        }