| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {TrackEventName} from 'isl-server/src/analytics/eventNames'; |
| b69ab31 | | | 9 | import type { |
| b69ab31 | | | 10 | ApplyMergeConflictsPreviewsFuncType, |
| b69ab31 | | | 11 | ApplyUncommittedChangesPreviewsFuncType, |
| b69ab31 | | | 12 | Dag, |
| b69ab31 | | | 13 | MergeConflictsPreviewContext, |
| b69ab31 | | | 14 | UncommittedChangesPreviewContext, |
| b69ab31 | | | 15 | } from '../previews'; |
| b69ab31 | | | 16 | import type {CommandArg, RunnableOperation} from '../types'; |
| b69ab31 | | | 17 | |
| b69ab31 | | | 18 | import {randomId} from 'shared/utils'; |
| b69ab31 | | | 19 | import {CommandRunner} from '../types'; |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | // NOTE: This file is sensitive to what is imported, as it is used by the vscode extension as well as the client. |
| b69ab31 | | | 22 | // It should not import the platform, even transitively, or vscode will use the wrong platform. |
| b69ab31 | | | 23 | // TODO: Can we adjust how the extension uses this to prevent import issues? |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | /** |
| b69ab31 | | | 26 | * Operations represent commands that mutate the repository, such as rebasing, committing, etc. |
| b69ab31 | | | 27 | * Operations are intended to be relatively long-lived processes which show progress, are cancellable, and must be run one-at-a-time. |
| b69ab31 | | | 28 | * This is as opposed to other commands like status, log, cat, which may be run in parallel and do not (necessarily) show stdout progress. |
| b69ab31 | | | 29 | * You can get arguments, get the preview applier function, get the optimistic state applier function, get documentation, etc. |
| b69ab31 | | | 30 | */ |
| b69ab31 | | | 31 | export abstract class Operation { |
| b69ab31 | | | 32 | static operationName: string; |
| b69ab31 | | | 33 | public id: string = randomId(); |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | constructor(public trackEventName: TrackEventName) {} |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | abstract getArgs(): Array<CommandArg>; |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | /** Optional stdin data piped to the process. */ |
| b69ab31 | | | 40 | getStdin(): string | undefined { |
| b69ab31 | | | 41 | return undefined; |
| b69ab31 | | | 42 | } |
| b69ab31 | | | 43 | |
| b69ab31 | | | 44 | /** Description of the operation. This can replace the default display. */ |
| b69ab31 | | | 45 | getDescriptionForDisplay(): OperationDescription | undefined { |
| b69ab31 | | | 46 | return undefined; |
| b69ab31 | | | 47 | } |
| b69ab31 | | | 48 | |
| b69ab31 | | | 49 | /** |
| b69ab31 | | | 50 | * When the operation starts running, prefill inline progress messages to show up next to one or more commits. |
| b69ab31 | | | 51 | * Note: most operations/runners never report additional inline progress, meaning this is typically shown for the duration of the operation. |
| b69ab31 | | | 52 | */ |
| b69ab31 | | | 53 | getInitialInlineProgress?(): Array<[hash: string, message: string]>; |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | public runner: CommandRunner = CommandRunner.Sapling; |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | makeOptimisticUncommittedChangesApplier?( |
| b69ab31 | | | 58 | context: UncommittedChangesPreviewContext, |
| b69ab31 | | | 59 | ): ApplyUncommittedChangesPreviewsFuncType | undefined; |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | makeOptimisticMergeConflictsApplier?( |
| b69ab31 | | | 62 | context: MergeConflictsPreviewContext, |
| b69ab31 | | | 63 | ): ApplyMergeConflictsPreviewsFuncType | undefined; |
| b69ab31 | | | 64 | |
| b69ab31 | | | 65 | /** Effects to `dag` before confirming the operation. */ |
| b69ab31 | | | 66 | previewDag(dag: Dag): Dag { |
| b69ab31 | | | 67 | return dag; |
| b69ab31 | | | 68 | } |
| b69ab31 | | | 69 | |
| b69ab31 | | | 70 | /** |
| b69ab31 | | | 71 | * Effects to `dag` after confirming the operation. |
| b69ab31 | | | 72 | * The operation is running or queued. |
| b69ab31 | | | 73 | */ |
| b69ab31 | | | 74 | optimisticDag(dag: Dag): Dag { |
| b69ab31 | | | 75 | return dag; |
| b69ab31 | | | 76 | } |
| b69ab31 | | | 77 | |
| b69ab31 | | | 78 | getRunnableOperation(): RunnableOperation { |
| b69ab31 | | | 79 | return { |
| b69ab31 | | | 80 | args: this.getArgs(), |
| b69ab31 | | | 81 | id: this.id, |
| b69ab31 | | | 82 | stdin: this.getStdin(), |
| b69ab31 | | | 83 | runner: this.runner, |
| b69ab31 | | | 84 | trackEventName: this.trackEventName, |
| b69ab31 | | | 85 | }; |
| b69ab31 | | | 86 | } |
| b69ab31 | | | 87 | } |
| b69ab31 | | | 88 | |
| b69ab31 | | | 89 | /** Access static opName field of an operation */ |
| b69ab31 | | | 90 | export function getOpName(op: Operation): string { |
| b69ab31 | | | 91 | return (op.constructor as unknown as {opName: string}).opName; |
| b69ab31 | | | 92 | } |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | /** Describe how to display a operation. */ |
| b69ab31 | | | 95 | export type OperationDescription = { |
| b69ab31 | | | 96 | /** If set, this replaces the default command arguments. */ |
| b69ab31 | | | 97 | description?: string; |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | /** |
| b69ab31 | | | 100 | * If set, this replaces the default command in the output tooltip. |
| b69ab31 | | | 101 | * It also indicates that the output lines might contain a JSON string |
| b69ab31 | | | 102 | * that is not suitable for human reading. |
| b69ab31 | | | 103 | */ |
| b69ab31 | | | 104 | tooltip?: string; |
| b69ab31 | | | 105 | }; |