Skip to content

Commit 19df164

Browse files
author
Mihail Slavchev
committed
move android-static-binding-generator
1 parent ec01ee7 commit 19df164

File tree

117 files changed

+4295
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+4295
-4
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,3 @@
88
[submodule "test-app/assets/app/tns_modules/shared"]
99
path = test-app/app/src/main/assets/app/tns_modules/shared
1010
url = git@github.com:NativeScript/common-runtime-tests-modules.git
11-
[submodule "android-static-binding-generator"]
12-
path = android-static-binding-generator
13-
url = https://github.com/NativeScript/android-static-binding-generator

android-static-binding-generator

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
node_modules
2+
.vscode
3+
project/staticbindinggenerator/.idea
4+
project/staticbindinggenerator/gradle
5+
project/staticbindinggenerator/gradlew
6+
project/staticbindinggenerator/gradlew.bat
7+
project/staticbindinggenerator/local.properties
8+
project/interfaces-names.txt
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# android-static-binding-generator
2+
This is a tool for javascript static analysis.
3+
* Input: valid javascript code.
4+
* Output: should be generated file with necessary information that is [NativeScript](https://www.nativescript.org/) specific.
5+
6+
## How to install
7+
8+
open project folder and run:
9+
```
10+
npm install
11+
```
12+
13+
## Usage with Sublime Text 3
14+
15+
* Open `project/parse.js` in Sublime Text 3
16+
* in Editor, select `Tools/BuildSystem/NodeJs`. You need to have [nodejs](https://nodejs.org/en/) installed. I'm using latest LTS(long term support) currently 4.2.5.
17+
* `Alt+R` to run nodejs in editor
18+
19+
## How to test
20+
* `npm install`
21+
* `npm test`
22+
23+
## More information:
24+
* babel: javasript transpiler
25+
* [babylon](https://github.com/babel/babel/tree/master/packages/babylon): babel javascript parser
26+
* [babel handbook](https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/README.md)
27+
* learning from [plugin handbook](https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md)
28+
29+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "static_analysis",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "project/parse.js",
6+
"scripts": {
7+
"test": "node node_modules/jasmine-node/lib/jasmine-node/cli.js tests/specs --verbose"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"babel-traverse": "^6.4.5",
13+
"babel-types": "^6.4.5",
14+
"babylon": "^6.4.5",
15+
"filewalker": "^0.1.2",
16+
"lazy": "^1.0.11"
17+
},
18+
"devDependencies": {
19+
"jasmine-node": "^1.14.5"
20+
}
21+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* The android static binding generator will generate bindings for the Javascript code you specify.
3+
*/
4+
5+
def isWinOs = System.properties['os.name'].toLowerCase().contains('windows')
6+
7+
def astParserDir = "$projectDir/parser"
8+
def interfaceNamesFilePath = "$projectDir/interfaces-names.txt"
9+
def bindingsFilePath = "$projectDir/bindings.txt"
10+
def cachedJarsFilePath = "$projectDir/cached.txt"
11+
12+
def shouldRun = true
13+
def absoluteOutDir = project.outDir
14+
def absoluteJsCodeDir = project.jsCodeDir
15+
def utf8 = java.nio.charset.StandardCharsets.UTF_8
16+
def current = ""
17+
18+
// depends on passed jars and generated interface-names
19+
task generateInterfaceNamesList() {
20+
doFirst {
21+
current = project.jarFiles
22+
def cache = new File(cachedJarsFilePath)
23+
24+
if (cache.exists()) {
25+
def contents = new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(cachedJarsFilePath)), utf8).trim()
26+
shouldRun = !contents.equals(current.toString())
27+
}
28+
29+
if (shouldRun) {
30+
javaexec {
31+
main "-jar"
32+
33+
def jarsAsStr = current.toString();
34+
def jarsArr = jarsAsStr.replaceAll(/[\[\]]/, "").split(", ")
35+
36+
def str = new LinkedList <String> ();
37+
str.add("interfacenamegenerator.jar")
38+
str.addAll(jarsArr)
39+
40+
args str.toArray()
41+
}
42+
java.nio.file.Files.write(java.nio.file.Paths.get(cachedJarsFilePath), [current.toString()], utf8)
43+
}
44+
}
45+
}
46+
47+
// if there are new dependencies the parser will run again
48+
task runAstParser () {
49+
inputs.files fileTree(dir: absoluteJsCodeDir)
50+
outputs.files(bindingsFilePath)
51+
52+
doFirst {
53+
exec {
54+
workingDir astParserDir
55+
56+
if(isWinOs) {
57+
commandLine "cmd", "/c", "node", "js_parser.js" , absoluteJsCodeDir, "../bindings.txt"
58+
}
59+
else {
60+
commandLine "node", "js_parser.js", absoluteJsCodeDir, "../bindings.txt"
61+
}
62+
}
63+
}
64+
}
65+
// run the static binding generator
66+
task generateBindings() {
67+
68+
inputs.files(bindingsFilePath)
69+
outputs.dir(absoluteOutDir)
70+
outputs.upToDateWhen({
71+
return !shouldRun
72+
})
73+
74+
doFirst {
75+
javaexec {
76+
main "-jar"
77+
78+
def str = new LinkedList <String> ();
79+
str.add("staticbindinggenerator.jar")
80+
str.add(bindingsFilePath)
81+
str.add(absoluteOutDir)
82+
str.addAll(project.jarFiles)
83+
84+
args str.toArray()
85+
}
86+
}
87+
}
88+
89+
runAstParser.dependsOn(generateInterfaceNamesList)
90+
generateBindings.dependsOn(runAstParser)
Binary file not shown.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var fs = require("fs"),
2+
path = require("path");
3+
module.exports = function Helpers(config) {
4+
5+
function cleanOutFile(filePath) {
6+
fs.truncateSync(filePath, 0);
7+
if(config && config.logger) {
8+
config.logger.info("+cleared out file: " + filePath);
9+
}
10+
}
11+
12+
function createFile(filePath) {
13+
if(ensureDirectories(filePath)) {
14+
fs.writeFileSync(filePath, "");
15+
if(config && config.logger) {
16+
config.logger.info("+created ast output file: ");
17+
}
18+
}
19+
cleanOutFile(filePath)
20+
}
21+
22+
function ensureDirectories(filePath) {
23+
var parentDir = path.dirname(filePath);
24+
if(fs.existsSync(parentDir)) {
25+
return true;
26+
}
27+
28+
ensureDirectories(parentDir);
29+
fs.mkdirSync(parentDir);
30+
return true;
31+
}
32+
33+
return {
34+
cleanOutFile: cleanOutFile,
35+
createFile: createFile,
36+
ensureDirectories: ensureDirectories
37+
}
38+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var stringifyOnce = function(obj, replacer, indent){
2+
var printedObjects = [];
3+
var printedObjectKeys = [];
4+
var passingIndent = 4;
5+
if(indent) {
6+
passingIndent = indent;
7+
}
8+
9+
function printOnceReplacer(key, value){
10+
if ( printedObjects.length > 2000){ // browsers will not print more than 20K, I don't see the point to allow 2K.. algorithm will not be fast anyway if we have too many objects
11+
return 'object too long';
12+
}
13+
var printedObjIndex = false;
14+
printedObjects.forEach(function(obj, index){
15+
if(obj===value){
16+
printedObjIndex = index;
17+
}
18+
});
19+
20+
if ( key == ''){ //root element
21+
printedObjects.push(obj);
22+
printedObjectKeys.push("root");
23+
return value;
24+
}
25+
26+
else if(printedObjIndex+"" != "false" && typeof(value)=="object"){
27+
if ( printedObjectKeys[printedObjIndex] == "root"){
28+
return "(pointer to root)";
29+
}else{
30+
return "(see " + ((!!value && !!value.constructor) ? value.constructor.name.toLowerCase() : typeof(value)) + " with key " + printedObjectKeys[printedObjIndex] + ")";
31+
}
32+
}else{
33+
34+
var qualifiedKey = key || "(empty key)";
35+
printedObjects.push(value);
36+
printedObjectKeys.push(qualifiedKey);
37+
if(replacer){
38+
return replacer(key, value);
39+
}else{
40+
return value;
41+
}
42+
}
43+
}
44+
return JSON.stringify(obj, printOnceReplacer, passingIndent);
45+
};
46+
47+
module.exports = stringifyOnce;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
var fs = require('fs'),
2+
path = require('path'),
3+
os = require('os'),
4+
fileHelpers = require("./file_helpers")();
5+
6+
module.exports = function (setting) {
7+
var logDirectory = path.dirname(setting.logPath);
8+
9+
// TODO: Logging to file disabled temporarily, console output is default
10+
11+
// if (!fs.existsSync(logDirectory)) {
12+
// console.error("couldn't find logDirectory so it will be created in place:" + setting.logPath);
13+
// fileHelpers.ensureDirectories(setting.logPath);
14+
// }
15+
// if (os.type().indexOf('Windows') === -1) {
16+
// var appLogStat = fs.statSync(logDirectory);
17+
// if (canWrite(process.uid === appLogStat.uid, process.gid === appLogStat.gid, appLogStat.mode)) {
18+
// console.error("ERROR WRITING TO LOG FILE DIRECTORY : " + logDirectory);
19+
// process.exit(-1);
20+
// }
21+
// }
22+
23+
var appLog = createLog(setting.APP_NAME, logDirectory, setting);
24+
25+
if (setting.disable) {
26+
for (var prop in appLog) {
27+
appLog[prop] = function () { };
28+
}
29+
}
30+
31+
return appLog;
32+
};
33+
34+
function canWrite(owner, inGroup, mode) {
35+
return owner && (mode & 00200) || // User is owner and owner can write.
36+
inGroup && (mode & 00020) || // User is in group and group can write.
37+
(mode & 00002); // Anyone can write.
38+
}
39+
40+
function createLog(appName, logDirectory, setting) {
41+
42+
var appLog = {};
43+
44+
function getRequestId() {
45+
return (process.domain && process.domain.id) || "";
46+
}
47+
48+
function getLogDate() {
49+
var today = new Date();
50+
51+
var fullYear = today.getFullYear();
52+
var month = today.getMonth();
53+
var day = today.getDate();
54+
var hours = today.getHours();
55+
var minutes = today.getMinutes();
56+
var seconds = today.getSeconds();
57+
58+
var logDate = fullYear + "-" + month + "-" + day + "/" + hours + ":" + minutes + ":" + seconds;
59+
return logDate;
60+
}
61+
62+
function setMessageWithFormat(message) {
63+
var res = getLogDate() + "\t" + message;
64+
return res;
65+
}
66+
67+
function appendToLogFile(line) {
68+
var logFile = logDirectory + path.sep + appName + ".log";
69+
var augmentedLine = getLogDate() + "\t" + line + os.EOL
70+
fs.appendFile(logFile, augmentedLine, function (err) {
71+
if (err) {
72+
throw "couldn't write to " + logFile;
73+
}
74+
});
75+
}
76+
77+
appLog.log = function (input) {
78+
console.log(setMessageWithFormat(input));
79+
}
80+
81+
appLog.info = appLog.log;
82+
83+
appLog.warn = function (input) {
84+
console.warn(setMessageWithFormat(input));
85+
}
86+
87+
appLog.error = function (input) {
88+
console.error(setMessageWithFormat(input));
89+
}
90+
91+
return appLog;
92+
}

0 commit comments

Comments
 (0)