Skip to content

Commit

Permalink
Code quality updates (microsoft#1710)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Dec 21, 2023
1 parent 8b21437 commit 71f9762
Show file tree
Hide file tree
Showing 323 changed files with 45,788 additions and 41,187 deletions.
4 changes: 3 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ indent_size = 2
[*.cs]

# Code style defaults
csharp_using_directive_placement = outside_namespace:suggestion
csharp_using_directive_placement = outside_namespace:error
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
dotnet_sort_system_directives_first = true
dotnet_style_readonly_field = true:suggestion
csharp_style_namespace_declarations = file_scoped:error

# License header
file_header_template = Copyright (c) Microsoft Corporation.\nLicensed under the MIT License.
dotnet_diagnostic.IDE0073.severity = error

# Suggest more modern language features when available
dotnet_style_object_initializer = true:suggestion
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/analyze.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
sarif_file: reports/ps-rule-results.sarif

- name: Upload results
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: always()
with:
name: PSRule-Sarif
Expand Down Expand Up @@ -78,7 +78,7 @@ jobs:
sarif_file: devskim-results.sarif

- name: Upload results
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: always()
with:
name: DevSkim-Sarif
Expand Down Expand Up @@ -110,7 +110,7 @@ jobs:
id: codeql-analyze

- name: Upload results
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: always()
with:
name: CodeQL-Sarif
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
run: Invoke-Build -Configuration Release -AssertStyle GitHubActions

- name: Upload module
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: Module
path: ./out/modules/PSRule/*
Expand All @@ -63,10 +63,10 @@ jobs:
# if-no-files-found: error

- name: Upload PSRule Results
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: always()
with:
name: Module.PSRule.TestResults
name: Results-PSRule
path: ./reports/ps-rule*.xml
retention-days: 3
if-no-files-found: error
Expand Down Expand Up @@ -125,7 +125,7 @@ jobs:
run: ./scripts/pipeline-deps.ps1

- name: Download module
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: Module
path: ./out/modules/PSRule
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ jobs:
uses: actions/configure-pages@v4

- name: Upload artifact
uses: actions/upload-pages-artifact@v2
uses: actions/upload-pages-artifact@v3
with:
path: '.'

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v3
uses: actions/deploy-pages@v4
97 changes: 48 additions & 49 deletions src/PSRule.Badges/BadgeResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,64 @@
using System.Reflection;
using Newtonsoft.Json;

namespace PSRule.Badges
namespace PSRule.Badges;

/// <summary>
/// A helper class for working with badge resources.
/// </summary>
internal static class BadgeResources
{
private const string DEFAULT_CULTURE_RESOURCE = "PSRule.Badges.Resources.en.json";

private static char[] _Char;
private static double[] _Width;

/// <summary>
/// A helper class for working with badge resources.
/// Load pre-calculated widths for characters.
/// </summary>
internal static class BadgeResources
private static void Load()
{
private const string DEFAULT_CULTURE_RESOURCE = "PSRule.Badges.Resources.en.json";
if (_Char != null && _Width != null)
return;

private static char[] _Char;
private static double[] _Width;

/// <summary>
/// Load pre-calculated widths for characters.
/// </summary>
private static void Load()
var assembly = Assembly.GetExecutingAssembly();
using var stream = assembly.GetManifestResourceStream(DEFAULT_CULTURE_RESOURCE);
using var reader = new StreamReader(stream);
var json = reader.ReadToEnd();
var d = JsonConvert.DeserializeObject<object[][]>(json);
_Char = new char[d.Length];
_Width = new double[d.Length];
for (var i = 0; i < d.Length; i++)
{
if (_Char != null && _Width != null)
return;

var assembly = Assembly.GetExecutingAssembly();
using var stream = assembly.GetManifestResourceStream(DEFAULT_CULTURE_RESOURCE);
using var reader = new StreamReader(stream);
var json = reader.ReadToEnd();
var d = JsonConvert.DeserializeObject<object[][]>(json);
_Char = new char[d.Length];
_Width = new double[d.Length];
for (var i = 0; i < d.Length; i++)
{
_Char[i] = Convert.ToChar(d[i][0]);
_Width[i] = Convert.ToDouble(d[i][1]);
}
_Char[i] = Convert.ToChar(d[i][0]);
_Width[i] = Convert.ToDouble(d[i][1]);
}
}

/// <summary>
/// Get the width in pixels for a character.
/// </summary>
private static double GetWidth(char c)
{
Load();
return Find(c);
}
/// <summary>
/// Get the width in pixels for a character.
/// </summary>
private static double GetWidth(char c)
{
Load();
return Find(c);
}

/// <summary>
/// Find the width in pixels for a character.
/// </summary>
private static double Find(char c)
{
var index = Array.BinarySearch(_Char, c);
return index >= 0 ? _Width[index] : 0d;
}
/// <summary>
/// Find the width in pixels for a character.
/// </summary>
private static double Find(char c)
{
var index = Array.BinarySearch(_Char, c);
return index >= 0 ? _Width[index] : 0d;
}

public static double Measure(string s)
{
var length = 0d;
for (var i = 0; i < s.Length; i++)
length += GetWidth(s[i]);
public static double Measure(string s)
{
var length = 0d;
for (var i = 0; i < s.Length; i++)
length += GetWidth(s[i]);

return length;
}
return length;
}
}
Loading

0 comments on commit 71f9762

Please sign in to comment.