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: Add a config plugin for Expo (iOS & Android) #915

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions packages/react-native-app-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"react-native": ">=0.63.0"
},
"devDependencies": {
"@expo/config-plugins": "^8.0.8",
"jest": "24.9.0",
"react": "16.9.0",
"react-native": "0.63.0"
Expand Down
54 changes: 54 additions & 0 deletions packages/react-native-app-auth/plugin/android/app-build-gradle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { AndroidConfig, withDangerousMod } = require('@expo/config-plugins');
const {
createGeneratedHeaderComment,
removeContents,
} = require('@expo/config-plugins/build/utils/generateCode');
const codeModAndroid = require('@expo/config-plugins/build/android/codeMod');
const fs = require('fs');

const withAppAuthAppBuildGradle = (rootConfig, options) =>
withDangerousMod(rootConfig, [
'android',
config => {
// detauls to app scheme
const authScheme = options?.authScheme ?? config.scheme ?? '';

// find the app/build.gradle file and checks its format
const appBuildGradlePath = AndroidConfig.Paths.getAppBuildGradleFilePath(
config.modRequest.projectRoot
);

// BEWARE: we update the app/build.gradle file *outside* of the standard Expo config procedure !
let contents = fs.readFileSync(appBuildGradlePath, 'utf-8');

if (contents.includes('manifestPlaceholders')) {
throw new Error(
'app/build.gradle already contains manifestPlaceholders, cannot update automatically !'
);
}

// let's add the manifestPlaceholders section !
contents = removeContents({
src: contents,
tag: 'react-native-app-auth',
}).contents;
contents = codeModAndroid.appendContentsInsideDeclarationBlock(
contents,
'defaultConfig',
`
${createGeneratedHeaderComment(contents, 'react-native-app-auth', '//')}
manifestPlaceholders = [
'appAuthRedirectScheme': '${authScheme}',
]
// @generated end react-native-app-auth
`
);

// and finally we write the file back to the disk
fs.writeFileSync(appBuildGradlePath, contents, 'utf-8');

return config;
},
]);

module.exports = { withAppAuthAppBuildGradle };
5 changes: 5 additions & 0 deletions packages/react-native-app-auth/plugin/android/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { withAppAuthAppBuildGradle } = require('./app-build-gradle');

module.exports = {
withAppAuthAppBuildGradle,
};
17 changes: 17 additions & 0 deletions packages/react-native-app-auth/plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { withPlugins, createRunOncePlugin } = require('@expo/config-plugins');
const { withAppAuthAppDelegate, withAppAuthAppDelegateHeader } = require('./ios');
const { withAppAuthAppBuildGradle } = require('./android');

const withAppAuth = config => {
return withPlugins(config, [
// iOS
withAppAuthAppDelegate,
withAppAuthAppDelegateHeader, // 👈 ️this one uses withDangerousMod !

// Android
withAppAuthAppBuildGradle, // 👈 ️this one uses withDangerousMod !
]);
};
Comment on lines +5 to +14

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const withAppAuth = config => {
return withPlugins(config, [
// iOS
withAppAuthAppDelegate,
withAppAuthAppDelegateHeader, // 👈 ️this one uses withDangerousMod !
// Android
withAppAuthAppBuildGradle, // 👈 ️this one uses withDangerousMod !
]);
};
const withAppAuth = (config, options) => {
return withPlugins(config, [
// iOS
withAppAuthAppDelegate,
withAppAuthAppDelegateHeader, // 👈 ️this one uses withDangerousMod !
// Android
config => withAppAuthAppBuildGradle(config, options), // 👈 ️this one uses withDangerousMod !
]);
};

I think this is missing, otherwise 'options?.authScheme' will always be undefined.

This works great on both iOS and Android.
Thanks a lot for your work!


const packageJson = require('../package.json');
module.exports = createRunOncePlugin(withAppAuth, packageJson.name, packageJson.version);
60 changes: 60 additions & 0 deletions packages/react-native-app-auth/plugin/ios/app-delegate-header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const { IOSConfig, withDangerousMod } = require('@expo/config-plugins');
const codeModIOs = require('@expo/config-plugins/build/ios/codeMod');
const {
createGeneratedHeaderComment,
removeContents,
} = require('@expo/config-plugins/build/utils/generateCode');
const fs = require('fs');
const { insertProtocolDeclaration } = require('./utils/insert-protocol-declaration');

const withAppAuthAppDelegateHeader = rootConfig =>
withDangerousMod(rootConfig, [
'ios',
config => {
// find the AppDelegate.h file in the project
const headerFilePath = IOSConfig.Paths.getAppDelegateObjcHeaderFilePath(
config.modRequest.projectRoot
);

// BEWARE: we update the AppDelegate.h file *outside* of the standard Expo config procedure !
let contents = fs.readFileSync(headerFilePath, 'utf-8');

// add a new import (unless it already exists)
contents = codeModIOs.addObjcImports(contents, [
'"RNAppAuthAuthorizationFlowManager.h"',
"<React/RCTLinkingManager.h>", // in reverse order because of the way the code-mod works
]);

// adds a new protocol to the AppDelegate interface (unless it already exists)
contents = insertProtocolDeclaration({
source: contents,
interfaceName: 'AppDelegate',
protocolName: 'RNAppAuthAuthorizationFlowManager',
baseClassName: 'EXAppDelegateWrapper',
});

// add a new property to the AppDelegate interface (unless it already exists)
contents = removeContents({
src: contents,
tag: 'react-native-app-auth',
}).contents;
contents = codeModIOs.insertContentsInsideObjcInterfaceBlock(
contents,
'@interface AppDelegate',
`
${createGeneratedHeaderComment(contents, 'react-native-app-auth', '//')}
@property(nonatomic, weak) id<RNAppAuthAuthorizationFlowManagerDelegate> authorizationFlowManagerDelegate;
// @generated end react-native-app-auth`,
{ position: 'head' }
);

// and finally we write the file back to the disk
fs.writeFileSync(headerFilePath, contents, 'utf-8');

return config;
},
]);

module.exports = {
withAppAuthAppDelegateHeader,
};
55 changes: 55 additions & 0 deletions packages/react-native-app-auth/plugin/ios/app-delegate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { withAppDelegate } = require('@expo/config-plugins');
const codeModIOs = require('@expo/config-plugins/build/ios/codeMod');
const {
createGeneratedHeaderComment,
removeContents,
} = require('@expo/config-plugins/build/utils/generateCode');

const withAppAuthAppDelegate = (rootConfig) =>
withAppDelegate(rootConfig, (config) => {
let { contents } = config.modResults;

// generation tags & headers
const tag1 = 'react-native-app-auth custom scheme';
const tag2 = 'react-native-app-auth deep linking';
const header1 = createGeneratedHeaderComment(contents, tag1, '//');
const header2 = createGeneratedHeaderComment(contents, tag2, '//');

// insert the code that handles the custom scheme redirections
contents = removeContents({ src: contents, tag: tag1 }).contents;
contents = codeModIOs.insertContentsInsideObjcFunctionBlock(
contents,
'application:openURL:options:',
` ${header1}
if ([self.authorizationFlowManagerDelegate resumeExternalUserAgentFlowWithURL:url]) {
return YES;
}
// @generated end ${tag1}`,
{ position: 'head' }
);

// insert the code that handles the deep linking continuation
contents = removeContents({ src: contents, tag: tag2 }).contents;
contents = codeModIOs.insertContentsInsideObjcFunctionBlock(
contents,
'application:continueUserActivity:restorationHandler:',
` ${header2}
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
if (self.authorizationFlowManagerDelegate) {
BOOL resumableAuth = [self.authorizationFlowManagerDelegate resumeExternalUserAgentFlowWithURL:userActivity.webpageURL];
if (resumableAuth) {
return YES;
}
}
}
// @generated end ${tag2}`,
{ position: 'head' }
);

config.modResults.contents = contents;
return config;
});

module.exports = {
withAppAuthAppDelegate,
};
7 changes: 7 additions & 0 deletions packages/react-native-app-auth/plugin/ios/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { withAppAuthAppDelegateHeader } = require('./app-delegate-header');
const { withAppAuthAppDelegate } = require('./app-delegate');

module.exports = {
withAppAuthAppDelegate,
withAppAuthAppDelegateHeader,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Inserts a protocol into an Objective-C class interface declaration.
* @param {string} source source code of the file
* @param {string} interfaceName Name of the interface to insert the protocol into (ex: AppDelegate)
* @param {string} protocolName Name of the protocol to add to the list of protocols (ex: RNAppAuthAuthorizationFlowManagerDelegate)
* @param {string|undefined} baseClassName Base class name of the interface (ex: NSObject)
* @returns {string} the patched source code
*/
const insertProtocolDeclaration = ({
source,
interfaceName,
protocolName,
baseClassName = 'NSObject',
}) => {
const matchInterfaceDeclarationRegexp = new RegExp(
`(@interface\\s+${interfaceName}\\s*:\\s*${baseClassName})(\\s*\\<(.*)\\>)?`
);
const match = source.match(matchInterfaceDeclarationRegexp);
if (match) {
const [line, interfaceDeclaration, , existingProtocols] = match;
if (!existingProtocols || !existingProtocols.includes(protocolName)) {
source = source.replace(
line,
`${interfaceDeclaration} <${
existingProtocols ? `${existingProtocols},` : ''
}${protocolName}>`
);
}
}
return source;
};

module.exports = {
insertProtocolDeclaration,
};
Loading