Skip to content

Commit

Permalink
Added VSCode support and style detection microsoft#731 microsoft#732
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite committed May 29, 2021
1 parent 89e2eb2 commit b7a2e61
Show file tree
Hide file tree
Showing 24 changed files with 870 additions and 234 deletions.
12 changes: 11 additions & 1 deletion docs/CHANGELOG-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ See [upgrade notes][upgrade-notes] for helpful information when upgrading from p

## Unreleased

What's changed since pre-release v1.4.0-B2105032:

- General improvements:
- Source location of objects from the pipeline are read from properties. [#729](https://github.com/microsoft/PSRule/issues/729)
- Assert output improvements:
- Added support for Visual Studio Code with `VisualStudioCode` style. [#731](https://github.com/microsoft/PSRule/issues/731)
- Updated output format provides support for problem matchers in task output.
- Automatically detect output style from environment variables. [#732](https://github.com/microsoft/PSRule/issues/732)
- _Assert-PSRule_ now defaults to `Detect` instead of `Client`.
- See [about_PSRule_Options] for details.

## v1.4.0-B2105032 (pre-release)

What's changed since pre-release v1.4.0-B2105019:
Expand All @@ -24,7 +35,6 @@ What's changed since pre-release v1.4.0-B2105004:
- General improvements:
- Source location of objects are included in results.
- Source location of objects from JSON and YAML input files are read automatically. [#624](https://github.com/microsoft/PSRule/issues/624)
- Source location of objects from the pipeline are read from properties. [#729](https://github.com/microsoft/PSRule/issues/729)
- Improved support for version constraints by:
- Constraints can include prerelease versions of other matching versions. [#714](https://github.com/microsoft/PSRule/issues/714)
- Constraints support using a `@prerelease` or `@pre` to include prerelease versions. [#717](https://github.com/microsoft/PSRule/issues/717)
Expand Down
21 changes: 16 additions & 5 deletions docs/commands/PSRule/en-US/Assert-PSRule.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,21 @@ Configures the style that results will be presented in.

The following styles are available:

- Client - Output is written to the host directly in green/ red to indicate outcome. This is the default.
- Plain - Output is written to the host as a plain string.
- AzurePipelines - Output is written with commands that can be interpreted by Azure Pipelines.
- GitHubActions - Output is written with commands that can be interpreted by GitHub Actions.
- Client - Output is written to the host directly in green/ red to indicate outcome.
- Plain - Output is written as an unformatted string.
This option can be redirected to a file.
- AzurePipelines - Output is written for integration Azure Pipelines.
- GitHubActions - Output is written for integration GitHub Actions.
- VisualStudioCode - Output is written for integration with Visual Studio Code.
- Detect - Output style will be detected by checking the environment variables.
This is the default.

Detect uses the following logic:

1. If the `TF_BUILD` environment variable is set to `true`, `AzurePipelines` will be used.
2. If the `GITHUB_ACTIONS` environment variable is set to `true`, `GitHubActions` will be used.
3. If the `TERM_PROGRAM` environment variable is set to `vscode`, `VisualStudioCode` will be used.
4. Use `Client`.

Each of these styles outputs to the host. To capture output as a string redirect the information stream.
For example: `6>&1`
Expand All @@ -372,7 +383,7 @@ For example: `6>&1`
Type: OutputStyle
Parameter Sets: (All)
Aliases:
Accepted values: Client, Plain, AzurePipelines, GitHubActions
Accepted values: Client, Plain, AzurePipelines, GitHubActions, VisualStudioCode, Detect

Required: False
Position: Named
Expand Down
33 changes: 27 additions & 6 deletions docs/commands/PSRule/en-US/New-PSRuleOption.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ New-PSRuleOption [[-Path] <String>] [-Configuration <ConfigurationOption>]
[-InputIgnoreGitPath <Boolean>] [-ObjectPath <String>] [-InputTargetType <String[]>]
[-InputPathIgnore <String[]>] [-LoggingLimitDebug <String[]>] [-LoggingLimitVerbose <String[]>]
[-LoggingRuleFail <OutcomeLogStream>] [-LoggingRulePass <OutcomeLogStream>] [-OutputAs <ResultFormat>]
[-OutputCulture <String[]>] [-OutputEncoding <OutputEncoding>] [-OutputFormat <OutputFormat>]
[-OutputOutcome <RuleOutcome>] [-OutputPath <String>] [-OutputStyle <OutputStyle>] [<CommonParameters>]
[-OutputBanner <BannerFormat>] [-OutputCulture <String[]>] [-OutputEncoding <OutputEncoding>]
[-OutputFormat <OutputFormat>] [-OutputOutcome <RuleOutcome>] [-OutputPath <String>]
[-OutputStyle <OutputStyle>] [<CommonParameters>]
```

### FromOption
Expand All @@ -41,8 +42,9 @@ New-PSRuleOption [-Option] <PSRuleOption> [-Configuration <ConfigurationOption>]
[-InputIgnoreGitPath <Boolean>] [-ObjectPath <String>] [-InputTargetType <String[]>]
[-InputPathIgnore <String[]>] [-LoggingLimitDebug <String[]>] [-LoggingLimitVerbose <String[]>]
[-LoggingRuleFail <OutcomeLogStream>] [-LoggingRulePass <OutcomeLogStream>] [-OutputAs <ResultFormat>]
[-OutputCulture <String[]>] [-OutputEncoding <OutputEncoding>] [-OutputFormat <OutputFormat>]
[-OutputOutcome <RuleOutcome>] [-OutputPath <String>] [-OutputStyle <OutputStyle>] [<CommonParameters>]
[-OutputBanner <BannerFormat>] [-OutputCulture <String[]>] [-OutputEncoding <OutputEncoding>]
[-OutputFormat <OutputFormat>] [-OutputOutcome <RuleOutcome>] [-OutputPath <String>]
[-OutputStyle <OutputStyle>] [<CommonParameters>]
```

### FromDefault
Expand All @@ -56,8 +58,9 @@ New-PSRuleOption [-Default] [-Configuration <ConfigurationOption>] [-SuppressTar
[-Format <InputFormat>] [-InputIgnoreGitPath <Boolean>] [-ObjectPath <String>] [-InputTargetType <String[]>]
[-InputPathIgnore <String[]>] [-LoggingLimitDebug <String[]>] [-LoggingLimitVerbose <String[]>]
[-LoggingRuleFail <OutcomeLogStream>] [-LoggingRulePass <OutcomeLogStream>] [-OutputAs <ResultFormat>]
[-OutputCulture <String[]>] [-OutputEncoding <OutputEncoding>] [-OutputFormat <OutputFormat>]
[-OutputOutcome <RuleOutcome>] [-OutputPath <String>] [-OutputStyle <OutputStyle>] [<CommonParameters>]
[-OutputBanner <BannerFormat>] [-OutputCulture <String[]>] [-OutputEncoding <OutputEncoding>]
[-OutputFormat <OutputFormat>] [-OutputOutcome <RuleOutcome>] [-OutputPath <String>]
[-OutputStyle <OutputStyle>] [<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -593,6 +596,24 @@ Accept pipeline input: False
Accept wildcard characters: False
```

### -OutputBanner

Sets the option `Output.Banner`.
The `Output.Banner` option configure information displayed with PSRule banner.
This option is only applicable when using `Assert-PSRule` cmdlet.

```yaml
Type: BannerFormat
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: Default
Accept pipeline input: False
Accept wildcard characters: False
```

### -OutputCulture

Sets the option `Output.Culture`.
Expand Down
25 changes: 22 additions & 3 deletions docs/commands/PSRule/en-US/Set-PSRuleOption.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ Set-PSRuleOption [[-Path] <String>] [-Option <PSRuleOption>] [-PassThru] [-Force
[-NotProcessedWarning <Boolean>] [-Format <InputFormat>] [-InputIgnoreGitPath <Boolean>]
[-ObjectPath <String>] [-InputPathIgnore <String[]>] [-InputTargetType <String[]>]
[-LoggingLimitDebug <String[]>] [-LoggingLimitVerbose <String[]>] [-LoggingRuleFail <OutcomeLogStream>]
[-LoggingRulePass <OutcomeLogStream>] [-OutputAs <ResultFormat>] [-OutputCulture <String[]>]
[-OutputEncoding <OutputEncoding>] [-OutputFormat <OutputFormat>] [-OutputOutcome <RuleOutcome>]
[-OutputPath <String>] [-OutputStyle <OutputStyle>] [-WhatIf] [-Confirm] [<CommonParameters>]
[-LoggingRulePass <OutcomeLogStream>] [-OutputAs <ResultFormat>] [-OutputBanner <BannerFormat>]
[-OutputCulture <String[]>] [-OutputEncoding <OutputEncoding>] [-OutputFormat <OutputFormat>]
[-OutputOutcome <RuleOutcome>] [-OutputPath <String>] [-OutputStyle <OutputStyle>] [-WhatIf] [-Confirm]
[<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -493,6 +494,24 @@ Accept pipeline input: False
Accept wildcard characters: False
```

### -OutputBanner

Sets the option `Output.Banner`.
The `Output.Banner` option configure information displayed with PSRule banner.
This option is only applicable when using `Assert-PSRule` cmdlet.

```yaml
Type: BannerFormat
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: Default
Accept pipeline input: False
Accept wildcard characters: False
```

### -OutputCulture

Sets the option `Output.Culture`.
Expand Down
21 changes: 16 additions & 5 deletions docs/concepts/PSRule/en-US/about_PSRule_Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1653,10 +1653,21 @@ If specified, the `-Style` parameter takes precedence, over this option.

The following styles are available:

- Client - Output is written to the host directly in green/ red to indicate outcome. This is the default.
- Plain - Output is written as an unformatted string. This option can be redirected to a file.
- AzurePipelines - Output is written with commands that can be interpreted by Azure Pipelines.
- GitHubActions - Output is written with commands that can be interpreted by GitHub Actions.
- Client - Output is written to the host directly in green/ red to indicate outcome.
- Plain - Output is written as an unformatted string.
This option can be redirected to a file.
- AzurePipelines - Output is written for integration Azure Pipelines.
- GitHubActions - Output is written for integration GitHub Actions.
- VisualStudioCode - Output is written for integration with Visual Studio Code.
- Detect - Output style will be detected by checking the environment variables.
This is the default.

Detect uses the following logic:

1. If the `TF_BUILD` environment variable is set to `true`, `AzurePipelines` will be used.
2. If the `GITHUB_ACTIONS` environment variable is set to `true`, `GitHubActions` will be used.
3. If the `TERM_PROGRAM` environment variable is set to `vscode`, `VisualStudioCode` will be used.
4. Use `Client`.

This option can be specified using:

Expand Down Expand Up @@ -2067,7 +2078,7 @@ output:
encoding: Default
format: None
outcome: Processed
style: Client
style: Detect

# Configure rule suppression
suppression: { }
Expand Down
51 changes: 50 additions & 1 deletion docs/upgrade-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,56 @@

This document contains notes to help upgrade from previous versions of PSRule.

## Upgrade to v0.22.0 from v1.0.0
## Upgrading to v1.4.0

Follow these notes to upgrade from PSRule version _v1.3.0_ to _v1.4.0_.

### Change in default output styles

Previously in PSRule _v1.3.0_ and prior the default style when using `Assert-PSRule` was `Client`.
From _v1.4.0_ PSRule now defaults to `Detect`.

The `Detect` output style falls back to `Client` however may detect one of the following styles instead:

- `AzurePipelines` - Output is written for integration Azure Pipelines.
- `GitHubActions` - Output is written for integration GitHub Actions.
- `VisualStudioCode` - Output is written for integration with Visual Studio Code.

Detect uses the following logic:

1. If the `TF_BUILD` environment variable is set to `true`, `AzurePipelines` will be used.
2. If the `GITHUB_ACTIONS` environment variable is set to `true`, `GitHubActions` will be used.
3. If the `TERM_PROGRAM` environment variable is set to `vscode`, `VisualStudioCode` will be used.
4. Use `Client`.

To force usage of the `Client` output style set the `Output.Style` option.
For example:

```yaml
# YAML: Using the output/style property
output:
style: Client
```

```bash
# Bash: Using environment variable
export PSRULE_OUTPUT_STYLE=Client
```

```yaml
# GitHub Actions: Using environment variable
env:
PSRULE_OUTPUT_STYLE: Client
```

```yaml
# Azure Pipelines: Using environment variable
variables:
- name: PSRULE_OUTPUT_STYLE
value: Client
```

## Upgrading to v1.0.0

Follow these notes to upgrade from PSRule version _v0.22.0_ to _v1.0.0_.

Expand Down
10 changes: 6 additions & 4 deletions schemas/PSRule-options.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -421,15 +421,17 @@
"style": {
"type": "string",
"title": "Output Style",
"description": "The style that results will be presented in. The default is Client.",
"markdownDescription": "The style that results will be presented in. The default is `Client`. [See help](https://microsoft.github.io/PSRule/concepts/PSRule/en-US/about_PSRule_Options.html#outputstyle)",
"description": "The style that results will be presented in. The default is Detect.",
"markdownDescription": "The style that results will be presented in. The default is `Detect`. [See help](https://microsoft.github.io/PSRule/concepts/PSRule/en-US/about_PSRule_Options.html#outputstyle)",
"enum": [
"Client",
"Plain",
"AzurePipelines",
"GitHubActions"
"GitHubActions",
"VisualStudioCode",
"Detect"
],
"default": "Client"
"default": "Detect"
}
},
"additionalProperties": false
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/Common/JsonConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private static PSObject ReadObject(JsonReader reader, bool bindTargetInfo)
if (bindTargetInfo)
{
result.UseTargetInfo(out PSRuleTargetInfo info);
info.WithSource(lineNumber, linePosition);
info.SetSource(lineNumber, linePosition);
}
return result;
}
Expand Down
57 changes: 57 additions & 0 deletions src/PSRule/Common/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;

namespace PSRule
{
Expand All @@ -11,5 +12,61 @@ public static bool IsUri(this string s)
{
return s.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || s.StartsWith("https://", StringComparison.OrdinalIgnoreCase);
}

public static string[] SplitSemantic(this string s, int width = 80)
{
if (s == null)
return null;

if (s.Length <= 80)
return new string[] { s };

var result = new List<string>();
var pos = 0;
while (pos < s.Length)
{
var i = pos + width - 1;
if (i >= s.Length)
i = s.Length - 1;

var breaks = s.IndexOfAny(new char[] { '\r', '\n' }, pos, i - pos);
if (breaks > -1)
i = breaks;
else
{
while (!IsSemanticStopChar(s[i]) && i > pos)
i--;
}

if (i == pos)
{
// move forward
}
if (char.IsPunctuation(s[i]))
i++;

while (i > pos && i < s.Length && IsLineBreak(s[i]))
i--;

if (pos != i)
result.Add(s.Substring(pos, i - pos));

while (i < s.Length && IsLineBreak(s[i]))
i++;

pos = i + 1;
}
return result.ToArray();
}

private static bool IsSemanticStopChar(char c)
{
return char.IsWhiteSpace(c) || (char.IsPunctuation(c) && char.GetUnicodeCategory(c) != System.Globalization.UnicodeCategory.DashPunctuation);
}

private static bool IsLineBreak(char c)
{
return char.GetUnicodeCategory(c) == System.Globalization.UnicodeCategory.LineSeparator;
}
}
}
2 changes: 1 addition & 1 deletion src/PSRule/Common/YamlConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ bool INodeDeserializer.Deserialize(IParser reader, Type expectedType, Func<IPars
if (value is PSObject pso)
{
pso.UseTargetInfo(out PSRuleTargetInfo info);
info.WithSource(lineNumber, linePosition);
info.SetSource(lineNumber, linePosition);
value = new PSObject[] { pso };
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/PSRule/Configuration/OutputOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ namespace PSRule.Configuration
public sealed class OutputOption : IEquatable<OutputOption>
{
private const ResultFormat DEFAULT_AS = ResultFormat.Detail;
private const BannerFormat DEFAULT_BANNER = BannerFormat.Default;
private const OutputEncoding DEFAULT_ENCODING = OutputEncoding.Default;
private const OutputFormat DEFAULT_FORMAT = OutputFormat.None;
private const RuleOutcome DEFAULT_OUTCOME = RuleOutcome.Processed;
private const OutputStyle DEFAULT_STYLE = OutputStyle.Client;
private const BannerFormat DEFAULT_BANNER = BannerFormat.Default;
private const OutputStyle DEFAULT_STYLE = OutputStyle.Detect;

internal static readonly OutputOption Default = new OutputOption
{
Expand Down
12 changes: 11 additions & 1 deletion src/PSRule/Configuration/OutputStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public enum OutputStyle
/// <summary>
/// Text written to output stream, with fails marked for GitHub Actions.
/// </summary>
GitHubActions = 3
GitHubActions = 3,

/// <summary>
/// Text is written to output stream formatted for Visual Studio Code.
/// </summary>
VisualStudioCode = 4,

/// <summary>
/// Automatically detect the style to use.
/// </summary>
Detect = 255
}
}

0 comments on commit b7a2e61

Please sign in to comment.