Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
feat: refactor module dependency anasys
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Oct 10, 2019
1 parent 01e6fa9 commit 8dabedd
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UI5Cache extends Map {
/**
* load cache
*/
UI5Cache.Load = () => {
UI5Cache.load = () => {
try {
if (existsSync(cachePath)) {
var binCache = readFileSync(cachePath, { encoding: "UTF-8" });
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"devDependencies": {
"@commitlint/cli": "^8",
"@commitlint/config-conventional": "^8",
"@types/jest": "^24.0.18",
"eslint": "^5.16.0",
"husky": "^2.3.0",
"jest": "^24.9.0",
Expand Down
72 changes: 72 additions & 0 deletions test_resources/Component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import UIComponent from "sap/ui/core/UIComponent";
import JSONModel from "sap/ui/model/json/JSONModel";
import * as Device from "sap/ui/Device";
import { createHelloDialog } from "./fragments/HelloDialog";
import { manifest } from "./manifest";
import Dialog from "sap/m/Dialog";
import BindingMode from "sap/ui/model/BindingMode";
import View from "sap/ui/core/mvc/View";
import Controller from "sap/ui/core/mvc/Controller";

export default class Component extends UIComponent {

private metadata = {
manifest
}

private _dialog: Dialog

init() {
super.init(this, arguments);
// set data model
var oData = {
recipient: {
name: "World"
}
};
var oModel = new JSONModel(oData);
this.setModel(oModel);

// set device model
var oDeviceModel = new JSONModel(Device);
oDeviceModel.setDefaultBindingMode(BindingMode.OneWay);
this.setModel(oDeviceModel, "device");

// create the views based on the url/hash
this.getRouter().initialize();

}

openHelloDialog() {
const oView = this.getAggregation("rootControl") as View;
// create dialog lazily
if (!this._dialog) {
var oFragmentController = {
onCloseDialog: () => {
this._dialog.close();
}
};
// create dialog via fragment factory
this._dialog = createHelloDialog(oFragmentController);
// connect dialog to the root view of this component (models, lifecycle)
oView.addDependent(this._dialog);
// forward compact/cozy style into dialog
jQuery.sap.syncStyleClass(((oView.getController() as Controller).getOwnerComponent() as Component).getContentDensityClass(), oView, this._dialog);
}
this._dialog.open();
}

private _sContentDensityClass: string

getContentDensityClass() {
if (!this._sContentDensityClass) {
if (!sap.ui.Device.support.touch) {
this._sContentDensityClass = "sapUiSizeCompact";
} else {
this._sContentDensityClass = "sapUiSizeCozy";
}
}
return this._sContentDensityClass;
}

}
28 changes: 28 additions & 0 deletions test_resources/ui5.wt.ts.model.formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
sap.ui.define("ui5/wt/ts/model/formatter", [], function () {
var _default = {};

var createFormatter = function createFormatter(oView) {
return {
statusText: function statusText(sStatus) {
var oResourceBundle = oView.getModel("i18n").getResourceBundle();

switch (sStatus) {
case "A":
return oResourceBundle.getText("invoiceStatusA");

case "B":
return oResourceBundle.getText("invoiceStatusB");

case "C":
return oResourceBundle.getText("invoiceStatusC");

default:
return oResourceBundle.getText("invoiceStatusA");
}
}
};
};

_default.createFormatter = createFormatter;
return _default;
})
79 changes: 56 additions & 23 deletions ui5.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var { UI5Cache } = require("./cache");

var { eachDeep } = require("deepdash")(require("lodash"));

var persistCache = UI5Cache.Load();
var persistCache = UI5Cache.load();

var FIVE_MINUTES = 5 * 60 * 1000;

Expand Down Expand Up @@ -92,12 +92,23 @@ var readURLFromCache = async url => {
return urlContent;
};

var isUi5CoreCoreJs = (mName = "") => {
return mName && (
mName.startsWith("jquery.") ||
mName.startsWith("sap-ui-") ||
mName.startsWith("ui5loader")
);
};

/**
* get the library name of a module
* @param {string} mName
*/
var getSourceLibraryName = mName => {
var rt;
if (isUi5CoreCoreJs(mName)) {
return "sap.ui.core";
}
forEach(UI5Libraries, libraryName => {
if (mName.startsWith(libraryName)) {
rt = libraryName;
Expand All @@ -106,11 +117,17 @@ var getSourceLibraryName = mName => {
return rt;
};

/**
* normalize library name sap/ui/core -> sap.ui.core
* @param {string} lName library name
*/
var normalizeLibraryName = (lName = "") => lName.replace(/\//g, ".");

var formatNodeModulesPath = mName => {
var nmPath = findNodeModules({ relative: false });
var libraryName = getSourceLibraryName(mName);
if (nmPath && libraryName) {
return pathJoin(nmPath[0], "@openui5", libraryName.replace(/\//g, "."), "src", `${mName}.js`);
return pathJoin(nmPath[0], "@openui5", normalizeLibraryName(libraryName), "src", `${mName}.js`);
} else {
return "";
}
Expand Down Expand Up @@ -169,19 +186,26 @@ var fetchAllResource = async(resourceList = [], resourceRoot = "") => {
var findUi5ModuleName = source => {
var mName = "";

var mNameReg = /sap\.ui\.define\("(.*?)".*?\)/g;

var group;
traverseSource(source, {
CallExpression({ node }) {
const nodeGet = path => get(node, path);
const callArguments = nodeGet("arguments");
if (callArguments) {
// with arguments

while ((group = mNameReg.exec(source)) != undefined) {
try {
mName = group[1];
} catch (error) {
log.error(
`can not found sap.ui.define([...]) with ${group[1]} in ${source}`
);
// sap.ui.define
if (
nodeGet("callee.object.object.name") == "sap" &&
nodeGet("callee.object.property.name") == "ui" &&
(nodeGet("callee.property.name") == "define" || nodeGet("callee.property.name") == "predefine")
) {
// find name
var literal = find(callArguments, arg => (arg.type == "Literal" || arg.type == "StringLiteral"));
mName = literal.value;
}
}
}
}
});

return mName;
};
Expand All @@ -204,7 +228,7 @@ var findAllUi5StandardModules = (source, sourceName = "") => {
if (
nodeGet("callee.object.object.name") == "sap" &&
nodeGet("callee.object.property.name") == "ui" &&
nodeGet("callee.property.name") == "define"
(nodeGet("callee.property.name") == "define" || nodeGet("callee.property.name") == "predefine")
) {
// find []
var arrayExpression = find(nodeGet("arguments"), arg => arg.type == "ArrayExpression");
Expand Down Expand Up @@ -319,16 +343,25 @@ var findAllUi5ViewModules = async(source, sourceName) => {
var findAllImportModules = (source, sourceName = "") => {
var base = dirname(sourceName);
var rt = [];
var matchedTexts = source.match(/import.*?["|'](.*?)["|']/g);
if (matchedTexts) {
rt = matchedTexts.map(t => {
var importName = /import.*?["|'](.*?)["|']/g.exec(t)[1];
if (importName.startsWith("./")) {
importName = pathJoin(base, importName).replace(/\\/g, "/");
var addImportedModules = (m) => {
if (m.startsWith("./") || m.startsWith("../")) {
// relative module
rt = rt.concat(pathJoin(base, m).replace(/\\/g, "/"));
} else {
rt = rt.concat(m);
}
};

traverseSource(source, {
ImportDeclaration: ({ node }) => {
const nodeGet = path => get(node, path);
const importedModuleName = nodeGet("source.value");
if (importedModuleName) {
addImportedModules(importedModuleName);
}
return importName;
});
}
}
});

return rt;
};

Expand Down
26 changes: 25 additions & 1 deletion ui5.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var { findAllUi5StandardModules } = require("./ui5");
var { findAllUi5StandardModules, findUi5ModuleName, findAllImportModules } = require("./ui5");
var { readFileSync } = require("fs");

const testDialogDependency = [
Expand Down Expand Up @@ -51,4 +51,28 @@ test('should find sap.ui.require modules', () => {

test('should find tsx files modules (empty but without errors)', () => {
expect(findAllUi5StandardModules(readFileSync("./test_resources/ProductRating.tsx"), "test/ProductRating").sort()).toStrictEqual([].sort());
});

test('should find ui5 module name', () => {
expect(
findUi5ModuleName(
readFileSync("./test_resources/ui5.wt.ts.model.formatter.js")
)
).toBe("ui5/wt/ts/model/formatter");
});

test("should find es6 imported sources", () => {
const expected = [ 'sap/ui/core/UIComponent',
'sap/ui/model/json/JSONModel',
'sap/ui/Device',
'ui5/wt/ts/fragments/HelloDialog',
'ui5/wt/ts/manifest',
'sap/m/Dialog',
'sap/ui/model/BindingMode',
'sap/ui/core/mvc/View',
'sap/ui/core/mvc/Controller'
] .sort();
expect(
findAllImportModules(readFileSync("./test_resources/Component.ts"), "ui5/wt/ts/Component").sort()
).toStrictEqual(expected);
});

0 comments on commit 8dabedd

Please sign in to comment.