Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion BuildTasks/Common/v4/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ import ToolRunner = trl.ToolRunner;
import uuidv5 from "uuidv5";
import * as tmp from "tmp";

interface VsixManifest {
PackageManifest: {
Metadata: {
Identity: {
_Id: string;
_Version: string;
_Publisher: string;
};
DisplayName: string;
GalleryFlags: string;
};
};
}

function writeBuildTempFile(taskName: string, data: string | Buffer): string {
const tempDir = tl.getVariable("Agent.TempDirectory");
const tempFile = tmp.tmpNameSync({ prefix: taskName, postfix: ".tmp", tmpdir: tempDir });
Expand Down Expand Up @@ -433,7 +447,21 @@ async function updateTaskManifests(manifestPaths: string[], updateTasksId: boole
tl.debug(`Updating Id...`);
const publisherId = tl.getInput("publisherId", false) || manifest.publisher;
const extensionTag = tl.getInput("extensionTag", false) || "";
const extensionId = `${(tl.getInput("extensionId", false) || manifest.id)}${extensionTag}`;

// Try to get extension ID from input, then from VSIX manifest, then from JSON manifest
let baseExtensionId = tl.getInput("extensionId", false);
if (!baseExtensionId) {
const vsixExtensionId = await getVsixExtensionId(extensionPath);
baseExtensionId = vsixExtensionId || manifest.id;
if (vsixExtensionId) {
tl.debug(`Using extension ID from VSIX manifest: ${vsixExtensionId}`);
} else {
tl.debug(`Using extension ID from JSON manifest: ${manifest.id}`);
}
} else {
tl.debug(`Using extension ID from input: ${baseExtensionId}`);
}
const extensionId = `${baseExtensionId}${extensionTag}`;

const originalTaskId: string = taskManifest.id || null;
taskManifest = updateTaskId(taskManifest, publisherId, extensionId);
Expand Down Expand Up @@ -504,6 +532,34 @@ function getManifest(path: string): Promise<unknown> {
});
}

async function getVsixExtensionId(manifestPath: string): Promise<string | undefined> {
try {
// Check if we're dealing with a VSIX directory
const manifestDir = path.dirname(manifestPath);
const vsixManifestPath = path.join(manifestDir, "extension.vsixmanifest");

// Check if the VSIX manifest exists
try {
await fse.access(vsixManifestPath);
} catch {
// VSIX manifest doesn't exist, not a VSIX directory
return undefined;
}

// Read and parse the VSIX manifest
const x2jsLib = require("x2js");
const x2js = new x2jsLib();

const vsixManifestData = await fse.readFile(vsixManifestPath, "utf8");
const vsixmanifest = x2js.xml2js(vsixManifestData) as VsixManifest;

return vsixmanifest.PackageManifest.Metadata.Identity._Id;
} catch (error) {
tl.debug(`Failed to read VSIX extension ID: ${error}`);
return undefined;
}
}

function getTaskManifestPaths(manifestPath: string, manifest: any): string[] {
const tasks = getTaskPathContributions(manifest);
const rootFolder = path.dirname(manifestPath);
Expand Down
21 changes: 20 additions & 1 deletion BuildTasks/Common/v4/package-lock.json

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

3 changes: 2 additions & 1 deletion BuildTasks/Common/v4/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"azure-pipelines-task-lib": "^4.17.3",
"fs-extra": "^11.3.0",
"tmp": "^0.2.3",
"uuidv5": "^1.0.0"
"uuidv5": "^1.0.0",
"x2js": "^3.4.4"
},
"devDependencies": {
"@types/node": "^20.17.27",
Expand Down
58 changes: 57 additions & 1 deletion BuildTasks/Common/v5/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ import fse from "fs-extra";
import uuidv5 from "uuidv5";
import tmp from "tmp";

interface VsixManifest {
PackageManifest: {
Metadata: {
Identity: {
_Id: string;
_Version: string;
_Publisher: string;
};
DisplayName: string;
GalleryFlags: string;
};
};
}

function writeBuildTempFile(taskName: string, data: any): string {
const tempDir = tl.getVariable("Agent.TempDirectory");
const tempFile = tmp.tmpNameSync({ prefix: taskName, postfix: ".tmp", tmpdir: tempDir });
Expand Down Expand Up @@ -442,7 +456,21 @@ async function updateTaskManifests(manifestPaths: string[], updateTasksId: boole
tl.debug(`Updating Id...`);
const publisherId = tl.getInput("publisherId", false) || manifest.publisher;
const extensionTag = tl.getInput("extensionTag", false) || "";
const extensionId = `${(tl.getInput("extensionId", false) || manifest.id)}${extensionTag}`;

// Try to get extension ID from input, then from VSIX manifest, then from JSON manifest
let baseExtensionId = tl.getInput("extensionId", false);
if (!baseExtensionId) {
const vsixExtensionId = await getVsixExtensionId(extensionPath);
baseExtensionId = vsixExtensionId || manifest.id;
if (vsixExtensionId) {
tl.debug(`Using extension ID from VSIX manifest: ${vsixExtensionId}`);
} else {
tl.debug(`Using extension ID from JSON manifest: ${manifest.id}`);
}
} else {
tl.debug(`Using extension ID from input: ${baseExtensionId}`);
}
const extensionId = `${baseExtensionId}${extensionTag}`;

const originalTaskId: string = taskManifest.id || null;
taskManifest = updateTaskId(taskManifest, publisherId, extensionId);
Expand Down Expand Up @@ -511,6 +539,34 @@ async function getManifest(path: string): Promise<unknown> {
}
}

async function getVsixExtensionId(manifestPath: string): Promise<string | undefined> {
try {
// Check if we're dealing with a VSIX directory
const manifestDir = path.dirname(manifestPath);
const vsixManifestPath = path.join(manifestDir, "extension.vsixmanifest");

// Check if the VSIX manifest exists
try {
await fs.access(vsixManifestPath);
} catch {
// VSIX manifest doesn't exist, not a VSIX directory
return undefined;
}

// Read and parse the VSIX manifest
const x2jsLib = (await import("x2js")).default;
const x2js = new x2jsLib();

const vsixManifestData = await fs.readFile(vsixManifestPath, "utf8");
const vsixmanifest = x2js.xml2js<VsixManifest>(vsixManifestData);

return vsixmanifest.PackageManifest.Metadata.Identity._Id;
} catch (error) {
tl.debug(`Failed to read VSIX extension ID: ${error}`);
return undefined;
}
}

function getTaskManifestPaths(manifestPath: string, manifest: any): string[] {
const tasks = getTaskPathContributions(manifest);
const rootFolder = path.dirname(manifestPath);
Expand Down
21 changes: 20 additions & 1 deletion BuildTasks/Common/v5/package-lock.json

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

3 changes: 2 additions & 1 deletion BuildTasks/Common/v5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"azure-pipelines-tasks-azure-arm-rest": "^3.252.1",
"fs-extra": "^11.3.0",
"tmp": "^0.2.3",
"uuidv5": "^1.0.0"
"uuidv5": "^1.0.0",
"x2js": "^3.4.4"
},
"devDependencies": {
"@types/node": "^20.17.27",
Expand Down
Loading