Skip to content

Commit

Permalink
new: Generate yaml schemas config.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Jul 15, 2024
1 parent a4301b8 commit f00e23d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/vscode-extension/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.13.0

- Added a new command that will generate local `yaml.schemas` settings.

## 0.12.0

- Requires VS Code ^1.77.0.
Expand Down
6 changes: 5 additions & 1 deletion packages/vscode-extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"private": true,
"version": "0.12.1",
"version": "0.13.0",
"name": "moon-console",
"publisher": "moonrepo",
"displayName": "moon console",
Expand Down Expand Up @@ -104,6 +104,10 @@
"command": "moon.openSettings",
"title": "moon: Open settings"
},
{
"command": "moon.appendSchemasConfig",
"title": "moon: Append YAML schemas configuration to settings"
},
{
"command": "moon.runTaskByInput",
"title": "moon: Run one or many tasks"
Expand Down
49 changes: 49 additions & 0 deletions packages/vscode-extension/src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fs from 'fs';
import path from 'path';
import { satisfies } from 'semver';
import vscode, { ShellExecution, Task, TaskScope } from 'vscode';
import { GraphVisualizerView } from './graphVisualizerView';
import type { Workspace } from './workspace';
Expand Down Expand Up @@ -90,3 +93,49 @@ export async function viewActionGraph(context: vscode.ExtensionContext, workspac
export async function viewProjectGraph(context: vscode.ExtensionContext, workspace: Workspace) {
await new GraphVisualizerView(context, workspace, 'project-graph').renderPanel();
}

export async function appendSchemasConfig(context: vscode.ExtensionContext, workspace: Workspace) {
const version = await workspace.getMoonVersion();

if (satisfies(version, '<1.27.0')) {
await vscode.window.showErrorMessage('YAML schemas require moon >= 1.27.0');

return;
}

const vscodeDir = path.join(workspace.folder!.uri.fsPath, '.vscode');
const settingsPath = path.join(vscodeDir, 'settings.json');

const settings = (
fs.existsSync(settingsPath) ? JSON.parse(fs.readFileSync(settingsPath, 'utf8')) : {}
) as Record<string, unknown>;

const schemas =
typeof settings['yaml.schemas'] === 'object' && !!settings['yaml.schemas']
? settings['yaml.schemas']
: {};

settings['yaml.schemas'] = {
...schemas,
'./.moon/cache/schemas/project.json': ['**/moon.yml'],
'./.moon/cache/schemas/tasks.json': [
path.join(workspace.rootPrefix, '.moon/tasks.yml'),
path.join(workspace.rootPrefix, '.moon/tasks/**/*.yml'),
],
'./.moon/cache/schemas/template.json': ['**/template.yml'],
'./.moon/cache/schemas/toolchain.json': [
path.join(workspace.rootPrefix, '.moon/toolchain.yml'),
],
'./.moon/cache/schemas/workspace.json': [
path.join(workspace.rootPrefix, '.moon/workspace.yml'),
],
};

if (!fs.existsSync(vscodeDir)) {
fs.mkdirSync(vscodeDir);
}

fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));

await vscode.window.showInformationMessage('Added `yaml.schemas` to `.vscode/settings.json`');
}
5 changes: 4 additions & 1 deletion packages/vscode-extension/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import vscode from 'vscode';
import { runTaskByInput, viewActionGraph, viewProjectGraph } from './commands';
import { appendSchemasConfig, runTaskByInput, viewActionGraph, viewProjectGraph } from './commands';
import { LastRunProvider } from './lastRunView';
import { ProjectsProvider } from './projectsView';
import { Workspace } from './workspace';
Expand All @@ -22,6 +22,9 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('moon.viewProjectGraph', () =>
viewProjectGraph(context, workspace),
),
vscode.commands.registerCommand('moon.appendSchemasConfig', () =>
appendSchemasConfig(context, workspace),
),

// Create a tree view for all moon projects
vscode.window.createTreeView('moonProjects', {
Expand Down

0 comments on commit f00e23d

Please sign in to comment.