Last active
November 17, 2018 23:30
-
-
Save boghyon/7f006a55551a64b6c4461afa2547d973 to your computer and use it in GitHub Desktop.
UI5 Tool: "Where used" Logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>GistRun</title> | |
<script> | |
window["sap-ui-config"] = { | |
libs: "sap.ui.core, sap.m, sap.ui.mdc", | |
preload: "async", | |
async: "true", | |
"xx-async": true, | |
}; | |
</script> | |
<script id="sap-ui-bootstrap" | |
src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js" | |
></script> | |
<script id="boghyon-where-used" src="whereUsed.js"></script> | |
</head> | |
<body> | |
<h1>Check out the console</h1> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Note: This is barely an MVP and hardly optimized. | |
* TODO: Put things in the worker thread since the iterator doesn't need DOM. | |
* _________________________________________________________ | |
* # What it does: | |
* Invokes a function that logs all modules with the search results appended accordingly. | |
* | |
* Sample log after searching for "sap.m.Button": ` | |
* In sap.m.ActionSheet ... | |
* A>/buttons/type: sap.m.Button | |
* E>/beforeClose/appData/origin/type: sap.m.Button | |
* E>/afterClose/appData/origin/type: sap.m.Button | |
* ` | |
* | |
* Or after searching for "styleClass": ` | |
* In sap.m.Column ... | |
* P>/styleClass/name: styleClass | |
* In sap.m.FlexItemData ... | |
* P>/styleClass/name: styleClass | |
* ` | |
* | |
* Whereas | |
* P === Property | |
* A === Aggregation | |
* E === Event | |
* z === Association | |
* x === Special settings | |
* _________________________________________________________ | |
* # How to use | |
* Just open any UI5 application, open browsers console, copy-paste and run this snippet. | |
* Or run this Gist in here: https://gist.run/?id=7f006a55551a64b6c4461afa2547d973 | |
* | |
* Adjust the `regex` as you wish. | |
*/ | |
sap.ui.getCore().attachInit(() => sap.ui.require([ | |
"sap/ui/qunit/utils/ControlIterator", | |
], function(ControlIterator) { | |
"use strict"; | |
const regex = /\bsap.m.Button\b/; // Example: Which module makes use of thze type 'sap.m.Button' and where? | |
_search(regex); | |
function _search(regex) { | |
return regex ? ControlIterator.run(function(controlName, controlClass) { | |
console.log(`In ${controlName} ...`); // Currently it just logs out. TODO: Return proper result objects | |
run(controlClass.getMetadata().getAllSettings(), regex) | |
}, { | |
includeElements: true, | |
includeNonInstantiable: true, | |
excludedLibraries: [ | |
"sap.ui.server.java", | |
"sap.ui.documentation", | |
"sap.ui.demoapps", | |
"sap.ui.server.abap", | |
"sap.ushell_abap", | |
"sap.apf", | |
"sap.ca.scfld.md", | |
// Deprecated libs | |
"themelib_sap_belize", | |
"themelib_sap_bluecrystal", | |
"sap.ui.commons", | |
"sap.ui.ux3", | |
"sap.me", | |
"sap.ca.ui" | |
], | |
}) : null; | |
function run(obj, regex, path = "") { | |
Object.keys(obj) | |
.filter(key => key[0] !== "_") | |
.map(log); | |
function log(key) { | |
const currentValue = obj[key]; | |
const fullPath = `${path}/${key}`; | |
if (typeof currentValue === "string") { | |
regex.test(currentValue) && console.log(` ${fullPath}:`, currentValue); | |
} | |
if (isObject(currentValue)) { | |
const constructorName = currentValue.constructor.name; | |
const path = constructorName !== "Object" ? `${constructorName}>${fullPath}` : fullPath; | |
return run(currentValue, regex, path); | |
} | |
} | |
} | |
function isObject(obj) { | |
return Object.prototype.toString.call(obj) === "[object Object]"; | |
} | |
}; | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment