Skip to content

Commit

Permalink
covert to multi-root workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
lonhutt committed Aug 20, 2024
1 parent b72ae4a commit e4fba45
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 123 deletions.
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"bugs": "https://github.com/salesforce/bazel-vscode-java/issues",
"preview": true,
"engines": {
"vscode": "^1.80.0"
"vscode": "^1.92.0"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -182,6 +182,11 @@
"command": "bazel.projectview.open",
"title": "Open the Bazel Project View file",
"category": "Bazel"
},
{
"command": "bazel.convert.workspace",
"title": "Convert to Multi-Root workspace",
"category": "Bazel"
}
],
"views": {
Expand Down Expand Up @@ -285,7 +290,7 @@
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"@vscode/test-electron": "^2.3.4",
"@vscode/vsce": "^2.22.0",
"@vscode/vsce": "^3.0.0",
"esbuild": "0.19.8",
"esbuild-plugin-eslint": "^0.3.7",
"esbuild-visualizer": "^0.4.1",
Expand Down
4 changes: 1 addition & 3 deletions src/bazelprojectparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ export async function getBazelProjectFile(): Promise<BazelProjectView> {

export function readBazelProject(bazelProjectFile: string): BazelProjectView {
return parseProjectFile({
root: workspace.workspaceFolders
? workspace.workspaceFolders[0].uri.fsPath
: './',
root: getWorkspaceRoot(),
imports: [bazelProjectFile],
projectView: {
directories: [],
Expand Down
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export namespace Commands {
export const BAZEL_TARGET_KILL = 'bazelTaskOutline.kill';

export const OPEN_BAZEL_PROJECT_FILE = 'bazel.projectview.open';

export const CONVERT_PROJECT_WORKSPACE = 'bazel.convert.workspace';
}

export function executeJavaLanguageServerCommand<T = unknown>(
Expand Down
129 changes: 12 additions & 117 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import { existsSync } from 'fs';
import { join } from 'path';
import { format } from 'util';
import {
ConfigurationTarget,
ExtensionContext,
FileType,
TextDocument,
Uri,
commands,
extensions,
tasks,
Expand All @@ -18,19 +15,19 @@ import {
getBazelTerminal,
} from './bazelLangaugeServerTerminal';
import { BazelTaskManager } from './bazelTaskManager';
import { getBazelProjectFile } from './bazelprojectparser';
import { Commands, executeJavaLanguageServerCommand } from './commands';
import { registerLSClient } from './loggingTCPServer';
import { ProjectViewManager } from './projectViewManager';
import { BazelRunTargetProvider } from './provider/bazelRunTargetProvider';
import { BazelTaskProvider } from './provider/bazelTaskProvider';
import { ExcludeConfig, FileWatcherExcludeConfig } from './types';
import {
getWorkspaceRoot,
initBazelProjectFile,
isBazelWorkspaceRoot,
} from './util';

const workspaceRoot = getWorkspaceRoot();
const workspaceRootName = workspaceRoot.split('/').reverse()[0];

export async function activate(context: ExtensionContext) {
// activates
Expand All @@ -40,7 +37,7 @@ export async function activate(context: ExtensionContext) {
// project view should reflect what's in the LS
// show any directories listed in the .bazelproject file
// fetch all projects loaded into LS and display those too
// show both .vscode and .eclipse folders
// show .eclipse folder
//

window.registerTreeDataProvider(
Expand Down Expand Up @@ -120,6 +117,13 @@ export async function activate(context: ExtensionContext) {
)
);

context.subscriptions.push(
commands.registerCommand(
Commands.CONVERT_PROJECT_WORKSPACE,
ProjectViewManager.covertToMultiRoot
)
);

// trigger a refresh of the tree view when any task get executed
tasks.onDidStartTask((_) => BazelRunTargetProvider.instance.refresh());
tasks.onDidEndTask((_) => BazelRunTargetProvider.instance.refresh());
Expand Down Expand Up @@ -214,117 +218,8 @@ function toggleBazelProjectSyncStatus(doc: TextDocument) {
}
}

async function syncProjectViewDirectories() {
if (workspaceRoot) {
BazelLanguageServerTerminal.debug('Syncing bazel project view');
const displayFolders = new Set<string>(['.eclipse', '.vscode']); // TODO bubble this out to a setting
try {
const bazelProjectFile = await getBazelProjectFile();
let viewAll = false;
if (bazelProjectFile.directories.includes('.')) {
viewAll = true;
} else {
bazelProjectFile.directories.forEach((d) => {
const dirRoot = d.split('/').filter((x) => x)[0];
displayFolders.add(dirRoot);
});
bazelProjectFile.targets.forEach((t) =>
displayFolders.add(
t.replace('//', '').replace(/:.*/, '').replace(/\/.*/, '')
)
);
}

workspace.fs.readDirectory(Uri.parse(workspaceRoot)).then(async (val) => {
const dirs = val.filter((x) => x[1] !== FileType.File).map((d) => d[0]);
const workspaceFilesConfig = workspace.getConfiguration('files');
const filesExclude = workspaceFilesConfig.get(
'exclude'
) as ExcludeConfig;
dirs.forEach(
(d) => (filesExclude[d] = viewAll ? false : !displayFolders.has(d))
);
await workspaceFilesConfig.update(
'exclude',
filesExclude,
ConfigurationTarget.Workspace
);

// if the updateFileWatcherExclusion setting is enabled
if (
workspace
.getConfiguration('bazel.projectview')
.get('updateFileWatcherExclusion')
) {
BazelLanguageServerTerminal.debug(
'updating files.watcherExclude setting'
);

const filesWatcherExclude =
workspaceFilesConfig.get<FileWatcherExcludeConfig>(
'watcherExclude',
{}
);

const fileWatcherKeys = Object.keys(filesWatcherExclude);
const hasOldEntry = fileWatcherKeys.filter(
(k) => k.includes('.vscode') && k.includes('.eclipse')
).length;

const fileWatcherExcludePattern = viewAll
? ''
: `**/!(${Array.from(displayFolders).join('|')})/**`;

if (viewAll) {
// if viewAll and existing config doesn't contain .vscode/.eclipse return
if (!hasOldEntry) {
return;
}
} else {
// if !viewAll and existing config contains identical entry return
if (fileWatcherKeys.includes(fileWatcherExcludePattern)) {
return;
}
}

// copy the old config obj, but remove any previous exclude based on the .bazelproject file
const newFilesWatcherExclude: FileWatcherExcludeConfig = {};
for (const val in filesWatcherExclude) {
if (!(val.includes('.eclipse') && val.includes('.vscode'))) {
newFilesWatcherExclude[val] = filesWatcherExclude[val];
}
}

if (fileWatcherExcludePattern) {
newFilesWatcherExclude[fileWatcherExcludePattern] = true;
}

// reload the workspace to make the updated file watcher exclusions take effect
workspaceFilesConfig
.update('watcherExclude', newFilesWatcherExclude)
.then((x) =>
window
.showWarningMessage(
'File watcher exclusions are out of date. Please reload the window to apply the change',
...['Reload', 'Ignore']
)
.then((opt) => {
if (opt === 'Reload') {
commands.executeCommand('workbench.action.reloadWindow');
}
if (opt === 'Ignore') {
workspace
.getConfiguration('bazel.projectview')
.update('updateFileWatcherExclusion', false);
}
})
);
}
});
} catch (err) {
throw new Error(`Could not read bazelproject file: ${err}`);
}
}
function syncProjectViewDirectories() {
ProjectViewManager.updateProjectView();
}

function openBazelProjectFile() {
Expand Down
Loading

0 comments on commit e4fba45

Please sign in to comment.