bug-report.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // cspell: ignore bugreport
  2. // cspell: ignore bugreportz
  3. import { AdbCommandBase, AdbSubprocessShellProtocol } from '@yume-chan/adb';
  4. import { DecodeUtf8Stream, PushReadableStream, ReadableStream, SplitStringStream, WrapReadableStream, WritableStream } from '@yume-chan/stream-extra';
  5. export interface BugReportZVersion {
  6. major: number;
  7. minor: number;
  8. supportProgress: boolean;
  9. supportStream: boolean;
  10. }
  11. export class BugReportZ extends AdbCommandBase {
  12. public static VERSION_REGEX = /(\d+)\.(\d+)/;
  13. public static BEGIN_REGEX = /BEGIN:(.*)/;
  14. public static PROGRESS_REGEX = /PROGRESS:(.*)\/(.*)/;
  15. public static OK_REGEX = /OK:(.*)/;
  16. public static FAIL_REGEX = /FAIL:(.*)/;
  17. /**
  18. * Retrieve the version of bugreportz.
  19. *
  20. * @returns a `BugReportVersion` object, or `undefined` if `bugreportz` is not available.
  21. */
  22. public async version(): Promise<BugReportZVersion | undefined> {
  23. // bugreportz requires shell protocol
  24. if (!AdbSubprocessShellProtocol.isSupported(this.adb)) {
  25. return undefined;
  26. }
  27. const { stderr, exitCode } = await this.adb.subprocess.spawnAndWait(['bugreportz', '-v']);
  28. if (exitCode !== 0 || stderr === '') {
  29. return undefined;
  30. }
  31. const match = stderr.match(BugReportZ.VERSION_REGEX);
  32. if (!match) {
  33. return undefined;
  34. }
  35. const major = parseInt(match[1]!, 10);
  36. const minor = parseInt(match[2]!, 10);
  37. return {
  38. major,
  39. minor,
  40. supportProgress: this.supportProgress(major, minor),
  41. supportStream: this.supportStream(major, minor),
  42. };
  43. }
  44. public supportProgress(major: number, minor: number): boolean {
  45. return major > 1 || minor >= 1;
  46. }
  47. /**
  48. * Create a zipped bugreport file.
  49. *
  50. * Compare to `stream`, this method will write the output to a file on device.
  51. * You can pull it later using sync protocol.
  52. *
  53. * @param onProgress Progress callback. Only specify this if `supportsProgress` is `true`.
  54. * @returns The path of the bugreport file.
  55. */
  56. public async generate(onProgress?: (progress: string, total: string) => void): Promise<string> {
  57. const process = await this.adb.subprocess.spawn([
  58. 'bugreportz',
  59. ...(onProgress ? ['-p'] : []),
  60. ]);
  61. let filename: string | undefined;
  62. let error: string | undefined;
  63. await process.stdout
  64. .pipeThrough(new DecodeUtf8Stream())
  65. .pipeThrough(new SplitStringStream('\n'))
  66. .pipeTo(new WritableStream<string>({
  67. write(line) {
  68. // `BEGIN:` and `PROGRESS:` only appear when `-p` is specified.
  69. let match = line.match(BugReportZ.PROGRESS_REGEX);
  70. if (match) {
  71. onProgress?.(match[1]!, match[2]!);
  72. }
  73. match = line.match(BugReportZ.BEGIN_REGEX);
  74. if (match) {
  75. filename = match[1]!;
  76. }
  77. match = line.match(BugReportZ.OK_REGEX);
  78. if (match) {
  79. filename = match[1];
  80. }
  81. match = line.match(BugReportZ.FAIL_REGEX);
  82. if (match) {
  83. // Don't report error now
  84. // We want to gather all output.
  85. error = match[1];
  86. }
  87. }
  88. }));
  89. if (error) {
  90. throw new Error(error);
  91. }
  92. if (!filename) {
  93. throw new Error('bugreportz did not return file name');
  94. }
  95. // Design choice: we don't automatically pull the file to avoid more dependency on `@yume-chan/adb`
  96. return filename;
  97. }
  98. public supportStream(major: number, minor: number): boolean {
  99. return major > 1 || minor >= 2;
  100. }
  101. public stream(): ReadableStream<Uint8Array> {
  102. return new PushReadableStream(async (controller) => {
  103. const process = await this.adb.subprocess.spawn(['bugreportz', '-s']);
  104. process.stdout
  105. .pipeTo(new WritableStream({
  106. async write(chunk) {
  107. await controller.enqueue(chunk);
  108. },
  109. }));
  110. process.stderr
  111. .pipeThrough(new DecodeUtf8Stream())
  112. .pipeTo(new WritableStream({
  113. async write(chunk) {
  114. controller.error(new Error(chunk));
  115. }
  116. }));
  117. await process.exit;
  118. controller.close();
  119. });
  120. }
  121. }
  122. // https://cs.android.com/android/platform/superproject/+/master:frameworks/native/cmds/bugreport/bugreport.cpp;drc=9b73bf07d73dbab5b792632e1e233edbad77f5fd;bpv=0;bpt=0
  123. export class BugReport extends AdbCommandBase {
  124. public generate(): ReadableStream<Uint8Array> {
  125. return new WrapReadableStream(async () => {
  126. const process = await this.adb.subprocess.spawn(['bugreport']);
  127. return process.stdout;
  128. });
  129. }
  130. }