Skip to content

Commit

Permalink
Merge pull request #52 from OneSignal/beta-10
Browse files Browse the repository at this point in the history
Add ability to configure version numbers, dedupe artifacts from `expo prebuild` - Beta 10 Release
  • Loading branch information
rgomezp authored Jan 27, 2022
2 parents 9a78934 + b6c0519 commit d8e9912
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 61 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog
All notable changes to this project will be documented in this file.

## January 2022

### `1.0.0-beta10` - 01/24/2022
#### Added
- ability to configure version numbers used in the NSE target's plist file.
- `OneSignalLog` class for better console logging.
- this changelog file.

#### Fixed
- re-running `expo prebuild` will no longer result in duplicate entitlements and dependencies added to native files.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 align="center">Welcome to onesignal-expo-plugin 👋</h1>
<p>
<img alt="Version" src="https://img.shields.io/badge/version-1.0.0--beta9-blue.svg?cacheSeconds=2592000" />
<img alt="Version" src="https://img.shields.io/badge/version-1.0.0--beta10-blue.svg?cacheSeconds=2592000" />
<a href="https://github.com/OneSignal/onesignal-expo-plugin#readme" target="_blank">
<img alt="Documentation" src="https://img.shields.io/badge/documentation-yes-brightgreen.svg" />
</a>
Expand Down Expand Up @@ -105,6 +105,16 @@ Alternatively, pass the app ID directly to the function:
OneSignal.setAppId("YOUR-ONESIGNAL-APP-ID");
```

### Versioning
In your configuration file, make sure you set:

| Property | Details |
|-------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `version` | Your app version. Corresponds to `CFBundleShortVersionString` on iOS. This value will be used in your NSE* target's plist file. |
| `ios.buildNumber` | Build number for your iOS standalone app. Corresponds to `CFBundleVersion` and must match Apple's specified format. This value will be used in your NSE* target's plist file. |

\* NSE = Notification Service Extension. Learn more about the NSE [here](https://documentation.onesignal.com/docs/service-extensions).

## Run
```sh
$ expo prebuild --clean
Expand Down
2 changes: 1 addition & 1 deletion examples/RNOneSignalExpoExample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"expo-splash-screen": "~0.11.2",
"expo-status-bar": "~1.0.4",
"expo-updates": "~0.8.1",
"onesignal-expo-plugin": "^1.0.0-beta7",
"onesignal-expo-plugin": "~1.0.0-beta10",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "~0.64.1",
Expand Down
30 changes: 12 additions & 18 deletions onesignal/withOneSignalAndroid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,26 @@
*/

import { ConfigPlugin, withAppBuildGradle } from '@expo/config-plugins';
import { ONESIGNAL_GRADLE } from '../support/androidConstants';
import { OneSignalLog } from '../support/OneSignalLog';
import { OneSignalPluginProps } from './withOneSignal';

// ---------- ---------- ---------- ----------
const oneSignalGradle = `
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.10, 0.99.99]'
}
}
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'`;

// ---------- ---------- ---------- ----------
const withGradleBuildConfig: ConfigPlugin<OneSignalPluginProps> = (config) => {
return withAppBuildGradle(config, (newConfig) => {
newConfig.modResults.contents = `${oneSignalGradle.trimStart()}\n\n${
newConfig.modResults.contents
}`;
let { contents } = newConfig.modResults;

// make sure we haven't previously added dependencies
if (!contents.includes(ONESIGNAL_GRADLE)) {
contents = `${ONESIGNAL_GRADLE}\n${contents}`;
} else {
OneSignalLog.log("OneSignal dependencies already added to build.gradle. Skipping...");
}

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

// ---------- ---------- ---------- ----------
export const withOneSignalAndroid: ConfigPlugin<OneSignalPluginProps> = (
config,
props,
Expand Down
50 changes: 31 additions & 19 deletions onesignal/withOneSignalIos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@ import {
withXcodeProject,
} from "@expo/config-plugins";
import { OneSignalPluginProps } from "./withOneSignal";
import fs from 'fs';
import * as fs from 'fs';
import xcode from 'xcode';
import { IPHONEOS_DEPLOYMENT_TARGET, TARGETED_DEVICE_FAMILY } from "../support/iosConstants";
import {
DEFAULT_BUNDLE_SHORT_VERSION,
DEFAULT_BUNDLE_VERSION,
IPHONEOS_DEPLOYMENT_TARGET,
TARGETED_DEVICE_FAMILY
} from "../support/iosConstants";
import { updatePodfile } from "../support/updatePodfile";
import { updateNSEEntitlements } from "../support/updateNSEEntitlements";
import NseUpdaterManager from "../support/NseUpdaterManager";
import { OneSignalLog } from "../support/OneSignalLog";

/* I N T E R F A C E S */
interface PluginOptions {
iosPath: string,
devTeam?: string,
bundleIdentifier?: string,
iPhoneDeploymentTarget?: string
iosPath: string,
devTeam?: string,
bundleVersion?: string,
bundleShortVersion?: string,
bundleIdentifier?: string,
iPhoneDeploymentTarget?: string
}

// ---------- ---------- ---------- ----------

/**
* Add 'app-environment' record with current environment to '<project-name>.entitlements' file
* @see https://documentation.onesignal.com/docs/react-native-sdk-setup#step-4-install-for-ios-using-cocoapods-for-ios-apps
Expand Down Expand Up @@ -77,13 +83,16 @@ const withAppGroupPermissions: ConfigPlugin<OneSignalPluginProps> = (
config
) => {
const APP_GROUP_KEY = "com.apple.security.application-groups";
return withEntitlementsPlist(config, (newConfig) => {
return withEntitlementsPlist(config, newConfig => {
if (!Array.isArray(newConfig.modResults[APP_GROUP_KEY])) {
newConfig.modResults[APP_GROUP_KEY] = [];
}
(newConfig.modResults[APP_GROUP_KEY] as Array<any>).push(
`group.${newConfig?.ios?.bundleIdentifier || ""}.onesignal`
);
let modResultsArray = (newConfig.modResults[APP_GROUP_KEY] as Array<any>);
let entitlement = `group.${newConfig?.ios?.bundleIdentifier || ""}.onesignal`;
if (modResultsArray.indexOf(entitlement) !== -1) {
return newConfig;
}
modResultsArray.push(entitlement);

return newConfig;
});
Expand All @@ -95,6 +104,8 @@ const withOneSignalNSE: ConfigPlugin<OneSignalPluginProps> = (config, onesignalP
iosPath: props.modRequest.platformProjectRoot,
bundleIdentifier: props.ios?.bundleIdentifier,
devTeam: onesignalProps?.devTeam,
bundleVersion: props.ios?.buildNumber,
bundleShortVersion: props?.version,
iPhoneDeploymentTarget: onesignalProps?.iPhoneDeploymentTarget
};

Expand All @@ -108,7 +119,6 @@ const withOneSignalNSE: ConfigPlugin<OneSignalPluginProps> = (config, onesignalP
});
}

// ---------- ---------- ---------- ----------
export const withOneSignalIos: ConfigPlugin<OneSignalPluginProps> = (
config,
props
Expand All @@ -126,10 +136,12 @@ export function xcodeProjectAddNse(
options: PluginOptions,
sourceDir: string
): void {
const { iosPath, devTeam, bundleIdentifier, iPhoneDeploymentTarget } = options;
const { iosPath, devTeam, bundleIdentifier, bundleVersion, bundleShortVersion, iPhoneDeploymentTarget } = options;

updatePodfile(iosPath);
updateNSEEntitlements(`group.${bundleIdentifier}.onesignal`)
NseUpdaterManager.updateNSEEntitlements(`group.${bundleIdentifier}.onesignal`)
NseUpdaterManager.updateNSEBundleVersion(bundleVersion ?? DEFAULT_BUNDLE_VERSION);
NseUpdaterManager.updateNSEBundleShortVersion(bundleShortVersion ?? DEFAULT_BUNDLE_SHORT_VERSION);

const projPath = `${iosPath}/${appName}.xcodeproj/project.pbxproj`;
const targetName = "OneSignalNotificationServiceExtension";
Expand All @@ -145,7 +157,7 @@ export function xcodeProjectAddNse(

xcodeProject.parse(function(err: Error) {
if (err) {
console.log(`Error parsing iOS project: ${JSON.stringify(err)}`);
OneSignalLog.log(`Error parsing iOS project: ${JSON.stringify(err)}`);
return;
}

Expand All @@ -159,7 +171,7 @@ export function xcodeProjectAddNse(
fs.createWriteStream(targetFile)
);
} catch (err) {
console.log(err);
OneSignalLog.log(err as string);
}
});

Expand All @@ -185,7 +197,7 @@ export function xcodeProjectAddNse(
projObjects['PBXContainerItemProxy'] = projObjects['PBXTargetDependency'] || {};

if (!!xcodeProject.pbxTargetByName(targetName)) {
console.log(targetName, "already exists in project. Skipping...");
OneSignalLog.log(`${targetName} already exists in project. Skipping...`);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "onesignal-expo-plugin",
"version": "1.0.0-beta9",
"version": "1.0.0-beta10",
"description": "The OneSignal Expo plugin allows you to use OneSignal without leaving the managed workflow. Developed in collaboration with SweetGreen.",
"main": "./app.plugin.js",
"scripts": {
Expand Down
42 changes: 42 additions & 0 deletions support/NseUpdaterManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as fs from 'fs';
import { BUNDLE_SHORT_VERSION_TEMPLATE_REGEX, BUNDLE_VERSION_TEMPLATE_REGEX, GROUP_IDENTIFIER_TEMPLATE_REGEX } from './iosConstants';
import { OneSignalLog } from './OneSignalLog';
import { ReaderManager } from './ReaderManager';

const entitlementsFilePath =`${__dirname}/serviceExtensionFiles/OneSignalNotificationServiceExtension.entitlements`;
const plistFilePath = `${__dirname}/serviceExtensionFiles/OneSignalNotificationServiceExtension-Info.plist`;

const logIfError = (err: NodeJS.ErrnoException | null) => {
if (err) {
OneSignalLog.error("Error updating OneSignal NSE Entitlement File.");
}
}

export default class NseUpdaterManager {
static async updateNSEEntitlements(groupIdentifier: string) {
let entitlementsFile = await ReaderManager.readFile(entitlementsFilePath);
entitlementsFile = entitlementsFile.replace(GROUP_IDENTIFIER_TEMPLATE_REGEX, groupIdentifier);

fs.writeFile(entitlementsFilePath, entitlementsFile, 'utf-8', (err) => {
logIfError(err);
})
}

static async updateNSEBundleVersion(version: string) {
let plistFile = await ReaderManager.readFile(plistFilePath);
plistFile = plistFile.replace(BUNDLE_VERSION_TEMPLATE_REGEX, version);

fs.writeFile(plistFilePath, plistFile, 'utf-8', err => {
logIfError(err);
});
}

static async updateNSEBundleShortVersion(version: string) {
let plistFile = await ReaderManager.readFile(plistFilePath);
plistFile = plistFile.replace(BUNDLE_SHORT_VERSION_TEMPLATE_REGEX, version);

fs.writeFile(plistFilePath, plistFile, 'utf-8', err => {
logIfError(err);
});
}
}
9 changes: 9 additions & 0 deletions support/OneSignalLog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class OneSignalLog {
static log(str: string) {
console.log(`\tonesignal-expo-plugin: ${str}`)
}

static error(str: string) {
console.error(`\tonesignal-expo-plugin: ${str}`)
}
}
3 changes: 2 additions & 1 deletion support/ReaderManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs';
import { OneSignalLog } from './OneSignalLog';

export class ReaderManager {
static async readFile(path: string, replace?: string): Promise<string> {
Expand All @@ -8,7 +9,7 @@ export class ReaderManager {
reject(err);
}
if (!data) {
console.error("Couldn't read file:", path);
OneSignalLog.error("Couldn't read file:" + path);
return;
}
resolve(data);
Expand Down
11 changes: 11 additions & 0 deletions support/androidConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const ONESIGNAL_GRADLE =
`buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.10, 0.99.99]'
}
}
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'`;
5 changes: 5 additions & 0 deletions support/iosConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ end`;
export const NSE_PODFILE_REGEX = /target 'OneSignalNotificationServiceExtension'/;

export const GROUP_IDENTIFIER_TEMPLATE_REGEX = /{{GROUP_IDENTIFIER}}/gm;
export const BUNDLE_SHORT_VERSION_TEMPLATE_REGEX = /{{BUNDLE_SHORT_VERSION}}/gm;
export const BUNDLE_VERSION_TEMPLATE_REGEX = /{{BUNDLE_VERSION}}/gm;

export const DEFAULT_BUNDLE_VERSION = '1';
export const DEFAULT_BUNDLE_SHORT_VERSION = '1.0';
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>{{BUNDLE_SHORT_VERSION}}</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>{{BUNDLE_VERSION}}</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
Expand Down
16 changes: 0 additions & 16 deletions support/updateNSEEntitlements.ts

This file was deleted.

5 changes: 3 additions & 2 deletions support/updatePodfile.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import fs from 'fs';
import { NSE_PODFILE_REGEX, NSE_PODFILE_SNIPPET } from './iosConstants';
import { OneSignalLog } from './OneSignalLog';
import { ReaderManager } from './ReaderManager';

export async function updatePodfile(iosPath: string) {
const podfile = await ReaderManager.readFile(`${iosPath}/Podfile`);
const matches = podfile.match(NSE_PODFILE_REGEX);

if (matches) {
console.info("OneSignalNotificationServiceExtension target already added to Podfile. Skipping...");
OneSignalLog.log("OneSignalNotificationServiceExtension target already added to Podfile. Skipping...");
} else {
fs.appendFile(`${iosPath}/Podfile`, NSE_PODFILE_SNIPPET, (err) => {
if (err) {
console.error("Error writing to Podfile");
OneSignalLog.error("Error writing to Podfile");
}
})
}
Expand Down

0 comments on commit d8e9912

Please sign in to comment.