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 { var allowPartiallySucceededBuilds: boolean = tl.getBoolInput("allowPartiallySucceededBuilds", false); var branchName: string = tl.getInput("branchName", false); var downloadPath: string = path.normalize(tl.getInput("downloadPath", true)); + var cleanDestinationFolder: boolean = tl.getBoolInput("cleanDestinationFolder", false); var downloadType: string = tl.getInput("downloadType", true); var tagFiltersInput: string = tl.getInput("tags", false); var tagFilters = []; @@ -109,6 +110,11 @@ async function main(): Promise { }); var artifacts = []; + // Clean destination folder if requested + if (cleanDestinationFolder) { + cleanUpFolder(downloadPath); + } + if (isCurrentBuild) { projectId = tl.getVariable("System.TeamProjectId"); definitionId = ''; diff --git a/Tasks/DownloadBuildArtifactsV0/task.json b/Tasks/DownloadBuildArtifactsV0/task.json index 475f71bde7d3..16579525c823 100644 --- a/Tasks/DownloadBuildArtifactsV0/task.json +++ b/Tasks/DownloadBuildArtifactsV0/task.json @@ -9,8 +9,8 @@ "author": "Microsoft Corporation", "version": { "Major": 0, - "Minor": 188, - "Patch": 3 + "Minor": 190, + "Patch": 0 }, "groups": [ { @@ -170,6 +170,13 @@ "required": true, "helpMarkDown": "Path on the agent machine where the artifacts will be downloaded" }, + { + "name": "cleanDestinationFolder", + "type":"boolean", + "label": "Clean destination folder", + "defaultValue": false, + "helpMarkDown": "Delete all existing files in destination folder before artifact download" + }, { "name": "parallelizationLimit", "type": "string", @@ -288,7 +295,9 @@ "IntegrityCheckNotPassed": "Artifact items integrity check failed", "IntegrityCheckPassed": "Artifact items integrity check successfully finished", "TarExtractionNotSupportedInWindows": "Tar extraction is not supported on Windows", - "NoTarsFound": "No tar archives were found to extract" + "NoTarsFound": "No tar archives were found to extract", + "CleaningDestinationFolder": "Cleaning destination folder: %s", + "NoFolderToClean": "Specified cleaning folder was not found. Nothing to clean" }, "outputVariables": [ { diff --git a/Tasks/DownloadBuildArtifactsV0/task.loc.json b/Tasks/DownloadBuildArtifactsV0/task.loc.json index e788e6a74f2e..8893c0a8de66 100644 --- a/Tasks/DownloadBuildArtifactsV0/task.loc.json +++ b/Tasks/DownloadBuildArtifactsV0/task.loc.json @@ -9,8 +9,8 @@ "author": "Microsoft Corporation", "version": { "Major": 0, - "Minor": 188, - "Patch": 3 + "Minor": 190, + "Patch": 0 }, "groups": [ { @@ -170,6 +170,13 @@ "required": true, "helpMarkDown": "ms-resource:loc.input.help.downloadPath" }, + { + "name": "cleanDestinationFolder", + "type": "boolean", + "label": "ms-resource:loc.input.label.cleanDestinationFolder", + "defaultValue": false, + "helpMarkDown": "ms-resource:loc.input.help.cleanDestinationFolder" + }, { "name": "parallelizationLimit", "type": "string", @@ -288,7 +295,9 @@ "IntegrityCheckNotPassed": "ms-resource:loc.messages.IntegrityCheckNotPassed", "IntegrityCheckPassed": "ms-resource:loc.messages.IntegrityCheckPassed", "TarExtractionNotSupportedInWindows": "ms-resource:loc.messages.TarExtractionNotSupportedInWindows", - "NoTarsFound": "ms-resource:loc.messages.NoTarsFound" + "NoTarsFound": "ms-resource:loc.messages.NoTarsFound", + "CleaningDestinationFolder": "ms-resource:loc.messages.CleaningDestinationFolder", + "NoFolderToClean": "ms-resource:loc.messages.NoFolderToClean" }, "outputVariables": [ {