Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,33 @@ const DOCUMENT_SELECTOR = [

export async function activate(context: vscode.ExtensionContext) {
telemetryHelper.setTelemetry('isActivationEvent', 'true');
logger.log('Activating extension...', 'ExtensionActivation');

await telemetryHelper.callWithTelemetryAndErrorHandling('azurePipelines.activate', async () => {
await activateYmlContributor(context);
});

context.subscriptions.push(telemetryHelper);

logger.log('Extension has been activated!', 'ExtensionActivated');
logger.log('Extension has been activated!', 'ExtensionActivation');
return schemaContributor;
}

async function activateYmlContributor(context: vscode.ExtensionContext) {
const serverOptions: languageclient.ServerOptions = getServerOptions(context);
const clientOptions: languageclient.LanguageClientOptions = getClientOptions();
const client = new languageclient.LanguageClient(LANGUAGE_IDENTIFIER, 'Azure Pipelines Language', serverOptions, clientOptions);
const client = new languageclient.LanguageClient(LANGUAGE_IDENTIFIER, 'Azure Pipelines Language Server', serverOptions, clientOptions);

const disposable = client.start();
context.subscriptions.push(disposable);

logger.log('Waiting for server to start...', 'ExtensionActivation');

// If this throws, the telemetry event in activate() will catch & log it
await client.onReady();

logger.log('Server started', 'ExtensionActivation');

// Fired whenever the server is about to validate a YAML file (e.g. on content change),
// and allows us to return a custom schema to use for validation.
client.onRequest(CUSTOM_SCHEMA_REQUEST, (resource: string) => {
Expand Down Expand Up @@ -75,6 +81,7 @@ async function activateYmlContributor(context: vscode.ExtensionContext) {

// Load the schema if we were activated because an Azure Pipelines file.
if (vscode.window.activeTextEditor?.document.languageId === LANGUAGE_IDENTIFIER) {
logger.log('Active file is Azure Pipelines, loading schema', 'ExtensionActivation');
await loadSchema(context, client);
}

Expand Down Expand Up @@ -119,18 +126,25 @@ async function loadSchema(
context: vscode.ExtensionContext,
client: languageclient.LanguageClient,
workspaceFolder?: vscode.WorkspaceFolder): Promise<void> {
logger.log('Loading schema...', 'LoadSchema');
if (workspaceFolder === undefined) {
logger.log('Detecting workspace folder', 'LoadSchema');
const textDocument = vscode.window.activeTextEditor?.document;
if (textDocument?.languageId !== LANGUAGE_IDENTIFIER) {
logger.log('Active file not Azure Pipelines', 'LoadSchema');
return;
}

workspaceFolder = vscode.workspace.getWorkspaceFolder(textDocument.uri);
}

logger.log(`Using workspace folder '${workspaceFolder?.name}'`, 'LoadSchema');
const schemaFilePath = await locateSchemaFile(context, workspaceFolder);
const schema = getSchemaAssociation(schemaFilePath);

logger.log(`Sending schema '${schemaFilePath}' to server`, 'LoadSchema');
client.sendNotification(SchemaAssociationNotification.type, schema);
logger.log('Sent schema to server', 'LoadSchema');
}

function getServerOptions(context: vscode.ExtensionContext): languageclient.ServerOptions {
Expand Down
1 change: 1 addition & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export function log(message: string, event?: string): void {

logMessage += message;
outputChannel.appendLine(logMessage);
console.log(logMessage);
}
10 changes: 5 additions & 5 deletions src/schema-association-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ export async function locateSchemaFile(
// TODO: Support auto-detection for Azure Pipelines files outside of the workspace.
if (workspaceFolder !== undefined) {
try {
logger.log(`Detecting schema for workspace folder ${workspaceFolder.name}`, 'SchemaDetection');
logger.log(`Detecting schema for workspace folder '${workspaceFolder.name}'`, 'SchemaDetection');
schemaUri = await autoDetectSchema(context, workspaceFolder);
if (schemaUri) {
logger.log(
`Detected schema for workspace folder ${workspaceFolder.name}: ${schemaUri.path}`,
`Detected schema for workspace folder '${workspaceFolder.name}': ${schemaUri.path}`,
'SchemaDetection');
return schemaUri.path;
}
} catch (error) {
// Well, we tried our best. Fall back to the predetermined schema paths.
// TODO: Re-throw error once we're more confident in the schema detection.
logger.log(
`Error auto-detecting schema for workspace folder ${workspaceFolder.name}: ${String(error)}`,
`Error auto-detecting schema for workspace folder '${workspaceFolder.name}': ${String(error)}`,
'SchemaDetection');
}
}
Expand All @@ -89,7 +89,7 @@ export async function locateSchemaFile(
}

logger.log(
`Using hardcoded schema for workspace folder ${workspaceFolder?.name ?? 'ANONYMOUS_WORKSPACE'}: ${schemaUri.path}`,
`Using hardcoded schema for workspace folder '${workspaceFolder?.name ?? 'ANONYMOUS_WORKSPACE'}': ${schemaUri.path}`,
'SchemaDetection');

// TODO: We should update getSchemaAssociations so we don't need to constantly
Expand Down Expand Up @@ -347,7 +347,7 @@ async function autoDetectSchema(
export async function getAzureDevOpsSession(context: vscode.ExtensionContext, options?: vscode.AuthenticationGetSessionOptions): Promise<vscode.AuthenticationSession | undefined> {
const rawTenantId = vscode.workspace.getConfiguration('azure-pipelines').get<string>('tenant', '');
const tenantId = rawTenantId.trim().length > 0 ? rawTenantId : undefined;
logger.log(`Getting session for tenant ${tenantId ?? 'default'}`, 'SchemaDetection');
logger.log(`Getting session for tenant '${tenantId ?? 'default'}'`, 'SchemaDetection');
const azureDevOpsSession = await vscode.authentication.getSession(
'microsoft',
tenantId ? [...AZURE_DEVOPS_SCOPES, `VSCODE_TENANT:${tenantId}`] : AZURE_DEVOPS_SCOPES,
Expand Down