public static Task <bool> TryWriteDocumentOutputAsync(this IOutputCommand command, IConsoleHost host, NewLineBehavior newLineBehavior, Func <OpenApiDocument> generator) { return(TryWriteFileOutputAsync(command, command.OutputFilePath, host, newLineBehavior, () => command.OutputFilePath.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ? OpenApiYamlDocument.ToYaml(generator()) : generator().ToJson())); }
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)); }
public static Task <bool> TryWriteFileOutputAsync(this IOutputCommand command, IConsoleHost host, NewLineBehavior newLineBehavior, Func <string> generator) { return(TryWriteFileOutputAsync(command, command.OutputFilePath, host, newLineBehavior, generator)); }