private static async Task <int> RunAsync([NotNull] CommandLineOptions commandLineOptions) { WorkerOptions workerOptions = new WorkerOptions(commandLineOptions); try { Worker worker = new Worker(); await worker.CopyAsync(workerOptions, Console.WriteLine); } catch (Exception ex) { Console.WriteLine(ex); return(1); } return(0); }
/// <summary> /// Reads the source file content, performs the requested replacements and writes the result to all target folders. /// </summary> /// <param name="workerOptions">Determines what exactly has to be done.</param> /// <param name="reportProgressAction">An optional action that is used to report progress to the caller.</param> /// <returns>A <see cref="Task"/> representing the ongoing task.</returns> public async Task CopyAsync(WorkerOptions workerOptions, Action <string>?reportProgressAction) { IEnumerable <string> entries = Directory.EnumerateDirectories(workerOptions.TargetDirectory, workerOptions.TargetSearchPattern, workerOptions.SearchOption); if (entries == null) { throw new InvalidOperationException("No target directories found."); } string sourceFileContent = await this.ReadTextAsync(workerOptions.SourcePath); if (sourceFileContent == null) { throw new InvalidOperationException("No source file content found."); } IEnumerable <string> destinationPathList = entries.Select(e => Path.Combine(e, Path.GetFileName(workerOptions.SourcePath))); await this.WriteFilesAsync(destinationPathList, sourceFileContent, workerOptions.ReplaceList, reportProgressAction); }