Skip to content

Commit

Permalink
improve readability of dependency tree
Browse files Browse the repository at this point in the history
  • Loading branch information
ThorbenLindhauer committed Sep 18, 2023
1 parent a3a1613 commit 8a52948
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
28 changes: 26 additions & 2 deletions common/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163287,8 +163287,32 @@ module.exports = async function (sbomDiff, template, partials = {}) {
return value + 1;
});

handlebars.registerHelper('indent', function(times) {
return ' '.repeat(times * 2);
// stores if the currently rendered item is the last one for each
// of the nested lists
const currentIndentationState = [];

handlebars.registerHelper('indent', function(times, lastItemInList) {

while (times > currentIndentationState.length) {
currentIndentationState.push(false);
}
while (times < currentIndentationState.length) {
currentIndentationState.pop();
}

currentIndentationState[times - 1] = lastItemInList;

const visualizationComponents = currentIndentationState.map((lastItemInListAtCurrentLevel, index) => {
const isMostDeeplyNestedList = index == currentIndentationState.length - 1;

if (lastItemInListAtCurrentLevel) {
return isMostDeeplyNestedList ? ' └─ ' : ' ';
} else {
return isMostDeeplyNestedList ? ' ├─ ' : ' │ ';
}
});

return visualizationComponents.join('');
});

handlebars.registerHelper('emojifyLicense', function(licenseType) {
Expand Down
28 changes: 26 additions & 2 deletions common/src/sbom-diff/format-handlebars-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,32 @@ module.exports = async function (sbomDiff, template, partials = {}) {
return value + 1;
});

handlebars.registerHelper('indent', function(times) {
return '&nbsp;'.repeat(times * 2);
// stores if the currently rendered item is the last one for each
// of the nested lists
const currentIndentationState = [];

handlebars.registerHelper('indent', function(times, lastItemInList) {

while (times > currentIndentationState.length) {
currentIndentationState.push(false);
}
while (times < currentIndentationState.length) {
currentIndentationState.pop();
}

currentIndentationState[times - 1] = lastItemInList;

const visualizationComponents = currentIndentationState.map((lastItemInListAtCurrentLevel, index) => {
const isMostDeeplyNestedList = index == currentIndentationState.length - 1;

if (lastItemInListAtCurrentLevel) {
return isMostDeeplyNestedList ? ' └─ ' : ' ';
} else {
return isMostDeeplyNestedList ? ' ├─ ' : ' │ ';
}
});

return visualizationComponents.join('');
});

handlebars.registerHelper('emojifyLicense', function(licenseType) {
Expand Down
8 changes: 4 additions & 4 deletions java-dependency-check/component-diff.hbs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{{!-- careful: preserve the double space characters at the end of the content lines to make sure that new lines are generated--}}
{{#indent nestingLevel}}{{/indent}}{{emojifyChangeType changeType}} {{#baseComponent}}{{ name }}: {{>componentVersion}}{{/baseComponent}} => {{#comparingComponent}}{{>componentVersion}}{{/comparingComponent}}
{{#indent nestingLevel lastItemInList}}{{/indent}}{{emojifyChangeType changeType}} {{#baseComponent}}{{ name }}: {{>componentVersion}}{{/baseComponent}} => {{#comparingComponent}}{{>componentVersion}}{{/comparingComponent}}
{{#if (shouldRenderComponentDiff this)}}
{{#each changedDependencies}}
{{>componentDiff nestingLevel=(increment ../nestingLevel)}}
{{>componentDiff nestingLevel=(increment ../nestingLevel) lastItemInList=@last}}
{{/each}}
{{#each addedDependencies}}
{{>componentTree nestingLevel=(increment ../nestingLevel) changeType='Added'}}
{{>componentTree nestingLevel=(increment ../nestingLevel) lastItemInList=@last changeType='Added'}}
{{/each}}
{{#each removedDependencies}}
{{>componentTree nestingLevel=(increment ../nestingLevel) changeType='Removed'}}
{{>componentTree nestingLevel=(increment ../nestingLevel) lastItemInList=@last changeType='Removed'}}
{{/each}}
{{else if (hasChanges this)}}{{#indent (increment nestingLevel)}}{{/indent}}(Repeating subtree omitted)
{{/if}}
4 changes: 2 additions & 2 deletions java-dependency-check/component-tree.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{#indent nestingLevel}}{{/indent}}{{emojifyChangeType changeType}} {{ name }}:{{>componentVersion}}
{{#indent nestingLevel lastItemInList}}{{/indent}}{{emojifyChangeType changeType}} {{ name }}:{{>componentVersion}}
{{#if (shouldRenderComponentTree this)}}
{{#each dependencies}}
{{>componentTree nestingLevel=(increment ../nestingLevel) changeType=../changeType}}
{{>componentTree nestingLevel=(increment ../nestingLevel) lastItemInList=@last changeType=../changeType}}
{{/each}}
{{else if (hasDependencies this)}}{{#indent (increment nestingLevel)}}{{/indent}}(Repeating subtree omitted)
{{/if}}
2 changes: 1 addition & 1 deletion java-dependency-check/component-version.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{#if thirdParty~}}
[{{ version }}](#{{ renderId }}-{{ urlEncode name }}{{ urlEncode ':' }}{{ urlEncode version }}) {{#if allLicensesGo}}{{emojifyLicense 'Go'}}{{/if}}{{#each carefulLicenseTypes}}{{#if used}}{{emojifyLicense type}}{{/if}}{{/each}}{{#if hasMultipleLicenses}}&#8252;{{/if}}
<a href="#{{ renderId }}-{{ urlEncode name }}{{ urlEncode ':' }}{{ urlEncode version }}">{{ version }}</a> {{#if allLicensesGo}}{{emojifyLicense 'Go'}}{{/if}}{{#each carefulLicenseTypes}}{{#if used}}{{emojifyLicense type}}{{/if}}{{/each}}{{#if hasMultipleLicenses}}&#8252;{{/if}}
{{~else~}}
{{version}}
{{~/if}}
2 changes: 2 additions & 0 deletions java-dependency-check/diff.hbs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Java dependency diff

{{#unless excludeTree}}
<pre>
{{#diff.rootComponentDiff}}
{{>componentDiff nestingLevel=0}}
{{/diff.rootComponentDiff}}
</pre>
{{else}}
Omitted due to character limit. See workflow artifacts for full diff file.
{{/unless}}
Expand Down

0 comments on commit 8a52948

Please sign in to comment.