-
Notifications
You must be signed in to change notification settings - Fork 443
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
aurelien-iapp
wants to merge
4
commits into
FormidableLabs:main
Choose a base branch
from
aurelien-iapp:expo-config-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/react-native-app-auth/plugin/android/app-build-gradle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const { withAppAuthAppBuildGradle } = require('./app-build-gradle'); | ||
|
||
module.exports = { | ||
withAppAuthAppBuildGradle, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ! | ||
]); | ||
}; | ||
|
||
const packageJson = require('../package.json'); | ||
module.exports = createRunOncePlugin(withAppAuth, packageJson.name, packageJson.version); |
60 changes: 60 additions & 0 deletions
60
packages/react-native-app-auth/plugin/ios/app-delegate-header.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
35 changes: 35 additions & 0 deletions
35
packages/react-native-app-auth/plugin/ios/utils/insert-protocol-declaration.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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!