Skip to content

Commit

Permalink
pw_ide: Read legacy settings files
Browse files Browse the repository at this point in the history
Change-Id: I010e867941ad3c94f44ac303269e713360bb697d
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/259657
Reviewed-by: Asad Memon <[email protected]>
Commit-Queue: Chad Norvell <[email protected]>
Docs-Not-Needed: Chad Norvell <[email protected]>
Lint: Lint 🤖 <[email protected]>
  • Loading branch information
chadnorvell authored and CQ Bot Account committed Feb 13, 2025
1 parent 7cee22f commit a8a94ac
Show file tree
Hide file tree
Showing 17 changed files with 2,302 additions and 1,269 deletions.
3,406 changes: 2,155 additions & 1,251 deletions pw_ide/ts/pigweed-vscode/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pw_ide/ts/pigweed-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
"hjson": "^3.2.2",
"js-yaml": "^4.1.0",
"node_modules-path": "^2.0.8",
"yaml": "^2.7.0",
"zod": "^3.24.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion pw_ide/ts/pigweed-vscode/src/bazel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
settings,
ConfigAccessor,
bazel_codelens,
} from './settings';
} from './settings/vscode';

/**
* Is there a path to the given tool configured in VS Code settings?
Expand Down
2 changes: 1 addition & 1 deletion pw_ide/ts/pigweed-vscode/src/bazelWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
RefreshCallbackResult,
} from './refreshManager';

import { bazel_executable, settings, workingDir } from './settings';
import { bazel_executable, settings, workingDir } from './settings/vscode';

/** Regex for finding ANSI escape codes. */
const ANSI_PATTERN = new RegExp(
Expand Down
2 changes: 1 addition & 1 deletion pw_ide/ts/pigweed-vscode/src/clangd/activeFilesCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { Disposable } from '../disposables';
import { didInit, didUpdateActiveFilesCache } from '../events';
import logger from '../logging';
import { OK, RefreshCallback, RefreshManager } from '../refreshManager';
import { settings, workingDir } from '../settings';
import { settings, workingDir } from '../settings/vscode';

/** Parse a compilation database and get the source files in the build. */
async function parseForSourceFiles(target: Target): Promise<Set<string>> {
Expand Down
2 changes: 1 addition & 1 deletion pw_ide/ts/pigweed-vscode/src/clangd/bazel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as path from 'path';
import { getReliableBazelExecutable } from '../bazel';
import logger from '../logging';
import { getPigweedProjectRoot } from '../project';
import { settings, stringSettingFor, workingDir } from '../settings';
import { settings, stringSettingFor, workingDir } from '../settings/vscode';

export const clangdPath = () =>
path.join(workingDir.get(), 'bazel-bin', 'clangd');
Expand Down
2 changes: 1 addition & 1 deletion pw_ide/ts/pigweed-vscode/src/clangd/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as path from 'path';

import { glob } from 'glob';

import { settings, workingDir } from '../settings';
import { settings, workingDir } from '../settings/vscode';

const CDB_FILE_NAME = 'compile_commands.json' as const;

Expand Down
2 changes: 1 addition & 1 deletion pw_ide/ts/pigweed-vscode/src/clangd/vscCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { didChangeClangdConfig, didChangeTarget } from '../events';
import { launchTroubleshootingLink } from '../links';
import logger from '../logging';
import { RefreshManager } from '../refreshManager';
import { settingFor, settings, stringSettingFor } from '../settings';
import { settingFor, settings, stringSettingFor } from '../settings/vscode';

export async function setTargetWithClangd(
target: Target | undefined,
Expand Down
7 changes: 5 additions & 2 deletions pw_ide/ts/pigweed-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ import {
isBootstrapProject,
} from './project';
import { RefreshManager } from './refreshManager';
import { settings, workingDir } from './settings';
import { ClangdFileWatcher, SettingsFileWatcher } from './settingsWatcher';
import { settings, workingDir } from './settings/vscode';
import {
ClangdFileWatcher,
SettingsFileWatcher,
} from './settings/settingsWatcher';

import {
InactiveVisibilityStatusBarItem,
Expand Down
2 changes: 1 addition & 1 deletion pw_ide/ts/pigweed-vscode/src/inactiveFileDecoration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {

import logger from './logging';
import { getPigweedProjectRoot } from './project';
import { settings, workingDir } from './settings';
import { settings, workingDir } from './settings/vscode';

type DecorationsMap = Map<string, FileDecoration | undefined>;

Expand Down
2 changes: 1 addition & 1 deletion pw_ide/ts/pigweed-vscode/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as path from 'path';

import { glob } from 'glob';

import type { Settings, WorkingDirStore } from './settings';
import type { Settings, WorkingDirStore } from './settings/vscode';

const PIGWEED_JSON = 'pigweed.json' as const;

Expand Down
42 changes: 42 additions & 0 deletions pw_ide/ts/pigweed-vscode/src/settings/legacy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2025 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

import { loadLegacySettings } from './legacy';

describe('load legacy settings', () => {
test('successfully loads valid settings', async () => {
const settingsData = `config_title: pw_ide
default_target: pw_strict_host_clang_debug
compdb_gen_cmd: gn gen out`;

const settings = await loadLegacySettings(settingsData);
expect(settings).toHaveProperty('default_target');
expect(settings?.default_target).toBe('pw_strict_host_clang_debug');
});

test('returns null on invalid settings', async () => {
const settingsData = `default_target:
- pw_strict_host_clang_debug`;

const settings = await loadLegacySettings(settingsData);
expect(settings).toBeNull();
});

test('returns null on unparsable settings', async () => {
const settingsData = '- 2a05:4800:1:100::';

const settings = await loadLegacySettings(settingsData);
expect(settings).toBeNull();
});
});
83 changes: 83 additions & 0 deletions pw_ide/ts/pigweed-vscode/src/settings/legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2025 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

import * as fs from 'fs';
import * as fs_p from 'fs/promises';
import * as path from 'path';

import { parse as parseYaml } from 'yaml';
import { z } from 'zod';
import { workingDir } from './vscode';

const DEFAULT_BUILD_DIR_NAME = 'out';
const DEFAULT_TARGET_INFERENCE = '?';
const DEFAULT_WORKSPACE_ROOT = workingDir.get();
const DEFAULT_WORKING_DIR = workingDir.get();
const LEGACY_SETTINGS_FILE_PATH = path.join(workingDir.get(), '.pw_ide.yaml');

const legacySettingsSchema = z.object({
cascade_settings: z.boolean().default(false),
clangd_alternate_path: z.string().optional(),
clangd_additional_query_drivers: z.array(z.string()).default([]),
compdb_gen_cmd: z.string().optional(),
compdb_search_paths: z
.array(z.array(z.string()))
.default([[DEFAULT_BUILD_DIR_NAME, DEFAULT_TARGET_INFERENCE]]),
default_target: z.string().optional(),
// TODO(chadnorvell): Test that this works with a non-default value.
workspace_root: z.string().default(DEFAULT_WORKSPACE_ROOT),
// needs some thought
sync: z.string().optional(),
targets_exclude: z.array(z.string()).default([]),
targets_include: z.array(z.string()).default([]),
target_inference: z.string().default('?'),
// TODO(chadnorvell): Test that this works with a non-default value.
working_dir: z.string().default(DEFAULT_WORKING_DIR),
});

type LegacySettings = z.infer<typeof legacySettingsSchema>;

export async function loadLegacySettings(
settingsData: string,
onFailureReturnDefault: true,
): Promise<LegacySettings>;

export async function loadLegacySettings(
settingsData: string,
onFailureReturnDefault?: false,
): Promise<LegacySettings | null>;

export async function loadLegacySettings(
settingsData: string,
onFailureReturnDefault = false,
): Promise<LegacySettings | null> {
const fallback = onFailureReturnDefault
? legacySettingsSchema.parse({})
: null;

const parsed = parseYaml(settingsData);
if (!parsed) return fallback;

try {
return legacySettingsSchema.parse(parsed);
} catch {
return fallback;
}
}

export async function loadLegacySettingsFile(): Promise<string | null> {
if (!fs.existsSync(LEGACY_SETTINGS_FILE_PATH)) return null;
const data = await fs_p.readFile(LEGACY_SETTINGS_FILE_PATH);
return data.toString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import * as vscode from 'vscode';
import { RelativePattern } from 'vscode';

import { Disposable } from './disposables';
import { ClangdActiveFilesCache } from './clangd';
import { getSettingsData, syncSettingsSharedToProject } from './configParsing';
import logger from './logging';
import { settings } from './settings';
import { Disposable } from '../disposables';
import { ClangdActiveFilesCache } from '../clangd';
import { getSettingsData, syncSettingsSharedToProject } from '../configParsing';
import logger from '../logging';
import { settings } from './vscode';

export class SettingsFileWatcher extends Disposable {
constructor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import * as vscode from 'vscode';

import logger from './logging';
import logger from '../logging';

interface Setting<T> {
(): T | undefined;
Expand Down
2 changes: 1 addition & 1 deletion pw_ide/ts/pigweed-vscode/src/statusBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from './events';

import { RefreshStatus } from './refreshManager';
import { settings } from './settings';
import { settings } from './settings/vscode';

const DEFAULT_TARGET_TEXT = 'Select a Target';
const ICON_IDLE = '$(check)';
Expand Down
2 changes: 1 addition & 1 deletion pw_ide/ts/pigweed-vscode/src/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import * as vscode from 'vscode';

import { vendoredBazeliskPath } from './bazel';
import logger from './logging';
import { bazel_executable, settings } from './settings';
import { bazel_executable, settings } from './settings/vscode';

type InitScript = 'activate' | 'bootstrap';

Expand Down

0 comments on commit a8a94ac

Please sign in to comment.