Skip to content
Browse files

feat(gen:view): Scaffold views with correct UI5 library.

- When the view generator is run it will scaffold out a view that uses the same UI5 library
 as used in the projects index.html (sap.m or sap.ui.commons).

- The view generator now includes a function to determine which library type is used by your
 app: whichUI5Library().

- Minor refactor to the classic app template - the Application.js had spurious whitespace and
 the _index.html created a variable (app) that was never used.

Fixes #53
  • Loading branch information...
1 parent cd9b039 commit 7e8f56d4d39aa818452319b3562980a8b9fdae78 @js1972 committed Feb 26, 2014
View
6 app/templates/application/_Application.js
@@ -36,11 +36,11 @@
main: function() {
// create app view and put to html root element
- var root = this.getRoot(); <%
+ var root = this.getRoot();<%
if (viewType === "xmlView") { %>
- sap.ui.xmlview("main", "view.Main").placeAt(root); <%
+ sap.ui.xmlview("main", "view.Main").placeAt(root);<%
} else { %>
- sap.ui.jsview("main", "view.Main").placeAt(root); <%
+ sap.ui.jsview("main", "view.Main").placeAt(root);<%
} %>
}
});
View
2 app/templates/application/_index.html
@@ -29,7 +29,7 @@
// Launch application
jQuery.sap.require("Application");
- var oApp = new Application({root: "content"}); // instantiate your application and mark the HTML element with id "content" as location for the UI
+ new Application({root: "content"}); // instantiate your application and mark the HTML element with id "content" as location for the UI
</script>
View
10 script-base.js
@@ -230,4 +230,14 @@
openUI5Utils.logResourceRootEditingError(e);
}
};
+
+
+ /**
+ * Check which library the index.html file is using.
+ *
+ * @return {String} Library "sap.m" or "sap.ui.commons".
+ */
+ Generator.prototype.whichLibrary = function() {
+ return openUI5Utils.whichLibrary();
+ };
}());
View
40 utils.js
@@ -142,8 +142,8 @@
var indexContent = fs.readFileSync(indexPath, "utf8").replace(/[\n\r\t]/g, " "); // File as long string w/o CR and TAB
namespace = indexContent.match(/(data\-sap\-ui\-resourceroots\s*\=\s*')(?:(?=(\\?))\2.)*?'/)[0].
- match(/"(.*?)"/)[0].
- split("\"")[1];
+ match(/"(.*?)"/)[0].
+ split("\"")[1];
} catch (e) {
namespace = "";
}
@@ -154,6 +154,39 @@
/**
+ * Which UI5 library is the app using.
+ * Parse the index.html file looking for the library declaration.
+ * If sap.m is found, return it, otherwise return sap.ui.commons.
+ *
+ * @return {string} Library: "sap.m" or "sap.ui.commons"
+ */
+ function whichLibrary() {
+ var library = "sap.ui.commons";
+
+ try {
+ var indexPath = path.join(process.cwd(), "index.html");
+ var indexContent = fs.readFileSync(indexPath, "utf8");
+ var lines = indexContent.split("\n");
+
+ lines.forEach(function(line) {
+ if (line.indexOf("data-sap-ui-libs") !== -1) {
+ if(line.indexOf("sap.m") !== -1) {
+ library = "sap.m";
+ }
+ }
+ });
+ } catch (e) {
+ console.log(chalk("Error determining the UI5 library: Unable to read index.html:"));
+ console.log(e.message);
+ library = "";
+ }
+
+ return library;
+ }
+
+
+
+ /**
* Method to add a line terminating comma on a specified line within a file.
*
* @param {Object} args.path: Path to filename (use CWD if not provided)
@@ -241,6 +274,7 @@
rewriteFile: rewriteFile,
addCommaToLine: addCommaToLine,
getNamespace: getNamespace,
- logResourceRootEditingError: logResourceRootEditingError
+ logResourceRootEditingError: logResourceRootEditingError,
+ whichLibrary: whichLibrary
};
}());
View
6 view/index.js
@@ -75,6 +75,12 @@
+ ViewGenerator.prototype.whichUI5Library = function() {
+ this.UI5library = this.whichLibrary();
+ };
+
+
+
/**
* Scaffolding of the view
*/
View
2 view/templates/application/view/_Main.view.js
@@ -21,7 +21,7 @@
*/
/*jshint unused: vars */
createContent: function(oController) {
- var oControl = new sap.ui.commons.Button({
+ var oControl = new <%= UI5library %>.Button({
text: "{i18n>WELCOME_MESSAGE}",
press: oController.doSomething
});
View
2 view/templates/application/view/_Main.view.xml
@@ -2,7 +2,7 @@
<core:View
xmlns:core="sap.ui.core"
xmlns:mvc="sap.ui.core.mvc"
- xmlns="sap.ui.commons"
+ xmlns="<%= UI5library %>"
xmlns:html="http://www.w3.org/1999/xhtml"
controllerName="<%= applicationNamespace%><%= viewName %>">
<Panel text="Hello World from an XML view">

0 comments on commit 7e8f56d

Please sign in to comment.
Something went wrong with that request. Please try again.