Пример #1
0
        public void Test_ManifestResourceName_StaticPrefix() {
            ResourceFileSet fileset = new ResourceFileSet();

            fileset.BaseDirectory = TempDirectory;
            fileset.Prefix = @"Root.MyProj.Howdy";
            string actualName = fileset.GetManifestResourceName(Path.Combine(
                fileset.BaseDirectory.FullName, "mydir" + Path.DirectorySeparatorChar 
                + "file.txt"));
            Assert.AreEqual(fileset.Prefix + ".file.txt", actualName, 
                "Incorrect manifest resource name.");
        
            fileset.BaseDirectory = new DirectoryInfo(TempDirName + Path.DirectorySeparatorChar);
            actualName = fileset.GetManifestResourceName(Path.Combine(
                fileset.BaseDirectory.FullName, "mydir" + Path.DirectorySeparatorChar 
                + "file.txt"));
            Assert.AreEqual(fileset.Prefix + ".file.txt", actualName, 
                "Incorrect manifest resource name.");

            fileset.BaseDirectory = new DirectoryInfo(TempDirName + Path.DirectorySeparatorChar);
            actualName = fileset.GetManifestResourceName(Path.Combine(
                fileset.BaseDirectory.FullName, "file.txt"));
            Assert.AreEqual(fileset.Prefix + ".file.txt", actualName,
                "Incorrect manifest resource name.");

            // resource with a different logical path
            fileset.BaseDirectory = new DirectoryInfo(TempDirName + Path.DirectorySeparatorChar);
            actualName = fileset.GetManifestResourceName(Path.Combine(
                fileset.BaseDirectory.FullName, "file.txt"), Path.Combine(
                fileset.BaseDirectory.FullName, "test" + Path.DirectorySeparatorChar + 
                "new.txt"));
            Assert.AreEqual(fileset.Prefix + ".file.txt", actualName,
                "Incorrect manifest resource name.");
        }
Пример #2
0
        public void Test_ManifestResourceName_PrefixWithSpecialCharacters() {
            ResourceFileSet fileset = new ResourceFileSet();
            fileset.BaseDirectory = TempDirectory;
            fileset.DynamicPrefix = true;

            string actualName;

            // if part of the prefix starts with a number, it should be
            // prefixed with an underscore
            fileset.Prefix = @"Root.1MyProj.Howdy";
            actualName = fileset.GetManifestResourceName(Path.Combine(
                fileset.BaseDirectory.FullName, "mydir" + Path.DirectorySeparatorChar 
                + "file.txt"));
            Assert.AreEqual("Root._1MyProj.Howdy" + ".mydir.file.txt", actualName,
                "Incorrect manifest resource name.");

            // any character in the prefix that is neither letter nor digit should
            // be replaced with an underscore
            fileset.Prefix = @"Root.-MyProj.H0w!dy";
            actualName = fileset.GetManifestResourceName(Path.Combine(
                fileset.BaseDirectory.FullName, "mydir" + Path.DirectorySeparatorChar 
                + "file.txt"));
            Assert.AreEqual("Root._MyProj.H0w_dy" + ".mydir.file.txt", actualName,
                "Incorrect manifest resource name.");

            // the file name part of a manifest resource name can start with a 
            // digit
            fileset.Prefix = @"Root.MyProj.Howdy";
            actualName = fileset.GetManifestResourceName(Path.Combine(
                fileset.BaseDirectory.FullName, "mydir" + Path.DirectorySeparatorChar 
                + "1file.txt"));
            Assert.AreEqual("Root.MyProj.Howdy" + ".mydir.1file.txt", actualName,
                "Incorrect manifest resource name.");

            // the extension part of a manifest resource name can start with a 
            // digit
            fileset.Prefix = @"Root.MyProj.Howdy";
            actualName = fileset.GetManifestResourceName(Path.Combine(
                fileset.BaseDirectory.FullName, "mydir" + Path.DirectorySeparatorChar 
                + "file.1txt"));
            Assert.AreEqual("Root.MyProj.Howdy" + ".mydir.file.1txt", actualName,
                "Incorrect manifest resource name.");

            // the file name part of a manifest resource name can contain 
            // characters that are neither letter nor digit
            fileset.Prefix = @"Root.MyProj.Howdy";
            actualName = fileset.GetManifestResourceName(Path.Combine(
                fileset.BaseDirectory.FullName, "mydir" + Path.DirectorySeparatorChar 
                + "f-ile.txt"));
            Assert.AreEqual("Root.MyProj.Howdy" + ".mydir.f-ile.txt", actualName,
                "Incorrect manifest resource name.");

            // the extension part of a manifest resource name can contain 
            // characters that are neither letter nor digit
            fileset.Prefix = @"Root.MyProj.Howdy";
            actualName = fileset.GetManifestResourceName(Path.Combine(
                fileset.BaseDirectory.FullName, "mydir" + Path.DirectorySeparatorChar 
                + "file.t!xt"));
            Assert.AreEqual("Root.MyProj.Howdy" + ".mydir.file.t!xt", actualName,
                "Incorrect manifest resource name.");
        }
Пример #3
0
        /// <summary>
        /// Determines the manifest resource name of the given resource file.
        /// </summary>
        /// <param name="resources">The <see cref="ResourceFileSet" /> containing information that will used to assemble the manifest resource name.</param>
        /// <param name="resourcePhysicalFile">The resource file of which the manifest resource name should be determined.</param>
        /// <param name="resourceLogicalFile">The logical location of the resource file.</param>
        /// <param name="dependentFile">The source file on which the resource file depends.</param>
        /// <returns>
        /// The manifest resource name of the specified resource file.
        /// </returns>
        public string GetManifestResourceName(ResourceFileSet resources, string resourcePhysicalFile, string resourceLogicalFile, string dependentFile) {
            if (resources == null) {
                throw new ArgumentNullException("resources");
            }

            if (resourcePhysicalFile == null) {
                throw new ArgumentNullException("resourcePhysicalFile");
            }

            if (resourceLogicalFile == null) {
                throw new ArgumentNullException("resourceLogicalFile");
            }

            // make sure the resource file exists
            if (!File.Exists(resourcePhysicalFile)) {
                throw new BuildException(string.Format(CultureInfo.InvariantCulture, 
                    ResourceUtils.GetString("NA2009"), resourcePhysicalFile), 
                    Location);
            }

            // will hold the manifest resource name
            string manifestResourceName = null;
          
            // check if we're dealing with a localized resource
            CultureInfo resourceCulture = CompilerBase.GetResourceCulture(
                resourceLogicalFile, dependentFile);

            // determine the resource type
            switch (Path.GetExtension(resourcePhysicalFile).ToLower(CultureInfo.InvariantCulture)) {
                case ".resx":
                    // try and get manifest resource name from dependent file
                    ResourceLinkage resourceLinkage = GetResourceLinkage(
                        dependentFile, resourceCulture);

                    if (resourceLinkage == null || !resourceLinkage.IsValid) {
                        // no resource linkage could be determined (no dependent
                        // file or dependent file does not exist) or dependent
                        // file is no (valid) source file
                        manifestResourceName = Path.ChangeExtension(
                            resources.GetManifestResourceName(resourcePhysicalFile,
                            resourceLogicalFile), "resources");
                    } else {
                        if (!resourceLinkage.HasClassName) {
                            // use filename of resource file to determine class name

                            string className = Path.GetFileNameWithoutExtension(
                                resourcePhysicalFile);

                            // cater for asax/aspx special cases. eg. a resource file 
                            // named "WebForm1.aspx(.resx)" will here be transformed to
                            // "WebForm1"
                            // we assume that the class name of a codebehind file 
                            // is equal to the file name of that codebehind file 
                            // (without extension)
                            if (Path.GetExtension(className) != string.Empty) {
                                string codebehindExtension = Path.GetExtension(
                                    className).ToLower(CultureInfo.InvariantCulture);
                                foreach (string extension in CodebehindExtensions) {
                                    if (extension == codebehindExtension) {
                                        className = Path.GetFileNameWithoutExtension(
                                            className);
                                        break;
                                    }
                                }
                            }

                            resourceLinkage.ClassName = className;
                        }

                        // ensure we have information necessary to determine the
                        // manifest resource name
                        if (resourceLinkage.IsValid) {
                            manifestResourceName = resourceLinkage.ToString() 
                                + ".resources";
                        } else {
                            // we should actually never get here, but just in case ...
                            throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                ResourceUtils.GetString("NA2010"), resourcePhysicalFile), Location);
                        }
                    }

                    break;
                case ".resources":
                    // determine resource name, and leave culture information
                    // in manifest resource name
                    manifestResourceName = resources.GetManifestResourceName(
                        resourcePhysicalFile, resourceLogicalFile);
                    break;
                default:
                    // VS.NET handles an embedded resource file named licenses.licx
                    // in the root of the project and without culture in a special
                    // way
                    if (Path.GetFileName(resourcePhysicalFile) == "licenses.licx") {
                        // the manifest resource name will be <output file>.licenses
                        // eg. TestAssembly.exe.licenses
                        manifestResourceName = Path.GetFileName(OutputFile.FullName)
                            + ".licenses";
                    } else {
                        // check if resource is localized
                        if (resourceCulture != null) {
                            // determine resource name
                            manifestResourceName = resources.GetManifestResourceName(
                                resourcePhysicalFile, resourceLogicalFile);

                            // remove culture name from name of resource
                            int cultureIndex = manifestResourceName.LastIndexOf("." + resourceCulture.Name);
                            manifestResourceName = manifestResourceName.Substring(0, cultureIndex) 
                                + manifestResourceName.Substring(cultureIndex).Replace("." 
                                + resourceCulture.Name, string.Empty);
                        } else {
                            manifestResourceName = resources.GetManifestResourceName(
                                resourcePhysicalFile, resourceLogicalFile);
                        }
                    }
                    break;
            }

            return manifestResourceName;
        }