Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a command to download the Hermes Sampling Profiler to a local machine #1246

Merged
merged 68 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
1167db0
added adb commands
jessieAnhNguyen Jul 7, 2020
2047721
feat: implemented download hermes profile command
jessieAnhNguyen Jul 21, 2020
4af2cbe
deleted unused adbkit packages
jessieAnhNguyen Jul 22, 2020
98e1ecf
changed files excluded node modules
jessieAnhNguyen Jul 22, 2020
24a248b
handled edge case when there's no file
jessieAnhNguyen Jul 22, 2020
b720baf
deleted unnecessary changes
jessieAnhNguyen Jul 22, 2020
4c0fb45
deleted unnecessary changes 2
jessieAnhNguyen Jul 22, 2020
defc7b7
Merge pull request #2 from MLH-Fellowship/jessie
jessieAnhNguyen Jul 27, 2020
b5d1d3c
added transformer files
jessieAnhNguyen Jul 28, 2020
89837ae
implemented --verbose flag
jessieAnhNguyen Jul 28, 2020
23cd80c
Merge branch 'jessie-cli2' into test/transformer
jessieAnhNguyen Jul 28, 2020
3e7c6f4
integrated transformer tool with CLI
jessieAnhNguyen Jul 28, 2020
cbc0c28
implemented --raw flag
jessieAnhNguyen Jul 28, 2020
28a55d4
deleted changelog comments and fixed small changes
jessieAnhNguyen Jul 30, 2020
680eccb
changed wording
jessieAnhNguyen Jul 30, 2020
ef6bb10
deleted unused event types and remove unused dev dependencies
jessieAnhNguyen Jul 30, 2020
2285b38
generated source map through
jessieAnhNguyen Aug 6, 2020
86c8110
Merge pull request #4 from MLH-Fellowship/test/transformer
jessieAnhNguyen Aug 10, 2020
652290d
fixed SHA-1 error
jessieAnhNguyen Aug 10, 2020
deffdd2
Merge branch 'master' of https://github.com/MLH-Fellowship/cli into f…
jessieAnhNguyen Aug 10, 2020
f560481
implemented the source map steps
jessieAnhNguyen Aug 10, 2020
e3fe062
added transformer package and profile-hermes.md
jessieAnhNguyen Aug 11, 2020
1cff3c8
Merge pull request #6 from MLH-Fellowship/feat/generate-source-map
jevakallio Aug 11, 2020
ca07916
Rename command to profile-hermes
jessieAnhNguyen Aug 11, 2020
867b6b2
Improve command line output examples
jessieAnhNguyen Aug 11, 2020
7ee8fc6
Extract source map functionality to its own file
jessieAnhNguyen Aug 11, 2020
4e0dd95
Readability improvements (@todo: fix @TODO comments)
jessieAnhNguyen Aug 11, 2020
c422253
upgrade hermes-profile-transformer package
jessieAnhNguyen Aug 11, 2020
3d9d04e
implemented @TODO: remove axios and use node-fetch
jessieAnhNguyen Aug 13, 2020
29e55ca
deleted small unintended changes and removed axios
jessieAnhNguyen Aug 13, 2020
1078bdb
removed axios package and deleted unnecessary changes
jessieAnhNguyen Aug 13, 2020
e050ab2
removed source-map related packages
jessieAnhNguyen Aug 13, 2020
b955db9
added new line after each logger
jessieAnhNguyen Aug 13, 2020
e086ec9
Merge pull request #7 from MLH-Fellowship/chore/pre-publish
jessieAnhNguyen Aug 13, 2020
423424f
Merge branch 'master' of https://github.com/MLH-Fellowship/cli
jessieAnhNguyen Aug 13, 2020
b37a804
upgraded hermes-profile-transformer package to latest
jessieAnhNguyen Aug 14, 2020
2f93778
moved profile-hermes folder out of cli to a separate package cli-hermes
jessieAnhNguyen Aug 18, 2020
ed90c7e
deleted the command from projectCommands in cli
jessieAnhNguyen Aug 18, 2020
aacd874
re-added the command in the projectCommands
saphal1998 Aug 18, 2020
86cba71
removed unused dependencies from cli
saphal1998 Aug 18, 2020
48b1b0f
added cli-hermes to the list of yarn link
jessieAnhNguyen Aug 18, 2020
e53ee19
Merge pull request #9 from MLH-Fellowship/movePackage
jessieAnhNguyen Aug 18, 2020
e3ff688
removed unused @types/ip dependency
jessieAnhNguyen Aug 18, 2020
f5e51e1
Merge remote-tracking branch 'upstream/master'
jessieAnhNguyen Aug 18, 2020
a0ad665
added @types/ip dependency in cli-hermes
jessieAnhNguyen Aug 18, 2020
1735396
moved @types/ip to dev dependency
jessieAnhNguyen Aug 18, 2020
1b0438b
added type definition for cli-hermes
saphal1998 Aug 18, 2020
5ebb4ba
fixed type definition
jessieAnhNguyen Aug 18, 2020
236b038
Added cli-hermes ref to tsconfig in packages/cli
saphal1998 Aug 18, 2020
8da90e5
Merge branch 'master' of github.com:MLH-Fellowship/cli
saphal1998 Aug 18, 2020
f7cb658
Removed unnecessary code comment
jessieAnhNguyen Aug 19, 2020
c85506e
made changes based on PR review
jessieAnhNguyen Aug 19, 2020
125836c
Merge branch 'master' of https://github.com/MLH-Fellowship/cli
jessieAnhNguyen Aug 19, 2020
b7dbb5e
added a verbose log
jessieAnhNguyen Aug 19, 2020
27f14d9
added cli-platform-android as a dependency
jessieAnhNguyen Aug 19, 2020
02263ae
added cli-platform-android as a dependency in cli
jessieAnhNguyen Aug 19, 2020
8fc1b01
Fixing path in tsconfig
saphal1998 Aug 19, 2020
bfc08b2
Merge conflict
saphal1998 Aug 19, 2020
4478249
Added type defs of platform-android to cli-hermes, path fixed
saphal1998 Aug 19, 2020
93acfcd
Fixed chalk version on cli-hermes
saphal1998 Aug 19, 2020
9607160
removed unnecessary comment
jessieAnhNguyen Aug 21, 2020
4e50329
Merge remote-tracking branch 'upstream/master'
jessieAnhNguyen Aug 21, 2020
dcabe01
Merge branch 'master' of https://github.com/MLH-Fellowship/cli
jessieAnhNguyen Aug 21, 2020
3e97a31
changed --filename flag and throw real error
jessieAnhNguyen Sep 1, 2020
f4b4041
Merge pull request #10 from MLH-Fellowship/review/edit
jessieAnhNguyen Sep 1, 2020
75095c5
Fix catch block in profile-hermes/index to match the error thrown
saphal1998 Sep 3, 2020
32a917e
Typecast error as CLIError
saphal1998 Sep 3, 2020
8d6fb8f
Apply suggestions from code review
saphal1998 Sep 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: implemented download hermes profile command
  • Loading branch information
jessieAnhNguyen committed Jul 21, 2020
commit 20477216dbcba7f70b7bf2279556dcfc88bc55b1
2 changes: 2 additions & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import info from './info/info';
import config from './config/config';
import init from './init';
import doctor from './doctor';
import profile from './profile';

export const projectCommands = [
start,
Expand All @@ -24,6 +25,7 @@ export const projectCommands = [
info,
config,
doctor,
profile,
] as Command[];

export const detachedCommands = [init, doctor] as DetachedCommand[];
175 changes: 101 additions & 74 deletions packages/cli/src/commands/profile/downloadProfile.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,111 @@
// @ts-ignore untyped
import getEnvironmentInfo from '../../tools/envinfo';
import {logger} from '@react-native-community/cli-tools';
import {Config} from '@react-native-community/cli-types';
import releaseChecker from '../../tools/releaseChecker';
import { listFiles } from './listFile';
import { pullFile } from './pullFile';
import {execSync} from 'child_process';
//import {projectConfig} from '../../../../platform-android/src/config/index';
import {logger, CLIError} from '@react-native-community/cli-tools';
import chalk from 'chalk';
import fs from 'fs';

// const info = async function getInfo(_argv: Array<string>, ctx: Config) {
// try {
// logger.info('Fetching system and libraries information...');
// const output = await getEnvironmentInfo(false);
// logger.log(output);
// } catch (err) {
// logger.error(`Unable to print environment info.\n${err}`);
// } finally {
// await releaseChecker(ctx.root);
// }
// };
type Options = {
fileName?: string;
};
const promise = require('adbkit/bluebird');
const adb = require('adbkit/lib/adb');
const client = adb.createClient();
function download(dstPath: string){
listFiles('com.awesomeproject');
pullFile(dstPath);
//get the last modified hermes profile
function getLatestFile(packageName: string): string {
try {
const file = execSync(`adb shell run-as ${packageName} ls cache/ -tp | grep -v /$ | head -1
`);
//console.log(file.toString());
return file.toString().trim();
//return parsePackagename(packages.toString());
} catch (e) {
throw new Error(e);
}
}
//get the package name of the running React Native app
function getPackageName(config: Config) {
const androidProject = config.project.android;

const hermesProfile = async function downloadProfile(_argv: Array<string>, ctx: Config, options: Options) {
try{
logger.info('Downloading the latest Hermes Sampling Profiler from your Android device...');
const output = await
if (!androidProject) {
throw new CLIError(`
Android project not found. Are you sure this is a React Native project?
If your Android files are located in a non-standard location (e.g. not inside \'android\' folder), consider setting
\`project.android.sourceDir\` option to point to a new location.
`);
}
const {manifestPath} = androidProject;
const androidManifest = fs.readFileSync(manifestPath, 'utf8');

}
catch (err){
logger.error(`Unable to download the Hermes Sampling Profiler.\n${err}`);
}
finally{
await
}
};
let packageNameMatchArray = androidManifest.match(/package="(.+?)"/);
if (!packageNameMatchArray || packageNameMatchArray.length === 0) {
throw new CLIError(
'Failed to build the app: No package name found. Found errors in /src/main/AndroidManifest.xml',
);
}

// type Command = {
// name: string,
// description?: string,
// func: (argv: Array<string>, config: ConfigT, args: Object) => ?Promise<void>,
// options?: Array<{
// name: string,
// description?: string,
// parse?: (val: string) => any,
// default?:
// | string
// | boolean
// | number
// | ((config: ConfigT) => string | boolean | number),
// }>,
// examples?: Array<{
// desc: string,
// cmd: string,
// }>,
// };
let packageName = packageNameMatchArray[1];

if (!validatePackageName(packageName)) {
logger.warn(
`Invalid application's package name "${chalk.bgRed(
packageName,
)}" in 'AndroidManifest.xml'. Read guidelines for setting the package name here: ${chalk.underline.dim(
'https://developer.android.com/studio/build/application-id',
)}`,
);
}
return packageName;
}
// Validates that the package name is correct
function validatePackageName(packageName: string) {
return /^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$/.test(packageName);
}

export default {
name: 'profile-hermes <destinationDir>',
description: 'Download the Hermes Sampling Profiler to the directory <destinationDir> of the local machine',
func: hermesProfile,
//options: download the latest or filename
options: [
{
name: '--filename <string>',
description: 'Filename of the profile to be downloaded',
},
],
examples: [
{
desc: 'Download the Hermes Sampling Profiler to the directory <destinationDir> of the local machine',
cmd: 'profile-hermes /users/name/desktop',
},
],
};
/**
* Executes the commands to pull a hermes profile
* Commands:
* adb shell run-as com.rnhermesapp cp cache/sampling-profiler-trace1502707982002849976.cpuprofile /sdcard/latest.cpuprofile
* adb pull /sdcard/latest.cpuprofile
*/

//const packageName = projectConfig(".",{} )?.packageName; //TODO: get AndroidProjectConfig
export async function downloadProfile(
ctx: Config,
dstPath?: string,
fileName?: string,
) {
try {
const packageName = getPackageName(ctx);

// const projectConfigResult = projectConfig(ctx.root, {});
// let packageName;
// if (projectConfigResult !== null) {
// packageName = projectConfigResult.packageName;
// } else {
// packageName = ''; //cannot get packageName since config is empty
// }
let file;
if (fileName !== undefined) {
file = fileName;
} else {
file = await getLatestFile(packageName);
}
logger.info(`File to be pulled: ${file}`);
// console.log(`adb shell run-as ${packageName} ls`);
// execSync(`adb shell run-as ${packageName} ls`);
execSync(`adb shell run-as ${packageName} cp cache/${file} /sdcard`);

//if not specify destination path, pull to the current directory
if (dstPath === undefined) {
//execSync(`adb pull /sdcard/${file} ${process.cwd()}`);
execSync(`adb pull /sdcard/${file} ${ctx.root}`);
console.log(
'Successfully pulled the file to the current root working directory',
);
}
//if specified destination path, pull to that directory
else {
execSync(`adb pull /sdcard/${file} ${dstPath}`);
console.log(`Successfully pulled the file to ${dstPath}`);
}
//return '';
} catch (e) {
throw new Error(e.message);
}
}
74 changes: 74 additions & 0 deletions packages/cli/src/commands/profile/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// @ts-ignore untyped
import {logger} from '@react-native-community/cli-tools';
import {Config} from '@react-native-community/cli-types';
import {downloadProfile} from './downloadProfile';

type Options = {
fileName?: string;
};

async function profile(
[dstPath]: Array<string>,
ctx: Config,
options: Options,
) {
try {
logger.info(
'Downloading a Hermes Sampling Profiler from your Android device...',
);
//logger.log(`options ${JSON.stringify(options)}`);
// logger.log(`${typeof options}`);
// logger.log(`${options.fileName}`);
if (options.fileName) {
//logger.log(options.fileName);
await downloadProfile(ctx, dstPath, options.fileName);
} else {
logger.info('No filename is provided, pulling latest file');
await downloadProfile(ctx, dstPath, undefined);
}
//logger.log(output);
} catch (err) {
logger.error(`Unable to download the Hermes Sampling Profiler.\n${err}`);
}
}

// type Command = {
// name: string,
// description?: string,
// func: (argv: Array<string>, config: ConfigT, args: Object) => ?Promise<void>,
// options?: Array<{
// name: string,
// description?: string,
// parse?: (val: string) => any,
// default?:
// | string
// | boolean
// | number
// | ((config: ConfigT) => string | boolean | number),
// }>,
// examples?: Array<{
// desc: string,
// cmd: string,
// }>,
// };

export default {
name: 'profile-hermes [destinationDir]', //profile-hermes ls
description:
'Download the Hermes Sampling Profiler to the directory <destinationDir> of the local machine',
func: profile, //how to give the args for this func: an array of arguments
options: [
//options: download the latest or fileName
{
name: '--fileName [string]',
description: 'Filename of the profile to be downloaded',
},
],
examples: [
{
desc:
'Download the Hermes Sampling Profiler to the directory <destinationDir> of the local machine',
cmd: 'profile-hermes /Users/phuonganh/Desktop',
},
],
};
35 changes: 0 additions & 35 deletions packages/cli/src/commands/profile/listFile.ts

This file was deleted.

38 changes: 0 additions & 38 deletions packages/cli/src/commands/profile/pullFile.ts

This file was deleted.