All files / services/validation/migrations shacl-validation-profiles-migration.ts

95% Statements 19/20
77.77% Branches 14/18
100% Functions 1/1
95% Lines 19/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64          1x                                 4x   4x     4x 4x   4x       4x 4x   4x   2x 1x     2x     2x       4x 1x     1x   1x   1x 1x        
import * as vscode from 'vscode';
import { getConfig } from '@src/utilities/vscode/config';
import { isLegacyShaclValidationConfig, migrateLegacyShaclValidationConfig } from './shacl-validation-legacy-migration';
import { ISettingsMigration } from '@src/services/core/settings-migration.interface';
 
const KEY = 'shacl.validation';
 
/**
 * Migrates the legacy `mentor.shacl.validation` value (`{ defaults, graphs }`)
 * to the profile-based model (`{ profiles }`).
 *
 * Legacy values were written to the user (global) scope, which prevented sharing
 * them with a team. The migrated settings are written to the workspace scope and
 * the legacy global value is cleared afterwards. Since the old document keys are
 * workspace-relative, a global value shared across workspaces was ambiguous
 * anyway; entries from other workspaces become inert assignments that can be
 * removed in the validation settings UI.
 *
 * Idempotent: does nothing when the workspace value already uses the new model
 * and no legacy global value remains.
 */
export class ShaclValidationProfilesMigration implements ISettingsMigration {
	readonly id = 'shacl.validation.profiles';
 
	readonly description = 'Migrate the legacy SHACL validation configuration to named validation profiles in workspace settings.';
 
	async migrate(): Promise<void> {
		const config = getConfig();
		const info = config.inspect<unknown>(KEY);
 
		Iif (!info) {
			return;
		}
 
		const { workspaceValue, globalValue } = info;
		const canWriteWorkspace = !!vscode.workspace.workspaceFolders?.length;
 
		if (workspaceValue !== undefined && !isLegacyShaclValidationConfig(workspaceValue)) {
			// Already migrated — only clean up a leftover legacy global value.
			if (isLegacyShaclValidationConfig(globalValue)) {
				await config.update(KEY, undefined, vscode.ConfigurationTarget.Global);
			}
 
			return;
		}
 
		const legacy = isLegacyShaclValidationConfig(workspaceValue)
			? workspaceValue
			: isLegacyShaclValidationConfig(globalValue) ? globalValue : undefined;
 
		if (!legacy || !canWriteWorkspace) {
			return;
		}
 
		const migrated = migrateLegacyShaclValidationConfig(legacy);
 
		await config.update(KEY, migrated, vscode.ConfigurationTarget.Workspace);
 
		Eif (globalValue !== undefined) {
			await config.update(KEY, undefined, vscode.ConfigurationTarget.Global);
		}
	}
}