forked from react-native-community/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Implemented command to download the Hermes Sampling Profiler to a local machine #2
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1167db0
added adb commands
jessieAnhNguyen 2047721
feat: implemented download hermes profile command
jessieAnhNguyen 4af2cbe
deleted unused adbkit packages
jessieAnhNguyen 98e1ecf
changed files excluded node modules
jessieAnhNguyen 24a248b
handled edge case when there's no file
jessieAnhNguyen b720baf
deleted unnecessary changes
jessieAnhNguyen 4c0fb45
deleted unnecessary changes 2
jessieAnhNguyen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| { | ||
| "editor.rulers": [ | ||
| 80 | ||
| ], | ||
| "editor.rulers": [80], | ||
| "files.exclude": { | ||
| "**/.git": true, | ||
| "**/node_modules": true, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import {Config} from '@react-native-community/cli-types'; | ||
| import {execSync} from 'child_process'; | ||
| import {logger, CLIError} from '@react-native-community/cli-tools'; | ||
| import chalk from 'chalk'; | ||
| import fs from 'fs'; | ||
|
|
||
| /** | ||
| * 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 | ||
| `); | ||
|
|
||
| return file.toString().trim(); | ||
| } 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; | ||
|
|
||
| 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'); | ||
|
|
||
| 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', | ||
| ); | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
|
|
||
| /** | ||
| * 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 | ||
| */ | ||
| export async function downloadProfile( | ||
| ctx: Config, | ||
| dstPath?: string, | ||
| fileName?: string, | ||
| ) { | ||
| try { | ||
| const packageName = getPackageName(ctx); | ||
|
|
||
| const file = fileName || (await getLatestFile(packageName)); | ||
| if (!file) { | ||
| logger.error( | ||
| 'There is no file in the cache/ directory. Did you record a profile from the developer menu?', | ||
| ); | ||
| process.exit(1); | ||
| } | ||
| logger.info(`File to be pulled: ${file}`); | ||
| 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} ${ctx.root}`); | ||
| console.log(`Successfully pulled the file to ${ctx.root}/${file}`); | ||
| } | ||
| //if specified destination path, pull to that directory | ||
| else { | ||
| execSync(`adb pull /sdcard/${file} ${dstPath}`); | ||
| console.log(`Successfully pulled the file to ${dstPath}/${file}`); | ||
| } | ||
| } catch (e) { | ||
| throw new Error(e.message); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // @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...', | ||
| ); | ||
|
|
||
| if (options.fileName) { | ||
| await downloadProfile(ctx, dstPath, options.fileName); | ||
| } else { | ||
| logger.info('No filename is provided, pulling latest file'); | ||
| await downloadProfile(ctx, dstPath, undefined); | ||
| } | ||
| } catch (err) { | ||
| logger.error(`Unable to download the Hermes Sampling Profiler.\n${err}`); | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| name: 'profile-hermes [destinationDir]', | ||
| description: | ||
| 'Download the Hermes Sampling Profiler to the directory <destinationDir> of the local machine', | ||
| func: profile, | ||
| 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', | ||
| }, | ||
| ], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.