Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

covert to multi-root workspace #122

Merged
merged 1 commit into from
Aug 26, 2024
Merged
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
922 changes: 808 additions & 114 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
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
131 changes: 13 additions & 118 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 @@ -39,8 +36,8 @@ export async function activate(context: ExtensionContext) {
// register TCP port with LS
// 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
// fetch all projects loaded into LS and display those as well
// show .eclipse folder
lonhutt marked this conversation as resolved.
Show resolved Hide resolved
//

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
Loading