Exemple #1
0
        public override bool Execute()
        {
            this.assignedFiles = new TaskItem[this.Files.Length];
            this.cultureNeutralAssignedFiles = new TaskItem[this.Files.Length];
            ArrayList list  = new ArrayList();
            ArrayList list2 = new ArrayList();
            bool      flag  = true;

            for (int i = 0; i < this.Files.Length; i++)
            {
                try
                {
                    this.AssignedFiles[i] = new TaskItem(this.Files[i]);
                    string metadata = this.AssignedFiles[i].GetMetadata("DependentUpon");
                    Culture.ItemCultureInfo itemCultureInfo = Culture.GetItemCultureInfo(this.AssignedFiles[i].ItemSpec, metadata);
                    if ((itemCultureInfo.culture != null) && (itemCultureInfo.culture.Length > 0))
                    {
                        this.AssignedFiles[i].SetMetadata("Culture", itemCultureInfo.culture);
                        this.AssignedFiles[i].SetMetadata("WithCulture", "true");
                        list.Add(this.AssignedFiles[i]);
                    }
                    else
                    {
                        list2.Add(this.AssignedFiles[i]);
                        this.AssignedFiles[i].SetMetadata("WithCulture", "false");
                    }
                    this.CultureNeutralAssignedFiles[i]          = new TaskItem(this.AssignedFiles[i]);
                    this.CultureNeutralAssignedFiles[i].ItemSpec = itemCultureInfo.cultureNeutralFilename;
                    base.Log.LogMessageFromResources(MessageImportance.Low, "AssignCulture.Comment", new object[] { this.AssignedFiles[i].GetMetadata("Culture"), this.AssignedFiles[i].ItemSpec });
                }
                catch (ArgumentException exception)
                {
                    base.Log.LogErrorWithCodeFromResources("AssignCulture.CannotExtractCulture", new object[] { this.Files[i].ItemSpec, exception.Message });
                    flag = false;
                }
            }
            this.assignedFilesWithCulture   = (ITaskItem[])list.ToArray(typeof(ITaskItem));
            this.assignedFilesWithNoCulture = (ITaskItem[])list2.ToArray(typeof(ITaskItem));
            return(flag);
        }
        internal static string CreateManifestNameImpl
        (
            string fileName,
            string linkFileName,
            bool prependCultureAsDirectory, // true by default
            string rootNamespace,           // May be null
            string dependentUponFileName,   // May be null
            string culture,
            Stream binaryStream,            // File contents binary stream, may be null
            TaskLoggingHelper log,
            bool treatAsCultureNeutral = false
        )
        {
            // Use the link file name if there is one, otherwise, fall back to file name.
            string embeddedFileName = linkFileName;

            if (string.IsNullOrEmpty(embeddedFileName))
            {
                embeddedFileName = fileName;
            }

            Culture.ItemCultureInfo info = Culture.GetItemCultureInfo(embeddedFileName, dependentUponFileName, treatAsCultureNeutral);

            // If the item has a culture override, respect that.
            if (!string.IsNullOrEmpty(culture))
            {
                info.culture = culture;
            }

            var manifestName = StringBuilderCache.Acquire();

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

                if (result.IsInsideConditionalBlock)
                {
                    log?.LogWarningWithCodeFromResources("CreateManifestResourceName.DefinitionFoundWithinConditionalDirective", dependentUponFileName, embeddedFileName);
                }

                if (!string.IsNullOrEmpty(result.Name))
                {
                    if (!string.IsNullOrEmpty(rootNamespace))
                    {
                        manifestName.Append(rootNamespace).Append('.');
                    }

                    manifestName.Append(result.Name);


                    // Append the culture if there is one.
                    if (!string.IsNullOrEmpty(info.culture))
                    {
                        manifestName.Append('.').Append(info.culture);
                    }
                }
            }

            // If there's no manifest name at this point, then fall back to using the
            // RootNamespace+Base file name
            if (manifestName.Length == 0)
            {
                // If Rootnamespace was null, then it wasn't set from the project resourceFile.
                // Empty namespaces are allowed.
                if (!string.IsNullOrEmpty(rootNamespace))
                {
                    manifestName.Append(rootNamespace).Append('.');
                }

                // only strip extension for .resx and .restext files
                string sourceExtension = Path.GetExtension(info.cultureNeutralFilename);
                if (
                    string.Equals(sourceExtension, resxFileExtension, StringComparison.OrdinalIgnoreCase)
                    ||
                    string.Equals(sourceExtension, restextFileExtension, StringComparison.OrdinalIgnoreCase)
                    ||
                    string.Equals(sourceExtension, resourcesFileExtension, StringComparison.OrdinalIgnoreCase)
                    )
                {
                    manifestName.Append(Path.GetFileNameWithoutExtension(info.cultureNeutralFilename));

                    // Append the culture if there is one.
                    if (!string.IsNullOrEmpty(info.culture))
                    {
                        manifestName.Append('.').Append(info.culture);
                    }

                    // If the original extension was .resources, add it back
                    if (string.Equals(sourceExtension, resourcesFileExtension, StringComparison.OrdinalIgnoreCase))
                    {
                        manifestName.Append(sourceExtension);
                    }
                }
                else
                {
                    manifestName.Append(Path.GetFileName(info.cultureNeutralFilename));

                    if (prependCultureAsDirectory)
                    {
                        // Prepend the culture as a subdirectory if there is one.
                        if (!string.IsNullOrEmpty(info.culture))
                        {
                            manifestName.Insert(0, Path.DirectorySeparatorChar);
                            manifestName.Insert(0, info.culture);
                        }
                    }
                }
            }

            return(StringBuilderCache.GetStringAndRelease(manifestName));
        }
        /// <summary>
        /// Utility function for creating a C#-style manifest name from
        /// a resource name. Note that this function attempts to emulate the
        /// Everret implementation of this code which can be found by searching for
        /// ComputeNonWFCResourceName() or ComputeWFCResourceName() in
        /// \vsproject\langproj\langbldmgrsite.cpp
        /// </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, // true by default
            string rootNamespace,           // May be null
            string dependentUponFileName,   // May be null
            string culture,                 // may be null
            Stream binaryStream,            // File contents binary stream, may be null
            TaskLoggingHelper log
        )
        {
            // Use the link file name if there is one, otherwise, fall back to file name.
            string embeddedFileName = FileUtilities.FixFilePath(linkFileName);

            if (embeddedFileName == null || embeddedFileName.Length == 0)
            {
                embeddedFileName = FileUtilities.FixFilePath(fileName);
            }

            dependentUponFileName = FileUtilities.FixFilePath(dependentUponFileName);
            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 = CSharpParserUtilities.GetFirstClassNameFullyQualified(binaryStream);

                if (result.IsInsideConditionalBlock && log != null)
                {
                    log.LogWarningWithCodeFromResources("CreateManifestResourceName.DefinitionFoundWithinConditionalDirective", dependentUponFileName, embeddedFileName);
                }

                if (result.Name != null && result.Name.Length > 0)
                {
                    manifestName.Append(result.Name);

                    // Append the culture if there is one.
                    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, '.');

                    if (prependCultureAsDirectory)
                    {
                        // Prepend the culture as a subdirectory if there is one.
                        if (info.culture != null && info.culture.Length > 0)
                        {
                            manifestName.Insert(0, Path.DirectorySeparatorChar);
                            manifestName.Insert(0, info.culture);
                        }
                    }
                }
            }

            return(manifestName.ToString());
        }
Exemple #4
0
        public override bool Execute()
        {
            AssignedFiles = new ITaskItem[Files.Length];
            CultureNeutralAssignedFiles = new ITaskItem[Files.Length];
            var cultureList   = new List <ITaskItem>();
            var noCultureList = new List <ITaskItem>();

            bool retValue = true;

            for (int i = 0; i < Files.Length; ++i)
            {
                try
                {
                    AssignedFiles[i] = new TaskItem(Files[i]);

                    string dependentUpon         = AssignedFiles[i].GetMetadata(ItemMetadataNames.dependentUpon);
                    Culture.ItemCultureInfo info = Culture.GetItemCultureInfo
                                                   (
                        AssignedFiles[i].ItemSpec,
                        dependentUpon,
                        // If 'WithCulture' is explicitly set to false, treat as 'culture-neutral' and keep the original name of the resource.
                        // https://github.com/dotnet/msbuild/issues/3064
                        AssignedFiles[i].GetMetadata("WithCulture").Equals("false", StringComparison.OrdinalIgnoreCase)
                                                   );

                    if (!string.IsNullOrEmpty(info.culture))
                    {
                        AssignedFiles[i].SetMetadata("Culture", info.culture);
                        AssignedFiles[i].SetMetadata("WithCulture", "true");
                        cultureList.Add(AssignedFiles[i]);
                    }
                    else
                    {
                        noCultureList.Add(AssignedFiles[i]);
                        AssignedFiles[i].SetMetadata("WithCulture", "false");
                    }

                    CultureNeutralAssignedFiles[i] =
                        new TaskItem(AssignedFiles[i])
                    {
                        ItemSpec = info.cultureNeutralFilename
                    };

                    Log.LogMessageFromResources
                    (
                        MessageImportance.Low,
                        "AssignCulture.Comment",
                        AssignedFiles[i].GetMetadata("Culture"),
                        AssignedFiles[i].ItemSpec
                    );
                }
                catch (ArgumentException e)
                {
                    Log.LogErrorWithCodeFromResources("AssignCulture.CannotExtractCulture", Files[i].ItemSpec, e.Message);
                    retValue = false;
                }
#if DEBUG
                catch (Exception e)
                {
                    Debug.Assert(false, "Unexpected exception in AssignCulture.Execute. " +
                                 "Please log a MSBuild bug specifying the steps to reproduce the problem. " +
                                 e);
                    throw;
                }
#endif
            }

            AssignedFilesWithCulture   = cultureList.ToArray();
            AssignedFilesWithNoCulture = noCultureList.ToArray();

            return(retValue);
        }
Exemple #5
0
        /// <summary>
        /// Execute.
        /// </summary>
        /// <returns></returns>
        public override bool Execute()
        {
            _assignedFiles = new TaskItem[Files.Length];
            _cultureNeutralAssignedFiles = new TaskItem[Files.Length];
            ArrayList cultureList   = new ArrayList();
            ArrayList noCultureList = new ArrayList();

            bool retValue = true;

            for (int i = 0; i < Files.Length; ++i)
            {
                try
                {
                    AssignedFiles[i] = new TaskItem(Files[i]);

                    string dependentUpon         = AssignedFiles[i].GetMetadata(ItemMetadataNames.dependentUpon);
                    Culture.ItemCultureInfo info = Culture.GetItemCultureInfo
                                                   (
                        AssignedFiles[i].ItemSpec,
                        dependentUpon
                                                   );

                    if (info.culture != null && info.culture.Length > 0)
                    {
                        AssignedFiles[i].SetMetadata("Culture", info.culture);
                        AssignedFiles[i].SetMetadata("WithCulture", "true");
                        cultureList.Add(AssignedFiles[i]);
                    }
                    else
                    {
                        noCultureList.Add(AssignedFiles[i]);
                        AssignedFiles[i].SetMetadata("WithCulture", "false");
                    }

                    CultureNeutralAssignedFiles[i]          = new TaskItem(AssignedFiles[i]);
                    CultureNeutralAssignedFiles[i].ItemSpec = info.cultureNeutralFilename;

                    Log.LogMessageFromResources
                    (
                        MessageImportance.Low,
                        "AssignCulture.Comment",
                        AssignedFiles[i].GetMetadata("Culture"),
                        AssignedFiles[i].ItemSpec
                    );
                }
                catch (ArgumentException e)
                {
                    Log.LogErrorWithCodeFromResources("AssignCulture.CannotExtractCulture", Files[i].ItemSpec, e.Message);
                    retValue = false;
                }
#if _DEBUG
                catch (Exception e)
                {
                    Debug.Assert(false, "Unexpected exception in AssignCulture.Execute. " +
                                 "Please log a MSBuild bug specifying the steps to reproduce the problem. " +
                                 e.Message);
                    throw;
                }
#endif
            }

            _assignedFilesWithCulture   = (ITaskItem[])cultureList.ToArray(typeof(ITaskItem));
            _assignedFilesWithNoCulture = (ITaskItem[])noCultureList.ToArray(typeof(ITaskItem));

            return(retValue);
        }
        internal static string CreateManifestNameImpl(string fileName, string linkFileName, bool prependCultureAsDirectory, string rootNamespace, string dependentUponFileName, Stream binaryStream, TaskLoggingHelper log)
        {
            string str = linkFileName;

            if ((str == null) || (str.Length == 0))
            {
                str = fileName;
            }
            Culture.ItemCultureInfo itemCultureInfo = Culture.GetItemCultureInfo(str, dependentUponFileName);
            StringBuilder           builder         = new StringBuilder();

            if (binaryStream != null)
            {
                ExtractedClassName firstClassNameFullyQualified = CSharpParserUtilities.GetFirstClassNameFullyQualified(binaryStream);
                if (firstClassNameFullyQualified.IsInsideConditionalBlock && (log != null))
                {
                    log.LogWarningWithCodeFromResources("CreateManifestResourceName.DefinitionFoundWithinConditionalDirective", new object[] { dependentUponFileName, str });
                }
                if ((firstClassNameFullyQualified.Name != null) && (firstClassNameFullyQualified.Name.Length > 0))
                {
                    builder.Append(firstClassNameFullyQualified.Name);
                    if ((itemCultureInfo.culture != null) && (itemCultureInfo.culture.Length > 0))
                    {
                        builder.Append(".").Append(itemCultureInfo.culture);
                    }
                }
            }
            if (builder.Length == 0)
            {
                if ((rootNamespace != null) && (rootNamespace.Length > 0))
                {
                    builder.Append(rootNamespace).Append(".");
                }
                string str2      = CreateManifestResourceName.MakeValidEverettIdentifier(Path.GetDirectoryName(itemCultureInfo.cultureNeutralFilename));
                string extension = Path.GetExtension(itemCultureInfo.cultureNeutralFilename);
                if (((string.Compare(extension, ".resx", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(extension, ".restext", StringComparison.OrdinalIgnoreCase) == 0)) || (string.Compare(extension, ".resources", StringComparison.OrdinalIgnoreCase) == 0))
                {
                    builder.Append(Path.Combine(str2, Path.GetFileNameWithoutExtension(itemCultureInfo.cultureNeutralFilename)));
                    builder.Replace(Path.DirectorySeparatorChar, '.');
                    builder.Replace(Path.AltDirectorySeparatorChar, '.');
                    if ((itemCultureInfo.culture != null) && (itemCultureInfo.culture.Length > 0))
                    {
                        builder.Append(".").Append(itemCultureInfo.culture);
                    }
                    if (string.Equals(extension, ".resources", StringComparison.OrdinalIgnoreCase))
                    {
                        builder.Append(extension);
                    }
                }
                else
                {
                    builder.Append(Path.Combine(str2, Path.GetFileName(itemCultureInfo.cultureNeutralFilename)));
                    builder.Replace(Path.DirectorySeparatorChar, '.');
                    builder.Replace(Path.AltDirectorySeparatorChar, '.');
                    if ((prependCultureAsDirectory && (itemCultureInfo.culture != null)) && (itemCultureInfo.culture.Length > 0))
                    {
                        builder.Insert(0, Path.DirectorySeparatorChar);
                        builder.Insert(0, itemCultureInfo.culture);
                    }
                }
            }
            return(builder.ToString());
        }