public void LooksLikeCultureButIsnt()
        {
            AssignCulture t = new AssignCulture();
            t.BuildEngine = new MockEngine();
            ITaskItem i = new TaskItem("MyResource.yy.resx");
            t.Files = new ITaskItem[] { i };
            t.Execute();

            Assert.Equal(1, t.AssignedFiles.Length);
            Assert.Equal(1, t.CultureNeutralAssignedFiles.Length);
            Assert.Equal(String.Empty, t.AssignedFiles[0].GetMetadata("Culture"));
            Assert.Equal("MyResource.yy.resx", t.AssignedFiles[0].ItemSpec);
            Assert.Equal("MyResource.yy.resx", t.CultureNeutralAssignedFiles[0].ItemSpec);
        }
        public void Basic()
        {
            AssignCulture t = new AssignCulture();
            t.BuildEngine = new MockEngine();
            ITaskItem i = new TaskItem("MyResource.fr.resx");
            t.Files = new ITaskItem[] { i };
            t.Execute();

            Assert.Equal(1, t.AssignedFiles.Length);
            Assert.Equal(1, t.CultureNeutralAssignedFiles.Length);
            Assert.Equal("fr", t.AssignedFiles[0].GetMetadata("Culture"));
            Assert.Equal("MyResource.fr.resx", t.AssignedFiles[0].ItemSpec);
            Assert.Equal("MyResource.resx", t.CultureNeutralAssignedFiles[0].ItemSpec);
        }
        // No dependent file
        internal static string GetResourceIdFromFileName(string fileName, string rootNamespace)
        {
            string culture = null;

            if (String.Compare(Path.GetExtension(fileName), ".resx", true) == 0)
            {
                fileName = Path.ChangeExtension(fileName, null);
            }
            else
            {
                string only_filename, extn;
                if (AssignCulture.TrySplitResourceName(fileName, out only_filename, out culture, out extn))
                {
                    //remove the culture from fileName
                    //foo.it.bmp -> foo.bmp
                    fileName = only_filename + "." + extn;
                }
                else
                {
                    culture = null;
                }
            }

            // spaces in folder name are changed to _, those in filename remain
            string dirname = Path.GetDirectoryName(fileName) ?? String.Empty;

            dirname  = dirname.Replace(' ', '_');
            fileName = Path.Combine(dirname, Path.GetFileName(fileName));
            string rname = fileName.Replace('/', '.').Replace('\\', '.');

            if (!String.IsNullOrEmpty(rootNamespace))
            {
                rname = rootNamespace + "." + rname;
            }
            if (culture == null)
            {
                return(rname);
            }
            else
            {
                return(Path.Combine(culture, rname));
            }
        }
        protected override string CreateManifestName(string fileName,
                                                     string linkFileName,
                                                     string rootNamespace,
                                                     string dependentUponFileName,
                                                     Stream binaryStream)
        {
            string filename_to_use = String.IsNullOrEmpty(linkFileName) ? fileName : linkFileName;

            if (String.IsNullOrEmpty(dependentUponFileName) || binaryStream == null)
            {
                return(GetResourceIdFromFileName(filename_to_use, rootNamespace));
            }

            string ns        = null;
            string classname = null;

            using (StreamReader rdr = new StreamReader(binaryStream)) {
                int numopen = 0;
                while (true)
                {
                    string tok = GetNextToken(rdr);
                    if (tok == null)
                    {
                        break;
                    }

                    if (tok == "@")
                    {
                        //Handle @namespace, @class
                        GetNextToken(rdr);
                        continue;
                    }

                    if (String.Compare(tok, "namespace", false) == 0)
                    {
                        ns = GetNextToken(rdr);
                    }

                    if (tok == "{")
                    {
                        numopen++;
                    }

                    if (tok == "}")
                    {
                        numopen--;
                        if (numopen == 0)
                        {
                            ns = String.Empty;
                        }
                    }

                    if (tok == "class")
                    {
                        classname = GetNextToken(rdr);
                        break;
                    }
                }

                if (classname == null)
                {
                    return(GetResourceIdFromFileName(filename_to_use, rootNamespace));
                }

                string culture, extn, only_filename;
                if (AssignCulture.TrySplitResourceName(filename_to_use, out only_filename, out culture, out extn))
                {
                    extn = "." + culture;
                }
                else
                {
                    extn = String.Empty;
                }

                if (ns == null)
                {
                    return(classname + extn);
                }
                else
                {
                    return(ns + '.' + classname + extn);
                }
            }
        }
Example #5
0
        protected override string CreateManifestName(string fileName,
                                                     string linkFileName,
                                                     string rootNamespace,
                                                     string dependentUponFileName,
                                                     Stream binaryStream)
        {
            string filename_to_use = String.IsNullOrEmpty(linkFileName) ? fileName : linkFileName;

            if (String.IsNullOrEmpty(dependentUponFileName) || binaryStream == null)
            {
                return(GetResourceIdFromFileName
                           (Path.GetFileName(filename_to_use), rootNamespace));
            }

            string ns        = null;
            string classname = null;

            using (StreamReader rdr = new StreamReader(binaryStream)) {
                while (true)
                {
                    string tok = GetNextToken(rdr);
                    if (tok == null)
                    {
                        break;
                    }

                    if (String.Compare(tok, "namespace", true) == 0)
                    {
                        string t = GetNextToken(rdr);
                        /* 'namespace' can be a attribute param also, */
                        if (t == ":" && GetNextToken(rdr) == "=")
                        {
                            continue;
                        }
                        ns = t;
                    }

                    if (String.Compare(tok, "class", true) == 0)
                    {
                        string t = GetNextToken(rdr);
                        /* 'class' can be a attribute param also, */
                        if (t == ":" && GetNextToken(rdr) == "=")
                        {
                            continue;
                        }
                        classname = t;
                        break;
                    }
                }

                if (classname == null)
                {
                    return(GetResourceIdFromFileName(filename_to_use, rootNamespace));
                }

                string culture, extn, only_filename;
                if (AssignCulture.TrySplitResourceName(filename_to_use, out only_filename, out culture, out extn))
                {
                    extn = "." + culture;
                }
                else
                {
                    extn = String.Empty;
                }

                string rname;
                if (ns == null)
                {
                    rname = classname + extn;
                }
                else
                {
                    rname = ns + '.' + classname + extn;
                }

                if (String.IsNullOrEmpty(rootNamespace))
                {
                    return(rname);
                }
                else
                {
                    return(rootNamespace + "." + rname);
                }
            }
        }
        public void CultureAttributePrecedenceWithBogusCulture()
        {
            AssignCulture t = new AssignCulture();
            t.BuildEngine = new MockEngine();
            ITaskItem i = new TaskItem("MyResource.fr.resx");
            i.SetMetadata("Culture", "invalid");   // Bogus culture.
            t.Files = new ITaskItem[] { i };
            t.Execute();

            Assert.Equal(1, t.AssignedFiles.Length);
            Assert.Equal(1, t.CultureNeutralAssignedFiles.Length);
            Assert.Equal("fr", t.AssignedFiles[0].GetMetadata("Culture"));
            Assert.Equal("MyResource.fr.resx", t.AssignedFiles[0].ItemSpec);
            Assert.Equal("MyResource.resx", t.CultureNeutralAssignedFiles[0].ItemSpec);
        }
        public void Regress283991()
        {
            AssignCulture t = new AssignCulture();
            t.BuildEngine = new MockEngine();
            ITaskItem i = new TaskItem("MyResource.fr.resx");
            i.SetMetadata("DependentUpon", "MyResourcE.fr.vb");
            t.Files = new ITaskItem[] { i };

            t.Execute();

            Assert.Equal(1, t.AssignedFiles.Length);
            Assert.Equal(0, t.AssignedFilesWithCulture.Length);
            Assert.Equal(1, t.AssignedFilesWithNoCulture.Length);
        }
        public void AttributeForwarding()
        {
            AssignCulture t = new AssignCulture();
            t.BuildEngine = new MockEngine();
            ITaskItem i = new TaskItem("MyResource.fr.resx");
            i.SetMetadata("MyAttribute", "My Random String");
            t.Files = new ITaskItem[] { i };
            t.Execute();

            Assert.Equal(1, t.AssignedFiles.Length);
            Assert.Equal(1, t.CultureNeutralAssignedFiles.Length);
            Assert.Equal("fr", t.AssignedFiles[0].GetMetadata("Culture"));
            Assert.Equal("My Random String", t.AssignedFiles[0].GetMetadata("MyAttribute"));
            Assert.Equal("MyResource.fr.resx", t.AssignedFiles[0].ItemSpec);
            Assert.Equal("MyResource.resx", t.CultureNeutralAssignedFiles[0].ItemSpec);
        }