|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import * as Effect from 'effect/Effect'; |
| 9 | +import * as vscode from 'vscode'; |
| 10 | +import { URI } from 'vscode-uri'; |
| 11 | +import { nls } from '../messages'; |
| 12 | +import { AllServicesLayer, ExtensionProviderService } from '../services/extensionProvider'; |
| 13 | +import { retrieveComponentSet } from '../shared/retrieve/retrieveComponentSet'; |
| 14 | + |
| 15 | +const retrievePaths = (paths: Set<string>) => |
| 16 | + Effect.gen(function* () { |
| 17 | + const api = yield* (yield* ExtensionProviderService).getServicesApi; |
| 18 | + const componentSetService = yield* api.services.ComponentSetService; |
| 19 | + const componentSet = yield* componentSetService.ensureNonEmptyComponentSet( |
| 20 | + yield* componentSetService.getComponentSetFromPaths(paths) |
| 21 | + ); |
| 22 | + yield* retrieveComponentSet({ componentSet }); |
| 23 | + }); |
| 24 | + |
| 25 | +/** Retrieve source paths from the default org */ |
| 26 | +const retrieveSourcePathsEffect = Effect.fn('retrieveSourcePaths')(function* (sourceUri: URI, uris: URI[]) { |
| 27 | + yield* Effect.annotateCurrentSpan({ sourceUri, uris }); |
| 28 | + const paths = new Set([sourceUri.path, ...uris.map(uri => uri.path)]); |
| 29 | + return yield* retrievePaths(paths); |
| 30 | +}); |
| 31 | + |
| 32 | +/** Retrieve source paths from the default org */ |
| 33 | + |
| 34 | +// When a single file is selected and "Retrieve Source from Org" is executed, |
| 35 | +// sourceUri is passed, and the uris array contains a single element, the same |
| 36 | +// path as sourceUri. |
| 37 | +// |
| 38 | +// When multiple files are selected and "Retrieve Source from Org" is executed, |
| 39 | +// sourceUri is passed, and is the path to the first selected file, and the uris |
| 40 | +// array contains an array of all paths that were selected. |
| 41 | +// |
| 42 | +// When editing a file and "Retrieve This Source from Org" is executed, |
| 43 | +// sourceUri is passed, but uris is undefined. |
| 44 | + |
| 45 | +export const retrieveSourcePaths = async (sourceUri: URI | undefined, uris: URI[] | undefined): Promise<void> => { |
| 46 | + const resolvedSourceUri = |
| 47 | + sourceUri ?? |
| 48 | + (await Effect.runPromise( |
| 49 | + Effect.gen(function* () { |
| 50 | + const api = yield* (yield* ExtensionProviderService).getServicesApi; |
| 51 | + return yield* (yield* api.services.EditorService).getActiveEditorUri; |
| 52 | + }) |
| 53 | + .pipe(Effect.provide(AllServicesLayer)) |
| 54 | + .pipe( |
| 55 | + Effect.catchTag('NoActiveEditorError', () => |
| 56 | + Effect.promise(() => |
| 57 | + vscode.window.showErrorMessage(nls.localize('retrieve_select_file_or_directory')) |
| 58 | + ).pipe(Effect.as(undefined)) |
| 59 | + ) |
| 60 | + ) |
| 61 | + )); |
| 62 | + |
| 63 | + if (!resolvedSourceUri) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + const resolvedUris = uris?.length ? uris : [resolvedSourceUri]; |
| 68 | + await Effect.runPromise( |
| 69 | + retrieveSourcePathsEffect(resolvedSourceUri, resolvedUris).pipe( |
| 70 | + Effect.catchAll(error => |
| 71 | + Effect.gen(function* () { |
| 72 | + const api = yield* (yield* ExtensionProviderService).getServicesApi; |
| 73 | + const channelService = yield* api.services.ChannelService; |
| 74 | + const errorMessage = error instanceof Error ? error.message : String(error); |
| 75 | + yield* channelService.appendToChannel(`Retrieve failed: ${errorMessage}`); |
| 76 | + yield* Effect.promise(() => vscode.window.showErrorMessage(errorMessage)); |
| 77 | + }).pipe(Effect.provide(AllServicesLayer), Effect.as(undefined)) |
| 78 | + ), |
| 79 | + Effect.provide(AllServicesLayer) |
| 80 | + ) |
| 81 | + ); |
| 82 | +}; |
0 commit comments