diff --git a/hooks/android/addResourcesClassImport.js b/hooks/android/addResourcesClassImport.js index 77ec351..a645bde 100644 --- a/hooks/android/addResourcesClassImport.js +++ b/hooks/android/addResourcesClassImport.js @@ -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 @@ -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); @@ -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); @@ -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!'); }) diff --git a/package.json b/package.json index e02ed20..a22169c 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -29,7 +29,7 @@ }, { "name": "cordova-android", - "version": ">=6.0.0" + "version": ">=7.0.0" }, { "name": "android-sdk", @@ -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" } - diff --git a/plugin.xml b/plugin.xml index 9a73358..5bb63d6 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ + id="cordova-plugin-em-jwt-auth" + version="1.6.2"> JWTAuth Get the user email and associated JWT tokens from both native @@ -38,7 +38,8 @@ - + + @@ -92,6 +93,13 @@ + diff --git a/src/android/AuthPendingResult.java b/src/android/AuthPendingResult.java index bb34a04..6f36aac 100644 --- a/src/android/AuthPendingResult.java +++ b/src/android/AuthPendingResult.java @@ -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; diff --git a/src/android/JWTAuthPlugin.java b/src/android/JWTAuthPlugin.java index 46f11bc..75ebb62 100644 --- a/src/android/JWTAuthPlugin.java +++ b/src/android/JWTAuthPlugin.java @@ -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; diff --git a/src/android/OpenIDAuth.java b/src/android/OpenIDAuth.java index dcb33d0..fda1053 100644 --- a/src/android/OpenIDAuth.java +++ b/src/android/OpenIDAuth.java @@ -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.*; diff --git a/src/android/OpenIDAuthStateManager.java b/src/android/OpenIDAuthStateManager.java index ec1f545..97db16a 100644 --- a/src/android/OpenIDAuthStateManager.java +++ b/src/android/OpenIDAuthStateManager.java @@ -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; diff --git a/src/android/PromptedAuth.java b/src/android/PromptedAuth.java index 7eeee3d..99e32f4 100644 --- a/src/android/PromptedAuth.java +++ b/src/android/PromptedAuth.java @@ -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;