void FillTemplateFile( string templateFile, IReadOnlyCollection <string> definitions = null, IReadOnlyDictionary <string, string> replacements = null) { var templateContent = ReadAllText(templateFile); WriteAllText(templateFile, TemplateUtility.FillTemplate(templateContent, definitions, replacements)); }
public static async Task <bool> DownloadExternalFile( string externalFile, int timeout, Action <string, string> warningSink, Action <string, string> errorSink) { try { var lines = File.ReadAllLines(externalFile).Where(x => !string.IsNullOrWhiteSpace(x)).ToList(); var uriConversion = Uri.TryCreate(lines.FirstOrDefault(), UriKind.Absolute, out var uri); ControlFlow.Assert(uriConversion, $"Could not parse URI for external file from first line of '{externalFile}'."); var outputFile = externalFile.Substring(0, externalFile.Length - 4); var previousHash = File.Exists(outputFile) ? FileSystemTasks.GetFileHash(outputFile) : null; var template = await HttpTasks.HttpDownloadStringAsync(uri.OriginalString); var replacements = lines.Skip(1) .Where(x => x.Contains('=')) .Select(x => x.Split('=')) .Where(x => x.Length == 2) .ToDictionary( x => $"_{x.First().Trim('_').ToUpperInvariant()}_", x => x.ElementAt(1)); var definitions = lines.Skip(1) .Where(x => !x.Contains('=') && !string.IsNullOrWhiteSpace(x)) .Select(x => x.ToUpperInvariant()) .ToList(); File.WriteAllText(outputFile, TemplateUtility.FillTemplate(template, definitions, replacements)); var newHash = FileSystemTasks.GetFileHash(outputFile); if (newHash != previousHash) { warningSink(externalFile, "External file has been updated."); } return(true); } catch (Exception exception) { errorSink(externalFile, exception.Message); return(false); } }
private async Task <bool> DownloadExternalFile(ExternalFilesData data) { try { var previousHash = File.Exists(data.OutputPath) ? FileSystemTasks.GetFileHash(data.OutputPath) : null; var template = (await HttpTasks.HttpDownloadStringAsync(data.Uri.OriginalString)).SplitLineBreaks(); TextTasks.WriteAllLines(data.OutputPath, TemplateUtility.FillTemplate(template, data.Tokens)); var newHash = FileSystemTasks.GetFileHash(data.OutputPath); if (newHash != previousHash) { LogWarning(message: $"External file '{data.OutputPath}' has been updated.", file: data.Identity); } return(true); } catch (Exception exception) { LogError(message: exception.Message, file: data.Identity); return(false); } }