Skip to content

Commit

Permalink
Merge pull request #41 from shankari/fix_id_and_upgrade
Browse files Browse the repository at this point in the history
Fix id and upgrade
  • Loading branch information
shankari authored Aug 1, 2020
2 parents fe20adc + d998b1b commit 7d0c33a
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 79 deletions.
117 changes: 56 additions & 61 deletions hooks/android/addResourcesClassImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,14 @@
* A hook to add resources class (R.java) import to Android classes which uses it.
*/

function getRegexGroupMatches(string, regex, index) {
index || (index = 1)

var matches = [];
var match;
if (regex.global) {
while (match = regex.exec(string)) {
matches.push(match[index]);
console.log('Match:', match);
}
}
else {
if (match = regex.exec(string)) {
matches.push(match[index]);
}
}
const fs = require('fs'),
path = require('path');

const PACKAGE_RE = /package ([^;]+);/;
const R_RE = /[^\.\w]R\./;
const BUILDCONFIG_RE = /[^\.\w]BuildConfig\./;
const MAINACT_RE = /[^\.\w]MainActivity\./;

return matches;
}

// Adapted from
// https://stackoverflow.com/a/5827895
Expand All @@ -31,10 +20,7 @@ function getRegexGroupMatches(string, regex, index) {
// javascript modules using plugin.xml, and this uses only standard modules

var walk = function(ctx, dir, done) {
var results = [];

var fs = ctx.requireCordovaModule('fs'),
path = ctx.requireCordovaModule('path');
let results = [];

fs.readdir(dir, function(err, list) {
if (err) return done(err);
Expand Down Expand Up @@ -62,15 +48,11 @@ module.exports = function (ctx) {
if (ctx.opts.cordova.platforms.indexOf('android') < 0)
return;

var fs = ctx.requireCordovaModule('fs'),
path = ctx.requireCordovaModule('path'),
Q = ctx.requireCordovaModule('q');

var platformSourcesRoot = path.join(ctx.opts.projectRoot, 'platforms/android/app/src/main/java/');
var pluginSourcesRoot = path.join(ctx.opts.plugin.dir, 'src/android');
const platformSourcesRoot = path.join(ctx.opts.projectRoot, 'platforms/android/app/src/main/java/');
const pluginSourcesRoot = path.join(ctx.opts.plugin.dir, 'src/android');

var androidPluginsData = JSON.parse(fs.readFileSync(path.join(ctx.opts.projectRoot, 'plugins', 'android.json'), 'utf8'));
var appPackage = androidPluginsData.installed_plugins[ctx.opts.plugin.id]['PACKAGE_NAME'];
const androidPluginsData = JSON.parse(fs.readFileSync(path.join(ctx.opts.projectRoot, 'plugins', 'android.json'), 'utf8'));
const appPackage = androidPluginsData.installed_plugins[ctx.opts.plugin.id]['PACKAGE_NAME'];

walk(ctx, pluginSourcesRoot, function (err, files) {
console.log("walk callback with files = "+files);
Expand All @@ -83,73 +65,86 @@ module.exports = function (ctx) {

files.filter(function (file) { return path.extname(file) === '.java'; })
.forEach(function (file) {
var deferral = Q.defer();

// console.log("Considering file "+file);
var filename = path.basename(file);
// console.log("basename "+filename);
// var file = path.join(pluginSourcesRoot, filename);
// console.log("newfile"+file);
fs.readFile(file, 'utf-8', function (err, contents) {
if (err) {
console.error('Error when reading file:', err)
deferral.reject();
return
}

if (contents.match(/[^\.\w]R\./) || contents.match(/[^\.\w]BuildConfig\./) || contents.match(/[^\.\w]MainActivity\./)) {
console.log('Trying to get packages from file:', filename);
var packages = getRegexGroupMatches(contents, /package ([^;]+);/);
for (var p = 0; p < packages.length; p++) {
try {
var package = packages[p];
console.log('Handling package:', package);
const cp = new Promise(function(resolve, reject) {
// console.log("Considering file "+file);
const filename = path.basename(file);
// console.log("basename "+filename);
// var file = path.join(pluginSourcesRoot, filename);
// console.log("newfile"+file);
fs.readFile(file, 'utf-8', function (err, contents) {
if (err) {
console.error('Error when reading file:', err)
reject();
}

var sourceFile = path.join(platformSourcesRoot, package.replace(/\./g, '/'), filename)
if (contents.match(R_RE) || contents.match(BUILDCONFIG_RE) || contents.match(MAINACT_RE)) {
console.log('file '+filename+' needs to be rewritten, checking package');
const packages = contents.match(PACKAGE_RE);
if (packages.length > 2) {
console.error('Java source files must have only one package, found ', packages.length);
reject();
}

const pkg = packages[1];
console.log('Handling package:', pkg);
try {
const sourceFile = path.join(platformSourcesRoot, pkg.replace(/\./g, '/'), filename)
console.log('sourceFile:', sourceFile);
if (!fs.existsSync(sourceFile))
throw 'Can\'t find file in installed platform directory: "' + sourceFile + '".';

var sourceFileContents = fs.readFileSync(sourceFile, 'utf8');
const sourceFileContents = fs.readFileSync(sourceFile, 'utf8');
if (!sourceFileContents)
throw 'Can\'t read file contents.';

var newContents = sourceFileContents;
let newContents = sourceFileContents;

if (contents.match(/[^\.\w]R\./)) {
if (contents.match(R_RE)) {
newContents = sourceFileContents
.replace(/(import ([^;]+).R;)/g, '')
.replace(/(package ([^;]+);)/g, '$1 \n// Auto fixed by post-plugin hook \nimport ' + appPackage + '.R;');
}

// replace BuildConfig as well
if (contents.match(/[^\.\w]BuildConfig\./)) {
if (contents.match(BUILDCONFIG_RE)) {
newContents = newContents
.replace(/(import ([^;]+).BuildConfig;)/g, '')
.replace(/(package ([^;]+);)/g, '$1 \n// Auto fixed by post-plugin hook \nimport ' + appPackage + '.BuildConfig;');
}

// replace MainActivity as well
if (contents.match(/[^\.\w]MainActivity\./)) {
if (contents.match(MAINACT_RE)) {
newContents = newContents
.replace(/(import ([^;]+).MainActivity;)/g, '')
.replace(/(package ([^;]+);)/g, '$1 \n// Auto fixed by post-plugin hook \nimport ' + appPackage + '.MainActivity;');
}

fs.writeFileSync(sourceFile, newContents, 'utf8');
break;
resolve();
}
catch (ex) {
console.log('Could not add import to "' + filename + '" using package "' + package + '". ' + ex);
reject();
}
// we should never really get here because we return
// from both the try and the catch blocks. But in case we do,
// let's reject so we can debug
reject();
} else {
// the file had no BuildConfig or R dependencies, no need
// to rewrite it. We can potentially get rid of this check
// since we re-check for the imports before re-writing them
// but it avoid unnecessary file rewrites, so we retain
// it for now
resolve();
}
}
});
});

deferrals.push(deferral.promise);
deferrals.push(cp);
});

Q.all(deferrals)
Promise.all(deferrals)
.then(function() {
console.log('Done with the hook!');
})
Expand Down
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "em-cordova-jwt-auth",
"version": "1.4.0",
"name": "cordova-plugin-em-jwt-auth",
"version": "1.6.2",
"description": "Several wrappers for authentication methods",
"license": "BSD-3-Clause",
"cordova": {
"id": "em-cordova-jwt-auth",
"id": "cordova-plugin-em-jwt-auth",
"platforms": [
"android",
"ios",
"windows"
"ios"
]
},
"repository": {
Expand All @@ -29,7 +29,7 @@
},
{
"name": "cordova-android",
"version": ">=6.0.0"
"version": ">=7.0.0"
},
{
"name": "android-sdk",
Expand All @@ -41,10 +41,8 @@
}
],
"author": "K. Shankari",
"license": "BSD 3-clause",
"bugs": {
"url": "https://github.com/e-mission/cordova-jwt-auth/issues"
},
"homepage": "https://e-mission/cordova-jwt-auth"
}

14 changes: 11 additions & 3 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
id="edu.berkeley.eecs.emission.cordova.auth"
version="1.6.2-alpha1">
id="cordova-plugin-em-jwt-auth"
version="1.6.2">

<name>JWTAuth</name>
<description>Get the user email and associated JWT tokens from both native
Expand Down Expand Up @@ -38,7 +38,8 @@
</feature>
</config-file>

<framework src="com.google.android.gms:play-services-auth:11.0.1"/>
<framework src="com.google.android.gms:play-services-auth:$AUTH_VERSION"/>
<preference name="AUTH_VERSION" default="11.0.1"/>

<!-- Configuration of OpenID auth plugin start -->
<framework src="net.openid:appauth:0.7.0"/>
Expand Down Expand Up @@ -92,6 +93,13 @@

<framework src="SystemConfiguration.framework"/>
<framework src="Security.framework"/>
<!--
<podspec>
<pod name="GoogleSignIn" spec="~> 5.0.0" />
<pod name="AppAuth" spec="~> 1.2" />
<pod name="JWT" spec="~> 3.0.0-beta.12" />
</podspec>
-->
<framework src="GoogleSignIn" type="podspec" spec="~> 5.0.0"/>
<framework src="AppAuth" type="podspec" spec="~> 1.2"/>
<framework src="JWT" type="podspec" spec="~> 3.0.0-beta.12"/>
Expand Down
2 changes: 1 addition & 1 deletion src/android/AuthPendingResult.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package edu.berkeley.eecs.emission.cordova.jwtauth;

import android.os.AsyncTask;
import android.support.annotation.NonNull;
import androidx.annotation.NonNull;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.common.api.CommonStatusCodes;
Expand Down
2 changes: 1 addition & 1 deletion src/android/JWTAuthPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import androidx.annotation.NonNull;
import android.widget.Toast;

import com.google.android.gms.common.api.ResultCallback;
Expand Down
2 changes: 1 addition & 1 deletion src/android/OpenIDAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import androidx.annotation.Nullable;

import net.openid.appauth.*;

Expand Down
6 changes: 3 additions & 3 deletions src/android/OpenIDAuthStateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.AnyThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.Log;

import net.openid.appauth.AuthState;
Expand Down
2 changes: 1 addition & 1 deletion src/android/PromptedAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import androidx.annotation.NonNull;

import edu.berkeley.eecs.emission.cordova.connectionsettings.ConnectionSettings;
import edu.berkeley.eecs.emission.cordova.unifiedlogger.Log;
Expand Down

0 comments on commit 7d0c33a

Please sign in to comment.