From b3ba909bb3684a467d4f7c9a546c0a757a2588f6 Mon Sep 17 00:00:00 2001 From: Chris Herman Date: Fri, 29 Mar 2024 13:59:40 -0400 Subject: [PATCH] feat(code): add plugin-google-signin impl --- .../plugin-google-signin/__tests__/index.ts | 121 +++++++++++++++++- packages/plugin-google-signin/package.json | 7 +- packages/plugin-google-signin/src/index.ts | 112 ++++++++++++++-- packages/plugin-google-signin/src/types.ts | 12 +- yarn.lock | 2 +- 5 files changed, 235 insertions(+), 19 deletions(-) diff --git a/packages/plugin-google-signin/__tests__/index.ts b/packages/plugin-google-signin/__tests__/index.ts index 4c0a393d44..8b5cd5ed0d 100644 --- a/packages/plugin-google-signin/__tests__/index.ts +++ b/packages/plugin-google-signin/__tests__/index.ts @@ -1,5 +1,122 @@ +/** + * @jest-environment-options {"requireTemplate": true} + */ + +import { BuildConfig, fs, path } from "@brandingbrand/code-cli-kit"; +import plugin, { type CodePluginGoogleSignin } from "../src"; + describe("plugin-google-signin", () => { - it("ios", async () => {}); + it("ios", async () => { + const config: BuildConfig & CodePluginGoogleSignin = { + ios: { + displayName: "App", + bundleId: "com.app", + }, + android: { + displayName: "App", + packageName: "com.app", + }, + codePluginGoogleSignin: { + plugin: { + ios: { + reversedClientId: "blah", + }, + android: {}, + }, + }, + }; + + await plugin.ios?.(config, {} as any); + + expect(await fs.readFile(path.ios.infoPlist, "utf-8")).toContain(` + + CFBundleURLSchemes + + blah + + + `); + + expect( + await fs.readFile( + path.project.resolve("ios", "app", "AppDelegate.mm"), + "utf-8" + ) + ) + .toContain(`if ([RNGoogleSignin application:application openURL:url options:options]) { + return YES; + }`); + + expect( + await fs.readFile( + path.project.resolve("ios", "app", "AppDelegate.mm"), + "utf-8" + ) + ).toContain("#import "); + }); + + it("android", async () => { + const config: BuildConfig & CodePluginGoogleSignin = { + ios: { + displayName: "App", + bundleId: "com.app", + }, + android: { + displayName: "App", + packageName: "com.app", + }, + codePluginGoogleSignin: { + plugin: { + ios: { + reversedClientId: "blah", + }, + android: {}, + }, + }, + }; + + await plugin.android?.(config, {} as any); + + expect(await fs.readFile(path.android.buildGradle, "utf-8")).toContain( + 'googlePlayServicesAuthVersion = "19.2.0"' + ); + + expect(await fs.readFile(path.android.appBuildGradle, "utf-8")).toContain( + "implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0" + ); + }); + + it("android versions", async () => { + const config: BuildConfig & CodePluginGoogleSignin = { + ios: { + displayName: "App", + bundleId: "com.app", + }, + android: { + displayName: "App", + packageName: "com.app", + }, + codePluginGoogleSignin: { + plugin: { + ios: { + reversedClientId: "blah", + }, + android: { + googlePlayServicesAuthVersion: "20.0.0", + swiperefreshlayoutVersion: "2.0.0", + }, + }, + }, + }; + + await plugin.android?.(config, {} as any); + + expect(await fs.readFile(path.android.buildGradle, "utf-8")).toContain( + 'googlePlayServicesAuthVersion = "20.0.0"' + ); - it("android", async () => {}); + expect(await fs.readFile(path.android.appBuildGradle, "utf-8")).toContain( + "implementation 'androidx.swiperefreshlayout:swiperefreshlayout:2.0.0" + ); + }); }); diff --git a/packages/plugin-google-signin/package.json b/packages/plugin-google-signin/package.json index c04301b324..88ef33b7fa 100644 --- a/packages/plugin-google-signin/package.json +++ b/packages/plugin-google-signin/package.json @@ -12,8 +12,13 @@ "preset": "@brandingbrand/code-jest-config" }, "types": "src/index.ts", + "dependencies": { + "merge-anything": "^5.1.7" + }, "peerDependencies": { - "@brandingbrand/code-cli-kit": "*" + "@brandingbrand/code-cli-kit": "*", + "@brandingbrand/code-plugin-firebase-app": "*", + "@react-native-google-signin/google-signin": "^9.0.2" }, "devDependencies": { "@brandingbrand/code-cli-kit": "*", diff --git a/packages/plugin-google-signin/src/index.ts b/packages/plugin-google-signin/src/index.ts index 1fed9b8732..719a7516f3 100644 --- a/packages/plugin-google-signin/src/index.ts +++ b/packages/plugin-google-signin/src/index.ts @@ -4,38 +4,122 @@ */ import { - type BuildConfig, - type PrebuildOptions, definePlugin, path, + string, + withInfoPlist, + withUTF8, } from "@brandingbrand/code-cli-kit"; +import { mergeAndConcat } from "merge-anything"; + +import type { CodePluginGoogleSignin } from "./types"; /** * Defines a plugin with functions for both iOS and Android platforms. * @alias module:Plugin - * @param {BuildConfig} build - The build configuration object. + * @param {BuildConfig & CodePluginGoogleSignin} build - The build configuration object. * @param {PrebuildOptions} options - The options object. */ -export default definePlugin({ +export default definePlugin({ /** * Function to be executed for iOS platform. - * @param {BuildConfig} build - The build configuration object for iOS. + * @param {BuildConfig & CodePluginGoogleSignin} build - The build configuration object for iOS. * @param {PrebuildOptions} options - The options object for iOS. * @returns {Promise} A promise that resolves when the process completes. */ - ios: async function ( - build: BuildConfig, - options: PrebuildOptions - ): Promise {}, + ios: async function (build, options): Promise { + if (build.codePluginGoogleSignin.plugin.ios.reversedClientId) { + await withInfoPlist((plist) => { + return mergeAndConcat(plist, { + CFBundleURLTypes: [ + { + CFBundleURLSchemes: [ + build.codePluginGoogleSignin.plugin.ios.reversedClientId, + ], + }, + ], + }); + }); + } + + await withUTF8( + path.project.resolve("ios", "app", "AppDelegate.mm"), + (content) => { + return string.replace( + content, + /(#import "AppDelegate.h")/, + `$1 + +#import + ` + ); + } + ); + + await withUTF8( + path.project.resolve("ios", "app", "AppDelegate.mm"), + (content) => { + if (content.match(/RCTLinkingManager/gm)) { + return string.replace( + content, + /(if \(\[RCTLinkingManager[\s\S]+?})/, + `$1 + if ([RNGoogleSignin application:application openURL:url options:options]) { + return YES; + }` + ); + } + + return string.replace( + content, + /(@end)/, + `- (BOOL)application:(UIApplication *)application +openURL:(NSURL *)url +options:(NSDictionary *)options +{ + if ([RNGoogleSignin application:application openURL:url options:options]) { + return YES; + } + + return NO; +} +$1` + ); + } + ); + }, /** * Function to be executed for Android platform. - * @param {BuildConfig & CodePluginAsset} build - The build configuration object for Android. + * @param {BuildConfig & CodePluginGoogleSignin} build - The build configuration object for Android. * @param {PrebuildOptions} options - The options object for Android. * @returns {Promise} A promise that resolves when the process completes. */ - android: async function ( - build: BuildConfig, - options: PrebuildOptions - ): Promise {}, + android: async function (build, options): Promise { + await withUTF8(path.android.buildGradle, (content) => { + return string.replace( + content, + /(ext {)/, + `$1 + googlePlayServicesAuthVersion = "${ + build.codePluginGoogleSignin.plugin.android + .googlePlayServicesAuthVersion || "19.2.0" + }"` + ); + }); + + await withUTF8(path.android.appBuildGradle, (content) => { + return string.replace( + content, + /(dependencies {)/, + `$1 + implementation 'androidx.swiperefreshlayout:swiperefreshlayout:${ + build.codePluginGoogleSignin.plugin.android.swiperefreshlayoutVersion || + "1.0.0" + }'` + ); + }); + }, }); + +export type { CodePluginGoogleSignin }; diff --git a/packages/plugin-google-signin/src/types.ts b/packages/plugin-google-signin/src/types.ts index 95243a6e16..c8aa4a3cf9 100644 --- a/packages/plugin-google-signin/src/types.ts +++ b/packages/plugin-google-signin/src/types.ts @@ -1,3 +1,13 @@ import type { Plugin } from "@brandingbrand/code-cli-kit"; -export type CodePluginGoogleSignin = {}; +export type CodePluginGoogleSignin = { + codePluginGoogleSignin: Plugin<{ + ios: { + reversedClientId: string; + }; + android: { + googlePlayServicesAuthVersion?: string; + swiperefreshlayoutVersion?: string; + }; + }>; +}; diff --git a/yarn.lock b/yarn.lock index 6f29f069f0..10095e3d5c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8520,7 +8520,7 @@ meow@^6.0.0: type-fest "^0.13.1" yargs-parser "^18.1.3" -merge-anything@5.1.7: +merge-anything@5.1.7, merge-anything@^5.1.7: version "5.1.7" resolved "https://registry.yarnpkg.com/merge-anything/-/merge-anything-5.1.7.tgz#94f364d2b0cf21ac76067b5120e429353b3525d7" integrity sha512-eRtbOb1N5iyH0tkQDAoQ4Ipsp/5qSR79Dzrz8hEPxRX10RWWR/iQXdoKmBSRCThY1Fh5EhISDtpSc93fpxUniQ==