Skip to content

Commit

Permalink
feat(code): add plugin-google-signin impl
Browse files Browse the repository at this point in the history
  • Loading branch information
crherman7 committed Mar 29, 2024
1 parent d78249f commit b3ba909
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 19 deletions.
121 changes: 119 additions & 2 deletions packages/plugin-google-signin/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -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(`<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>blah</string>
</array>
</dict>
</array>`);

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 <RNGoogleSignin/RNGoogleSignin.h>");
});

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"
);
});
});
7 changes: 6 additions & 1 deletion packages/plugin-google-signin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
112 changes: 98 additions & 14 deletions packages/plugin-google-signin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodePluginGoogleSignin>({
/**
* 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<void>} A promise that resolves when the process completes.
*/
ios: async function (
build: BuildConfig,
options: PrebuildOptions
): Promise<void> {},
ios: async function (build, options): Promise<void> {
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 <RNGoogleSignin/RNGoogleSignin.h>
`
);
}
);

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<UIApplicationOpenURLOptionsKey,id> *)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<void>} A promise that resolves when the process completes.
*/
android: async function (
build: BuildConfig,
options: PrebuildOptions
): Promise<void> {},
android: async function (build, options): Promise<void> {
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 };
12 changes: 11 additions & 1 deletion packages/plugin-google-signin/src/types.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}>;
};
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8520,7 +8520,7 @@ meow@^6.0.0:
type-fest "^0.13.1"
yargs-parser "^18.1.3"

[email protected]:
[email protected], 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==
Expand Down

0 comments on commit b3ba909

Please sign in to comment.