Skip to content

Commit 9cb84cb

Browse files
soulfreshchingor13
andauthored
feat: added expo strategy and updater (#1646)
* feat: added expo strategy and updater Added `strategies/expo` and `updaters/expo/app-json` for versioning of Expo based React Native projects. This updater performs the same functions as the `node` updater but also updates the `app.json` file found in Expo projects. * chore: added expo to the strategy list in the docs * chore: updated copyright year on all expo strategy/updater files * chore: added expo to src/factory Added `expo` as a type available on the global factory. Also updated the Expo SDK version number passed between the Expo strategy and updater to use a `Version` object instead of a string. * chore: fixed expo strategy expo sdk version test * chore: fixed broken CLI snapshot tests * fix: treat expo ios and android build numbers as optional If a project has not specified the `expo.ios.buildNumber` or `expo.android.versionCode`, don't attempt to set those. Co-authored-by: Jeff Ching <chingor@google.com>
1 parent a652b99 commit 9cb84cb

12 files changed

Lines changed: 521 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Release Please automates releases for the following flavors of repositories:
141141
| `krm-blueprint` | [A kpt package, with 1 or more KRM files and a CHANGELOG.md](https://github.com/GoogleCloudPlatform/blueprints/tree/main/catalog/project) |
142142
| `maven` | [Strategy for Maven projects, generates SNAPSHOT version after each release and updates `pom.xml` automatically](docs/java.md) |
143143
| `node` | [A Node.js repository, with a package.json and CHANGELOG.md](https://github.com/yargs/yargs) |
144+
| `expo` | [An Expo based React Native repository, with a package.json, app.json and CHANGELOG.md](https://github.com/yargs/yargs) |
144145
| `ocaml` | [An OCaml repository, containing 1 or more opam or esy files and a CHANGELOG.md](https://github.com/grain-lang/binaryen.ml) |
145146
| `php` | A repository with a composer.json and a CHANGELOG.md |
146147
| `python` | [A Python repository, with a setup.py, setup.cfg, CHANGELOG.md](https://github.com/googleapis/python-storage) and optionally a pyproject.toml and a &lt;project&gt;/\_\_init\_\_.py |

__snapshots__/app-json.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
exports['AppJson updateContent updates the app versions 1'] = `
2+
{
3+
"expo": {
4+
"owner": "some-owner",
5+
"name": "Some Name",
6+
"slug": "some-slug",
7+
"version": "3.2.1",
8+
"orientation": "portrait",
9+
"icon": "./assets/icon-inverse.png",
10+
"scheme": "someschema",
11+
"splash": {
12+
"image": "./assets/splash.png",
13+
"resizeMode": "cover",
14+
"backgroundColor": "#FFFFFF"
15+
},
16+
"updates": {
17+
"fallbackToCacheTimeout": 0,
18+
"url": "some-url-here"
19+
},
20+
"assetBundlePatterns": [
21+
"**/*"
22+
],
23+
"ios": {
24+
"bundleIdentifier": "com.somedomain",
25+
"buildNumber": "3.2.1",
26+
"supportsTablet": true,
27+
"config": {
28+
"usesNonExemptEncryption": false
29+
}
30+
},
31+
"android": {
32+
"package": "com.somedomain",
33+
"versionCode": "440030201",
34+
"adaptiveIcon": {
35+
"foregroundImage": "./assets/icon-inverse.png",
36+
"backgroundColor": "#FFFFFF"
37+
}
38+
},
39+
"web": {
40+
"favicon": "./assets/favicon.png"
41+
}
42+
}
43+
}
44+
45+
`

__snapshots__/cli.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ Options:
3737
[string]
3838
--release-type what type of repo is a release being created
3939
for?
40-
[choices: "dart", "dotnet-yoshi", "elixir", "go", "go-yoshi", "helm", "java",
41-
"java-backport", "java-bom", "java-lts", "java-yoshi", "krm-blueprint",
42-
"maven", "node", "ocaml", "php", "php-yoshi", "python", "ruby", "ruby-yoshi",
43-
"rust", "simple", "terraform-module"]
40+
[choices: "dart", "dotnet-yoshi", "elixir", "expo", "go", "go-yoshi", "helm",
41+
"java", "java-backport", "java-bom", "java-lts", "java-yoshi",
42+
"krm-blueprint", "maven", "node", "ocaml", "php", "php-yoshi", "python",
43+
"ruby", "ruby-yoshi", "rust", "simple", "terraform-module"]
4444
--config-file where can the config file be found in the
4545
project? [default: "release-please-config.json"]
4646
--manifest-file where can the manifest file be found in the
@@ -227,10 +227,10 @@ Options:
227227
[string]
228228
--release-type what type of repo is a release being created
229229
for?
230-
[choices: "dart", "dotnet-yoshi", "elixir", "go", "go-yoshi", "helm", "java",
231-
"java-backport", "java-bom", "java-lts", "java-yoshi", "krm-blueprint",
232-
"maven", "node", "ocaml", "php", "php-yoshi", "python", "ruby", "ruby-yoshi",
233-
"rust", "simple", "terraform-module"]
230+
[choices: "dart", "dotnet-yoshi", "elixir", "expo", "go", "go-yoshi", "helm",
231+
"java", "java-backport", "java-bom", "java-lts", "java-yoshi",
232+
"krm-blueprint", "maven", "node", "ocaml", "php", "php-yoshi", "python",
233+
"ruby", "ruby-yoshi", "rust", "simple", "terraform-module"]
234234
--config-file where can the config file be found in the
235235
project?
236236
[default: "release-please-config.json"]

docs/customizing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Release Please automates releases for the following flavors of repositories:
1717
| `krm-blueprint` | [A kpt package, with 1 or more KRM files and a CHANGELOG.md](https://github.com/GoogleCloudPlatform/blueprints/tree/main/catalog/project) |
1818
| `maven` | [Strategy for Maven projects, generates SNAPSHOT version after each release and updates `pom.xml` automatically](java.md) |
1919
| `node` | [A Node.js repository, with a package.json and CHANGELOG.md](https://github.com/yargs/yargs) |
20+
| `expo` | [An Expo based React Native repository, with a package.json, app.json and CHANGELOG.md](https://github.com/yargs/yargs) |
2021
| `ocaml` | [An OCaml repository, containing 1 or more opam or esy files and a CHANGELOG.md](https://github.com/grain-lang/binaryen.ml) |
2122
| `php` | A repository with a composer.json and a CHANGELOG.md |
2223
| `python` | [A Python repository, with a setup.py, setup.cfg, CHANGELOG.md](https://github.com/googleapis/python-storage) and optionally a pyproject.toml and a &lt;project&gt;/\_\_init\_\_.py |

src/factory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {Helm} from './strategies/helm';
3030
import {Elixir} from './strategies/elixir';
3131
import {Dart} from './strategies/dart';
3232
import {Node} from './strategies/node';
33+
import {Expo} from './strategies/expo';
3334
import {GitHub} from './github';
3435
import {ReleaserConfig} from './manifest';
3536
import {AlwaysBumpPatch} from './versioning-strategies/always-bump-patch';
@@ -88,6 +89,7 @@ const releasers: Record<string, ReleaseBuilder> = {
8889
}),
8990
'krm-blueprint': options => new KRMBlueprint(options),
9091
node: options => new Node(options),
92+
expo: options => new Expo(options),
9193
ocaml: options => new OCaml(options),
9294
php: options => new PHP(options),
9395
'php-yoshi': options => new PHPYoshi(options),

src/strategies/expo.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import {BuildUpdatesOptions} from './base';
16+
import {Node} from './node';
17+
import {Update} from '../update';
18+
import {AppJson} from '../updaters/expo/app-json';
19+
import {Version} from '../version';
20+
21+
/**
22+
* Strategy for building Expo based React Native projects. This strategy extends
23+
* the Node strategy to additionally update the `app.json` file of a project.
24+
*/
25+
export class Expo extends Node {
26+
protected async buildUpdates(
27+
options: BuildUpdatesOptions
28+
): Promise<Update[]> {
29+
const version = options.newVersion;
30+
const updates = await super.buildUpdates(options);
31+
const expoSDKVersion = await this.getExpoSDKVersion();
32+
33+
updates.push({
34+
path: this.addPath('app.json'),
35+
createIfMissing: false,
36+
updater: new AppJson({version, expoSDKVersion}),
37+
});
38+
39+
return updates;
40+
}
41+
42+
/**
43+
* Determine the Expo SDK version by parsing the package.json dependencies.
44+
*/
45+
async getExpoSDKVersion(): Promise<Version> {
46+
const pkgJsonContents = await this.getPkgJsonContents();
47+
const pkg = JSON.parse(pkgJsonContents.parsedContent);
48+
return Version.parse(
49+
pkg.dependencies?.expo ||
50+
pkg.devDependencies?.expo ||
51+
pkg.peerDependencies?.expo ||
52+
pkg.optionalDependencies?.expo ||
53+
'0.0.0'
54+
);
55+
}
56+
}

src/strategies/node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class Node extends BaseStrategy {
8484
return component.match(/^@[\w-]+\//) ? component.split('/')[1] : component;
8585
}
8686

87-
private async getPkgJsonContents(): Promise<GitHubFileContents> {
87+
protected async getPkgJsonContents(): Promise<GitHubFileContents> {
8888
if (!this.pkgJsonContents) {
8989
try {
9090
this.pkgJsonContents = await this.github.getFileContentsOnBranch(

src/updaters/expo/app-json.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import {jsonStringify} from '../../util/json-stringify';
16+
import {logger as defaultLogger, Logger} from '../../util/logger';
17+
import {DefaultUpdater, UpdateOptions} from '../default';
18+
import {Version} from '../../version';
19+
20+
export interface AppJson {
21+
expo: {
22+
version: string;
23+
ios?: {
24+
buildNumber?: string;
25+
};
26+
android?: {
27+
versionCode?: string;
28+
};
29+
};
30+
}
31+
32+
export interface AppJsonOptions extends UpdateOptions {
33+
expoSDKVersion: Version;
34+
}
35+
36+
/**
37+
* This updates a React Natve Expo project app.json file's main, ios and android
38+
* versions. All values except the `android.versionCode` are standard semver
39+
* version numbers. For the `android.versionCode`, the semver number is used as
40+
* the basis for the `versionCode`.
41+
*/
42+
export class AppJson extends DefaultUpdater {
43+
expoSDKVersion: Version;
44+
constructor(options: AppJsonOptions) {
45+
super(options);
46+
this.expoSDKVersion = options.expoSDKVersion;
47+
}
48+
/**
49+
* Given initial file contents, return updated contents.
50+
*/
51+
updateContent(content: string, logger: Logger = defaultLogger): string {
52+
const parsed = JSON.parse(content) as AppJson;
53+
54+
logger.info(
55+
`updating Expo version from ${parsed.expo.version} to ${this.version}`
56+
);
57+
parsed.expo.version = this.version.toString();
58+
59+
if (parsed.expo.ios?.buildNumber) {
60+
logger.info(
61+
`updating iOS version from ${parsed.expo.ios.buildNumber} to ${this.version}`
62+
);
63+
parsed.expo.ios.buildNumber = this.version.toString();
64+
}
65+
66+
if (parsed.expo.android?.versionCode) {
67+
// Android versionCode
68+
// https://developer.android.com/studio/publish/versioning#appversioning
69+
let expoMajorVersion = 0;
70+
try {
71+
expoMajorVersion = this.expoSDKVersion.major;
72+
} catch (e) {
73+
// Rethrow with a nice error message.
74+
throw new Error(
75+
'Unable to determine the Expo SDK version for this project. Make sure that the expo package is installed for your project.'
76+
);
77+
}
78+
79+
// Implements the `versionCode` strategy described by Maxi Rosson
80+
// @see https://medium.com/@maxirosson/versioning-android-apps-d6ec171cfd82
81+
const versionCode =
82+
expoMajorVersion * 10000000 +
83+
this.version.major * 10000 +
84+
this.version.minor * 100 +
85+
this.version.patch;
86+
logger.info(
87+
`updating Android version from ${parsed.expo.android.versionCode} to ${versionCode}`
88+
);
89+
parsed.expo.android.versionCode = versionCode.toString();
90+
}
91+
92+
return jsonStringify(parsed, content);
93+
}
94+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "node-test-repo",
3+
"version": "0.123.4",
4+
"repository": {
5+
"url": "git@github.com:samples/node-test-repo.git"
6+
},
7+
"dependencies": {
8+
"expo": "44.0.0"
9+
}
10+
}

0 commit comments

Comments
 (0)