Skip to content

Commit 80141e9

Browse files
Merge pull request #9 from SimpleStateMachine/NewSubRule
New sub rule
2 parents e568db5 + d97d7ce commit 80141e9

36 files changed

+684
-184
lines changed

.dockerignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**/.dockerignore
2+
**/.env
3+
**/.git
4+
**/.gitignore
5+
**/.project
6+
**/.settings
7+
**/.toolstarget
8+
**/.vs
9+
**/.vscode
10+
**/.idea
11+
**/*.*proj.user
12+
**/*.dbmdl
13+
**/*.jfm
14+
**/azds.yaml
15+
**/bin
16+
**/charts
17+
**/docker-compose*
18+
**/Dockerfile*
19+
**/node_modules
20+
**/npm-debug.log
21+
**/obj
22+
**/secrets.dev.yaml
23+
**/values.dev.yaml
24+
LICENSE
25+
README.md

Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
2+
WORKDIR /app
3+
4+
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
5+
WORKDIR /src
6+
COPY ["SimpleStateMachine.StructuralSearch.Action/SimpleStateMachine.StructuralSearch.Action.csproj", "SimpleStateMachine.StructuralSearch.Action/"]
7+
RUN dotnet restore "SimpleStateMachine.StructuralSearch.Action/SimpleStateMachine.StructuralSearch.Action.csproj"
8+
COPY . .
9+
WORKDIR "/src/SimpleStateMachine.StructuralSearch.Action"
10+
RUN dotnet build "SimpleStateMachine.StructuralSearch.Action.csproj" -c Release -o /app/build
11+
12+
FROM build AS publish
13+
RUN dotnet publish "SimpleStateMachine.StructuralSearch.Action.csproj" -c Release -o /app/publish
14+
15+
FROM base AS final
16+
WORKDIR /app
17+
COPY --from=publish /app/publish .
18+
ENTRYPOINT ["dotnet", "SimpleStateMachine.StructuralSearch.Action.dll"]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using CommandLine;
2+
3+
namespace SimpleStateMachine.StructuralSearch.Action;
4+
5+
public class ActionInputs
6+
{
7+
string _repositoryName = null!;
8+
string _branchName = null!;
9+
10+
public ActionInputs()
11+
{
12+
if (Environment.GetEnvironmentVariable("GREETINGS") is { Length: > 0 } greetings)
13+
{
14+
Console.WriteLine(greetings);
15+
}
16+
}
17+
18+
[Option('o', "owner",
19+
Required = true,
20+
HelpText = "The owner, for example: \"dotnet\". Assign from `github.repository_owner`.")]
21+
public string Owner { get; set; } = null!;
22+
23+
[Option('n', "name",
24+
Required = true,
25+
HelpText = "The repository name, for example: \"samples\". Assign from `github.repository`.")]
26+
public string Name
27+
{
28+
get => _repositoryName;
29+
set => ParseAndAssign(value, str => _repositoryName = str);
30+
}
31+
32+
[Option('b', "branch",
33+
Required = true,
34+
HelpText = "The branch name, for example: \"refs/heads/main\". Assign from `github.ref`.")]
35+
public string Branch
36+
{
37+
get => _branchName;
38+
set => ParseAndAssign(value, str => _branchName = str);
39+
}
40+
41+
[Option('d', "dir",
42+
Required = true,
43+
HelpText = "The root directory to start recursive searching from.")]
44+
public string Directory { get; set; } = null!;
45+
46+
[Option('w', "workspace",
47+
Required = true,
48+
HelpText = "The workspace directory, or repository root directory.")]
49+
public string WorkspaceDirectory { get; set; } = null!;
50+
51+
static void ParseAndAssign(string? value, Action<string> assign)
52+
{
53+
if (value is { Length: > 0 } && assign is not null)
54+
{
55+
assign(value.Split("/")[^1]);
56+
}
57+
}
58+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System.Text;
2+
using SimpleStateMachine.StructuralSearch.Action;
3+
using System.Web;
4+
using CommandLine;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.FileSystemGlobbing;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
using static CommandLine.Parser;
10+
11+
using IHost host = Host.CreateDefaultBuilder(args)
12+
.ConfigureServices((_, services) =>
13+
{
14+
15+
})
16+
.Build();
17+
18+
static TService Get<TService>(IHost host)
19+
where TService : notnull =>
20+
host.Services.GetRequiredService<TService>();
21+
22+
static async Task StartAnalysisAsync(ActionInputs inputs, IHost host)
23+
{
24+
// using ProjectWorkspace workspace = Get<ProjectWorkspace>(host);
25+
using CancellationTokenSource tokenSource = new();
26+
27+
Console.CancelKeyPress += delegate { tokenSource.Cancel(); };
28+
29+
// var projectAnalyzer = Get<ProjectMetricDataAnalyzer>(host);
30+
31+
Matcher matcher = new();
32+
matcher.AddIncludePatterns(new[] { "**/*.csproj", "**/*.vbproj" });
33+
34+
// Dictionary<string, CodeAnalysisMetricData> metricData = new(StringComparer.OrdinalIgnoreCase);
35+
var projects = matcher.GetResultsInFullPath(inputs.Directory);
36+
37+
foreach (var project in projects)
38+
{
39+
// var metrics =
40+
// await projectAnalyzer.AnalyzeAsync(
41+
// workspace, project, tokenSource.Token);
42+
//
43+
// foreach (var (path, metric) in metrics)
44+
// {
45+
// metricData[path] = metric;
46+
// }
47+
}
48+
49+
// var updatedMetrics = false;
50+
// var title = "";
51+
// StringBuilder summary = new();
52+
// if (metricData is { Count: > 0 })
53+
// {
54+
// var fileName = "CODE_METRICS.md";
55+
// var fullPath = Path.Combine(inputs.Directory, fileName);
56+
// var logger = Get<ILoggerFactory>(host).CreateLogger(nameof(StartAnalysisAsync));
57+
// var fileExists = File.Exists(fullPath);
58+
//
59+
// logger.LogInformation(
60+
// $"{(fileExists ? "Updating" : "Creating")} {fileName} markdown file with latest code metric data.");
61+
//
62+
// summary.AppendLine(
63+
// title =
64+
// $"{(fileExists ? "Updated" : "Created")} {fileName} file, analyzed metrics for {metricData.Count} projects.");
65+
//
66+
// foreach (var (path, _) in metricData)
67+
// {
68+
// summary.AppendLine($"- *{path}*");
69+
// }
70+
//
71+
// var contents = metricData.ToMarkDownBody(inputs);
72+
// await File.WriteAllTextAsync(
73+
// fullPath,
74+
// contents,
75+
// tokenSource.Token);
76+
//
77+
// updatedMetrics = true;
78+
// }
79+
// else
80+
// {
81+
// summary.Append("No metrics were determined.");
82+
// }
83+
//
84+
// // https://docs.github.com/actions/reference/workflow-commands-for-github-actions#setting-an-output-parameter
85+
// Console.WriteLine($"::set-output name=updated-metrics::{updatedMetrics}");
86+
// Console.WriteLine($"::set-output name=summary-title::{title}");
87+
// Console.WriteLine($"::set-output name=summary-details::{summary}");
88+
89+
Environment.Exit(0);
90+
}
91+
92+
var parser = Default.ParseArguments(() => new ActionInputs(), args);
93+
parser.WithNotParsed(
94+
errors =>
95+
{
96+
Get<ILoggerFactory>(host)
97+
.CreateLogger("DotNet.GitHubAction.Program")
98+
.LogError(
99+
string.Join(Environment.NewLine, errors.Select(error => error.ToString())));
100+
101+
Environment.Exit(2);
102+
});
103+
104+
await parser.WithParsedAsync(options => StartAnalysisAsync(options, host));
105+
await host.RunAsync();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="CommandLineParser" Version="2.8.0" />
13+
<PackageReference Include="MarkdownBuilder" Version="0.2.0" />
14+
<PackageReference Include="Microsoft.CodeAnalysis.Metrics" Version="3.3.3" />
15+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
16+
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="6.0.0" />
17+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
18+
</ItemGroup>
19+
20+
</Project>

SimpleStateMachine.StructuralSearch.sln

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleStateMachine.Structur
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleStateMachine.StructuralSearch.Sandbox", "src\SimpleStateMachine.StructuralSearch.Sandbox\SimpleStateMachine.StructuralSearch.Sandbox.csproj", "{263ABF7D-9728-4224-A584-5412E158D4BB}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleStateMachine.StructuralSearch.Action", "SimpleStateMachine.StructuralSearch.Action\SimpleStateMachine.StructuralSearch.Action.csproj", "{E22FFF76-E929-4E01-B2B3-4B9283705EDE}"
13+
EndProject
14+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6CB879CC-E497-41C0-AEBD-864E9D3807A6}"
15+
ProjectSection(SolutionItems) = preProject
16+
Dockerfile = Dockerfile
17+
.dockerignore = .dockerignore
18+
.gitignore = .gitignore
19+
LICENSE = LICENSE
20+
README.md = README.md
21+
global.json = global.json
22+
action.yml = action.yml
23+
EndProjectSection
24+
EndProject
1225
Global
1326
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1427
Debug|Any CPU = Debug|Any CPU
@@ -31,6 +44,10 @@ Global
3144
{263ABF7D-9728-4224-A584-5412E158D4BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
3245
{263ABF7D-9728-4224-A584-5412E158D4BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
3346
{263ABF7D-9728-4224-A584-5412E158D4BB}.Release|Any CPU.Build.0 = Release|Any CPU
47+
{E22FFF76-E929-4E01-B2B3-4B9283705EDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
48+
{E22FFF76-E929-4E01-B2B3-4B9283705EDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
49+
{E22FFF76-E929-4E01-B2B3-4B9283705EDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
50+
{E22FFF76-E929-4E01-B2B3-4B9283705EDE}.Release|Any CPU.Build.0 = Release|Any CPU
3451
EndGlobalSection
3552
GlobalSection(SolutionProperties) = preSolution
3653
HideSolutionNode = FALSE

action.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: 'Structural search'
2+
description: 'A Github action that maintains a CODE_METRICS.md file, reporting cyclomatic complexity, maintainability index, etc.'
3+
branding:
4+
icon: sliders
5+
color: purple
6+
inputs:
7+
owner:
8+
description: 'The owner of the repo. Assign from github.repository_owner. Example, "dotnet".'
9+
required: true
10+
name:
11+
description: 'The repository name. Example, "samples".'
12+
required: true
13+
branch:
14+
description: 'The branch name. Assign from github.ref. Example, "refs/heads/main".'
15+
required: true
16+
dir:
17+
description: 'The root directory to work from. Example, "path/to/code".'
18+
required: true
19+
workspace:
20+
description: 'The workspace directory.'
21+
required: false
22+
default: '/github/workspace'
23+
outputs:
24+
summary-title:
25+
description: 'The title of the code metrics action.'
26+
summary-details:
27+
description: 'A detailed summary of all the projects that were flagged.'
28+
updated-metrics:
29+
description: 'A boolean value, indicating whether or not the CODE_METRICS.md was updated as a result of running this action.'
30+
runs:
31+
using: 'docker'
32+
image: 'Dockerfile'
33+
args:
34+
- '-o'
35+
- ${{ inputs.owner }}
36+
- '-n'
37+
- ${{ inputs.name }}
38+
- '-b'
39+
- ${{ inputs.branch }}
40+
- '-d'
41+
- ${{ inputs.dir }}
42+
- '-w'
43+
- ${{ inputs.workspace }}

src/SimpleStateMachine.StructuralSearch.Sandbox/Expr.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public double Invoke()
137137
BinaryOperatorType.Mul => Left.Invoke() * Right.Invoke(),
138138
BinaryOperatorType.Div => Left.Invoke() / Right.Invoke(),
139139
BinaryOperatorType.Sub => Left.Invoke() - Right.Invoke(),
140+
_ => throw new ArgumentOutOfRangeException()
140141
};
141142
}
142143
}

src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/FullConfig.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
$var$ = $value1$;
2121
FindRules:
2222
- $sign$ In ("Is", "==", "!=", "is not")
23-
- $value$ In ($value1$, "$value1$.Value", $value2$, "$value2$.Value")
23+
- $value$ In ($value1$, "$value1$\.Value", $value2$, "$value2$\.Value")
2424
ReplaceTemplate: |-
2525
$var$ = $value1$ ?? $value2$;
2626
ReplaceRules:

src/SimpleStateMachine.StructuralSearch.Tests/ConfigurationFile/ShortConfig.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
$var$ = $value1$;
2020
FindRules:
2121
- $sign$ In ("Is", "==", "!=", "is not")
22-
- $value$ In ($value1$, "$value1$.Value", $value2$, "$value2$.Value")
22+
- $value$ In ($value1$, "$value1$\.Value", $value2$, "$value2$\.Value")
2323
ReplaceTemplate: |-
2424
$var$ = $value1$ ?? $value2$;
2525
ReplaceRules:

0 commit comments

Comments
 (0)