private void GenerateResourceHeader(MemoryStream resourceBuffer, ResourceConfigItem resource, string basePath) { if (resource.Header == null) { Logger.ZLogTrace("Resource header is not specified."); return; } Logger.ZLogTrace("Writing header for resource config item {0}", resource.Name); var headerInfo = ResourceHeaderInfo; var headerContent = GetHeaderContent(resource, basePath); ApplyTokens(headerContent, headerInfo); if (headerContent.Length > 0) { var b = OutputEncoding.GetBytes(headerContent.ToString()); resourceBuffer.Write(b, 0, b.Length); NewLine(resourceBuffer); Logger.ZLogTrace("Wrote " + headerContent.Length + " symbols as a resource header"); } else { Logger.ZLogTrace("No header content written as it was empty"); } Logger.ZLogTrace("Done writing header for resource config item {0}", resource.Name); }
private void GenerateResourceFileRemark(StringBuilder resourceBuffer, ResourceConfigItem item, FileInfo file, string dirPathInFileName) { if (item.Remark != null) { this.Log.Trace("Inserting resource file remark"); var fileName = this.MakeStandardPath(file.Name); var filePath = this.MakeStandardPath(Path.Combine(dirPathInFileName, fileName)); var remarkInfo = new Dictionary <string, string>() { ["name"] = fileName, ["path"] = filePath }; var remarkBuffer = ApplyTokens(item.Remark, remarkInfo); remarkBuffer.Replace("\r\n", "\n"); var remarkLines = remarkBuffer.ToString().Split(new[] { "\n" }, StringSplitOptions.None); foreach (var remarkLine in remarkLines) { resourceBuffer.Append(remarkLine); NewLine(resourceBuffer); } } }
private void GenerateResourseHeader(StringBuilder resourceBuffer, ResourceConfigItem resource, string basePath) { if (resource.Header == null) { this.Log.Trace("Resource header is not specified."); return; } this.Log.Trace("Writing header for resource config item " + resource.Name); var headerInfo = ResourceHeaderInfo; var headerContent = GetHeaderContent(resource, basePath); ApplyTokens(headerContent, headerInfo); if (headerContent.Length > 0) { resourceBuffer.Append(headerContent); NewLine(resourceBuffer); this.Log.Trace("Wrote " + headerContent.Length + " symbols as a resource header"); } else { this.Log.Trace("No header content written as it was empty"); } this.Log.Trace("Done writing header for resource config item " + resource.Name); }
private void GenerateResourceFileRemark(MemoryStream resourceBuffer, ResourceConfigItem item, string fileName, string dirPathInFileName) { if (item.Remark != null) { Logger.ZLogTrace("Inserting resource file remark"); var filePath = MakeStandardPath(Path.Combine(dirPathInFileName, fileName)); var remarkInfo = new Dictionary <string, string>() { ["name"] = fileName, ["path"] = filePath }; var remarkBuffer = ApplyTokens(item.Remark, remarkInfo); remarkBuffer.Replace(Emitter.CRLF, Emitter.NEW_LINE); var remarkLines = remarkBuffer.ToString().Split(new[] { Emitter.NEW_LINE }, StringSplitOptions.None); foreach (var remarkLine in remarkLines) { var b = OutputEncoding.GetBytes(remarkLine); resourceBuffer.Write(b, 0, b.Length); NewLine(resourceBuffer); } } }
private void ApplyDefaultResourceConfigSetting(ResourceConfigItem defaultSetting, ResourceConfigItem current) { if (!current.Extract.HasValue) { current.Extract = defaultSetting.Extract; } if (!current.Inject.HasValue) { current.Inject = defaultSetting.Inject; } if (current.Output == null) { current.Output = defaultSetting.Output; } if (current.Files == null && defaultSetting.Files != null) { current.Files = defaultSetting.Files.Select(x => x).ToArray(); } if (current.Header == null) { current.Header = defaultSetting.Header; } if (current.Remark == null) { current.Remark = defaultSetting.Remark; } }
private string CheckInvalidCharacters(ResourceConfigItem resource, string s, ICollection <char> invalidChars) { if (s == null) { return(null); } if (s.Select(x => invalidChars.Contains(x)).Where(x => x).Any()) { var message = "There is invalid path character contained in resource.output setting = " + s + " for resource " + resource.Name; if (resource.Silent != true) { throw new ArgumentException(message); } else { this.Log.Trace(message); s = null; } } return(s); }
private void ExtractResource(string path, string fileName, ResourceConfigItem resource, byte[] code) { Logger.ZLogTrace("Extracting resource {0}", resource.Name); if (!resource.Extract.HasValue || !resource.Extract.Value) { Logger.ZLogTrace("Skipping extracting resource as no extract option enabled for this resource"); return; } try { var fullPath = Path.GetFullPath(Path.Combine(path, fileName)); Logger.ZLogTrace("Writing resource {0}", resource.Name + " into " + fullPath); EnsureDirectoryExists(path); File.WriteAllBytes(fullPath, code); AddExtractedResourceOutput(resource, code); Logger.ZLogTrace("Done writing resource into file"); } catch (Exception ex) { Logger.ZLogError(ex, "Error extracting resource {0}", resource.Name); throw; } }
protected virtual void AddExtractedResourceOutput(ResourceConfigItem resource, byte[] code) { if (resource.Load ?? true) { Emitter.AddOutputItem(Outputs.ResourcesForHtml, resource.Name, code, TranslatorOutputKind.Resource, location: resource.Output); } }
private void ValidateResourceSettings(ResourceConfigItem defaultSetting, IEnumerable <ResourceConfigItem> rawNonDefaultResources) { var defaultExtract = defaultSetting.Extract ?? false; var rawNonDefaultResourcesWithExtractAndNoOutput = rawNonDefaultResources .Where(x => x.Output == null && (x.Extract == true || defaultExtract)); if (defaultSetting.Output != null && rawNonDefaultResourcesWithExtractAndNoOutput.Count() > 1) { string defaultOutputFileName = null; try { defaultOutputFileName = Path.GetFileName(defaultSetting.Output); } catch (Exception) { } if (!string.IsNullOrEmpty(defaultOutputFileName)) { this.Log.Error("The resource config setting has a default output setting " + defaultSetting.Output + " containing file part " + defaultOutputFileName + " .However, there are several resources with no output setting defined and active extract option." + " It means the resources will be overwritten by each other."); } } }
private byte[] CheckResourceOnBomAndAddToBuffer(MemoryStream buffer, ResourceConfigItem item, FileInfo file) { var content = File.ReadAllBytes(file.FullName); if (content.Length > 0) { var bomLength = !item.RemoveBom.HasValue || item.RemoveBom.Value ? GetBomLength(content) : 0; if (bomLength > 0) { this.Log.Trace("Found BOM symbols (" + bomLength + " byte length)"); } if (bomLength < content.Length) { buffer.Write(content, bomLength, content.Length - bomLength); } else { this.Log.Trace("Skipped resource as it contains only BOM"); } } return(content); }
public void GetResourceOutputPath(string projectPath, ResourceConfigItem resource, ref string resourceOutputFileName, ref string resourceOutputDirName) { this.Log.Trace("Checking output path setting " + resource.Output); try { var pathParts = this.GetPathComponents(resource.Output); resourceOutputDirName = pathParts[0]; this.Log.Trace("Resource output setting directory relative to project root is " + resourceOutputDirName); resourceOutputFileName = pathParts[1]; this.Log.Trace("Resource output setting file name is " + resourceOutputFileName); if (resourceOutputDirName != null) { this.Log.Trace("Checking resource output directory on invalid characters"); resourceOutputDirName = CheckInvalidCharacters(resource, resourceOutputDirName, this.InvalidPathChars); } if (resourceOutputDirName != null) { this.Log.Trace("Getting absolute resource output directory"); resourceOutputDirName = Path.Combine(projectPath, resourceOutputDirName); this.Log.Trace("Resource output directory is " + resourceOutputDirName); if (resourceOutputFileName != null) { this.Log.Trace("Checking resource output file name on invalid characters"); resourceOutputFileName = CheckInvalidCharacters(resource, resourceOutputFileName, Path.GetInvalidFileNameChars()); if (resourceOutputFileName == null) { this.Log.Trace("Setting resource output directory to null as file name part contains invalid characters"); resourceOutputDirName = null; } } } else { this.Log.Trace("Setting resource output file name to null as directory part contains invalid characters"); resourceOutputFileName = null; } } catch (Exception ex) when(ex is ArgumentException || ex is ArgumentNullException || ex is PathTooLongException) { this.Log.Trace("Could not extract directory name from resource output setting"); this.Log.Error(ex.ToString()); if (resource.Silent != true) { throw; } resourceOutputDirName = null; resourceOutputFileName = null; } }
private StringBuilder GetHeaderContent(ResourceConfigItem resource, string basePath) { Logger.ZLogTrace("Getting header content..."); var isFileName = false; string convertedHeaderPath = null; string resourceHeader = resource.Header; try { Logger.ZLogTrace("Checking if resource header setting is a file path..."); Logger.ZLogTrace("Converting slashes in resource header setting..."); var configHelper = new ConfigHelper(); convertedHeaderPath = configHelper.ConvertPath(resourceHeader); Logger.ZLogTrace("Checking if " + convertedHeaderPath + " contains file name..."); var headerFileName = Path.GetFileName(convertedHeaderPath); isFileName = !string.IsNullOrEmpty(headerFileName); } catch (ArgumentException ex) { Logger.ZLogTrace(ex.ToString()); } if (isFileName) { Logger.ZLogTrace("Checking if header content file exists"); var fullHeaderPath = Path.Combine(basePath, convertedHeaderPath); if (File.Exists(fullHeaderPath)) { Logger.ZLogTrace("Reading header content file at " + fullHeaderPath); using (var m = new StreamReader(fullHeaderPath, OutputEncoding, true)) { var sb = new StringBuilder(m.ReadToEnd()); if (m.CurrentEncoding != OutputEncoding) { Logger.ZLogTrace("Converting resource header file {0} from encoding {1} to encoding {2}", fullHeaderPath, m.CurrentEncoding.EncodingName, OutputEncoding.EncodingName); } Logger.ZLogTrace("Read {0} symbols from the header file {1}", sb.Length, fullHeaderPath); return(sb); } } Logger.ZLogWarning("Could not find header content file at {0} for resource {1}", fullHeaderPath, resource.Name); } Logger.ZLogTrace("Considered resource header setting to be a header content"); return(new StringBuilder(resourceHeader)); }
private byte[] CheckResourceOnBomAndAddToBuffer(MemoryStream buffer, ResourceConfigItem item, FileInfo file, bool oneFileResource) { byte[] content; if (oneFileResource) { this.Log.Trace("Reading resource file " + file.FullName + " as one-file-resource"); content = File.ReadAllBytes(file.FullName); } else { this.Log.Trace("Reading resource file " + file.FullName + " via StreamReader with byte order mark detection option"); using (var m = new StreamReader(file.FullName, Translator.OutputEncoding, true)) { content = Translator.OutputEncoding.GetBytes(m.ReadToEnd()); if (m.CurrentEncoding != OutputEncoding) { this.Log.Info("Converting resource file " + file.FullName + " from encoding " + m.CurrentEncoding.EncodingName + " into default encoding" + Translator.OutputEncoding.EncodingName); } } } if (content.Length > 0) { var checkBom = (oneFileResource && item.RemoveBom == true) || (!oneFileResource && (!item.RemoveBom.HasValue || item.RemoveBom.Value)); var bomLength = checkBom ? GetBomLength(content) : 0; if (bomLength > 0) { this.Log.Trace("Found BOM symbols (" + bomLength + " byte length)"); } if (bomLength < content.Length) { buffer.Write(content, bomLength, content.Length - bomLength); } else { this.Log.Trace("Skipped resource as it contains only BOM"); } } return(content); }
private StringBuilder GetHeaderContent(ResourceConfigItem resource, string basePath) { this.Log.Trace("Getting header content..."); var isFileName = false; string convertedHeaderPath = null; string resourceHeader = resource.Header; try { this.Log.Trace("Checking if resource header setting is a file path..."); this.Log.Trace("Converting slashes in resource header setting..."); var configHelper = new ConfigHelper(); convertedHeaderPath = configHelper.ConvertPath(resourceHeader); this.Log.Trace("Checking if " + convertedHeaderPath + " contains file name..."); var headerFileName = Path.GetFileName(convertedHeaderPath); isFileName = !string.IsNullOrEmpty(headerFileName); } catch (ArgumentException ex) { this.Log.Trace(ex.ToString()); } if (isFileName) { this.Log.Trace("Checking if header content file exists"); var fullHeaderPath = Path.Combine(basePath, convertedHeaderPath); if (File.Exists(fullHeaderPath)) { this.Log.Trace("Reading header content file at " + fullHeaderPath); var sb = new StringBuilder(File.ReadAllText(fullHeaderPath, OutputEncoding)); this.Log.Trace("Read " + sb.Length + " symbols from the header file " + fullHeaderPath); return(sb); } this.Log.Warn("Could not find header content file at " + fullHeaderPath + "for resource " + resource.Name); } this.Log.Trace("Considered resource header setting to be a header content"); return(new StringBuilder(resourceHeader)); }
private void ExtractResource(string outputPath, string projectPath, ResourceConfigItem resource, byte[] code) { if (!resource.Extract.HasValue || !resource.Extract.Value) { return; } this.Log.Trace("Extracting resource " + resource.Name); string resourceOutputFileName = null; string resourceOutputDirName = null; if (resource.Output != null) { this.GetResourceOutputPath(outputPath, resource, ref resourceOutputFileName, ref resourceOutputDirName); } if (resourceOutputDirName == null) { resourceOutputDirName = outputPath; this.Log.Trace("Using project output path " + resourceOutputDirName); } if (string.IsNullOrWhiteSpace(resourceOutputFileName)) { resourceOutputFileName = resource.Name; this.Log.Trace("Using resource name as file name " + resourceOutputFileName); } try { var path = Path.Combine(resourceOutputDirName, resourceOutputFileName); this.Log.Trace("Writing resource " + resource.Name + " into " + path); this.EnsureDirectoryExists(resourceOutputDirName); File.WriteAllBytes(path, code); this.Log.Trace("Done writing resource into file"); } catch (Exception ex) { this.Log.Error(ex.ToString()); throw; } }
private Tuple <string, string> GetFullPathForResource(string outputPath, ResourceConfigItem resource) { string resourceOutputFileName = null; string resourceOutputDirName = null; if (resource.Output != null) { GetResourceOutputPath(outputPath, resource, ref resourceOutputFileName, ref resourceOutputDirName); } if (resourceOutputDirName == null) { resourceOutputDirName = outputPath; Logger.ZLogTrace("Using project output path " + resourceOutputDirName); } if (string.IsNullOrWhiteSpace(resourceOutputFileName)) { resourceOutputFileName = resource.Name; Logger.ZLogTrace("Using resource name as file name " + resourceOutputFileName); } return(Tuple.Create(resourceOutputDirName, resourceOutputFileName)); }
private void ReadResourseFiles(string outputPath, MemoryStream buffer, ResourceConfigItem item) { this.Log.Trace("Reading resource with " + item.Files.Length + " items"); foreach (var fileName in item.Files) { this.Log.Trace("Reading resource item(s) in location " + fileName); try { string directoryPath; directoryPath = outputPath; var dirPathInFileName = this.GetPathComponents(fileName)[0]; var filePathCleaned = fileName; if (!string.IsNullOrEmpty(dirPathInFileName)) { directoryPath = Path.Combine(directoryPath, dirPathInFileName); this.Log.Trace("Cleaned folder path part: " + dirPathInFileName + " from location: " + fileName + " and added to the directory path: " + directoryPath); filePathCleaned = Path.GetFileName(filePathCleaned); } var directory = new DirectoryInfo(directoryPath); if (!directory.Exists) { throw new InvalidOperationException("Could not find any folder: " + directory.FullName + " for resource " + item.Name + " and location " + fileName); } this.Log.Trace("Searching files for resources in folder: " + directoryPath); var files = directory.GetFiles(filePathCleaned, SearchOption.TopDirectoryOnly); if (!files.Any()) { throw new InvalidOperationException("Could not find any file in folder: " + directory.FullName + " for resource " + item.Name + " and location " + fileName); } //var needNewLine = files.Length > 0; foreach (var file in files) { //if (needNewLine) //{ // NewLine(buffer); //} GenerateResourceFileRemark(buffer, item, file, dirPathInFileName); this.Log.Trace("Reading resource item at " + file.FullName); var content = File.ReadAllBytes(file.FullName); if (content.Length > 0) { var bomLength = !item.RemoveBom.HasValue || item.RemoveBom.Value ? GetBomLength(content) : 0; if (bomLength > 0) { this.Log.Trace("Found BOM symbols (" + bomLength + " byte length)"); } if (bomLength < content.Length) { buffer.Write(content, bomLength, content.Length - bomLength); } else { this.Log.Trace("Skipped resource as it contains only BOM"); } } this.Log.Trace("Read " + content.Length + " bytes"); } } catch (Exception ex) { this.Log.Error(ex.ToString()); throw; } } }
public void PrepareResourcesConfig() { Logger.ZLogTrace("Preparing resources config..."); var config = AssemblyInfo.Resources; var rawResources = config.Items; var resources = new List <ResourceConfigItem>(); ResourceConfigItem defaultSetting = null; if (rawResources != null) { Array.ForEach(rawResources, (x) => { if (x.Name != null) { var parts = x.Name.Split(new[] { '#' }, StringSplitOptions.None); if (parts.Length > 1) { x.Assembly = parts[0]; x.Name = parts[1]; } } }); var defaultResources = rawResources.Where(x => x.Name == null); if (defaultResources.Count() > 1) { Logger.ZLogError("There are more than one default resource in the configuration setting file (resources section). Will use the first occurrence as a default resource settings"); } defaultSetting = defaultResources.FirstOrDefault(); if (defaultSetting != null) { Logger.ZLogTrace("The resources config section has a default settings"); defaultSetting.SetDefaulValues(); var rawNonDefaultResources = rawResources.Where(x => x.Name != null); ValidateResourceSettings(defaultSetting, rawNonDefaultResources); foreach (var resource in rawNonDefaultResources) { ApplyDefaultResourceConfigSetting(defaultSetting, resource); resources.Add(resource); } } else { Array.ForEach(rawResources, x => x.SetDefaulValues()); resources.AddRange(rawResources); } Logger.ZLogTrace("The resources config section has {0} non-default settings", resources.Count); } var toEmbed = resources.Where(x => x.Files != null && x.Files.Count() > 0).ToArray(); var toExtract = resources.Where(x => x.Files == null || x.Files.Count() <= 0).ToArray(); config.Prepare(defaultSetting, toEmbed, toExtract); Logger.ZLogTrace("Done preparing resources config"); return; }
protected virtual void AddExtractedResourceOutput(ResourceConfigItem resource, byte[] code) { Emitter.AddOutputItem(this.Outputs.Resources, resource.Name, code, TranslatorOutputKind.Resource, location: resource.Output); }
public void GetResourceOutputPath(string basePath, ResourceConfigItem resource, ref string resourceOutputFileName, ref string resourceOutputDirName) { GetResourceOutputPath(basePath, resource.Output, resource.Name, resource.Silent, ref resourceOutputFileName, ref resourceOutputDirName); }
private void ReadResourseFiles(string outputPath, StringBuilder resourceBuffer, ResourceConfigItem item) { this.Log.Trace("Reading resource with " + item.Files.Length + " items"); foreach (var fileName in item.Files) { this.Log.Trace("Reading resource item(s) in location " + fileName); try { string directoryPath; directoryPath = outputPath; var dirPathInFileName = this.GetPathComponents(fileName)[0]; var filePathCleaned = fileName; if (!string.IsNullOrEmpty(dirPathInFileName)) { directoryPath = Path.Combine(directoryPath, dirPathInFileName); this.Log.Trace("Cleaned folder path part: " + dirPathInFileName + " from location: " + fileName + " and added to the directory path: " + directoryPath); filePathCleaned = Path.GetFileName(filePathCleaned); } var directory = new DirectoryInfo(directoryPath); if (!directory.Exists) { throw new InvalidOperationException("Could not find any folder: " + directory.FullName + " for resource " + item.Name + " and location " + fileName); } this.Log.Trace("Searching files for resources in folder: " + directoryPath); var files = directory.GetFiles(filePathCleaned, SearchOption.TopDirectoryOnly); if (!files.Any()) { throw new InvalidOperationException("Could not find any file in folder: " + directory.FullName + " for resource " + item.Name + " and location " + fileName); } foreach (var file in files) { NewLine(resourceBuffer); GenerateResourceFileRemark(resourceBuffer, item, file, dirPathInFileName); this.Log.Trace("Reading resource item at " + file.FullName); var content = File.ReadAllText(file.FullName); resourceBuffer.Append(content); this.Log.Trace("Read " + content.Length + " bytes"); } } catch (Exception ex) { this.Log.Error(ex.ToString()); throw; } } }
private void CheckConsoleConfigSetting(List <ResourceConfigItem> resources, ResourceConfigItem @default) { this.Log.Trace("CheckConsoleConfigSetting..."); var consoleResourceName = "bridge.console.js"; var consoleResourceMinifiedName = FileHelper.GetMinifiedJSFileName(consoleResourceName); var consoleFormatted = resources.Where(x => x.Name == consoleResourceName && (x.Assembly == null || x.Assembly == Translator.Bridge_ASSEMBLY)).FirstOrDefault(); var consoleMinified = resources.Where(x => x.Name == consoleResourceMinifiedName && (x.Assembly == null || x.Assembly == Translator.Bridge_ASSEMBLY)).FirstOrDefault(); if (this.AssemblyInfo.Console.Enabled != true) { this.Log.Trace("Switching off Bridge Console..."); if (consoleFormatted == null) { consoleFormatted = new ResourceConfigItem() { Name = consoleResourceName }; this.Log.Trace("Adding resource setting for " + consoleResourceName); resources.Add(consoleFormatted); } else { if (this.AssemblyInfo.Console.Enabled.HasValue) { this.Log.Trace("Overriding resource setting for " + consoleResourceName + " as bridge.json has console option explicitly"); } else { this.Log.Trace("Not overriding resource setting for " + consoleResourceName + " as bridge.json does NOT have console option explicitly"); consoleFormatted = null; } } if (consoleFormatted != null) { consoleFormatted.Output = null; consoleFormatted.Extract = false; consoleFormatted.Inject = false; consoleFormatted.Files = new string[0]; } if (consoleMinified == null) { consoleMinified = new ResourceConfigItem() { Name = consoleResourceMinifiedName }; this.Log.Trace("Adding resource setting for " + consoleResourceMinifiedName); resources.Add(consoleMinified); } else { if (this.AssemblyInfo.Console.Enabled.HasValue) { this.Log.Trace("Overriding resource setting for " + consoleResourceMinifiedName + " as bridge.json has console option explicitly"); } else { this.Log.Trace("Not overriding resource setting for " + consoleResourceMinifiedName + " as bridge.json does NOT have console option explicitly"); consoleMinified = null; } } if (consoleMinified != null) { consoleMinified.Output = null; consoleMinified.Extract = false; consoleMinified.Inject = false; consoleMinified.Files = new string[0]; } this.Log.Trace("Switching off Bridge Console done"); } else { if (consoleFormatted != null) { if (consoleFormatted.Extract != true) { consoleFormatted.Extract = true; this.Log.Trace("Setting resources.extract = true for " + consoleResourceName + " as bridge.json has console option has true explicitly"); } } else { if (@default != null && @default.Extract != true) { consoleFormatted = new ResourceConfigItem() { Name = consoleResourceName, Extract = true, Inject = false }; this.Log.Trace("Adding resource setting for " + consoleResourceName + " as default resource has extract != true"); resources.Add(consoleFormatted); } } if (consoleMinified != null) { if (consoleMinified.Extract != true) { consoleMinified.Extract = true; this.Log.Trace("Setting resources.extract = true for " + consoleResourceMinifiedName + " as bridge.json has console option has true explicitly"); } } else { if (@default != null && @default.Extract != true) { consoleMinified = new ResourceConfigItem() { Name = consoleResourceMinifiedName, Extract = true, Inject = false }; this.Log.Trace("Adding resource setting for " + consoleResourceMinifiedName + " as default resource has extract != true"); resources.Add(consoleMinified); } } } this.Log.Trace("CheckConsoleConfigSetting done"); }
/// <summary> /// Reads files for the resource item and return a value indication whether it is a resource for SourceMap generation /// </summary> /// <param name="outputPath">Project output path</param> /// <param name="buffer"></param> /// <param name="item">Resource</param> /// <returns>Bool value indication whether it is a resource for SourceMap generation</returns> private bool ReadResourceFiles(string outputPath, MemoryStream buffer, ResourceConfigItem item) { Logger.ZLogTrace("Reading resource with " + item.Files.Length + " items"); var needSourceMap = false; var useDefaultEncoding = item.Files.Length > 1; foreach (var fileName in item.Files) { Logger.ZLogTrace("Reading resource item(s) in location " + fileName); try { string directoryPath; directoryPath = outputPath; var dirPathInFileName = FileHelper.GetDirectoryAndFilenamePathComponents(fileName)[0]; var filePathCleaned = fileName; if (!string.IsNullOrEmpty(dirPathInFileName)) { directoryPath = Path.Combine(directoryPath, dirPathInFileName); Logger.ZLogTrace("Cleaned folder path part: " + dirPathInFileName + " from location: " + fileName + " and added to the directory path: " + directoryPath); filePathCleaned = Path.GetFileName(filePathCleaned); } directoryPath = Path.GetFullPath(directoryPath); var fullFileName = Path.Combine(directoryPath, filePathCleaned); GenerateResourceFileRemark(buffer, item, filePathCleaned, dirPathInFileName); var outputItem = FindTranslatorOutputItem(fullFileName); if (outputItem != null) { Logger.ZLogTrace("Found required file for resources in Outputs " + fullFileName); if (outputItem.HasGeneratedSourceMap) { Logger.ZLogTrace("The output item HasGeneratedSourceMap so that the resource requires SourceMap also"); needSourceMap = true; } var content = outputItem.Content.GetContentAsBytes(); buffer.Write(content, 0, content.Length); } else { var directory = new DirectoryInfo(directoryPath); if (!directory.Exists) { throw new InvalidOperationException($"Missing resource from json config file, could not find folder: '{directory.FullName}' for resource '{item.Name}' with file name '{fileName}'"); } Logger.ZLogTrace("Searching files for resources in folder: " + directoryPath); var file = directory.GetFiles(filePathCleaned, SearchOption.TopDirectoryOnly).FirstOrDefault(); if (file == null) { throw new InvalidOperationException($"Missing resource from json config file, could not find in folder '{directory.FullName}' the required resource '{item.Name}' with file name '{fileName}'"); } Logger.ZLogTrace("Reading resource item at " + file.FullName); var resourceAsOneFile = item.Header == null && item.Remark == null && item.Files.Length <= 1; var content = CheckResourceOnBomAndAddToBuffer(buffer, item, file, resourceAsOneFile); Logger.ZLogTrace("Read " + content.Length + " bytes"); } } catch (Exception ex) { Logger.ZLogError(ex, "Error reading resources files for item {0}", item); throw; } } return(needSourceMap); }
/// <summary> /// Reads files for the resource item and return a value indication whether it is a resource for SourceMap generation /// </summary> /// <param name="outputPath">Project output path</param> /// <param name="buffer"></param> /// <param name="item">Resource</param> /// <returns>Bool value indication whether it is a resource for SourceMap generation</returns> private bool ReadResourseFiles(string outputPath, MemoryStream buffer, ResourceConfigItem item) { this.Log.Trace("Reading resource with " + item.Files.Length + " items"); var needSourceMap = false; foreach (var fileName in item.Files) { this.Log.Trace("Reading resource item(s) in location " + fileName); try { string directoryPath; directoryPath = outputPath; var dirPathInFileName = this.GetPathComponents(fileName)[0]; var filePathCleaned = fileName; if (!string.IsNullOrEmpty(dirPathInFileName)) { directoryPath = Path.Combine(directoryPath, dirPathInFileName); this.Log.Trace("Cleaned folder path part: " + dirPathInFileName + " from location: " + fileName + " and added to the directory path: " + directoryPath); filePathCleaned = Path.GetFileName(filePathCleaned); } directoryPath = Path.GetFullPath(directoryPath); var fullFileName = Path.Combine(directoryPath, filePathCleaned); GenerateResourceFileRemark(buffer, item, filePathCleaned, dirPathInFileName); var outputItem = this.FindTranslatorOutputItem(fullFileName); if (outputItem != null) { this.Log.Trace("Found required file for resources in Outputs " + fullFileName); if (outputItem.HasGeneratedSourceMap) { this.Log.Trace("The output item HasGeneratedSourceMap so that the resource requires SourceMap also"); needSourceMap = true; } var content = outputItem.Content.GetContentAsBytes(); buffer.Write(content, 0, content.Length); } else { var directory = new DirectoryInfo(directoryPath); if (!directory.Exists) { throw new InvalidOperationException("Could not find any folder: " + directory.FullName + " for resource " + item.Name + " and location " + fileName); } this.Log.Trace("Searching files for resources in folder: " + directoryPath); var file = directory.GetFiles(filePathCleaned, SearchOption.TopDirectoryOnly).FirstOrDefault(); if (file == null) { throw new InvalidOperationException("Could not find any file in folder: " + directory.FullName + " for resource " + item.Name + " and location " + fileName); } this.Log.Trace("Reading resource item at " + file.FullName); var content = CheckResourceOnBomAndAddToBuffer(buffer, item, file); this.Log.Trace("Read " + content.Length + " bytes"); } } catch (Exception ex) { this.Log.Error(ex.ToString()); throw; } } return(needSourceMap); }