addons/isl/src/operations/Operation.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {TrackEventName} from 'isl-server/src/analytics/eventNames';
b69ab319import type {
b69ab3110 ApplyMergeConflictsPreviewsFuncType,
b69ab3111 ApplyUncommittedChangesPreviewsFuncType,
b69ab3112 Dag,
b69ab3113 MergeConflictsPreviewContext,
b69ab3114 UncommittedChangesPreviewContext,
b69ab3115} from '../previews';
b69ab3116import type {CommandArg, RunnableOperation} from '../types';
b69ab3117
b69ab3118import {randomId} from 'shared/utils';
b69ab3119import {CommandRunner} from '../types';
b69ab3120
b69ab3121// NOTE: This file is sensitive to what is imported, as it is used by the vscode extension as well as the client.
b69ab3122// It should not import the platform, even transitively, or vscode will use the wrong platform.
b69ab3123// TODO: Can we adjust how the extension uses this to prevent import issues?
b69ab3124
b69ab3125/**
b69ab3126 * Operations represent commands that mutate the repository, such as rebasing, committing, etc.
b69ab3127 * Operations are intended to be relatively long-lived processes which show progress, are cancellable, and must be run one-at-a-time.
b69ab3128 * This is as opposed to other commands like status, log, cat, which may be run in parallel and do not (necessarily) show stdout progress.
b69ab3129 * You can get arguments, get the preview applier function, get the optimistic state applier function, get documentation, etc.
b69ab3130 */
b69ab3131export abstract class Operation {
b69ab3132 static operationName: string;
b69ab3133 public id: string = randomId();
b69ab3134
b69ab3135 constructor(public trackEventName: TrackEventName) {}
b69ab3136
b69ab3137 abstract getArgs(): Array<CommandArg>;
b69ab3138
b69ab3139 /** Optional stdin data piped to the process. */
b69ab3140 getStdin(): string | undefined {
b69ab3141 return undefined;
b69ab3142 }
b69ab3143
b69ab3144 /** Description of the operation. This can replace the default display. */
b69ab3145 getDescriptionForDisplay(): OperationDescription | undefined {
b69ab3146 return undefined;
b69ab3147 }
b69ab3148
b69ab3149 /**
b69ab3150 * When the operation starts running, prefill inline progress messages to show up next to one or more commits.
b69ab3151 * Note: most operations/runners never report additional inline progress, meaning this is typically shown for the duration of the operation.
b69ab3152 */
b69ab3153 getInitialInlineProgress?(): Array<[hash: string, message: string]>;
b69ab3154
b69ab3155 public runner: CommandRunner = CommandRunner.Sapling;
b69ab3156
b69ab3157 makeOptimisticUncommittedChangesApplier?(
b69ab3158 context: UncommittedChangesPreviewContext,
b69ab3159 ): ApplyUncommittedChangesPreviewsFuncType | undefined;
b69ab3160
b69ab3161 makeOptimisticMergeConflictsApplier?(
b69ab3162 context: MergeConflictsPreviewContext,
b69ab3163 ): ApplyMergeConflictsPreviewsFuncType | undefined;
b69ab3164
b69ab3165 /** Effects to `dag` before confirming the operation. */
b69ab3166 previewDag(dag: Dag): Dag {
b69ab3167 return dag;
b69ab3168 }
b69ab3169
b69ab3170 /**
b69ab3171 * Effects to `dag` after confirming the operation.
b69ab3172 * The operation is running or queued.
b69ab3173 */
b69ab3174 optimisticDag(dag: Dag): Dag {
b69ab3175 return dag;
b69ab3176 }
b69ab3177
b69ab3178 getRunnableOperation(): RunnableOperation {
b69ab3179 return {
b69ab3180 args: this.getArgs(),
b69ab3181 id: this.id,
b69ab3182 stdin: this.getStdin(),
b69ab3183 runner: this.runner,
b69ab3184 trackEventName: this.trackEventName,
b69ab3185 };
b69ab3186 }
b69ab3187}
b69ab3188
b69ab3189/** Access static opName field of an operation */
b69ab3190export function getOpName(op: Operation): string {
b69ab3191 return (op.constructor as unknown as {opName: string}).opName;
b69ab3192}
b69ab3193
b69ab3194/** Describe how to display a operation. */
b69ab3195export type OperationDescription = {
b69ab3196 /** If set, this replaces the default command arguments. */
b69ab3197 description?: string;
b69ab3198
b69ab3199 /**
b69ab31100 * If set, this replaces the default command in the output tooltip.
b69ab31101 * It also indicates that the output lines might contain a JSON string
b69ab31102 * that is not suitable for human reading.
b69ab31103 */
b69ab31104 tooltip?: string;
b69ab31105};