From e6aab1600c8d50e44518ae0de54995f2716b66ad Mon Sep 17 00:00:00 2001 From: Tatyana Kostromskaya <86476103+tatyana-kostromskaya@users.noreply.github.com> Date: Mon, 12 Jul 2021 08:17:18 +0300 Subject: [PATCH] [DownloadBuildArtifactsV0] Add clean up logic of destination folder (#15025) * Add clean logic * Add comments to the code * Bump version and fix interface * Resolve comments --- .../resources.resjson/en-US/resources.resjson | 6 ++- Tasks/DownloadBuildArtifactsV0/file_helper.ts | 38 ++++++++++++++++++- Tasks/DownloadBuildArtifactsV0/main.ts | 8 +++- Tasks/DownloadBuildArtifactsV0/task.json | 15 ++++++-- Tasks/DownloadBuildArtifactsV0/task.loc.json | 15 ++++++-- 5 files changed, 73 insertions(+), 9 deletions(-) diff --git a/Tasks/DownloadBuildArtifactsV0/Strings/resources.resjson/en-US/resources.resjson b/Tasks/DownloadBuildArtifactsV0/Strings/resources.resjson/en-US/resources.resjson index 5d0842feff33..3a3d02c97b1f 100644 --- a/Tasks/DownloadBuildArtifactsV0/Strings/resources.resjson/en-US/resources.resjson +++ b/Tasks/DownloadBuildArtifactsV0/Strings/resources.resjson/en-US/resources.resjson @@ -29,6 +29,8 @@ "loc.input.help.itemPattern": "Specify files to be downloaded as multi line minimatch pattern. [More Information](https://aka.ms/minimatchexamples)
The default pattern (\\*\\*) will download all files across all artifacts in the build if \"Specific files\" option is selected. To download all files within artifact drop use drop/**.
", "loc.input.label.downloadPath": "Destination directory", "loc.input.help.downloadPath": "Path on the agent machine where the artifacts will be downloaded", + "loc.input.label.cleanDestinationFolder": "Clean destination folder", + "loc.input.help.cleanDestinationFolder": "Delete all existing files in destination folder before artifact download", "loc.input.label.parallelizationLimit": "Parallelization limit", "loc.input.help.parallelizationLimit": "Number of files to download simultaneously", "loc.input.label.checkDownloadedFiles": "Check downloaded files", @@ -63,5 +65,7 @@ "loc.messages.IntegrityCheckNotPassed": "Artifact items integrity check failed", "loc.messages.IntegrityCheckPassed": "Artifact items integrity check successfully finished", "loc.messages.TarExtractionNotSupportedInWindows": "Tar extraction is not supported on Windows", - "loc.messages.NoTarsFound": "No tar archives were found to extract" + "loc.messages.NoTarsFound": "No tar archives were found to extract", + "loc.messages.CleaningDestinationFolder": "Cleaning destination folder: %s", + "loc.messages.NoFolderToClean": "Specified cleaning folder was not found. Nothing to clean" } \ No newline at end of file diff --git a/Tasks/DownloadBuildArtifactsV0/file_helper.ts b/Tasks/DownloadBuildArtifactsV0/file_helper.ts index 5dc021696cd8..c0b323c746e1 100644 --- a/Tasks/DownloadBuildArtifactsV0/file_helper.ts +++ b/Tasks/DownloadBuildArtifactsV0/file_helper.ts @@ -1,4 +1,4 @@ -import { Stats, statSync as getFile } from 'fs'; +import { Stats, statSync as getFile, readdirSync } from 'fs'; import * as path from 'path'; import * as tl from 'azure-pipelines-task-lib/task'; @@ -102,3 +102,39 @@ function extractTar(tarArchivePath: string, extractedFilesDir: string): void { throw new Error(`Couldn't extract artifact files from a tar archive: ${tarExecResult.error}`); } } + +/** + * Removes content from specified folder path + * @param {string} folderToClean - path to the folder to clean up content + * @throws File system exeptions + * @returns void + */ +export function cleanUpFolder(folderToClean: string): void { + console.log(tl.loc('CleaningDestinationFolder', folderToClean)); + + // stat the specified folder path + let destinationFolderStats: tl.FsStats; + try { + destinationFolderStats = tl.stats(folderToClean); + } catch (err) { + if (err.code != 'ENOENT') { + throw err; + } else { + tl.warning(tl.loc('NoFolderToClean')); + } + } + + if (destinationFolderStats) { + if (destinationFolderStats.isDirectory()) { + // delete the child items + readdirSync(folderToClean) + .forEach((item: string) => { + let itemPath = path.join(folderToClean, item); + tl.rmRF(itemPath); + }); + } else { + // specified folder is not a directory. delete it. + tl.rmRF(folderToClean); + } + } +} diff --git a/Tasks/DownloadBuildArtifactsV0/main.ts b/Tasks/DownloadBuildArtifactsV0/main.ts index 9f6c96fbebf1..3c4e1e467148 100644 --- a/Tasks/DownloadBuildArtifactsV0/main.ts +++ b/Tasks/DownloadBuildArtifactsV0/main.ts @@ -19,7 +19,7 @@ import { DownloadHandlerFilePath } from './DownloadHandlers/DownloadHandlerFileP import { resolveParallelProcessingLimit } from './download_helper'; -import { extractTarsIfPresent } from './file_helper'; +import { extractTarsIfPresent, cleanUpFolder } from './file_helper'; var taskJson = require('./task.json'); @@ -80,6 +80,7 @@ async function main(): Promise