/// <summary> /// Procesa un bloque de sentencias /// </summary> private async Task ProcessBlockAsync(BlockLogModel parent, BlockSentence sentence, CancellationToken cancellationToken) { using (BlockLogModel block = parent.CreateBlock(LogModel.LogType.Info, sentence.GetMessage("Start block"))) { await ExecuteAsync(block, sentence.Sentences, cancellationToken); } }
/// <summary> /// Procesa un script /// </summary> private async Task ProcessScriptAsync(BlockLogModel parent, ExecuteScriptSentence sentence, NormalizedDictionary <object> parameters, CancellationToken cancellationToken) { // Ejecuta la sentencia using (BlockLogModel block = parent.CreateBlock(LogModel.LogType.Info, $"Start execute script")) { try { PowerShellController controller = new PowerShellController(this, Step); // Ejecuta el script await controller.ExecuteAsync(block, sentence, parameters); // Muestra los errores if (controller.Errors.Count > 0) { AddErrors(block, controller.Errors); } else { block.Info("End script execution"); } } catch (Exception exception) { AddError(block, $"Error when execute script Powershell. {exception.Message}"); } } }
/// <summary> /// Procesa una copia de archivos /// </summary> private async Task ProcessCopyAsync(BlockLogModel parent, CopySentence sentence) { using (BlockLogModel block = parent.CreateBlock(LogModel.LogType.Info, $"Start copy from '{sentence.Source}' to '{sentence.Target}'")) { string source = Step.Project.GetFullFileName(sentence.Source); string target = Step.Project.GetFullFileName(sentence.Target); try { if (Directory.Exists(source)) { await LibHelper.Files.HelperFiles.CopyPathAsync(source, target, sentence.Mask, sentence.Recursive, sentence.FlattenPaths); } else if (File.Exists(source)) { if (!string.IsNullOrWhiteSpace(Path.GetExtension(target))) { await LibHelper.Files.HelperFiles.CopyFileAsync(source, target); } else { await LibHelper.Files.HelperFiles.CopyFileAsync(source, Path.Combine(target, Path.GetFileName(source))); } } else { AddError(block, $"Cant find source file or path '{source}'"); } } catch (Exception exception) { AddError(block, $"Error when copy '{source}' to '{target}'. {exception.Message}"); } } }
/// <summary> /// Descarga un archivo del blob /// </summary> private async Task ProcessDownloadAsync(BlockLogModel parent, DownloadBlobSentence sentence) { using (BlockLogModel block = parent.CreateBlock(LogModel.LogType.Info, $"Start downloading from '{sentence.Source.ToString()}'")) { CloudConnection connection = GetConnection(sentence.StorageKey); if (connection == null) { AddError(block, $"Can't find the connection for '{sentence.StorageKey}'"); } else { try { // Descarga el archivo using (ICloudStorageManager manager = new StorageManager().OpenAzureStorageBlob(connection.StorageConnectionString)) { string fileName = Step.Project.GetFullFileName(sentence.FileName); // Crea el directorio LibHelper.Files.HelperFiles.MakePath(System.IO.Path.GetDirectoryName(fileName)); // Descarga el archivo await manager.DownloadAsync(GetContainerName(sentence.Source.Container), sentence.Source.Blob, fileName); } // Log block.Info($"Downloaded file '{sentence.FileName}' to '{sentence.Source.ToString()}'"); } catch (Exception exception) { AddError(block, $"Error when download '{sentence.FileName}' to '{sentence.Source.ToString()}'", exception); } } } }
/// <summary> /// Procesa la sentencia de descarga de una carpeta /// </summary> private async Task ProcessDownloadFolderAsync(BlockLogModel parent, DownloadBlobFolderSentence sentence, CancellationToken cancellationToken) { using (BlockLogModel block = parent.CreateBlock(LogModel.LogType.Info, $"Start downloading from '{sentence.Source.ToString()}'")) { CloudConnection connection = GetConnection(sentence.StorageKey); if (connection == null) { AddError(block, $"Can't find the connection for '{sentence.StorageKey}"); } else { try { string path = Step.Project.GetFullFileName(sentence.Path); string container = GetContainerName(sentence.Source.Container); // Log block.Info($"Start download '{container}/{sentence.Source.Blob}' to '{path}'"); // Crea la carpeta LibHelper.Files.HelperFiles.MakePath(path); // Descarga la carpeta using (ICloudStorageManager manager = new StorageManager().OpenAzureStorageBlob(connection.StorageConnectionString)) { List <LibBlobStorage.Metadata.BlobModel> blobs = await manager.ListBlobsAsync(container, sentence.Source.Blob); foreach (LibBlobStorage.Metadata.BlobModel blob in blobs) { if (blob.Length != 0 && !cancellationToken.IsCancellationRequested) { string fileName = System.IO.Path.Combine(path, blob.LocalFileName); // Log block.Info($"Download '{blob.FullFileName}'"); // Crea el directorio LibHelper.Files.HelperFiles.MakePath(System.IO.Path.GetDirectoryName(fileName)); // Descarga el archivo await manager.DownloadAsync(container, blob.FullFileName, fileName); } } } // Log block.Info($"End download '{container}/{sentence.Source.Blob}' to '{path}'"); } catch (Exception exception) { AddError(block, $"Error when download to '{sentence.Path}'", exception); } } } }
/// <summary> /// Procesa una copia de archivos /// </summary> private async Task ProcessCopyAsync(BlockLogModel parent, CopyBlobSentence sentence, CancellationToken cancellationToken) { string processType = sentence.Move ? "move" : "copy"; string blobTarget = sentence.Target.Blob; // Obtiene el nombre del archivo destino si hay que transformarlo en un nombre único if (sentence.TransformFileName) { blobTarget = System.IO.Path.GetFileNameWithoutExtension(sentence.Target.Blob) + $" {DateTime.UtcNow:yyyy-MM-dd HH_mm_ss_ms}" + System.IO.Path.GetExtension(sentence.Target.Blob); } // Procesa la instrucción using (BlockLogModel block = parent.CreateBlock(LogModel.LogType.Info, $"Start {processType} from '{sentence.Source.ToString()}' to '{sentence.Target.Container}/{blobTarget}'")) { CloudConnection connection = GetConnection(sentence.StorageKey); if (connection == null) { AddError(block, $"Can't find the connection for '{sentence.StorageKey}'"); } else { try { // Copia / mueve el archivo using (ICloudStorageManager manager = new StorageManager().OpenAzureStorageBlob(connection.StorageConnectionString)) { if (sentence.Move) { await manager.MoveAsync(GetContainerName(sentence.Source.Container), sentence.Target.Blob, GetContainerName(sentence.Target.Container), blobTarget, cancellationToken); } else { await manager.CopyAsync(GetContainerName(sentence.Source.Container), sentence.Target.Blob, GetContainerName(sentence.Target.Container), blobTarget, cancellationToken); } } // Log block.Info($"End {processType} from '{sentence.Source.ToString()}' to '{sentence.Target.Container}/{blobTarget}'"); } catch (Exception exception) { AddError(block, $"Error when {processType} from '{sentence.Source.ToString()}' to '{sentence.Target.Container}/{blobTarget}'", exception); } } } }
/// <summary> /// Convierte un archivo /// </summary> private async Task ProcessConvertFileAsync(BlockLogModel parent, ConvertFileSentence sentence, CancellationToken cancellationToken) { using (BlockLogModel block = parent.CreateBlock(LogModel.LogType.Info, $"Start convert file '{sentence.FileNameSource}' to {sentence.FileNameTarget}")) { string source = Step.Project.GetFullFileName(sentence.FileNameSource); string target = Step.Project.GetFullFileName(sentence.FileNameTarget); if (!File.Exists(source)) { AddError(block, $"Cant find the file '{source}'"); } else { await ConvertFileAsync(block, source, target, GetExcelOptions(sentence.ExcelWithHeader, sentence.ExcelSheetIndex), cancellationToken); } } }
/// <summary> /// Procesa un borrado de archivos /// </summary> private async Task ProcessDeleteAsync(BlockLogModel parent, DeleteSentence sentence) { // Evita el warning await Task.Delay(1); // Borra los archivos / directorios using (BlockLogModel block = parent.CreateBlock(LogModel.LogType.Info, $"Start delete '{sentence.Path}'")) { string path = Step.Project.GetFullFileName(sentence.Path); try { if (Directory.Exists(path)) { if (!string.IsNullOrWhiteSpace(sentence.Mask)) { LibHelper.Files.HelperFiles.KillFiles(path, sentence.Mask); } else { LibHelper.Files.HelperFiles.KillPath(path); } } else if (File.Exists(path)) { LibHelper.Files.HelperFiles.KillFile(path); } else { block.Info($"Cant find file or path '{path}' for delete"); } } catch (Exception exception) { AddError(block, $"Error when delete '{path}'. {exception.Message}"); } } }
/// <summary> /// Sube un archivo al blob /// </summary> private async Task ProcessUploadAsync(BlockLogModel parent, UploadBlobSentence sentence) { using (BlockLogModel block = parent.CreateBlock(LogModel.LogType.Info, $"Start uploading to '{sentence.Target.ToString()}'")) { CloudConnection connection = GetConnection(sentence.StorageKey); if (connection == null) { AddError(block, $"Can't find the connection for '{sentence.StorageKey}'"); } else { string fileName = Step.Project.GetFullFileName(sentence.FileName); if (!System.IO.File.Exists(fileName)) { AddError(block, $"Can't find the file '{fileName}'"); } else { try { // Sube el archivo using (ICloudStorageManager manager = new StorageManager().OpenAzureStorageBlob(connection.StorageConnectionString)) { await manager.UploadAsync(GetContainerName(sentence.Target.Container), sentence.Target.Blob, fileName); } // Log block.Info($"Uploaded file '{sentence.FileName}' to '{sentence.Target.ToString()}'"); } catch (Exception exception) { AddError(block, $"Error when upload '{sentence.FileName}' to '{sentence.Target.ToString()}'", exception); } } } } }
/// <summary> /// Convierte los archivos de un directorio /// </summary> private async Task ProcessConvertPathAsync(BlockLogModel parent, ConvertPathSentence sentence, CancellationToken cancellationToken) { using (BlockLogModel block = parent.CreateBlock(LogModel.LogType.Info, $"Start convert path '{sentence.Path}' from {sentence.Source} to {sentence.Target}")) { string path = Step.Project.GetFullFileName(sentence.Path); if (!Directory.Exists(path)) { AddError(block, $"Cant find the path '{path}'"); } else { foreach (string source in Directory.GetFiles(path, "*" + sentence.SourceExtension)) { if (!cancellationToken.IsCancellationRequested && !HasError) { await ConvertFileAsync(block, source, Path.Combine(path, Path.GetFileNameWithoutExtension(source) + sentence.TargetExtension), GetExcelOptions(sentence.ExcelWithHeader, sentence.ExcelSheetIndex), cancellationToken); } } } } }
/// <summary> /// Ejecuta una sentencia de proceso /// </summary> private async Task ProcessExecuteAsync(BlockLogModel parent, ExecuteSentence sentence) { // Evita el warning await Task.Delay(1); // Ejecuta la sentencia using (BlockLogModel block = parent.CreateBlock(LogModel.LogType.Info, $"Start execution '{sentence.Process}'")) { try { LibHelper.Processes.SystemProcessHelper processor = new LibHelper.Processes.SystemProcessHelper(); // Ejecuta el proceso processor.ExecuteApplication(sentence.Process, ConvertArguments(sentence.Arguments), true, sentence.Timeout); // Log block.Info($"End execution '{sentence.Process}'"); } catch (Exception exception) { AddError(block, $"Error when execute '{sentence.Process}'. {exception.Message}"); } } }