Ejemplo n.º 1
0
        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) && await DynamicApis.DirectoryExistsAsync(directory).ConfigureAwait(false) == false)
                {
                    await DynamicApis.DirectoryCreateDirectoryAsync(directory).ConfigureAwait(false);
                }

                var data = generator();
                if (!await DynamicApis.FileExistsAsync(path) || await DynamicApis.FileReadAllTextAsync(path) != data)
                {
                    await DynamicApis.FileWriteAllTextAsync(path, data).ConfigureAwait(false);

                    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);
        }
Ejemplo n.º 2
0
        protected async Task <bool> TryWriteFileOutputAsync(string path, IConsoleHost host, Func <string> generator)
        {
            if (!string.IsNullOrEmpty(path))
            {
                var directory = DynamicApis.PathGetDirectoryName(path);
                if (!string.IsNullOrEmpty(directory) && await DynamicApis.DirectoryExistsAsync(directory).ConfigureAwait(false) == false)
                {
                    await DynamicApis.DirectoryCreateDirectoryAsync(directory).ConfigureAwait(false);
                }

                var data = generator();
                await DynamicApis.FileWriteAllTextAsync(path, data).ConfigureAwait(false);

                host?.WriteMessage("Code has been successfully written to file.\n");

                return(true);
            }
            return(false);
        }