public static void WriteAsync()

in src/Core/Vipr/FileWriter.cs [25:82]


        public static void WriteAsync(IEnumerable<TextFile> textFilesToWrite, string outputDirectoryPath = null)
        {
            if (!string.IsNullOrWhiteSpace(outputDirectoryPath) && !Directory.Exists(outputDirectoryPath))
                Directory.CreateDirectory(outputDirectoryPath);

            /* Write asynchronously in batches of 50
            * This prevents I/O from slowing down
            * from an overload of requests
            */
            //TODO: Allow this value to be set in the program configuration
            var batchSize = 50;
            var batchCount = 0;
            List<Task> tasks = new List<Task>();

            foreach (var file in textFilesToWrite)
            {
                tasks = new List<Task>();
                var filePath = file.RelativePath;

                if (!string.IsNullOrWhiteSpace(outputDirectoryPath))
                    filePath = Path.Combine(outputDirectoryPath, filePath);

                if (!String.IsNullOrWhiteSpace(Environment.CurrentDirectory) &&
                    !Path.IsPathRooted(filePath))
                    filePath = Path.Combine(Environment.CurrentDirectory, filePath);

                if (!Directory.Exists(Path.GetDirectoryName(filePath)))
                {
                    try
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    }
                    catch (IOException e)
                    {
                        Logger.Error("Failed to create directory for file", e);
                    }
                }

                // Create a new async task to write the file
                tasks.Add(WriteToDisk(filePath, file.Contents));
                batchCount++;

                // Reached the batch limit. Wait for all tasks to complete
                if (batchCount > batchSize)
                {
                    Task t = Task.WhenAll(tasks);
                    t.Wait();
                    batchCount = 0;
                }
            }

            // Final task wait
            if (tasks.Count > 0)
            {
                Task t = Task.WhenAll(tasks);
                t.Wait();
            }
        }