public static Task <bool> TryWriteFileOutputAsync(this IOutputCommand command, string path, IConsoleHost host, NewLineBehavior newLineBehavior, Func <string> generator) { if (!string.IsNullOrEmpty(path)) { var directory = DynamicApis.PathGetDirectoryName(path); if (!string.IsNullOrEmpty(directory) && DynamicApis.DirectoryExists(directory) == false) { DynamicApis.DirectoryCreateDirectory(directory); } var data = generator(); data = data?.Replace("\r", "") ?? ""; data = newLineBehavior == NewLineBehavior.Auto ? data.Replace("\n", Environment.NewLine) : newLineBehavior == NewLineBehavior.CRLF ? data.Replace("\n", "\r\n") : data; if (!DynamicApis.FileExists(path) || DynamicApis.FileReadAllText(path) != data) { DynamicApis.FileWriteAllText(path, data); host?.WriteMessage("Code has been successfully written to file.\n"); } else { host?.WriteMessage("Code has been successfully generated but not written to file (no change detected).\n"); } return(Task.FromResult(true)); } return(Task.FromResult(false)); }
protected bool TryWriteFileOutput(string path, IConsoleHost host, Func <string> generator) { if (!string.IsNullOrEmpty(path)) { var directory = DynamicApis.PathGetDirectoryName(path); if (!string.IsNullOrEmpty(directory) && !DynamicApis.DirectoryExists(directory)) { DynamicApis.DirectoryCreateDirectory(directory); } DynamicApis.FileWriteAllText(path, generator()); host?.WriteMessage("Code has been successfully written to file.\n"); return(true); } return(false); }
public static async Task <bool> TryWriteFileOutputAsync(this IOutputCommand command, string path, IConsoleHost host, Func <string> generator) { if (!string.IsNullOrEmpty(path)) { var directory = DynamicApis.PathGetDirectoryName(path); if (!string.IsNullOrEmpty(directory) && DynamicApis.DirectoryExists(directory) == false) { DynamicApis.DirectoryCreateDirectory(directory); } var data = generator(); if (!DynamicApis.FileExists(path) || DynamicApis.FileReadAllText(path) != data) { DynamicApis.FileWriteAllText(path, data); host?.WriteMessage("Code has been successfully written to file.\n"); } else { host?.WriteMessage("Code has been successfully generated but not written to file (no change detected).\n"); } return(true); } return(false); }