| // Copyright 2025 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| import * as vscode from 'vscode'; |
| import { window } from 'vscode'; |
| import { Setup } from '../extension'; |
| import * as logger from '../logger'; |
| import { registerCommandWithAnalyticsEvent } from '../analytics/vscode_events'; |
| import { registerBuildCommands } from './build'; |
| import { FuchsiaDiagnostics } from './problem_matcher'; |
| import { PkgHistory, registerSetCommand, toList } from './set'; |
| |
| /** |
| * initialize fuchsia workflow interaction commands |
| */ |
| export function setUpWorkflowInteraction(ctx: vscode.ExtensionContext, setup: Setup) { |
| let collection = new FuchsiaDiagnostics(ctx.extension.packageJSON); |
| let pkgHistory = new PkgHistory(); |
| |
| registerBuildCommands(ctx, setup, collection); |
| registerSetCommand(ctx, setup, pkgHistory); |
| |
| /** Registers fx.serve with analytics */ |
| registerCommandWithAnalyticsEvent('fuchsia.fx.serve', () => { |
| void window.withProgress({ |
| location: vscode.ProgressLocation.Notification, |
| title: 'fx serve', |
| cancellable: true |
| }, async (progress, token) => { |
| let cmd = setup.fx.runAsync(['serve', '--background'], handleData, handleData); |
| |
| token.onCancellationRequested(() => { |
| console.log('User cancelled fx serve operation'); |
| cmd?.stop(); |
| }); |
| |
| let exitCode = await cmd?.exitCode; |
| return exitCode === 0 ? Promise.resolve() : Promise.reject(exitCode); |
| |
| /** callback to handle buffer from process */ |
| function handleData(buffer: Buffer) { |
| const msg = new TextDecoder().decode(buffer); |
| logger.info(msg, 'fx serve'); |
| if (msg.includes('resolve')) { |
| let match = msg.match(/(.*) ([\[\w+\]])\s+(.*)/); |
| if (match) { |
| progress.report({ message: `${match[match.length - 1]}` }); |
| } |
| } |
| } |
| }); |
| }); |
| |
| /** Registers repository.server.stop with analytics */ |
| registerCommandWithAnalyticsEvent('fuchsia.repository.server.stop', () => { |
| setup.ffx.runFfx(['repository', 'server', 'stop']) |
| .then(() => { |
| logger.info('Stopped the repository server', 'fx serve'); |
| }) |
| .catch((err) => { |
| logger.error(err); |
| }); |
| }); |
| |
| /** Register `fuchsia.fx.use` */ |
| registerCommandWithAnalyticsEvent('fuchsia.fx.use', () => { |
| setup.fx.runAsync(['use'], |
| (data) => { |
| void (async () => { |
| const msg = new TextDecoder().decode(data); |
| const list = toList(msg.split('\n')); |
| const selected = await window.showQuickPick(list, { |
| title: 'Select build directory' |
| }); |
| let dir = selected?.label; |
| |
| if (!dir?.includes('(current)')) { |
| /** callback to handle buffer from process */ |
| function handleData(buffer: Buffer) { |
| logger.info(new TextDecoder().decode(buffer), 'fx use'); |
| } |
| let cmd = setup.fx.runAsync(['use', `${dir}`], handleData, handleData); |
| |
| let exitCode = await cmd?.exitCode; |
| if (exitCode !== 0) { |
| logger.error(`fx use ${dir} exited with code ${exitCode}`, 'fx use'); |
| throw new Error('fx use returned a nonzero exit code'); |
| } |
| logger.info(`Build directory changed to: ${dir}`, 'fx use'); |
| } |
| })(); |
| }, () => { |
| // Ignore since err returns 'INFO: listing build directories:' |
| } |
| ); |
| }); |
| |
| } |