microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/debuggingConfiguration/debugScenarioNameGenerator.ts
153lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import { PlatformType, ExpoPlatform } from "../launchArgs"; |
| 5 | import { DebugScenarioType, DEBUG_TYPES } from "./debugConfigTypesAndConstants"; |
| 6 | |
| 7 | interface DebugScenarioName { |
| 8 | debugScenarioType: string; |
| 9 | prePlatformTypeDescription?: string; |
| 10 | platformType?: string; |
| 11 | postPlatformTypeDescription?: string; |
| 12 | experimentalDescription?: string; |
| 13 | expoPlatformType?: ExpoPlatform; |
| 14 | } |
| 15 | |
| 16 | export class DebugScenarioNameGenerator { |
| 17 | public static createScenarioName( |
| 18 | debugScenarioType: DebugScenarioType, |
| 19 | debugType: string, |
| 20 | platformType?: PlatformType | string, |
| 21 | useHermesEngine: boolean = false, |
| 22 | isExperimental: boolean = false, |
| 23 | expoPlatformType?: ExpoPlatform, |
| 24 | ): string { |
| 25 | const debugScenarioName: DebugScenarioName = |
| 26 | this.createScenarioAccordingToDebugScenarioType(debugScenarioType); |
| 27 | debugScenarioName.platformType = this.getPlatformTypeName(platformType); |
| 28 | |
| 29 | if (platformType === PlatformType.Exponent) { |
| 30 | debugScenarioName.expoPlatformType = expoPlatformType; |
| 31 | } |
| 32 | |
| 33 | if (debugType === DEBUG_TYPES.REACT_NATIVE) { |
| 34 | this.configureNotDirectModeScenario( |
| 35 | debugScenarioName, |
| 36 | debugScenarioType, |
| 37 | debugType, |
| 38 | platformType, |
| 39 | ); |
| 40 | } else { |
| 41 | this.configureDirectModeScenario( |
| 42 | debugScenarioName, |
| 43 | debugScenarioType, |
| 44 | debugType, |
| 45 | useHermesEngine, |
| 46 | platformType, |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | if (platformType === PlatformType.ExpoWeb || !useHermesEngine) { |
| 51 | isExperimental = true; |
| 52 | } |
| 53 | |
| 54 | if (isExperimental) { |
| 55 | debugScenarioName.experimentalDescription = "- Experimental"; |
| 56 | } |
| 57 | |
| 58 | return this.debugScenarioNameToString(debugScenarioName); |
| 59 | } |
| 60 | |
| 61 | private static createScenarioAccordingToDebugScenarioType( |
| 62 | debugScenarioType: DebugScenarioType, |
| 63 | ): DebugScenarioName { |
| 64 | switch (debugScenarioType) { |
| 65 | case DebugScenarioType.RunApp: |
| 66 | return { |
| 67 | debugScenarioType: "Run", |
| 68 | }; |
| 69 | case DebugScenarioType.DebugApp: |
| 70 | return { |
| 71 | debugScenarioType: "Debug", |
| 72 | }; |
| 73 | case DebugScenarioType.AttachApp: |
| 74 | return { |
| 75 | debugScenarioType: "Attach to", |
| 76 | }; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private static configureNotDirectModeScenario( |
| 81 | debugScenarioName: DebugScenarioName, |
| 82 | debugScenarioType: DebugScenarioType, |
| 83 | debugType: string, |
| 84 | platformType?: PlatformType | string, |
| 85 | ): void { |
| 86 | if (debugScenarioType === DebugScenarioType.AttachApp) { |
| 87 | debugScenarioName.platformType = "packager"; |
| 88 | } |
| 89 | if (platformType === PlatformType.Exponent) { |
| 90 | debugScenarioName.prePlatformTypeDescription = "in"; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private static configureDirectModeScenario( |
| 95 | debugScenarioName: DebugScenarioName, |
| 96 | debugScenarioType: DebugScenarioType, |
| 97 | debugType: string, |
| 98 | useHermesEngine: boolean, |
| 99 | platformType?: PlatformType | string, |
| 100 | ) { |
| 101 | if (useHermesEngine) { |
| 102 | debugScenarioName.postPlatformTypeDescription = |
| 103 | debugScenarioType === DebugScenarioType.AttachApp ? "Hermes application" : "Hermes"; |
| 104 | } |
| 105 | switch (platformType) { |
| 106 | case PlatformType.iOS: |
| 107 | if (!useHermesEngine) { |
| 108 | debugScenarioName.prePlatformTypeDescription = "Direct"; |
| 109 | } |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private static getPlatformTypeName(platformType?: PlatformType | string): string { |
| 115 | switch (platformType) { |
| 116 | case PlatformType.Android: |
| 117 | return "Android"; |
| 118 | case PlatformType.iOS: |
| 119 | return "iOS"; |
| 120 | case PlatformType.Exponent: |
| 121 | return "Exponent"; |
| 122 | case PlatformType.Windows: |
| 123 | return "Windows"; |
| 124 | case PlatformType.macOS: |
| 125 | return "macOS"; |
| 126 | case PlatformType.ExpoWeb: |
| 127 | return "Expo Web"; |
| 128 | default: |
| 129 | return ""; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private static debugScenarioNameToString(debugScenarioName: DebugScenarioName): string { |
| 134 | let debugScenarioNameStr = debugScenarioName.debugScenarioType; |
| 135 | if (debugScenarioName.prePlatformTypeDescription) { |
| 136 | debugScenarioNameStr += ` ${debugScenarioName.prePlatformTypeDescription}`; |
| 137 | } |
| 138 | if (debugScenarioName.platformType) { |
| 139 | debugScenarioNameStr += ` ${debugScenarioName.platformType}`; |
| 140 | } |
| 141 | if (debugScenarioName.expoPlatformType) { |
| 142 | debugScenarioNameStr += ` ${debugScenarioName.expoPlatformType}`; |
| 143 | } |
| 144 | if (debugScenarioName.postPlatformTypeDescription) { |
| 145 | debugScenarioNameStr += ` ${debugScenarioName.postPlatformTypeDescription}`; |
| 146 | } |
| 147 | if (debugScenarioName.experimentalDescription) { |
| 148 | debugScenarioNameStr += ` ${debugScenarioName.experimentalDescription}`; |
| 149 | } |
| 150 | |
| 151 | return debugScenarioNameStr; |
| 152 | } |
| 153 | } |
| 154 | |