Skip to content

Commit

Permalink
[INTERNAL] Enhance tree and build commands with project graph options
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed May 18, 2022
1 parent d49c0fc commit d035a5c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 8 deletions.
14 changes: 13 additions & 1 deletion lib/cli/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ build.builder = function(cli) {
default: false,
type: "boolean"
})
.option("x-graph-mode", {
describe: "Uses an experimental project graph instead of a dependency tree",
default: false,
type: "boolean"
})
.example("ui5 build", "Preload build for project without dependencies")
.example("ui5 build self-contained --all", "Self-contained build for project including dependencies")
.example("ui5 build --all --exclude-task=* --include-task=createDebugFiles generateAppPreload",
Expand Down Expand Up @@ -142,7 +147,13 @@ async function handleBuild(argv) {
};
}

const tree = await normalizer.generateProjectTree(normalizerOptions);
let tree;
let graph;
if (argv.xGraphMode) {
graph = await normalizer.generateProjectGraph(normalizerOptions);
} else {
tree = await normalizer.generateProjectTree(normalizerOptions);
}
const buildSettings = (tree.builder && tree.builder.settings) || {};

const {includedDependencies, excludedDependencies} = buildHelper.createDependencyLists({
Expand All @@ -161,6 +172,7 @@ async function handleBuild(argv) {

await builder.build({
tree: tree,
graph: graph,
destPath: argv.dest,
cleanDest: argv["clean-dest"],
buildDependencies: buildAll,
Expand Down
102 changes: 95 additions & 7 deletions lib/cli/commands/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ tree.builder = function(cli) {
describe:
"Overrides the framework version defined by the project. Only supported in combination with --full",
type: "string"
}).check((argv) => {
})
.option("x-graph-mode", {
describe: "Uses an experimental project graph instead of a dependency tree",
default: false,
type: "boolean"
})
.option("x-perf", {
describe: "Outputs performance measurements",
default: false,
type: "boolean"
})
.check((argv) => {
if (argv.frameworkVersion && !argv.full) {
throw new Error(`"framework-version" can only be used in combination with option "--full"`);
} else {
Expand All @@ -42,6 +53,7 @@ tree.builder = function(cli) {
tree.handler = async function(argv) {
const normalizer = require("@ui5/project").normalizer;
const treeify = require("treeify");
const chalk = require("chalk");

const options = {
translatorName: argv.translator,
Expand All @@ -57,15 +69,91 @@ tree.handler = async function(argv) {
};
}

let projectTree;
if (argv.full) {
projectTree = await normalizer.generateProjectTree(options);
let startTime;
let elapsedTime;
if (argv.xPerf) {
startTime = process.hrtime();
}
if (argv.xGraphMode) {
const graph = await normalizer.generateProjectGraph(options);

if (argv.xPerf) {
elapsedTime = getElapsedTime(startTime);
}

const projects = {};
const indentWidth = 4;
await graph.traverseBreadthFirst(async ({project, getDependencies}) => {
const deps = getDependencies().map((dep) => {
return dep.getName();
});
projects[project.getName()] = {
render: function(indentation, connectorIndices, lastChild) {
let baseString = " ".repeat(indentation * indentWidth);
connectorIndices.forEach((idx) => {
baseString = `${baseString.slice(0, idx)}${baseString.slice(idx + 1)}`;
});
const connectorString = lastChild ? "╰─" : "├─";
console.log(
`${baseString}${connectorString} ${chalk.bold(project.getName())} ` +
chalk.dim(`(${project.getVersion()}, ${project.getType()}) `) +
chalk.dim.italic(`${project.getPath()}`)
);

const lastIdx = deps.length -1;
const newConnectorIndices = [...connectorIndices];
if (!lastChild) {
newConnectorIndices.push(indentation * indentWidth);
}
deps.forEach((dep, i) => {
projects[dep].render(indentation + 1, newConnectorIndices, i === lastIdx);
});
}
};
});

const projectKeys = Object.keys(projects);
console.log(chalk.bold.underline(`Dependencies (${projectKeys.length}):`));
projects[projectKeys[0]].render(0, [], true);
console.log("");

const extensions = Object.entries(graph.getAllExtensions());
console.log(chalk.bold.underline(`Extensions (${extensions.length}):`));
if (extensions.length) {
extensions.forEach((extension) => {
console.log(
`${" ".repeat(indentWidth)} ├─ ${extension.getName()}` +
chalk.dim(`(${extension.getVersion()}, ${extension.getType()}) `) +
chalk.dim.italic(`${extension.getPath()}`));
});
} else {
console.log(chalk.italic(`None`));
}
} else {
projectTree = await normalizer.generateDependencyTree(options);
let projectTree;
if (argv.full) {
projectTree = await normalizer.generateProjectTree(options);
} else {
projectTree = await normalizer.generateDependencyTree(options);
}
if (argv.xPerf) {
elapsedTime = getElapsedTime(startTime);
}


const output = argv.json ? JSON.stringify(projectTree, null, 4) : treeify.asTree(projectTree, true);
console.log(output);
}

const output = argv.json ? JSON.stringify(projectTree, null, 4) : treeify.asTree(projectTree, true);
console.log(output);
if (argv.xPerf) {
console.log("");
console.log(chalk.blue(`Normalizer took ${chalk.bold(elapsedTime)}`));
}
};

function getElapsedTime(startTime) {
const timeDiff = process.hrtime(startTime);
const prettyHrtime = require("pretty-hrtime");
return prettyHrtime(timeDiff);
}
module.exports = tree;

0 comments on commit d035a5c

Please sign in to comment.