Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ability to embed into platform host projects #1799

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ thumbs.db
android-runtime.iml
test-app/build-tools/*.log
test-app/analytics/build-statistics.json
package-lock.json
package-lock.json
test-app/build-tools/jsparser/tests/cases/*/internal/livesync.js
test-app/build-tools/*.jar
7 changes: 7 additions & 0 deletions build-artifacts/project-template-gradle/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
pluginManagement {
repositories {
gradlePluginPortal()
mavenLocal()
}
}

rootProject.name = "__PROJECT_NAME__"
include ':app'//, ':runtime', ':runtime-binding-generator'

Expand Down
12 changes: 10 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,18 @@ task copyFilesToProjectTemeplate {
copy {
from "$TEST_APP_PATH/app/src/main/java/com/tns/"
include "*.java"
exclude "NativeScriptApplication.java"
exclude "NativeScriptActivity.java"
exclude "TestNativeScriptApplication.java"
exclude "TestNativeScriptActivity.java"
into "$DIST_FRAMEWORK_PATH/app/src/main/java/com/tns"
}
copy {
from "$TEST_APP_PATH/app/src/main/java/com/tns/internal"
into "$DIST_FRAMEWORK_PATH/app/src/main/java/com/tns/internal"
}
copy {
from "$TEST_APP_PATH/app/src/main/java/com/tns/embedding"
into "$DIST_FRAMEWORK_PATH/app/src/main/java/com/tns/embedding"
}
copy {
from "$BUILD_TOOLS_PATH/static-binding-generator/build/libs/static-binding-generator.jar"
into "$DIST_FRAMEWORK_PATH/build-tools"
Expand Down Expand Up @@ -295,6 +299,10 @@ task copyFilesToProjectTemeplate {
from "$TEST_APP_PATH/app/build.gradle"
into "$DIST_FRAMEWORK_PATH/app"
}
copy {
from "$TEST_APP_PATH/app/nativescript.gradle"
into "$DIST_FRAMEWORK_PATH/app"
}
copy {
from "$TEST_APP_PATH/build.gradle"
into "$DIST_FRAMEWORK_PATH"
Expand Down
2 changes: 0 additions & 2 deletions test-app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@ app/app.iml
treeNodeStream.dat
treeStringsStream.dat
treeValueStream.dat
NativeScriptActivity.java
NativeScriptApplication.java
**/com/tns/gen
4 changes: 2 additions & 2 deletions test-app/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

<application
android:requestLegacyExternalStorage="true"
android:name="com.tns.NativeScriptApplication"
android:name="com.tns.TestNativeScriptApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.tns.NativeScriptActivity" android:exported="true">
<activity android:name="com.tns.TestNativeScriptActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
4 changes: 2 additions & 2 deletions test-app/app/src/main/assets/app/MyActivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
}

@JavaProxy("com.tns.NativeScriptActivity")
@JavaProxy("com.tns.TestNativeScriptActivity")
class MyActivity extends android.app.Activity
{
onCreate(bundle: android.os.Bundle)
Expand Down Expand Up @@ -62,7 +62,7 @@ var MyActivity = (function (_super) {

};
MyActivity = __decorate([
JavaProxy("com.tns.NativeScriptActivity")
JavaProxy("com.tns.TestNativeScriptActivity")
], MyActivity);
return MyActivity;
})(android.app.Activity);
2 changes: 1 addition & 1 deletion test-app/app/src/main/assets/app/MyApp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// demonstrates how to extend class in JavaScript with prebuilt Java proxy
var MyApp = android.app.Application.extend("com.tns.NativeScriptApplication",
var MyApp = android.app.Application.extend("com.tns.TestNativeScriptApplication",
{
onCreate: function()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void callback(String str) {
private String readString() throws Exception {
String str = null;

Context context = com.tns.NativeScriptApplication.getInstance();
Context context = com.tns.TestNativeScriptApplication.getInstance();

InputStream inputStream = null;
try {
Expand Down
6 changes: 6 additions & 0 deletions test-app/app/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.nativescript.staticbindinggenerator;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -11,8 +12,10 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -112,6 +115,10 @@ private static void validateInput() throws IOException {

List<DataRow> inputFile = Generator.getRows(SBG_INPUT_FILE);
inputDir = new File(inputFile.get(0).getRow());
Path assetsInternalDirPath = inputDir.getParentFile().toPath().resolve("internal");
extractResource(assetsInternalDirPath.resolve("ts_helpers.js"), "ts_helpers.js");
extractResource(assetsInternalDirPath.resolve("livesync.js"), "livesync.js");

webpackWorkersExcludePath = Paths.get(inputDir.getAbsolutePath(), "__worker-chunks.json").toString();

if (!inputDir.exists() || !inputDir.isDirectory()) {
Expand All @@ -132,7 +139,9 @@ private static void validateInput() throws IOException {
* This output file should contain all the information needed to generate java counterparts to the traversed js classes.
* */
private static void runJsParser() {
String parserPath = Paths.get(System.getProperty("user.dir"), "jsparser", "js_parser.js").toString();
Path jsParserPath = Paths.get(System.getProperty("user.dir"), "jsparser", "js_parser.js");
extractResource(jsParserPath, "js_parser.js");
String parserPath = jsParserPath.toString();
NodeJSProcess nodeJSProcess = new NodeJSProcessImpl(new ProcessExecutorImpl(), new EnvironmentVariablesReaderImpl());
int exitCode = nodeJSProcess.runScript(parserPath);

Expand All @@ -141,6 +150,40 @@ private static void runJsParser() {
}
}

private static void extractResource(Path savePath, String resourceName) {
File jsParserFile = savePath.toFile();
if (!jsParserFile.exists()) {
try {
jsParserFile.getParentFile().mkdirs();
jsParserFile.createNewFile();
InputStream source = Main.class.getResourceAsStream("/" + resourceName);
if (source == null) {
throw new RuntimeException(resourceName + " not found in resources");
}
FileUtils.copyInputStreamToFile(source, jsParserFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

private static void maybeExtractJsParserSource(Path jsParserPath) {
File jsParserFile = jsParserPath.toFile();
if (!jsParserFile.exists()) {
try {
jsParserFile.getParentFile().mkdirs();
jsParserFile.createNewFile();
InputStream source = Main.class.getResourceAsStream("/js_parser.js");
if (source == null) {
throw new RuntimeException("js_parser.js not found in resources");
}
FileUtils.copyInputStreamToFile(source, jsParserFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

private static Boolean rootTraversed = false;

private static void traverseDirectory(File currentDir, boolean traverseExplicitly) throws IOException, JSONException {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (global.__onLiveSync) {
global.__onLiveSync();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
(function () {

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length;
var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;

if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") {
r = global.Reflect.decorate(decorators, target, key, desc);
}
else {
for (var i = decorators.length - 1; i >= 0; i--) {
if (d = decorators[i]) {
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
}
}
}
return c > 3 && r && Object.defineProperty(target, key, r), r;
};

// For backward compatibility.
var __native = function (thiz) {
// we are setting the __container__ property to the base class when the super method is called
// if the constructor returns the __native(this) call we will use the old implementation
// copying all the properties to the result
// otherwise if we are using the result from the super() method call we won't need such logic
// as thiz already contains the parent properties
// this way we now support both implementations in typescript generated constructors:
// 1: super(); return __native(this);
// 2: return super() || this;
if (thiz.__container__) {
var result = thiz.__proto__;

for (var prop in thiz) {
if (thiz.hasOwnProperty(prop)) {
thiz.__proto__[prop] = thiz[prop];
delete thiz[prop];
}
}

thiz.constructor = undefined;
thiz.__proto__ = undefined;
Object.freeze(thiz);
Object.preventExtensions(thiz);
return result;
} else {
return thiz;
}
};

var __extends = function (Child, Parent) {
var extendNativeClass = !!Parent.extend && (Parent.extend.toString().indexOf("[native code]") > -1);
if (!extendNativeClass) {
__extends_ts(Child, Parent);
return;
}
if (Parent.__isPrototypeImplementationObject) {
throw new Error("Can not extend an already extended native object.");
}

function extend(thiz) {
var child = thiz.__proto__.__child;
if (!child.__extended) {
var parent = thiz.__proto__.__parent;
child.__extended = parent.extend(child.name, child.prototype, true);
// This will deal with "i instanceof child"
child[Symbol.hasInstance] = function (instance) {
return instance instanceof this.__extended;
}
}
return child.__extended;
};

Parent.__activityExtend = function (parent, name, implementationObject) {
__log("__activityExtend called");
return parent.extend(name, implementationObject);
};

Parent.call = function (thiz) {
var Extended = extend(thiz);
thiz.__container__ = true;
if (arguments.length > 1) {
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1))));
}
else {
thiz.__proto__ = new Extended()
}
return thiz.__proto__;
};

Parent.apply = function (thiz, args) {
var Extended = extend(thiz);
thiz.__container__ = true;
if (args && args.length > 0) {
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args)));
}
else {
thiz.__proto__ = new Extended();
}
return thiz.__proto__;
};
__extends_ns(Child, Parent);
Child.__isPrototypeImplementationObject = true;
Child.__proto__ = Parent;
Child.prototype.__parent = Parent;
Child.prototype.__child = Child;
}

var __extends_ts = function (child, parent) {
extendStaticFunctions(child, parent);
assignPrototypeFromParentToChild(parent, child);
};

var __extends_ns = function (child, parent) {
if (!parent.extend) {
assignPropertiesFromParentToChild(parent, child);
}

assignPrototypeFromParentToChild(parent, child);
};

var extendStaticFunctions =
Object.setPrototypeOf
|| (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; })
|| assignPropertiesFromParentToChild;

function hasInternalProtoProperty() {
return { __proto__: [] } instanceof Array;
}

function assignPropertiesFromParentToChild(parent, child) {
for (var property in parent) {
if (parent.hasOwnProperty(property)) {
child[property] = parent[property];
}
}
}

function assignPrototypeFromParentToChild(parent, child) {
function __() {
this.constructor = child;
}

if (parent === null) {
child.prototype = Object.create(null);
} else {
__.prototype = parent.prototype;
child.prototype = new __();
}
}


function JavaProxy(className) {
return function (target) {
var extended = target.extend(className, target.prototype)
extended.name = className;
return extended;
};
}

function Interfaces(interfacesArr) {
return function (target) {
if (interfacesArr instanceof Array) {
// attach interfaces: [] to the object
target.prototype.interfaces = interfacesArr;
}
}
}

Object.defineProperty(global, "__native", { value: __native });
Object.defineProperty(global, "__extends", { value: __extends });
Object.defineProperty(global, "__decorate", { value: __decorate });

global.JavaProxy = JavaProxy;
global.Interfaces = Interfaces;
})()
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void testParseNamedExtend() throws IOException {
Assert.assertEquals(row.getColumn(), "");
Assert.assertEquals(row.getNewClassName(), "");
Assert.assertEquals(row.getMethods()[0], "toString");
Assert.assertEquals(row.getFilename(), "com.tns.NativeScriptActivity");
Assert.assertEquals(row.getFilename(), "com.tns.TestNativeScriptActivity");
Assert.assertEquals(row.getJsFilename(), "MyActivity.js");
Assert.assertEquals(row.getInterfaces()[0], "");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
java.lang.Object*****toString*com.tns.NativeScriptActivity*MyActivity.js*
java.lang.Object*****toString*com.tns.TestNativeScriptActivity*MyActivity.js*
Loading
Loading