Skip to content
This repository was archived by the owner on Jan 15, 2021. It is now read-only.

Commit dcd4242

Browse files
committed
Merge pull request #428 from thaliproject/issue_411
Add support for installing jxcore-cordova plugin using JXC
2 parents 7be5460 + c131883 commit dcd4242

File tree

11 files changed

+190
-267
lines changed

11 files changed

+190
-267
lines changed

plugin.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<license>MIT</license>
99
<keywords>cordova,jxcore,node.js,thali</keywords>
1010
<engines>
11-
<engine name="cordova-android" version="~4.1.0" />
12-
<engine name="cordova-ios" version="~3.9.0" />
11+
<engine name="cordova-android" version=">=4.1.0" />
12+
<engine name="cordova-ios" version=">=3.9.0" />
1313
<engine name="android-sdk" version=">=19" />
1414
</engines>
1515

@@ -30,8 +30,7 @@
3030
<source-file src="src/android/java/io/jxcore/node/OutgoingSocketThread.java" target-dir="src/io/jxcore/node/" />
3131
<source-file src="src/android/java/io/jxcore/node/SocketThreadBase.java" target-dir="src/io/jxcore/node/" />
3232
<source-file src="src/android/java/io/jxcore/node/StreamCopyingThread.java" target-dir="src/io/jxcore/node/" />
33-
<hook type="after_prepare" src="scripts/androidAfterPrepare.js"/>
34-
<!--<hook type="after_platform_add" src="scripts/androidAfterPlatformAdd.js"/>-->
33+
<hook type="before_compile" src="scripts/androidBeforeCompile.js" />
3534
</platform>
3635

3736
<!-- iOS -->

scripts/androidAfterPrepare.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

scripts/androidBeforeCompile.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
var fs = require('fs-extra-promise');
4+
var path = require('path');
5+
6+
/**
7+
* We've tried various strategies in plugin.xml to set the minimum sdk
8+
* in the Android manifest to an acceptable value such as uses-sdk in
9+
* the platform/config section and also the android-minSdkVersion and they do
10+
* work in that they add an appropriate uses-sdk to AndroidManifest.xml
11+
* but unfortunately it still leaves the default uses-sdk element set to
12+
* a min of 10 in there! I'm sure there is some fix but I'm tired of fighting
13+
* with it so we just force the change. One last thing to try is to create a
14+
* build-extras.gradle next to our plugin.xml and try to either stick in
15+
* an android declare block with a defaultConfig and a minSdkVersion or use
16+
* the special ext property that Cordova declares. But honestly, I'll figure it
17+
* out later.
18+
*/
19+
var updateAndroidSDKVersion = function (appRoot) {
20+
var androidManifestLocation = path.join(appRoot, 'platforms/android/AndroidManifest.xml');
21+
var originalContent = fs.readFileSync(androidManifestLocation).toString();
22+
// Different version of Cordova use different mins, yes we need to replace this with xpath
23+
var newContent = originalContent
24+
.replace('android:minSdkVersion="10"', 'android:minSdkVersion="19"')
25+
.replace('android:minSdkVersion="14"', 'android:minSdkVersion="19"');
26+
fs.writeFileSync(androidManifestLocation, newContent);
27+
};
28+
29+
var replaceJXCoreExtension = function (appRoot) {
30+
var sourceFile = path.join(appRoot, 'plugins/org.thaliproject.p2p/src/android/java/io/jxcore/node/JXcoreExtension.java');
31+
var targetFile = path.join(appRoot, 'platforms/android/src/io/jxcore/node/JXcoreExtension.java');
32+
try {
33+
var sourceContent = fs.readFileSync(sourceFile);
34+
fs.writeFileSync(targetFile, sourceContent);
35+
} catch (e) {
36+
console.log(e);
37+
console.log('Failed to update the JXcoreExtension.java file!');
38+
console.log('Please make sure plugins org.thaliproject.p2p and io.jxcore.node are installed.')
39+
// Exit the process on this error, because it is a hard requirement for this
40+
// plugin to get the right JXcoreExtension.java or otherwise there will be
41+
// a build error when the app is tried to be built.
42+
process.exit(-1);
43+
}
44+
};
45+
46+
var removeInstallFromPlatform = function (appRoot) {
47+
var installDir = path.join(appRoot, 'platforms/android/assets/www/jxcore/node_modules/thali/install');
48+
fs.removeSync(installDir);
49+
};
50+
51+
module.exports = function (context) {
52+
var appRoot = context.opts.projectRoot;
53+
updateAndroidSDKVersion(appRoot);
54+
replaceJXCoreExtension(appRoot);
55+
removeInstallFromPlatform(appRoot);
56+
};

test/www/jxcore/lib/testUtils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var LogCallback;
22
var myName;
33
var os = require('os');
4+
var tmp = require('tmp');
45

56
/**
67
* Turn Bluetooth and WiFi either on or off
@@ -91,3 +92,19 @@ if (typeof jxcore !== 'undefined' && jxcore.utils.OSInfo().isMobile) {
9192
console.log(message);
9293
}
9394
}
95+
96+
/**
97+
* Returns the file path to the temporary directory that can be used by tests
98+
* to store data that does not have to be persisted between app restarts.
99+
* The temporary directory is removed when the process exits.
100+
*/
101+
var tmpObject = null;
102+
exports.tmpDirectory = function () {
103+
if (tmpObject === null) {
104+
tmp.setGracefulCleanup();
105+
tmpObject = tmp.dirSync({
106+
unsafeCleanup: true
107+
});
108+
}
109+
return tmpObject.name;
110+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
var install = require('../../../../thali/install/install.js');
4+
var tape = require('../lib/thali-tape');
5+
var testUtils = require('../lib/testUtils.js');
6+
var exec = require('child_process').exec;
7+
var path = require('path');
8+
9+
var test = tape({
10+
setup: function(t) {
11+
t.end();
12+
},
13+
teardown: function(t) {
14+
t.end();
15+
}
16+
});
17+
18+
test('two required plugins should get installed', function (t) {
19+
var tmpDirectory = testUtils.tmpDirectory();
20+
var testAppName = 'TestApp';
21+
var testAppDirectory = path.join(tmpDirectory, testAppName);
22+
var cordovaCreateCommand = 'cordova create ' + testAppName;
23+
exec(cordovaCreateCommand, { cwd: tmpDirectory }, function (err, stdout, stderr) {
24+
if (err) {
25+
t.fail('Cordova command should not fail!');
26+
t.end();
27+
return;
28+
}
29+
install(function () {
30+
var cordovaPluginsCommand = 'cordova plugins list';
31+
exec(cordovaPluginsCommand, { cwd: testAppDirectory }, function (err, stdout, stderr) {
32+
t.ok(stdout.indexOf('io.jxcore.node' >= 0), 'jxcore cordova plugin is installed');
33+
t.ok(stdout.indexOf('org.thaliproject.p2p' >= 0), 'thali cordova plugin is installed');
34+
t.end();
35+
});
36+
}, testAppDirectory);
37+
});
38+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
var fs = require('fs');
5+
6+
var testUtils = require('../lib/testUtils.js');
7+
var tape = require('../lib/thali-tape');
8+
9+
var test = tape({
10+
setup: function(t) {
11+
t.end();
12+
},
13+
teardown: function(t) {
14+
t.end();
15+
}
16+
});
17+
18+
test('test utils should return same temporary folder when called multiple times', function (t) {
19+
var firstDirectory = testUtils.tmpDirectory();
20+
var secondDirectory = testUtils.tmpDirectory();
21+
t.equal(firstDirectory, secondDirectory);
22+
t.end();
23+
});
24+
25+
test('should be able to write to the temporary folder', function (t) {
26+
var temporaryDirectory = testUtils.tmpDirectory();
27+
console.log(temporaryDirectory);
28+
fs.mkdir(path.join(temporaryDirectory, 'somePath'), function (err) {
29+
t.equal(err, null, 'no error returned when creating a subfolder');
30+
t.end();
31+
});
32+
});

test/www/jxcore/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
"request-promise": "0.4.3",
3636
"socket.io-client": "^1.3.6",
3737
"supertest-as-promised": "^2.0.2",
38-
"thali" : "^2.0.1",
3938
"tape": "^4.0.0",
4039
"tape-catch": "^1.0.4",
41-
"thali": "^1.0.22",
40+
"thali" : "^2.0.1",
41+
"tmp": "0.0.28",
4242
"urlsafe-base64": "^1.0.0",
4343
"uuid": "^2.0.0",
4444
"wrapping-tape": "^0.0.3"

0 commit comments

Comments
 (0)