Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Nov 27, 2024
1 parent 32260f3 commit 18fc681
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"./schemas/PSRule-options.schema.json": [
"/tests/PSRule.Tests/PSRule.*.yml",
"/docs/scenarios/*/ps-rule.yaml",
"/ps-rule.yaml"
"/ps-rule.yaml",
"/ps-rule-ci.yaml"
],
"./schemas/PSRule-language.schema.json": [
"/**/**.Rule.yaml",
Expand Down
9 changes: 9 additions & 0 deletions docs/CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers

What's changed since pre-release v3.0.0-B0315:

- New features:
- VSCode extension set to use Microsoft verified name by @BernieWhite.
[#2636](https://github.com/microsoft/PSRule/issues/2636)
- General improvements:
- Expose format options to emitters by @BernieWhite.
[#1838](https://github.com/microsoft/PSRule/issues/1838)
- Added support for overriding options path from the default in VSCode by @BernieWhite.
[#2635](https://github.com/microsoft/PSRule/issues/2635)
- Engineering:
- Migrated VSCode extension into PSRule repository by @BernieWhite.
[#2615](https://github.com/microsoft/PSRule/issues/2615)
- VSCode extension will now sit side-by-side with the other core PSRule components.
- Bug fixes:
- Fixes path filtering of ignored files includes prefixed files by @BernieWhite.
[#2624](https://github.com/microsoft/PSRule/issues/2624)
Expand Down
4 changes: 3 additions & 1 deletion docs/updates/v3_0.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version: 3.0

### New home and identity

The Visual Studio Code (VSCode) extension for PSRule now lives side-by-side with the majority of the PSRule code base in `https://github.com/microsoft/PSRule`.
The Visual Studio Code (VSCode) extension for PSRule now lives side-by-side with core components of [PSRule on GitHub][1].

As part of this change we are now publishing the extension as a **verified** Microsoft extension with the ID `ps-rule.code`.

Expand All @@ -16,6 +16,8 @@ but also streamline how we deliver updates in the future.

Bringing together the code base is the first step in building an all improved rich experience in VSCode for PSRule.

[1]: https://github.com/microsoft/PSRule

### Runtime integration

Previously to use PSRule within VSCode,
Expand Down
4 changes: 4 additions & 0 deletions ps-rule-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ output:
culture:
- en-US

include:
module:
- PSRule.Rules.MSFT.OSS

format:
powershell_data:
type: []
Expand Down
27 changes: 27 additions & 0 deletions src/PSRule.CommandLine/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.CommandLine;

/// <summary>
/// String extension methods.
/// </summary>
public static class StringExtensions
{
private const char QUOTE = '"';
private const char APOSTROPHE = '\'';

/// <summary>
/// Remove paired single and double quotes from the start and end of a string.
/// </summary>
public static string? TrimQuotes(this string? value)
{
if (string.IsNullOrEmpty(value) || value.Length < 2)
return value;

if (value[0] == QUOTE && value[value.Length - 1] == QUOTE)
return value.Substring(1, value.Length - 2);

return value[0] == APOSTROPHE && value[value.Length - 1] == APOSTROPHE ? value.Substring(1, value.Length - 2) : value;
}
}
7 changes: 6 additions & 1 deletion src/PSRule.EditorServices/ClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,15 @@ private void AddModule()

private ClientContext GetClientContext(InvocationContext invocation)
{
var option = invocation.ParseResult.GetValueForOption(_Global_Option);
var option = invocation.ParseResult.GetValueForOption(_Global_Option).TrimQuotes();
var verbose = invocation.ParseResult.GetValueForOption(_Global_Verbose);
var debug = invocation.ParseResult.GetValueForOption(_Global_Debug);

if (!string.IsNullOrEmpty(option))
{
option = Environment.GetRootedPath(option);
}

option ??= Path.Combine(Environment.GetWorkingPath(), "ps-rule.yaml");

return new ClientContext
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule.Types/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static CultureInfo GetCurrentCulture()
/// <param name="normalize">When set to <c>true</c> the returned path uses forward slashes instead of backslashes.</param>
/// <param name="basePath">The base path to use. When <c>null</c> of unspecified, the current working path will be used.</param>
/// <returns>A absolute path.</returns>
internal static string GetRootedPath(string? path, bool normalize = false, string? basePath = null)
public static string GetRootedPath(string? path, bool normalize = false, string? basePath = null)
{
basePath ??= GetWorkingPath();
if (string.IsNullOrEmpty(path))
Expand Down
43 changes: 27 additions & 16 deletions src/code/commands/openOptionsFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as fse from 'fs-extra';
import { RelativePattern, TextDocument, Uri, window, workspace, WorkspaceFolder } from 'vscode';
import { logger } from '../logger';
import { getActiveOrFirstWorkspace } from '../utils';
import { configuration } from '../configuration';

/**
* Open an existing options file.
Expand All @@ -13,9 +14,10 @@ import { getActiveOrFirstWorkspace } from '../utils';
*/
export async function openOptionsFile(path: string | undefined): Promise<void> {
const optionFilePath = await getOptionFile(path);
if (optionFilePath === '' || optionFilePath === undefined) return;
const active: WorkspaceFolder | undefined = getActiveOrFirstWorkspace();
if (optionFilePath === '' || optionFilePath === undefined || !active) return;

const uri = Uri.file(optionFilePath);
const uri = Uri.joinPath(active.uri, optionFilePath);
logger.verbose(`Using options path ${uri.fsPath}`);
const exists = await fse.pathExists(uri.fsPath);
if (!exists)
Expand All @@ -29,22 +31,31 @@ async function getOptionFile(path: string | undefined): Promise<string | undefin
// Require an active workspace.
const active: WorkspaceFolder | undefined = getActiveOrFirstWorkspace();
if (!active) return Promise.resolve(undefined);
if (!(path === '' || path === undefined)) return Promise.resolve(path);
if (!(path === '' || path === undefined)) return path;

const workspaceUri: Uri = active.uri;

// Check if options file is overridden in settings.
const optionsPath = configuration.get().optionsPath;
let optionsPathUri: Uri | undefined = undefined;
if (optionsPath !== undefined && optionsPath !== '' && workspaceUri !== undefined) {
optionsPathUri = Uri.joinPath(workspaceUri, optionsPath);
}

const names: string[] = [];

if (optionsPathUri !== undefined && fse.existsSync(optionsPathUri.fsPath)) {
names.push(workspace.asRelativePath(optionsPathUri.fsPath));
}

// Search for any options files in the workspace.
const searchPattern = new RelativePattern(workspaceUri, '**/ps-rule.yaml');
return new Promise<string | undefined>((resolve) => {
workspace.findFiles(searchPattern).then(files => {
if (files === undefined || files.length === 0)
resolve(undefined);

const names: string[] = [];
files.forEach(item => {
names.push(item.path);
});
window.showQuickPick(names, { title: 'Options file' }).then(item => {
return resolve(item);
});
var files = await workspace.findFiles(searchPattern);
if (files !== undefined && files.length > 0) {
files.forEach(item => {
names.push(workspace.asRelativePath(item.path));
});
});
}

return await window.showQuickPick(names, { title: 'Options file', placeHolder: 'Select an options file' });
}

0 comments on commit 18fc681

Please sign in to comment.