Skip to content

Commit

Permalink
Merge pull request #153 from EzzhevNikita/users/EzzhevNikita/add-abil…
Browse files Browse the repository at this point in the history
…ity-to-overwrite-while-extraction

Add option to overwrite files in target for extract7z function
  • Loading branch information
mmrazik authored Mar 25, 2022
2 parents 5dbfaf3 + aeb2d4f commit 426ba81
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
8 changes: 6 additions & 2 deletions Invoke-7zdec.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ param(
[string]$Source,

[Parameter(Mandatory = $true)]
[string]$Target)
[string]$Target,

[Parameter(Mandatory = $false)]
[boolean]$OverrideDestDirectory)

# This script translates the output from 7zdec into UTF8. Node has limited
# built-in support for encodings.
Expand Down Expand Up @@ -35,8 +38,9 @@ $writer = New-Object System.IO.StreamWriter($stdout, $utf8)
Set-Location -LiteralPath $Target

# Print the ##command.
$overrideDest = $(If ($OverrideDestDirectory) { "-aoa" } Else { "" })
$_7zdec = Join-Path -Path "$PSScriptRoot" -ChildPath "externals/7zdec.exe"
[System.Console]::WriteLine("##[command]$_7zdec x `"$Source`"")
[System.Console]::WriteLine("##[command]$_7zdec $overrideDest x `"$Source`"")

# The $OutputEncoding variable instructs PowerShell how to interpret the output
# from the external command.
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azure-pipelines-tool-lib",
"version": "1.2.0",
"version": "1.3.0",
"description": "Azure Pipelines Tool Installer Lib for CI/CD Tasks",
"main": "tool.js",
"scripts": {
Expand Down
22 changes: 14 additions & 8 deletions tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,10 @@ export async function cacheFile(sourceFile: string,
* time of this writing, it is freely available from the LZMA SDK that is available on the 7zip website.
* Be sure to check the current license agreement. If 7zr.exe is bundled with your task, then the path
* to 7zr.exe can be pass to this function.
* @param overwriteDest Overwrite files in destination catalog. Optional.
* @returns path to the destination directory
*/
export async function extract7z(file: string, dest?: string, _7zPath?: string): Promise<string> {
export async function extract7z(file: string, dest?: string, _7zPath?: string, overwriteDest?: boolean): Promise<string> {
if (process.platform != 'win32') {
throw new Error('extract7z() not supported on current OS');
}
Expand All @@ -402,20 +403,25 @@ export async function extract7z(file: string, dest?: string, _7zPath?: string):

if (_7zPath) {
// extract
let _7z: trm.ToolRunner = tl.tool(_7zPath)
.arg('x') // eXtract files with full paths
.arg('-bb1') // -bb[0-3] : set output log level
.arg('-bd') // disable progress indicator
.arg('-sccUTF-8') // set charset for for console input/output
.arg(file);
const _7z: trm.ToolRunner = tl.tool(_7zPath);
if (overwriteDest) {
_7z.arg('-aoa');
}

_7z.arg('x') // eXtract files with full paths
.arg('-bb1') // -bb[0-3] : set output log level
.arg('-bd') // disable progress indicator
.arg('-sccUTF-8') // set charset for for console input/output
.arg(file);
await _7z.exec();
}
else {
// extract
let escapedScript = path.join(__dirname, 'Invoke-7zdec.ps1').replace(/'/g, "''").replace(/"|\n|\r/g, ''); // double-up single quotes, remove double quotes and newlines
let escapedFile = file.replace(/'/g, "''").replace(/"|\n|\r/g, '');
let escapedTarget = dest.replace(/'/g, "''").replace(/"|\n|\r/g, '');
let command: string = `& '${escapedScript}' -Source '${escapedFile}' -Target '${escapedTarget}'`
const overrideDestDirectory: number = overwriteDest ? 1 : 0;
const command: string = `& '${escapedScript}' -Source '${escapedFile}' -Target '${escapedTarget}' -OverrideDestDirectory ${overrideDestDirectory}`;
let powershellPath = tl.which('powershell', true);
let powershell: trm.ToolRunner = tl.tool(powershellPath)
.line('-NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command')
Expand Down

0 comments on commit 426ba81

Please sign in to comment.