Skip to content

Commit

Permalink
Hermes: Add scripts to package, remove shelljs
Browse files Browse the repository at this point in the history
Summary:
The new Hermes scripts need to be included in the `react-native` npm.

The `shelljs` dependency that was used by the Hermes scripts is a dev dependency, so instead of adding to the `react-native` npm size, we refactored its use out of hermes-utils.js.

Changelog:
[General][Added] - Add Hermes scripts to package

Reviewed By: cortinico

Differential Revision: D36387135

fbshipit-source-id: 12d0bc29d365c4cb18d33a0d390e6e7d34864b7a
  • Loading branch information
hramos authored and facebook-github-bot committed May 24, 2022
1 parent 55133ea commit 004b860
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 42 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
"scripts/codegen/codegen-utils.js",
"scripts/codegen/generate-artifacts-executor.js",
"scripts/codegen/generate-specs-cli-executor.js",
"scripts/hermes/hermes-utils.js",
"scripts/hermes/prepare-hermes-for-build.js",
"scripts/ios-configure-glog.sh",
"scripts/xcode/with-environment.sh",
"scripts/launchPackager.bat",
Expand Down
20 changes: 12 additions & 8 deletions scripts/__tests__/hermes-utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ const MemoryFs = require('metro-memory-fs');

let execCalls;
let fs;
let shelljs;

jest.mock('shelljs', () => ({
echo: jest.fn(),
exec: jest.fn(command => {
jest.mock('child_process', () => ({
execSync: jest.fn(command => {
if (command.startsWith('curl')) {
fs.writeFileSync(
path.join(SDKS_DIR, 'download', `hermes-${hermesTagSha}.tgz`),
Expand All @@ -58,7 +56,6 @@ jest.mock('shelljs', () => ({
return {code: 0};
}
}),
exit: jest.fn(),
}));

function populateMockFilesystem() {
Expand Down Expand Up @@ -118,12 +115,17 @@ describe('hermes-utils', () => {
populateMockFilesystem();

execCalls = Object.create(null);
shelljs = require('shelljs');
});
describe('readHermesTag', () => {
it('should return main if .hermesversion does not exist', () => {
expect(readHermesTag()).toEqual('main');
});
it('should fail if hermes tag is empty', () => {
fs.writeFileSync(path.join(SDKS_DIR, '.hermesversion'), '');
expect(() => {
readHermesTag();
}).toThrow('[Hermes] .hermesversion file is empty.');
});
it('should return tag from .hermesversion if file exists', () => {
fs.writeFileSync(path.join(SDKS_DIR, '.hermesversion'), hermesTag);
expect(readHermesTag()).toEqual(hermesTag);
Expand Down Expand Up @@ -152,6 +154,7 @@ describe('hermes-utils', () => {
});
describe('downloadHermesTarball', () => {
it('should download Hermes tarball to download dir', () => {
fs.writeFileSync(path.join(SDKS_DIR, '.hermesversion'), hermesTag);
downloadHermesTarball();
expect(execCalls.curl).toBeTruthy();
expect(
Expand Down Expand Up @@ -188,9 +191,10 @@ describe('hermes-utils', () => {
expect(fs.existsSync(path.join(SDKS_DIR, 'hermes'))).toBeTruthy();
});
it('should fail if Hermes tarball does not exist', () => {
expandHermesTarball();
expect(() => {
expandHermesTarball();
}).toThrow('[Hermes] Failed to expand Hermes tarball.');
expect(execCalls.tar).toBeUndefined();
expect(shelljs.exit.mock.calls.length).toBeGreaterThan(0);
});
});
describe('copyBuildScripts', () => {
Expand Down
76 changes: 42 additions & 34 deletions scripts/hermes/hermes-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

const fs = require('fs');
const path = require('path');
const {echo, exec, exit} = require('shelljs');
const {execSync} = require('child_process');

const SDKS_DIR = path.normalize(path.join(__dirname, '..', '..', 'sdks'));
const HERMES_DIR = path.join(SDKS_DIR, 'hermes');
Expand Down Expand Up @@ -41,14 +41,21 @@ function prepareFileSystem() {

function readHermesTag() {
if (fs.existsSync(HERMES_TAG_FILE_PATH)) {
const data = fs.readFileSync(HERMES_TAG_FILE_PATH, {
encoding: 'utf8',
flag: 'r',
});
return data.trim();
} else {
return 'main';
const data = fs
.readFileSync(HERMES_TAG_FILE_PATH, {
encoding: 'utf8',
flag: 'r',
})
.trim();

if (data.length > 0) {
return data;
} else {
throw new Error('[Hermes] .hermesversion file is empty.');
}
}

return 'main';
}

function setHermesTag(hermesTag) {
Expand All @@ -64,10 +71,11 @@ function setHermesTag(hermesTag) {
}

function getHermesTagSHA(hermesTag) {
return exec(
return execSync(
`git ls-remote https://github.com/facebook/hermes ${hermesTag} | cut -f 1`,
{silent: true},
).trim();
)
.toString()
.trim();
}

function getHermesTarballDownloadPath(hermesTag) {
Expand All @@ -87,11 +95,13 @@ function downloadHermesTarball() {
return;
}

echo(`[Hermes] Downloading Hermes source code for commit ${hermesTagSHA}`);
if (exec(`curl ${hermesTarballUrl} -Lo ${hermesTarballDownloadPath}`).code) {
echo('[Hermes] Failed to download Hermes tarball.');
exit(1);
return;
console.info(
`[Hermes] Downloading Hermes source code for commit ${hermesTagSHA}`,
);
try {
execSync(`curl ${hermesTarballUrl} -Lo ${hermesTarballDownloadPath}`);
} catch (error) {
throw new Error(`[Hermes] Failed to download Hermes tarball. ${error}`);
}
}

Expand All @@ -103,32 +113,24 @@ function expandHermesTarball() {
prepareFileSystem();

if (!fs.existsSync(hermesTarballDownloadPath)) {
echo(
`[Hermes] Failed to expand Hermes tarball, no file found at ${hermesTarballDownloadPath}.`,
);
exit(1);
return;
throw new Error(`[Hermes] Failed to expand Hermes tarball.`);
}

echo(`[Hermes] Expanding Hermes tarball for commit ${hermesTagSHA}`);
if (
exec(
console.info(`[Hermes] Expanding Hermes tarball for commit ${hermesTagSHA}`);
try {
execSync(
`tar -zxf ${hermesTarballDownloadPath} --strip-components=1 --directory ${HERMES_DIR}`,
).code
) {
echo('[Hermes] Failed to expand Hermes tarball.');
exit(1);
return;
);
} catch (error) {
throw new Error('[Hermes] Failed to expand Hermes tarball.');
}
}

function copyBuildScripts() {
if (!fs.existsSync(HERMES_DIR)) {
echo(
throw new Error(
'[Hermes] Failed to copy Hermes build scripts, no Hermes source directory found.',
);
exit(1);
return;
}

fs.copyFileSync(
Expand Down Expand Up @@ -163,8 +165,14 @@ set_target_properties(native-hermesc PROPERTIES
IMPORTED_LOCATION "${MACOS_HERMESC_PATH}"
)`;

fs.mkdirSync(MACOS_BIN_DIR, {recursive: true});
fs.writeFileSync(MACOS_IMPORT_HERMESC_PATH, IMPORT_HERMESC_TEMPLATE);
try {
fs.mkdirSync(MACOS_BIN_DIR, {recursive: true});
fs.writeFileSync(MACOS_IMPORT_HERMESC_PATH, IMPORT_HERMESC_TEMPLATE);
} catch (error) {
console.warn(
`[Hermes] Re-compiling hermesc. Unable to configure make: ${error}`,
);
}
}

module.exports = {
Expand Down

0 comments on commit 004b860

Please sign in to comment.