Пример #1
0
        protected virtual void AddReferencedOutput(string outputPath, AssemblyDefinition assembly, string resourceName, string fileName, Func <string, string> preHandler = null)
        {
            var res = assembly.MainModule.Resources.FirstOrDefault(r => r.Name == resourceName);

            if (res == null)
            {
                throw new InvalidOperationException("Could not read resource " + resourceName + " in " + assembly.FullName);
            }

            using (var resourcesStream = ((EmbeddedResource)res).GetResourceStream())
            {
                if (FileHelper.IsJS(fileName) || preHandler != null)
                {
                    using (var reader = new StreamReader(resourcesStream))
                    {
                        var content = reader.ReadToEnd();
                        Emitter.AddOutputItem(this.Outputs.References, fileName, content, TranslatorOutputKind.Reference, outputPath);
                    }
                }
                else
                {
                    var binary = ReadStream(resourcesStream);
                    Emitter.AddOutputItem(this.Outputs.References, fileName, binary, TranslatorOutputKind.Reference, outputPath);
                }
            }
        }
Пример #2
0
        protected virtual void AddLocaleOutput(EmbeddedResource resource, string outputPath)
        {
            var fileName = resource.Name.Substring(Translator.LocalesPrefix.Length);

            if (!string.IsNullOrWhiteSpace(this.AssemblyInfo.LocalesOutput))
            {
                outputPath = Path.Combine(outputPath, this.AssemblyInfo.LocalesOutput);
            }

            var content = this.ReadEmbeddedResource(resource);

            Emitter.AddOutputItem(this.Outputs.Locales, fileName, new StringBuilder(content), TranslatorOutputKind.Locale, location: outputPath);
        }
Пример #3
0
        protected virtual List <TranslatorOutputItem> CombineOutputs()
        {
            this.Log.Trace("Combining outputs...");

            var result = new List <TranslatorOutputItem>();

            var disableAsm = this.AssemblyInfo.Assembly.DisableInitAssembly;

            this.AssemblyJsDocWritten = false;

            var fileHelper = new FileHelper();

            foreach (var outputPair in this.Outputs)
            {
                var fileName = outputPair.Key;
                var output   = outputPair.Value;

                this.Log.Trace("File name " + (fileName ?? ""));

                bool isJs = fileHelper.IsJS(fileName);

                var tmp = new StringBuilder(output.TopOutput.Length + output.BottomOutput.Length + output.NonModuletOutput.Length + 1000);

                OutputTop(output, tmp);

                OutputNonModule(disableAsm, fileName, output, isJs, tmp);

                OutputBottom(output, tmp);

                var outputKind = TranslatorOutputKind.ProjectOutput;

                if (output.IsMetadata)
                {
                    outputKind = outputKind | TranslatorOutputKind.Metadata;
                }

                Emitter.AddOutputItem(result, fileName, tmp, outputKind, null);
            }

            this.Log.Trace("Combining outputs done");

            return(result);
        }
Пример #4
0
 protected virtual void AddExtractedResourceOutput(ResourceConfigItem resource, byte[] code)
 {
     Emitter.AddOutputItem(this.Outputs.Resources, resource.Name, code, TranslatorOutputKind.Resource, location: resource.Output);
 }
Пример #5
0
        protected virtual void AddReferencedOutput(string outputPath, AssemblyDefinition assembly, BridgeResourceInfo resource, string fileName, Func <string, string> preHandler = null)
        {
            this.Log.Trace("Adding referenced output " + resource.Name);

            var currentAssembly = GetAssemblyNameForResource(assembly);

            var combinedResource = (resource.Parts != null && resource.Parts.Length > 0);

            TranslatorOutputItemContent content = null;

            if (combinedResource)
            {
                this.Log.Trace("The resource contains parts");

                var contentBuffer = new StringBuilder();
                var needNewLine   = false;
                var noConsole     = this.AssemblyInfo.Console.Enabled != true;
                var fileHelper    = new FileHelper();

                foreach (var part in resource.Parts)
                {
                    this.Log.Trace("Handling part " + part.Assembly + " " + part.ResourceName);

                    bool needPart = true;

                    System.Reflection.AssemblyName partAssemblyName = null;

                    if (part.Assembly != null)
                    {
                        partAssemblyName = GetAssemblyNameFromResource(part.Assembly);

                        if (noConsole &&
                            partAssemblyName.Name == Translator.Bridge_ASSEMBLY &&
                            (string.Compare(part.Name, BridgeConsoleName, true) == 0 ||
                             string.Compare(part.Name, fileHelper.GetMinifiedJSFileName(BridgeConsoleName), true) == 0)
                            )
                        {
                            // Skip Bridge console resource
                            needPart = false;

                            this.Log.Trace("Skipping this part as it is Bridge Console and the Console option disabled");
                        }
                        else
                        {
                            var partContentName = GetExtractedResourceName(part.Assembly, part.Name);
                            needPart = ExtractedScripts.Add(partContentName);

                            if (!needPart)
                            {
                                this.Log.Trace("Skipping this part as it is already added");
                            }
                        }
                    }

                    if (needPart)
                    {
                        if (needNewLine)
                        {
                            NewLine(contentBuffer);
                        }

                        string partContent      = null;
                        var    resourcePartName = part.ResourceName;

                        if (partAssemblyName == null)
                        {
                            this.Log.Trace("Using assembly " + assembly.FullName + " to search resource part " + resourcePartName);

                            partContent = ReadEmbeddedResource(assembly, resourcePartName, true, preHandler).Item2;
                        }
                        else
                        {
                            var partAssembly = this.References
                                               .Where(x => x.Name.Name == partAssemblyName.Name)
                                               .OrderByDescending(x => x.Name.Version)
                                               .FirstOrDefault();

                            if (partAssembly == null)
                            {
                                this.Log.Warn("Did not find assembly for resource part " + resourcePartName + " by assembly name " + partAssemblyName.Name + ". Skipping this item!");
                                continue;
                            }

                            if (partAssembly.Name.Version != partAssemblyName.Version)
                            {
                                this.Log.Info("Found different assembly version (higher) " + partAssembly.FullName + " for resource part" + resourcePartName + " from " + assembly.FullName);
                            }
                            else
                            {
                                this.Log.Trace("Found exact assembly version " + partAssembly.FullName + " for resource part" + resourcePartName);
                            }

                            var resourcePartFound = false;

                            try
                            {
                                partContent       = ReadEmbeddedResource(partAssembly, resourcePartName, true, preHandler).Item2;
                                resourcePartFound = true;
                            }
                            catch (InvalidOperationException)
                            {
                                this.Log.Trace("Did not find resource part " + resourcePartName + " in " + partAssembly.FullName + ". Will try to find it by short name " + part.Name);
                            }

                            if (!resourcePartFound)
                            {
                                try
                                {
                                    partContent       = ReadEmbeddedResource(partAssembly, part.Name, true, preHandler).Item2;
                                    resourcePartFound = true;
                                }
                                catch (InvalidOperationException)
                                {
                                    this.Log.Trace("Did not find resource part " + part.Name + " in " + partAssembly.FullName);
                                }

                                if (!resourcePartFound)
                                {
                                    if (partAssembly.Name.Version != partAssemblyName.Version)
                                    {
                                        var partAssemblyExactVersion = this.References
                                                                       .Where(x => x.FullName == partAssemblyName.FullName)
                                                                       .FirstOrDefault();

                                        if (partAssemblyExactVersion != null)
                                        {
                                            this.Log.Trace("Trying to find it in the part's assembly by long name " + part.Name);

                                            try
                                            {
                                                partContent       = ReadEmbeddedResource(partAssemblyExactVersion, resourcePartName, true, preHandler).Item2;
                                                resourcePartFound = true;
                                            }
                                            catch (InvalidOperationException)
                                            {
                                                this.Log.Trace("Did not find resource part " + resourcePartName + " in " + partAssemblyExactVersion.FullName + ". Will try to find it by short name " + part.Name);
                                            }

                                            if (!resourcePartFound)
                                            {
                                                try
                                                {
                                                    partContent       = ReadEmbeddedResource(partAssemblyExactVersion, part.Name, true, preHandler).Item2;
                                                    resourcePartFound = true;
                                                }
                                                catch (InvalidOperationException)
                                                {
                                                    this.Log.Trace("Did not find resource part " + part.Name + " in " + partAssemblyExactVersion.FullName + ". Will try to find it by the resource's assembly by long name " + resourcePartName);
                                                }
                                            }
                                        }
                                    }

                                    if (!resourcePartFound)
                                    {
                                        partContent       = ReadEmbeddedResource(assembly, resourcePartName, true, preHandler).Item2;
                                        resourcePartFound = true;
                                    }
                                }
                            }
                        }

                        contentBuffer.Append(partContent);

                        needNewLine = true;
                    }
                }

                content = contentBuffer;
            }
            else
            {
                var readAsString = FileHelper.IsJS(fileName) || preHandler != null;

                var notCombinedResource = ReadEmbeddedResource(assembly, resource.Name, readAsString, preHandler);

                if (readAsString)
                {
                    content = notCombinedResource.Item2;
                }
                else
                {
                    content = notCombinedResource.Item1;
                }
            }

            ExtractedScripts.Add(GetExtractedResourceName(currentAssembly, resource.Name));

            Emitter.AddOutputItem(this.Outputs.References, fileName, content, TranslatorOutputKind.Reference, location: outputPath, assembly: currentAssembly);
        }