예제 #1
0
        public static Action GenerateFile(ProjectFile file, SingleFileCustomToolResult result, bool internalClass)
        {
            return(delegate {
                var dnp = file.Project as DotNetProject;
                if (dnp == null)
                {
                    var err = "ResXFileCodeGenerator can only be used with .NET projects";
                    result.Errors.Add(new CompilerError(null, 0, 0, null, err));
                    return;
                }

                var provider = dnp.LanguageBinding.GetCodeDomProvider();
                if (provider == null)
                {
                    var err = "ResXFileCodeGenerator can only be used with languages that support CodeDOM";
                    result.Errors.Add(new CompilerError(null, 0, 0, null, err));
                    return;
                }

                var outputfile = file.FilePath.ChangeExtension(".Designer." + provider.FileExtension);
                var ns = CustomToolService.GetFileNamespace(file, outputfile);
                var cn = provider.CreateValidIdentifier(file.FilePath.FileNameWithoutExtension);
                var rd = new Dictionary <object, object> ();

                using (var r = new ResXResourceReader(file.FilePath)) {
                    r.UseResXDataNodes = true;
                    r.BasePath = file.FilePath.ParentDirectory;

                    foreach (DictionaryEntry e in r)
                    {
                        rd.Add(e.Key, e.Value);
                    }
                }

                string[] unmatchable;
                var ccu = StronglyTypedResourceBuilder.Create(rd, cn, ns, provider, internalClass, out unmatchable);

                if (TargetsPcl2Framework(dnp))
                {
                    FixupPclTypeInfo(ccu);
                }

                foreach (var p in unmatchable)
                {
                    var msg = string.Format("Could not generate property for resource ID '{0}'", p);
                    result.Errors.Add(new CompilerError(file.FilePath, 0, 0, null, msg));
                }

                using (var w = new StreamWriter(outputfile, false, Encoding.UTF8))
                    provider.GenerateCodeFromCompileUnit(ccu, w, new CodeGeneratorOptions());

                result.GeneratedFilePath = outputfile;
            });
        }
예제 #2
0
        public static Action GenerateFile(ProjectFile file, SingleFileCustomToolResult result, bool internalClass)
        {
            return(delegate {
                var dnp = file.Project as DotNetProject;
                if (dnp == null)
                {
                    var err = "ResXFileCodeGenerator can only be used with .NET projects";
                    result.Errors.Add(new CompilerError(null, 0, 0, null, err));
                    return;
                }

                var provider = dnp.LanguageBinding.GetCodeDomProvider();
                if (provider == null)
                {
                    var err = "ResXFileCodeGenerator can only be used with languages that support CodeDOM";
                    result.Errors.Add(new CompilerError(null, 0, 0, null, err));
                    return;
                }

                var outputfile = file.FilePath.ChangeExtension(".Designer." + provider.FileExtension);
                var ns = CustomToolService.GetFileNamespace(file, outputfile);
                var cn = provider.CreateValidIdentifier(file.FilePath.FileNameWithoutExtension);

                string[] unmatchable;
                var ccu = StronglyTypedResourceBuilder.Create(file.FilePath, cn, ns, provider, internalClass, out unmatchable);

                foreach (var p in unmatchable)
                {
                    var msg = string.Format("Could not generate property for resource ID '{0}'", p);
                    result.Errors.Add(new CompilerError(file.FilePath, 0, 0, null, msg));
                }

                using (var w = new StreamWriter(outputfile, false, Encoding.UTF8))
                    provider.GenerateCodeFromCompileUnit(ccu, w, new CodeGeneratorOptions());

                result.GeneratedFilePath = outputfile;
            });
        }
예제 #3
0
        public async static Task GenerateFile(ProjectFile file, SingleFileCustomToolResult result, bool internalClass)
        {
            var dnp = file.Project as DotNetProject;

            if (dnp == null)
            {
                var err = "ResXFileCodeGenerator can only be used with .NET projects";
                result.Errors.Add(new CompilerError(null, 0, 0, null, err));
                return;
            }

            var provider = dnp.LanguageBinding.GetCodeDomProvider();

            if (provider == null)
            {
                const string err = "ResXFileCodeGenerator can only be used with languages that support CodeDOM";
                result.Errors.Add(new CompilerError(null, 0, 0, null, err));
                return;
            }

            var outputfile = file.FilePath.ChangeExtension(".Designer." + provider.FileExtension);

            //use the Visual Studio naming polict, so it matches code generated by VS
            var codeNamespace = CustomToolService.GetFileNamespace(file, outputfile, true);

            //no need to escape/cleanup, StronglyTypedResourceBuilder does that
            var name = file.FilePath.FileNameWithoutExtension;

            //NOTE: we fix it up later with the real resource ID, this is just a fallback in case that fails
            var resourcesNamespace = dnp.GetDefaultNamespace(outputfile, true);

            var rd                   = new Dictionary <object, object> ();
            var filePath             = file.FilePath;
            var targetsPcl2Framework = TargetsPcl2Framework(dnp);

            //Compute the *real* ID of the embedded resource. It can be affected by LogicalName so might not match the
            //class name, but StronglyTypedResourceBuilder only accepts a single name for both.
            //Fix this by patching it into the CodeDOM later.
            var          id     = file.ResourceId;
            const string suffix = ".resources";

            if (id.Length > suffix.Length && id.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
            {
                id = id.Substring(0, id.Length - suffix.Length);
            }

            await Task.Run(() => {
                using (var r = new ResXResourceReader(filePath)) {
                    r.BasePath = filePath.ParentDirectory;
                    foreach (DictionaryEntry e in r)
                    {
                        rd.Add(e.Key, e.Value);
                    }
                }

                string[] unmatchable;
                var ccu = StronglyTypedResourceBuilder.Create(rd, name, codeNamespace, resourcesNamespace, provider, internalClass, out unmatchable);

                if (targetsPcl2Framework)
                {
                    FixupPclTypeInfo(ccu);
                }

                FixupEmbeddedResourceID(ccu, id);

                foreach (var p in unmatchable)
                {
                    var msg = string.Format("Could not generate property for resource ID '{0}'", p);
                    result.Errors.Add(new CompilerError(filePath, 0, 0, null, msg));
                }

                lock (file) {
                    // Avoid race if ResXFileCodeGenerator is called more than once for the same file
                    using (var w = new StreamWriter(outputfile, false, Encoding.UTF8))
                        provider.GenerateCodeFromCompileUnit(ccu, w, new CodeGeneratorOptions());
                }

                result.GeneratedFilePath = outputfile;
            });
        }