Skip to content

Commit 9d0f176

Browse files
robingenzclaude
andcommitted
feat!: only a store build creates the binary, every other build writes the resource file alone
The Xcode phase runs `binary create` in an archive, which DEPLOYMENT_POSTPROCESSING marks, and the Gradle task in a build type other than debug; every other build runs `resource-file write`, which writes hotcodepush.json and creates nothing. Both commands take `--embedded-bundle-path` and `--resource-file-path` in place of `--path` and `--out`, and only `binary create` takes the version and build. The build step needs a CLI that has `resource-file write`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d5db074 commit 9d0f176

3 files changed

Lines changed: 75 additions & 36 deletions

File tree

‎android/hotcodepush.gradle‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// The build step of the Android build. The app's build.gradle applies this file with the one line `hotcodepush init`
2-
// adds; for every variant, the task below runs the CLI's `binary create` after React Native's bundling, which writes
3-
// hotcodepush.json into the variant's assets and registers the binary. A variant React Native bundles nothing for gets
2+
// adds; for every variant, the task below runs the CLI after React Native's bundling: a store build, a build type other
3+
// than debug, runs `binary create`, which writes hotcodepush.json into the variant's assets and creates the binary; any
4+
// other build runs `resource-file write`, which writes the file alone. A variant React Native bundles nothing for gets
45
// the file without an embedded bundle, and no binary.
56

67
import javax.inject.Inject
@@ -36,6 +37,9 @@ abstract class HotCodePushBinaryCreateTask extends DefaultTask {
3637
@Internal
3738
abstract DirectoryProperty getStagingDirectory()
3839

40+
@Input
41+
abstract Property<Boolean> getStoreBuild()
42+
3943
@Inject
4044
abstract ExecOperations getExecOperations()
4145

@@ -53,15 +57,15 @@ abstract class HotCodePushBinaryCreateTask extends DefaultTask {
5357
}
5458
stagingDirectory.get().asFile.mkdirs()
5559
def npx = Os.isFamily(Os.FAMILY_WINDOWS) ? ['cmd', '/c', 'npx'] : ['npx']
60+
def command = storeBuild.get()
61+
? ['binary', 'create', '--binary-version', binaryVersion.get(), '--binary-build', binaryBuild.get()]
62+
: ['resource-file', 'write']
5663
execOperations.exec {
5764
it.workingDir = projectRoot.get().asFile
58-
it.commandLine(npx + [
59-
'hotcodepush', 'binary', 'create',
65+
it.commandLine(npx + ['hotcodepush'] + command + [
6066
'--platform', 'android',
61-
'--path', stagingDirectory.get().asFile.path,
62-
'--binary-version', binaryVersion.get(),
63-
'--binary-build', binaryBuild.get(),
64-
'--out', outputDirectory.file('hotcodepush.json').get().asFile.path,
67+
'--embedded-bundle-path', stagingDirectory.get().asFile.path,
68+
'--resource-file-path', outputDirectory.file('hotcodepush.json').get().asFile.path,
6569
])
6670
}
6771
}
@@ -75,7 +79,7 @@ androidComponents.onVariants(androidComponents.selector().all()) { variant ->
7579
def reactExtension = project.extensions.findByName('react')
7680
def binaryCreateTask = tasks.register("createHotCodePushBinary${variantName}", HotCodePushBinaryCreateTask) {
7781
it.group = 'hotcodepush'
78-
it.description = "Writes hotcodepush.json into the ${variant.name} assets and registers the binary of a bundled build."
82+
it.description = "Writes hotcodepush.json into the ${variant.name} assets and creates the binary of a store build."
7983
it.binaryBuild.set(variant.outputs[0].versionCode.map { String.valueOf(it) })
8084
it.binaryVersion.set(variant.outputs[0].versionName)
8185
if (bundleTask != null) {
@@ -84,7 +88,8 @@ androidComponents.onVariants(androidComponents.selector().all()) { variant ->
8488
}
8589
it.projectRoot.set(reactExtension == null ? rootProject.layout.projectDirectory.dir('..') : reactExtension.root)
8690
it.stagingDirectory.set(layout.buildDirectory.dir("intermediates/hotcodepush/${variant.name}"))
87-
// The resource file carries the build's time as the floor and the registration is per build: the step never counts as up to date.
91+
it.storeBuild.set(variant.buildType != 'debug')
92+
// The resource file carries the build's time as the floor and a store build creates its binary on every run: the step never counts as up to date.
8893
it.outputs.upToDateWhen { false }
8994
}
9095
variant.sources.assets.addGeneratedSourceDirectory(binaryCreateTask) { it.outputDirectory }

‎scripts/binary-create-xcode.sh‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/bin/sh
22
# The build step of the Xcode build, run by the "Create HotCodePush binary" phase `hotcodepush init` adds after
3-
# "Bundle React Native code and images": the CLI's `binary create` writes hotcodepush.json into the app
4-
# and registers the binary. The phase holds one line; what it runs lives here.
3+
# "Bundle React Native code and images": the CLI writes hotcodepush.json into the app and, in a store build, creates
4+
# the binary. The phase holds one line; what it runs lives here.
55
set -e
66

77
DEST="$CONFIGURATION_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH"
88
PROJECT_ROOT="${PROJECT_ROOT:-$PROJECT_DIR/..}"
99

1010
# A Debug configuration runs the development server's JavaScript, and what React Native embeds for a device there is a
11-
# development bundle: binary create is given an empty directory instead, so it writes the resource file without an
12-
# embedded bundle and asks the API nothing, on a device and a simulator alike. `*Debug*` is React Native's match for DEV.
11+
# development bundle: the CLI is given an empty directory instead, so it writes the resource file without an embedded
12+
# bundle and asks the API nothing, on a device and a simulator alike. `*Debug*` is React Native's match for DEV.
1313
case "$CONFIGURATION" in
1414
*Debug*)
1515
EMBEDDED_ASSETS_PATH="$DERIVED_FILE_DIR/hotcodepush-no-bundle"
@@ -20,20 +20,25 @@ case "$CONFIGURATION" in
2020
;;
2121
esac
2222

23-
# The identity the device reports, from the built app's processed Info.plist: Expo's prebuild writes the version and
24-
# build into the plist as literals and leaves MARKETING_VERSION and CURRENT_PROJECT_VERSION at the template's.
25-
INFO_PLIST="$TARGET_BUILD_DIR/$INFOPLIST_PATH"
26-
BINARY_VERSION=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$INFO_PLIST")
27-
BINARY_BUILD=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$INFO_PLIST")
23+
# The store build is an archive, which Xcode marks with DEPLOYMENT_POSTPROCESSING, the setting behind "Run script only
24+
# when installing": it creates the binary under the version and build the device reports, from the built app's processed
25+
# Info.plist, since Expo's prebuild writes them into the plist as literals and leaves MARKETING_VERSION and
26+
# CURRENT_PROJECT_VERSION at the template's. A Run or a plain build writes the resource file alone.
27+
if [ "$DEPLOYMENT_POSTPROCESSING" = "YES" ]; then
28+
INFO_PLIST="$TARGET_BUILD_DIR/$INFOPLIST_PATH"
29+
BINARY_VERSION=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$INFO_PLIST")
30+
BINARY_BUILD=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$INFO_PLIST")
31+
set -- binary create --binary-version "$BINARY_VERSION" --binary-build "$BINARY_BUILD"
32+
else
33+
set -- resource-file write
34+
fi
2835

2936
# Xcode's PATH has no Node; React Native's .xcode.env names the binary, and npx sits beside it.
3037
PATH="$(dirname "$NODE_BINARY"):$PATH"
3138
export PATH
3239

3340
cd "$PROJECT_ROOT"
34-
npx hotcodepush binary create \
41+
npx hotcodepush "$@" \
3542
--platform ios \
36-
--path "$EMBEDDED_ASSETS_PATH" \
37-
--binary-version "$BINARY_VERSION" \
38-
--binary-build "$BINARY_BUILD" \
39-
--out "$DEST/hotcodepush.json"
43+
--embedded-bundle-path "$EMBEDDED_ASSETS_PATH" \
44+
--resource-file-path "$DEST/hotcodepush.json"

‎scripts/binary-create-xcode.test.ts‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,56 @@ describe.skipIf(process.platform !== 'darwin')('binary-create-xcode.sh', () => {
5858
rmSync(buildDirectoryPath, { force: true, recursive: true });
5959
});
6060

61-
it('should pass the version and build of the processed Info.plist, which the device reports', () => {
62-
expect(runScript({ CONFIGURATION: 'Release' })).toEqual([
61+
it('should run binary create when the build is an archive', () => {
62+
expect(
63+
runScript({ CONFIGURATION: 'Release', DEPLOYMENT_POSTPROCESSING: 'YES' }),
64+
).toEqual([
6365
'hotcodepush',
6466
'binary',
6567
'create',
66-
'--platform',
67-
'ios',
68-
'--path',
69-
appPath,
7068
'--binary-version',
7169
'2.4.1',
7270
'--binary-build',
7371
'57',
74-
'--out',
72+
'--platform',
73+
'ios',
74+
'--embedded-bundle-path',
75+
appPath,
76+
'--resource-file-path',
77+
join(appPath, 'hotcodepush.json'),
78+
]);
79+
});
80+
81+
it('should run resource-file write when the build is not an archive', () => {
82+
expect(
83+
runScript({ CONFIGURATION: 'Release', DEPLOYMENT_POSTPROCESSING: 'NO' }),
84+
).toEqual([
85+
'hotcodepush',
86+
'resource-file',
87+
'write',
88+
'--platform',
89+
'ios',
90+
'--embedded-bundle-path',
91+
appPath,
92+
'--resource-file-path',
7593
join(appPath, 'hotcodepush.json'),
7694
]);
7795
});
7896

97+
it('should pass the version and build of the processed Info.plist, which the device reports, when the build is an archive', () => {
98+
const args = runScript({
99+
CONFIGURATION: 'Release',
100+
DEPLOYMENT_POSTPROCESSING: 'YES',
101+
});
102+
103+
expect(readOption(args, '--binary-version')).toBe('2.4.1');
104+
expect(readOption(args, '--binary-build')).toBe('57');
105+
});
106+
79107
it('should pass a directory without the bundle when the configuration is Debug on a device', () => {
80108
const embeddedAssetsPath = readOption(
81109
runScript({ CONFIGURATION: 'Debug', PLATFORM_NAME: 'iphoneos' }),
82-
'--path',
110+
'--embedded-bundle-path',
83111
);
84112

85113
expect(embeddedAssetsPath).not.toBe(appPath);
@@ -89,7 +117,7 @@ describe.skipIf(process.platform !== 'darwin')('binary-create-xcode.sh', () => {
89117
it('should pass a directory without the bundle when the configuration is Debug on a simulator', () => {
90118
const embeddedAssetsPath = readOption(
91119
runScript({ CONFIGURATION: 'Debug', PLATFORM_NAME: 'iphonesimulator' }),
92-
'--path',
120+
'--embedded-bundle-path',
93121
);
94122

95123
expect(embeddedAssetsPath).not.toBe(appPath);
@@ -99,20 +127,21 @@ describe.skipIf(process.platform !== 'darwin')('binary-create-xcode.sh', () => {
99127
function readOption(args: string[], flag: string): string {
100128
const value = args[args.indexOf(flag) + 1];
101129
if (value === undefined) {
102-
throw new Error(`binary create was run without ${flag}`);
130+
throw new Error(`the CLI was run without ${flag}`);
103131
}
104132
return value;
105133
}
106134

107135
/**
108-
* Runs the script with the build settings Xcode gives a phase of the app target, the template's version settings
109-
* among them, and returns the arguments it ran binary create with.
136+
* Runs the script with the build settings Xcode gives a phase of the app target in a build, not an archive, the
137+
* template's version settings among them, and returns the arguments it ran the CLI with.
110138
*/
111139
function runScript(buildSettings: Record<string, string>): string[] {
112140
execFileSync('/bin/sh', [SCRIPT_PATH], {
113141
env: {
114142
CONFIGURATION_BUILD_DIR: buildDirectoryPath,
115143
CURRENT_PROJECT_VERSION: '1',
144+
DEPLOYMENT_POSTPROCESSING: 'NO',
116145
DERIVED_FILE_DIR: join(buildDirectoryPath, 'DerivedSources'),
117146
INFOPLIST_PATH: join(APP_NAME, 'Info.plist'),
118147
MARKETING_VERSION: '1.0',

0 commit comments

Comments
 (0)