예제 #1
0
파일: Translator.cs 프로젝트: nrnoble/h5
        public TranslatorOutputItem FindTranslatorOutputItem(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                return null;
            }

            foreach (var output in Outputs.GetOutputs())
            {
                if (output.FullPath.LocalPath == filePath)
                {
                    return output;
                }
            }

            return null;
        }
예제 #2
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");
            }
        }
예제 #3
0
        public virtual void Save(string projectOutputPath, string defaultFileName)
        {
            using (new Measure(Logger, $"Saving results to output folder '{projectOutputPath}'"))
            {
                var  outputs           = Outputs.GetOutputs().ToList();
                var  dtsReferences     = new List <string>();
                bool addNoLibReference = false;

                foreach (var item in outputs)
                {
                    if (item.OutputType == TranslatorOutputType.TypeScript && item.OutputKind == TranslatorOutputKind.Reference)
                    {
                        string filePath = DefineOutputItemFullPath(item, projectOutputPath, defaultFileName);
                        string path     = "./" + FileHelper.GetRelativePath(filePath, projectOutputPath).Replace(Path.DirectorySeparatorChar, '/');

                        if (!dtsReferences.Contains(path))
                        {
                            dtsReferences.Add(path);
                        }

                        if (!addNoLibReference && H5.Translator.Validator.IsTypeFromH5Core(item.Assembly))
                        {
                            addNoLibReference = true;
                        }
                    }
                }

                Parallel.ForEach(outputs, item =>
                {
                    Logger.ZLogTrace("Output {0}", item.Name);

                    string filePath = DefineOutputItemFullPath(item, projectOutputPath, defaultFileName);

                    if (item.IsEmpty && (item.MinifiedVersion == null || item.MinifiedVersion.IsEmpty))
                    {
                        Logger.LogTrace("Output {0} is empty", filePath);
                        return;
                    }

                    var file = FileHelper.CreateFileDirectory(filePath);
                    Logger.LogTrace("Output full name {0}", file.FullName);

                    byte[] buffer = null;
                    string content;

                    if (item.OutputType == TranslatorOutputType.TypeScript && item.OutputKind == TranslatorOutputKind.ProjectOutput)
                    {
                        content = item.Content.GetContentAsString();
                        var sb  = new StringBuilder();
                        var nl  = Emitter.NEW_LINE;

                        if (addNoLibReference)
                        {
                            sb.Append("/// <reference no-default-lib=\"true\"/>" + nl);
                        }

                        foreach (var reference in dtsReferences)
                        {
                            sb.Append($"/// <reference path=\"{reference}\" />" + nl);
                        }

                        if (sb.Length > 0 && !content.StartsWith("/// <reference path="))
                        {
                            sb.Append(nl);
                        }

                        sb.Append(content);
                        SaveToFile(file.FullName, sb.ToString());
                    }
                    else if (CheckIfRequiresSourceMap(item))
                    {
                        content = item.Content.GetContentAsString();
                        content = GenerateSourceMap(file.FullName, content);

                        item.HasGeneratedSourceMap = true;

                        SaveToFile(file.FullName, content);
                    }
                    else
                    {
                        buffer = item.Content.Buffer;

                        if (buffer != null)
                        {
                            SaveToFile(file.FullName, null, buffer);
                        }
                        else
                        {
                            content = item.Content.GetContentAsString();
                            SaveToFile(file.FullName, content);
                        }
                    }
                });
            }
        }