Skip to content

Commit 77f4417

Browse files
authored
Merge pull request #1018 from salesforcecli/t/managed-packaging/W-17561980/display-dependency-graph-3
W-17561980 Complete CLI Command to Display Dependencies
2 parents 2dcff8d + 37559dd commit 77f4417

6 files changed

Lines changed: 227 additions & 2 deletions

File tree

command-snapshot.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,14 @@
368368
"flags": ["api-version", "dot-code", "flags-dir", "json", "loglevel", "package", "target-dev-hub", "verbose"],
369369
"plugin": "@salesforce/plugin-packaging"
370370
},
371+
{
372+
"alias": [],
373+
"command": "package:version:displaydependencies",
374+
"flagAliases": ["apiversion", "target-hub-org", "targetdevhubusername"],
375+
"flagChars": ["p", "v"],
376+
"flags": ["api-version", "edge-direction", "flags-dir", "json", "loglevel", "package", "target-dev-hub", "verbose"],
377+
"plugin": "@salesforce/plugin-packaging"
378+
},
371379
{
372380
"alias": ["force:package:version:list"],
373381
"command": "package:version:list",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# summary
2+
3+
Display the dependency graph for an unlocked or 2GP managed package version.
4+
5+
# examples
6+
7+
- Display the dependency graph for a package version with the specified alias, using your default Dev Hub org and the default edge-direction:
8+
9+
<%= config.bin %> <%= command.id %> --package package_version_alias
10+
11+
- Display the dependency graph for a package version with the specified ID and display the graph using a root-last edge direction. Use the Dev Hub org with username devhub@example.com:
12+
13+
<%= config.bin %> <%= command.id %> --package 04t... --edge-direction root-last --target-dev-hub devhub@example.com
14+
15+
- Display the dependency graph of a version create request with the specified ID, using your default Dev Hub org and the default edge-direction:
16+
17+
<%= config.bin %> <%= command.id %> --package 08c...
18+
19+
# flags.package.summary
20+
21+
ID or alias of the package version (starts with 04t) or the package version create request (starts with 08c) to display the dependency graph for.
22+
23+
# flags.package.description
24+
25+
Before running this command, update your sfdx-project.json file to specify the calculateTransitiveDependencies attribute, and set the value to true. This command returns GraphViz code, which can be compiled to a graph using DOT code or another graph visualization software.
26+
27+
# flags.edge-direction.summary
28+
29+
Order (root-first or root-last) in which the dependencies are displayed.
30+
31+
# flags.edge-direction.description
32+
33+
A root-first graph declares the root as the package that must be installed last. A root-last graph is the reverse order of root-first. If you specify "--edge-direction root-last", the graph displays the packages in the order they must be installed. The root starts with the farthest leaf of the package dependencies and ends with the base package, which must be installed last.
34+
35+
# flags.verbose.summary
36+
37+
Display both the package version ID (starts with 04t) and the version number (major.minor.patch.build) in each node.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"bugs": "https://github.com/forcedotcom/cli/issues",
77
"dependencies": {
88
"@oclif/core": "^4",
9-
"@salesforce/core": "^8.15.0",
9+
"@salesforce/core": "^8.18.5",
1010
"@salesforce/kit": "^3.2.3",
1111
"@salesforce/packaging": "^4.11.0",
12-
"@salesforce/sf-plugins-core": "^12.2.2",
12+
"@salesforce/sf-plugins-core": "^12.2.3",
1313
"chalk": "^5.4.1"
1414
},
1515
"devDependencies": {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$ref": "#/definitions/DisplayDependenciesCommandResult",
4+
"definitions": {
5+
"DisplayDependenciesCommandResult": {
6+
"anyOf": [
7+
{
8+
"type": "string"
9+
},
10+
{
11+
"type": "null"
12+
}
13+
]
14+
}
15+
}
16+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2022, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@salesforce/sf-plugins-core';
8+
import { Messages } from '@salesforce/core/messages';
9+
import { Package } from '@salesforce/packaging';
10+
import { requiredHubFlag } from '../../../utils/hubFlag.js';
11+
import { maybeGetProject } from '../../../utils/getProject.js';
12+
13+
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
14+
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_displaydependencies');
15+
export type DisplayDependenciesCommandResult = string | void;
16+
17+
export class PackageVersionDisplayDependenciesCommand extends SfCommand<DisplayDependenciesCommandResult> {
18+
public static readonly summary = messages.getMessage('summary');
19+
public static readonly examples = messages.getMessages('examples');
20+
public static readonly deprecateAliases = true;
21+
22+
public static readonly flags = {
23+
loglevel,
24+
'target-dev-hub': requiredHubFlag,
25+
'api-version': orgApiVersionFlagWithDeprecations,
26+
package: Flags.string({
27+
char: 'p',
28+
summary: messages.getMessage('flags.package.summary'),
29+
description: messages.getMessage('flags.package.description'),
30+
required: true,
31+
}),
32+
'edge-direction': Flags.custom<'root-first' | 'root-last'>({
33+
options: ['root-first', 'root-last'],
34+
})({
35+
summary: messages.getMessage('flags.edge-direction.summary'),
36+
description: messages.getMessage('flags.edge-direction.description'),
37+
default: 'root-first',
38+
}),
39+
verbose: Flags.boolean({
40+
summary: messages.getMessage('flags.verbose.summary'),
41+
default: false,
42+
}),
43+
};
44+
45+
public async run(): Promise<DisplayDependenciesCommandResult> {
46+
const { flags } = await this.parse(PackageVersionDisplayDependenciesCommand);
47+
const packageDependencyGraph = await Package.getDependencyGraph(
48+
flags.package,
49+
await maybeGetProject(),
50+
flags['target-dev-hub'].getConnection(flags['api-version']),
51+
{
52+
verbose: flags['verbose'],
53+
edgeDirection: flags['edge-direction'],
54+
}
55+
);
56+
57+
const dotProducer = await packageDependencyGraph.getDependencyDotProducer();
58+
const dotCodeResult = dotProducer.produce();
59+
60+
if (flags.json) {
61+
return dotCodeResult;
62+
}
63+
this.log(dotCodeResult);
64+
}
65+
}

test/commands/package/packageVersion.nut.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,105 @@ describe('package:version:*', () => {
470470
});
471471
});
472472

473+
describe('package:version:displaydependencies', () => {
474+
type PackageVersionCreateRequestResult = {
475+
Id: string;
476+
Package2Version: {
477+
SubscriberPackageVersionId: string;
478+
};
479+
};
480+
let devHubOrg: Org;
481+
let configAggregator: ConfigAggregator;
482+
let testPackageRequestId: string; // 08c ID
483+
let testSubscriberPackageVersionId: string; // 04t ID
484+
const NODE_LABEL_REGEX = /label="[^"]*@\d+\.\d+\.\d+\.\d+"/;
485+
const EDGE_REGEX = /\t node_\w+ -> node_\w+/g;
486+
487+
before('dependencies project setup', async function () {
488+
const query = 'SELECT Id, Package2Version.SubscriberPackageVersionId FROM Package2VersionCreateRequest LIMIT 10';
489+
configAggregator = await ConfigAggregator.create();
490+
devHubOrg = await Org.create({ aliasOrUsername: configAggregator.getPropertyValue<string>('target-dev-hub') });
491+
// Check API version before proceeding
492+
const apiVersion = parseFloat(devHubOrg.getConnection().getApiVersion());
493+
if (apiVersion < 65.0) {
494+
// eslint-disable-next-line no-console
495+
console.log(
496+
`Skipping package:version:displaydependencies tests - API version ${apiVersion} is below required version 65.0 for displaydependencies command.`
497+
);
498+
this.skip();
499+
}
500+
const pvRecords = (await devHubOrg.getConnection().tooling.query<PackageVersionCreateRequestResult>(query))
501+
.records;
502+
503+
if (!pvRecords || pvRecords.length === 0) {
504+
throw new Error('No package version create requests found with dependency graph json');
505+
}
506+
const pv = pvRecords[0];
507+
testPackageRequestId = pv.Id;
508+
testSubscriberPackageVersionId = pv.Package2Version.SubscriberPackageVersionId;
509+
});
510+
it('should print the correct DOT code output (default)', () => {
511+
const command = `package:version:displaydependencies -p ${testPackageRequestId}`;
512+
const result = execCmd(command, { ensureExitCode: 0 }).shellOutput.stdout;
513+
expect(result).to.contain('strict digraph G {');
514+
const hasValidNode = result.includes('node_');
515+
expect(hasValidNode).to.be.true;
516+
expect(result).to.match(NODE_LABEL_REGEX);
517+
const hasMultipleNodes = result.split('\n').filter((line) => line.includes('node_')).length > 1;
518+
if (hasMultipleNodes) {
519+
expect(result).to.contain('->');
520+
}
521+
});
522+
it('should print the correct DOT code output (verbose)', () => {
523+
const command = `package:version:displaydependencies -p ${testPackageRequestId} --verbose`;
524+
const result = execCmd(command, { ensureExitCode: 0 }).shellOutput.stdout;
525+
expect(result).to.contain('strict digraph G {');
526+
const hasSubscriberPackageVersionId = /\(04t.{15}\)/.test(result);
527+
const hasVersionBeingBuilt = result.includes('(VERSION_BEING_BUILT)');
528+
expect(hasSubscriberPackageVersionId || hasVersionBeingBuilt).to.be.true;
529+
});
530+
it('should print the correct DOT code output (json)', () => {
531+
const command = `package:version:displaydependencies -p ${testPackageRequestId} --json`;
532+
const result = execCmd<string>(command, { ensureExitCode: 0 });
533+
const dotCode = result.jsonOutput?.result;
534+
expect(dotCode).to.be.a('string');
535+
expect(dotCode).to.contain('strict digraph G {');
536+
});
537+
it('should print the correct DOT code output (root-last)', () => {
538+
const commandRootFirst = `package:version:displaydependencies -p ${testPackageRequestId} --edge-direction root-first`;
539+
const resultRootFirst = execCmd(commandRootFirst, { ensureExitCode: 0 }).shellOutput.stdout;
540+
const commandRootLast = `package:version:displaydependencies -p ${testPackageRequestId} --edge-direction root-last`;
541+
const resultRootLast = execCmd(commandRootLast, { ensureExitCode: 0 }).shellOutput.stdout;
542+
expect(resultRootFirst).to.contain('strict digraph G {');
543+
expect(resultRootLast).to.contain('strict digraph G {');
544+
const hasMultipleNodes = ((resultRootFirst.match(/node_/g) && resultRootLast.match(/node_/g)) || []).length > 1;
545+
if (hasMultipleNodes) {
546+
const edgeLinesFirst = resultRootFirst.match(EDGE_REGEX) || [];
547+
const edgeLinesLast = resultRootLast.match(EDGE_REGEX) || [];
548+
expect(edgeLinesFirst.length).to.equal(edgeLinesLast.length);
549+
expect(resultRootFirst).to.not.equal(resultRootLast);
550+
} else {
551+
expect(resultRootFirst).to.contain('node_');
552+
expect(resultRootLast).to.contain('node_');
553+
}
554+
});
555+
it('should work with 04t package version ID if available', function () {
556+
if (!testSubscriberPackageVersionId) {
557+
this.skip();
558+
}
559+
const command = `package:version:displaydependencies -p ${testSubscriberPackageVersionId}`;
560+
const result = execCmd(command, { ensureExitCode: 0 }).shellOutput.stdout;
561+
expect(result).to.contain('strict digraph G {');
562+
const hasValidNode = result.includes('node_');
563+
expect(hasValidNode).to.be.true;
564+
expect(result).to.match(NODE_LABEL_REGEX);
565+
const hasMultipleNodes = result.split('\n').filter((line) => line.includes('node_')).length > 1;
566+
if (hasMultipleNodes) {
567+
expect(result).to.contain('->');
568+
}
569+
});
570+
});
571+
473572
describe('package:version:ancestrydisplay', () => {
474573
type PackageVersionQueryResult = PackagingSObjects.Package2Version & {
475574
Package2: {

0 commit comments

Comments
 (0)