Beispiel #1
0
        private IEnumerable <Tuple <H5ResourceInfo, byte[]> > PrepareAndExtractResources(string outputPath, string projectPath)
        {
            if (AssemblyInfo.Resources.HasEmbedResources())
            {
                // There are resources defined in the config so let's grab files
                // Find all items and put in the order
                Logger.ZLogTrace("Preparing resources specified in config...");

                foreach (var resource in AssemblyInfo.Resources.EmbedItems)
                {
                    Logger.ZLogTrace("Preparing resource {0}", resource.Name);

                    if (resource.Inject != true && resource.Extract != true)
                    {
                        Logger.ZLogTrace("Skipping the resource as it has inject != true and extract != true");
                        continue;
                    }

                    using (var resourceBuffer = new MemoryStream(500 * 1024))
                    {
                        GenerateResourceHeader(resourceBuffer, resource, projectPath);

                        var needSourceMap = ReadResourceFiles(projectPath, resourceBuffer, resource);

                        if (resourceBuffer.Length > 0)
                        {
                            Logger.ZLogTrace("Prepared file items for resource {0}", resource.Name);

                            var resourcePath = GetFullPathForResource(outputPath, resource);

                            var code = resourceBuffer.ToArray();

                            if (needSourceMap)
                            {
                                TranslatorOutputItemContent content = code;

                                var fullPath = Path.GetFullPath(Path.Combine(resourcePath.Item1, resourcePath.Item2));

                                content = GenerateSourceMap(fullPath, content.GetContentAsString());

                                code = content.GetContentAsBytes();
                            }

                            ExtractResource(resourcePath.Item1, resourcePath.Item2, resource, code);

                            var info = new H5ResourceInfo
                            {
                                Name     = resource.Name,
                                FileName = resource.Name,
                                Path     = string.IsNullOrWhiteSpace(resource.Output) ? null : resource.Output
                            };

                            yield return(Tuple.Create(info, code));
                        }
                        else
                        {
                            Logger.ZLogError("No files found for resource {0}", resource.Name);
                        }
                    }

                    Logger.ZLogTrace("Done preparing resource {0}", resource.Name);
                }

                Logger.ZLogTrace("Done preparing resources specified in config...");
            }
            else
            {
                // There are no resources defined in the config so let's just grab files
                Logger.ZLogTrace("Preparing outputs for resources");

                var nonMinifiedCombinedPartsDone = false;
                var minifiedCombinedPartsDone    = false;

                foreach (var outputItem in Outputs.GetOutputs(true))
                {
                    H5ResourceInfoPart[] parts = null;

                    Logger.ZLogTrace("Getting output full path: {0}", outputItem.FullPath.ToString());
                    Logger.ZLogTrace("Getting output local path: {0}", outputItem.FullPath.LocalPath);

                    var isCombined = outputItem.OutputKind.HasFlag(TranslatorOutputKind.Combined) && Outputs.Combined != null;
                    var isMinified = outputItem.OutputKind.HasFlag(TranslatorOutputKind.Minified);

                    if (isCombined)
                    {
                        Logger.ZLogTrace("The resource item is combined. Setting up its parts.");

                        if (!isMinified)
                        {
                            Logger.ZLogTrace("Choosing non-minified parts.");
                            parts = Outputs.CombinedResourcePartsNonMinified.Select(x => x.Key).ToArray();
                        }

                        if (isMinified)
                        {
                            Logger.ZLogTrace("Choosing minified parts.");
                            parts = Outputs.CombinedResourcePartsMinified.Select(x => x.Key).ToArray();
                        }
                    }

                    var info = new H5ResourceInfo
                    {
                        Name     = outputItem.Name,
                        FileName = outputItem.Name,
                        Path     = null,
                        Parts    = parts
                    };

                    byte[] content;

                    if (!outputItem.HasGeneratedSourceMap)
                    {
                        Logger.ZLogTrace("The output item does not have HasGeneratedSourceMap so we use it right from the Outputs");
                        content = outputItem.Content.GetContentAsBytes();
                        Logger.ZLogTrace("The output is of content " + content.Length + " length");
                    }
                    else
                    {
                        Logger.ZLogTrace("Reading content file as the output has HasGeneratedSourceMap");
                        content = File.ReadAllBytes(outputItem.FullPath.LocalPath);
                        Logger.ZLogTrace("Read " + content.Length + " bytes for " + info.Name);
                    }

                    yield return(Tuple.Create(info, content));

                    if (isCombined)
                    {
                        Dictionary <H5ResourceInfoPart, string> partSource = null;

                        if (!isMinified && !nonMinifiedCombinedPartsDone)
                        {
                            Logger.ZLogTrace("Preparing non-minified combined resource parts");

                            partSource = Outputs.CombinedResourcePartsNonMinified;
                        }

                        if (isMinified && !minifiedCombinedPartsDone)
                        {
                            Logger.ZLogTrace("Preparing minified combined resource parts");

                            partSource = Outputs.CombinedResourcePartsMinified;
                        }

                        if (partSource != null)
                        {
                            foreach (var part in partSource)
                            {
                                //if (part.Key.Assembly != null)
                                //{
                                //    Logger.ZLogTrace("Resource part " + part.Key.ResourceName + " is not embedded into resources as it is from another assembly " + part.Key.Assembly);
                                //    continue;
                                //}

                                if (part.Value == null)
                                {
                                    Logger.ZLogTrace("Resource part " + part.Key.ResourceName + " from " + part.Key.Assembly + " is not embedded into resources as it is empty");
                                    continue;
                                }

                                var partResource = new H5ResourceInfo
                                {
                                    Name     = null,
                                    FileName = part.Key.ResourceName,
                                    Path     = null,
                                    Parts    = null
                                };

                                var partContent = new byte[0];

                                if (!string.IsNullOrEmpty(part.Value))
                                {
                                    if (CheckIfRequiresSourceMap(part.Key))
                                    {
                                        var partSourceMap = GenerateSourceMap(part.Key.Name, part.Value);
                                        partContent = OutputEncoding.GetBytes(partSourceMap);
                                    }
                                    else
                                    {
                                        partContent = OutputEncoding.GetBytes(part.Value);
                                    }
                                }

                                yield return(Tuple.Create(partResource, partContent));
                            }

                            if (!isMinified)
                            {
                                nonMinifiedCombinedPartsDone = true;
                            }

                            if (isMinified)
                            {
                                minifiedCombinedPartsDone = true;
                            }
                        }
                    }
                }

                Logger.ZLogTrace("Done preparing output files for resources");
            }
        }
Beispiel #2
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);
        }
Beispiel #3
0
        private IEnumerable <Tuple <BridgeResourceInfo, byte[]> > PrepareAndExtractResources(string outputPath, string projectPath)
        {
            if (this.AssemblyInfo.Resources.HasEmbedResources())
            {
                // There are resources defined in the config so let's grab files
                // Find all items and put in the order
                this.Log.Trace("Preparing resources specified in config...");

                foreach (var resource in this.AssemblyInfo.Resources.EmbedItems)
                {
                    this.Log.Trace("Preparing resource " + resource.Name);

                    if (resource.Inject != true && resource.Extract != true)
                    {
                        this.Log.Trace("Skipping the resource as it has inject != true and extract != true");
                        continue;
                    }

                    using (var resourceBuffer = new MemoryStream(500 * 1024))
                    {
                        this.GenerateResourseHeader(resourceBuffer, resource, projectPath);

                        var needSourceMap = this.ReadResourseFiles(projectPath, resourceBuffer, resource);

                        if (resourceBuffer.Length > 0)
                        {
                            this.Log.Trace("Prepared file items for resource " + resource.Name);

                            var resourcePath = GetFullPathForResource(outputPath, resource);

                            var code = resourceBuffer.ToArray();

                            if (needSourceMap)
                            {
                                TranslatorOutputItemContent content = code;

                                var fullPath = Path.GetFullPath(Path.Combine(resourcePath.Item1, resourcePath.Item2));

                                content = GenerateSourceMap(fullPath, content.GetContentAsString());

                                code = content.GetContentAsBytes();
                            }

                            this.ExtractResource(resourcePath.Item1, resourcePath.Item2, resource, code);

                            var info = new BridgeResourceInfo
                            {
                                Name     = resource.Name,
                                FileName = resource.Name,
                                Path     = string.IsNullOrWhiteSpace(resource.Output) ? null : resource.Output
                            };

                            yield return(Tuple.Create(info, code));
                        }
                        else
                        {
                            this.Log.Error("No files found for resource " + resource.Name);
                        }
                    }

                    this.Log.Trace("Done preparing resource " + resource.Name);
                }

                this.Log.Trace("Done preparing resources specified in config...");
            }
            else
            {
                // There are no resources defined in the config so let's just grab files
                this.Log.Trace("Preparing outputs for resources");

                foreach (var outputItem in this.Outputs.GetOutputs(true))
                {
                    this.Log.Trace("Getting output " + outputItem.FullPath.LocalPath);

                    var info = new BridgeResourceInfo
                    {
                        Name     = outputItem.Name,
                        FileName = outputItem.Name,
                        Path     = null
                    };

                    byte[] content;

                    if (!outputItem.HasGeneratedSourceMap)
                    {
                        this.Log.Trace("The output item does not have HasGeneratedSourceMap so we use it right from the Outputs");
                        content = outputItem.Content.GetContentAsBytes();
                        this.Log.Trace("The output is of content " + content.Length + " length");
                    }
                    else
                    {
                        this.Log.Trace("Reading content file as the output has HasGeneratedSourceMap");
                        content = File.ReadAllBytes(outputItem.FullPath.LocalPath);
                        this.Log.Trace("Read " + content.Length + " bytes for " + info.Name);
                    }

                    yield return(Tuple.Create(info, content));
                }

                this.Log.Trace("Done preparing output files for resources");
            }
        }
Beispiel #4
0
        internal static bool AddOutputItem(List <TranslatorOutputItem> target, string fileName, TranslatorOutputItemContent content, TranslatorOutputKind outputKind, string location = null, string assembly = null)
        {
            var fileHelper = new FileHelper();

            var outputType = fileHelper.GetOutputType(fileName);

            TranslatorOutputItem output = null;

            bool isMinJs = fileHelper.IsMinJS(fileName);

            var searchName = fileName;

            if (isMinJs)
            {
                searchName = fileHelper.GetNonMinifiedJSFileName(fileName);
            }

            output = target.FirstOrDefault(x => string.Compare(x.Name, searchName, StringComparison.InvariantCultureIgnoreCase) == 0);

            if (output != null)
            {
                bool isAdded;

                if (isMinJs)
                {
                    isAdded = output.MinifiedVersion == null;

                    output.MinifiedVersion = new TranslatorOutputItem
                    {
                        Name       = fileName,
                        OutputType = outputType,
                        OutputKind = outputKind | TranslatorOutputKind.Minified,
                        Location   = location,
                        Content    = content,
                        IsMinified = true,
                        Assembly   = assembly
                    };
                }
                else
                {
                    isAdded        = output.IsEmpty;
                    output.IsEmpty = false;
                }

                return(isAdded);
            }

            output = new TranslatorOutputItem
            {
                Name       = searchName,
                OutputType = outputType,
                OutputKind = outputKind,
                Location   = location,
                Content    = new TranslatorOutputItemContent((string)null),
                Assembly   = assembly
            };

            if (isMinJs)
            {
                output.IsEmpty = true;

                output.MinifiedVersion = new TranslatorOutputItem
                {
                    Name       = fileName,
                    OutputType = outputType,
                    OutputKind = outputKind | TranslatorOutputKind.Minified,
                    Location   = location,
                    Content    = content,
                    IsMinified = true,
                    Assembly   = assembly
                };
            }
            else
            {
                output.Content = content;
            }

            target.Add(output);

            return(true);
        }
Beispiel #5
0
        protected virtual void AddReferencedOutput(string outputPath, AssemblyDefinition assembly, H5ResourceInfo resource, string fileName, Func <string, string> preHandler = null)
        {
            Logger.ZLogTrace("Adding referenced output {0}", resource.Name);

            var currentAssembly = GetAssemblyNameForResource(assembly);

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

            TranslatorOutputItemContent content = null;

            if (combinedResource)
            {
                Logger.ZLogTrace("The resource contains parts");

                var contentBuffer = new StringBuilder();
                var needNewLine   = false;
                var fileHelper    = new FileHelper();

                foreach (var part in resource.Parts)
                {
                    Logger.ZLogTrace("Handling part {0} {1}", part.Assembly, part.ResourceName);

                    bool needPart = true;

                    System.Reflection.AssemblyName partAssemblyName = null;

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

                        if (partAssemblyName.Name == H5_ASSEMBLY && (string.Compare(part.Name, H5ConsoleName, true) == 0 || string.Compare(part.Name, fileHelper.GetMinifiedJSFileName(H5ConsoleName), true) == 0))
                        {
                            //RFO: Skip H5 console resource, as this is now removed. We keep the Check here just so that it continues to compile old H5 versions without it
                            needPart = false;
                        }
                        else
                        {
                            var partContentName = GetExtractedResourceName(part.Assembly, part.Name);
                            needPart = ExtractedScripts.Add(partContentName);

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

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

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

                        if (partAssemblyName == null)
                        {
                            Logger.ZLogTrace("Using assembly {0} to search resource part {1}", assembly.FullName, resourcePartName);

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

                            if (partAssembly == null)
                            {
                                Logger.ZLogWarning("Did not find assembly for resource part {0} by assembly name {1}. Skipping this item!", resourcePartName, partAssemblyName.Name);
                                continue;
                            }

                            if (partAssembly.Name.Version != partAssemblyName.Version)
                            {
                                Logger.ZLogInformation("Found different assembly version (higher) {0} for resource part {1} from {2}", partAssembly.FullName, resourcePartName, assembly.FullName);
                            }
                            else
                            {
                                Logger.ZLogTrace("Found exact assembly version {0} for resource part {1}.", partAssembly.FullName, resourcePartName);
                            }

                            var resourcePartFound = false;

                            try
                            {
                                partContent       = ReadEmbeddedResource(partAssembly, resourcePartName, true, preHandler).Item2;
                                resourcePartFound = true;
                            }
                            catch (InvalidOperationException)
                            {
                                Logger.ZLogTrace("Did not find resource part {0} in {1}. Will try to find it by short name {2}", resourcePartName, partAssembly.FullName, part.Name);
                            }

                            if (!resourcePartFound)
                            {
                                try
                                {
                                    partContent       = ReadEmbeddedResource(partAssembly, part.Name, true, preHandler).Item2;
                                    resourcePartFound = true;
                                }
                                catch (InvalidOperationException)
                                {
                                    Logger.ZLogTrace("Did not find resource part {0} in {1}", part.Name, partAssembly.FullName);
                                }

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

                                        if (partAssemblyExactVersion != null)
                                        {
                                            Logger.ZLogTrace("Trying to find it in the part's assembly by long name {0}", part.Name);

                                            try
                                            {
                                                partContent       = ReadEmbeddedResource(partAssemblyExactVersion, resourcePartName, true, preHandler).Item2;
                                                resourcePartFound = true;
                                            }
                                            catch (InvalidOperationException)
                                            {
                                                Logger.ZLogTrace("Did not find resource part {0} in {1}. Will try to find it by short name {2}", resourcePartName, partAssemblyExactVersion.FullName, part.Name);
                                            }

                                            if (!resourcePartFound)
                                            {
                                                try
                                                {
                                                    partContent       = ReadEmbeddedResource(partAssemblyExactVersion, part.Name, true, preHandler).Item2;
                                                    resourcePartFound = true;
                                                }
                                                catch (InvalidOperationException)
                                                {
                                                    Logger.ZLogTrace("Did not find resource part {0} in {1}. Will try to find it by the resource's assembly by long name {2} ", part.Name, partAssemblyExactVersion.FullName, 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(Outputs.References, fileName, content, TranslatorOutputKind.Reference, location: outputPath, assembly: currentAssembly);
        }