Permalink
Browse files

Using r.js as build tool.

  • Loading branch information...
1 parent baa1a0f commit b7068ad06981d0a765e542a3e46c6f3a8208d6d3 Jens Arps committed Sep 8, 2011
Showing with 318 additions and 9 deletions.
  1. +3 −0 .gitmodules
  2. +6 −1 CHANGELOG
  3. +1 −0 libs/r.js
  4. +1 −0 platforms/android.js
  5. +9 −8 tests/test_AMD_built.html
  6. +43 −0 util/Makefile
  7. +64 −0 util/clean.js
  8. +191 −0 util/fileUtil.js
View
@@ -7,3 +7,6 @@
[submodule "libs/requirejs"]
path = libs/requirejs
url = git://github.com/jrburke/requirejs.git
+[submodule "libs/r.js"]
+ path = libs/r.js
+ url = git://github.com/jrburke/r.js.git
View
@@ -11,4 +11,9 @@
* Promoted embed._toArray to embed.toArray
* Promoted embed._toDom to embed.toDom
* Removed pretty print option from embed.toJson
-* Added stripComments param to embed.fromJson
+* Added stripComments param to embed.fromJson
+* Re-enabled style as a possible key object to embed.create
+* Re-introduced a bunch of new tests.
+* Created a new test runner that allows to test a single feature
+ and a specific implementation of it.
+* embed.style returns node.style properties instead of computed style
Submodule r.js added at db6256
@@ -1,4 +1,5 @@
// Implementation map for Android 2.1+
+
define({
'embed': 'embed',
@@ -2,15 +2,16 @@
<html>
<head>
<title>AMD test, built file</title>
- <script src="../src/require.js"></script>
- <script src="../src/built.js"></script>
+<!-- <script src="../src/require.js"></script>-->
+<!-- <script src="../src/built.js"></script>-->
<script>
- require(['embed', 'feature!array'], function(embed) {
- console.log('embedjs loaded.');
- console.log(embed);
- console.log(embed.forEach.toString());
- });
- </script>
+<!-- require(['embed', 'feature!array'], function(embed) {-->
+<!-- console.log('embedjs loaded.');-->
+<!-- console.log(embed);-->
+<!-- console.log(embed.forEach.toString());-->
+<!-- });-->
+ </script>
+ <script src="../build/testbuild-android.js.cleaned.js"></script>
</head>
<body>
<p>This tests dynamic feature implementation in a built file.</p>
View
@@ -0,0 +1,43 @@
+#-------------------#
+# defaults #
+#-------------------#
+
+ifndef profile
+profile=testbuild
+endif
+
+ifndef platform
+platform=android
+endif
+
+ifndef optimize
+optimize=uglify
+endif
+
+
+#-------------------#
+# filename #
+#-------------------#
+
+filename=$(profile)-$(platform).js
+
+
+#-------------------#
+# paths #
+#-------------------#
+
+requirepath=../libs/requirejs
+rjspath=../libs/r.js
+rhinopath=$(rjspath)/lib/rhino
+closurepath=$(rjspath)/lib/closure
+
+
+#-------------------#
+# go #
+#-------------------#
+
+build:
+ java -classpath $(rhinopath)/js.jar:$(closurepath)/compiler.jar org.mozilla.javascript.tools.shell.Main $(rjspath)/dist/r-0.25.0.js -o baseUrl=../src name=../profiles/$(profile) out=../build/$(filename) paths.implementations=../platforms/$(platform) optimize=$(optimize)
+
+clean:
+ java -classpath $(rhinopath)/js.jar org.mozilla.javascript.tools.shell.Main ./clean.js ./ ./ ../build/$(filename)
View
@@ -0,0 +1,64 @@
+var _jsToolsPath = arguments[0][0] == '/' ? arguments[0] : environment["user.dir"] + "/" + arguments[0];
+load(_jsToolsPath + "fileUtil.js");
+
+var filepath = arguments[1];
+var filename = arguments[2];
+var contents = fileUtil.readFile(filename);
+var cleaned = ';embed={};(function(a){';
+
+var statements = contents.split('define(');
+
+print(statements.length);
+statements.forEach(function(_s){
+ print('statement:');
+ print(_s);
+ print('');
+});
+
+// last one is the build profile
+statements.pop();
+
+// filter out feature plugin and impl map
+statements = statements.filter(function(_s){
+ return _s.length > 0 && _s.indexOf('"feature"') != 0 && _s.indexOf('"implementations"') != 0;
+});
+
+print(statements.length);
+statements.forEach(function(_s, index){
+ print('-----------------------');
+
+ // get function body
+
+ var start = _s.indexOf('function(');
+ var end = _s.lastIndexOf(')');
+
+ print(start + ' - ' + end);
+
+ var func = _s.substring(start, end);
+ print('func:');
+ print(func);
+
+ start = func.indexOf('{') + 1;
+ end = func.lastIndexOf('return');
+
+ var body = func.substring(start, end);
+
+ print('');
+ print('body:');
+ print(body);
+
+ if(index == 0){
+ // The first staement is from embed.js, there we need to get rid of the 'var a = {};'
+ start = body.indexOf(';') + 1;
+ body = body.substring(start, body.length);
+ }
+
+ cleaned += body;
+})
+
+cleaned += '})(embed);';
+
+var output = filepath + '/' + filename;
+
+fileUtil.saveUtf8File(output, cleaned);
+
View
@@ -0,0 +1,191 @@
+//Helper functions to deal with file I/O.
+
+var fileUtil = {};
+
+fileUtil.getLineSeparator = function(){
+ //summary: Gives the line separator for the platform.
+ //For web builds override this function.
+ return java.lang.System.getProperty("line.separator"); //Java String
+}
+
+fileUtil.getFilteredFileList = function(/*String*/startDir, /*RegExp*/regExpFilters, /*boolean?*/makeUnixPaths, /*boolean?*/startDirIsJavaObject){
+ //summary: Recurses startDir and finds matches to the files that match regExpFilters.include
+ //and do not match regExpFilters.exclude. Or just one regexp can be passed in for regExpFilters,
+ //and it will be treated as the "include" case.
+ //Ignores files/directories that start with a period (.).
+ var files = [];
+
+ var topDir = startDir;
+ if(!startDirIsJavaObject){
+ topDir = new java.io.File(startDir);
+ }
+
+ var regExpInclude = regExpFilters.include || regExpFilters;
+ var regExpExclude = regExpFilters.exclude || null;
+
+ if(topDir.exists()){
+ var dirFileArray = topDir.listFiles();
+ for (var i = 0; i < dirFileArray.length; i++){
+ var file = dirFileArray[i];
+ if(file.isFile()){
+ var filePath = file.getPath();
+ if(makeUnixPaths){
+ //Make sure we have a JS string.
+ filePath = new String(filePath);
+ if(filePath.indexOf("/") == -1){
+ filePath = filePath.replace(/\\/g, "/");
+ }
+ }
+
+ var ok = true;
+ if(regExpInclude){
+ ok = filePath.match(regExpInclude);
+ }
+ if(ok && regExpExclude){
+ ok = !filePath.match(regExpExclude);
+ }
+
+ if(ok && !file.getName().match(/^\./)){
+ files.push(filePath);
+ }
+ }else if(file.isDirectory() && !file.getName().match(/^\./)){
+ var dirFiles = this.getFilteredFileList(file, regExpFilters, makeUnixPaths, true);
+ files.push.apply(files, dirFiles);
+ }
+ }
+ }
+
+ return files; //Array
+}
+
+
+fileUtil.copyDir = function(/*String*/srcDir, /*String*/destDir, /*RegExp*/regExpFilter, /*boolean?*/onlyCopyNew){
+ //summary: copies files from srcDir to destDir using the regExpFilter to determine if the
+ //file should be copied. Returns a list file name strings of the destinations that were copied.
+ var fileNames = fileUtil.getFilteredFileList(srcDir, regExpFilter, true);
+ var copiedFiles = [];
+
+ for(var i = 0; i < fileNames.length; i++){
+ var srcFileName = fileNames[i];
+ var destFileName = srcFileName.replace(srcDir, destDir);
+
+ if(fileUtil.copyFile(srcFileName, destFileName, onlyCopyNew)){
+ copiedFiles.push(destFileName);
+ }
+ }
+
+ return copiedFiles.length ? copiedFiles : null; //Array or null
+}
+
+fileUtil.copyFile = function(/*String*/srcFileName, /*String*/destFileName, /*boolean?*/onlyCopyNew){
+ //summary: copies srcFileName to destFileName. If onlyCopyNew is set, it only copies the file if
+ //srcFileName is newer than destFileName. Returns a boolean indicating if the copy occurred.
+ var destFile = new java.io.File(destFileName);
+
+ //logger.trace("Src filename: " + srcFileName);
+ //logger.trace("Dest filename: " + destFileName);
+
+ //If onlyCopyNew is true, then compare dates and only copy if the src is newer
+ //than dest.
+ if(onlyCopyNew){
+ var srcFile = new java.io.File(srcFileName);
+ if(destFile.exists() && destFile.lastModified() >= srcFile.lastModified()){
+ return false; //Boolean
+ }
+ }
+
+ //Make sure destination dir exists.
+ var parentDir = destFile.getParentFile();
+ if(!parentDir.exists()){
+ if(!parentDir.mkdirs()){
+ throw "Could not create directory: " + parentDir.getAbsolutePath();
+ }
+ }
+
+ //Java's version of copy file.
+ var srcChannel = new java.io.FileInputStream(srcFileName).getChannel();
+ var destChannel = new java.io.FileOutputStream(destFileName).getChannel();
+ destChannel.transferFrom(srcChannel, 0, srcChannel.size());
+ srcChannel.close();
+ destChannel.close();
+
+ return true; //Boolean
+}
+
+fileUtil.readFile = function(/*String*/path, /*String?*/encoding){
+ //summary: reads a file and returns a string
+ encoding = encoding || "utf-8";
+ var file = new java.io.File(path);
+ var lineSeparator = fileUtil.getLineSeparator();
+ var input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding));
+ try{
+ var stringBuffer = new java.lang.StringBuffer();
+ var line = input.readLine();
+
+ // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
+ // http://www.unicode.org/faq/utf_bom.html
+
+ // Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK:
+ // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
+ if(line && line.length() && line.charAt(0) === 0xfeff){
+ // Eat the BOM, since we've already found the encoding on this file,
+ // and we plan to concatenating this buffer with others; the BOM should
+ // only appear at the top of a file.
+ line = line.substring(1);
+ }
+ while(line !== null){
+ stringBuffer.append(line);
+ stringBuffer.append(lineSeparator);
+ line = input.readLine();
+ }
+ //Make sure we return a JavaScript string and not a Java string.
+ return new String(stringBuffer.toString()); //String
+ }finally{
+ input.close();
+ }
+}
+
+fileUtil.saveUtf8File = function(/*String*/fileName, /*String*/fileContents){
+ //summary: saves a file using UTF-8 encoding.
+ fileUtil.saveFile(fileName, fileContents, "utf-8");
+}
+
+fileUtil.saveFile = function(/*String*/fileName, /*String*/fileContents, /*String?*/encoding){
+ //summary: saves a file.
+ var outFile = new java.io.File(fileName);
+ var outWriter;
+
+ var parentDir = outFile.getParentFile();
+ if(!parentDir.exists()){
+ if(!parentDir.mkdirs()){
+ throw "Could not create directory: " + parentDir.getAbsolutePath();
+ }
+ }
+
+ if(encoding){
+ outWriter = new java.io.OutputStreamWriter(new java.io.FileOutputStream(outFile), encoding);
+ }else{
+ outWriter = new java.io.OutputStreamWriter(new java.io.FileOutputStream(outFile));
+ }
+
+ var os = new java.io.BufferedWriter(outWriter);
+ try{
+ os.write(fileContents);
+ }finally{
+ os.close();
+ }
+}
+
+fileUtil.deleteFile = function(/*String*/fileName){
+ //summary: deletes a file or directory if it exists.
+ var file = new java.io.File(fileName);
+ if(file.exists()){
+ if(file.isDirectory()){
+ var files = file.listFiles();
+ for(var i = 0; i < files.length; i++){
+ this.deleteFile(files[i]);
+ }
+ }
+ file["delete"]();
+ }
+}

0 comments on commit b7068ad

Please sign in to comment.