Skip to content

Commit 4d5240d

Browse files
committed
Add recipe descriptor
Signed-off-by: OlegDokuka <[email protected]>
1 parent ca2505a commit 4d5240d

File tree

6 files changed

+188
-3
lines changed

6 files changed

+188
-3
lines changed

Rewrite/Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<Company>Moderne Inc.</Company>
44
<PackageIcon>openrewrite.png</PackageIcon>
55
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
6-
<Version>0.8.0-preview-5</Version>
6+
<Version>0.8.0-preview-6</Version>
77

8-
<RewriteRemoteVersion>0.8.0-preview-4</RewriteRemoteVersion>
8+
<RewriteRemoteVersion>0.8.0-preview-5</RewriteRemoteVersion>
99
<RewriteRemoteDir Condition="'$(RewriteRemoteDir)'==''">..\..\..\..\..\moderneinc\rewrite-remote\Rewrite.Remote</RewriteRemoteDir>
1010
</PropertyGroup>
1111
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Rewrite.Core;
2+
3+
[AttributeUsage(AttributeTargets.Field)]
4+
public sealed class OptionAttribute : Attribute
5+
{
6+
7+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
namespace Rewrite.Core.Config;
2+
3+
public class OptionDescriptor
4+
{
5+
// Properties with public getters and private setters for immutability
6+
public string Name { get; }
7+
public string Type { get; }
8+
public string? DisplayName { get; }
9+
public string? Description { get; }
10+
public string? Example { get; }
11+
public List<string>? Valid { get; }
12+
public bool Required { get; }
13+
public object? Value { get; }
14+
15+
// Constructor to initialize the properties
16+
public OptionDescriptor(
17+
string name,
18+
string type,
19+
string? displayName,
20+
string? description,
21+
string? example,
22+
List<string>? valid,
23+
bool required,
24+
object? value)
25+
{
26+
Name = name;
27+
Type = type;
28+
DisplayName = displayName;
29+
Description = description;
30+
Example = example;
31+
Valid = valid;
32+
Required = required;
33+
Value = value;
34+
}
35+
36+
// Override Equals and GetHashCode to handle equality based on Name, Type, and Value
37+
public override bool Equals(object? obj)
38+
{
39+
if (obj is OptionDescriptor other)
40+
{
41+
return Name == other.Name &&
42+
Type == other.Type &&
43+
DisplayName == other.DisplayName &&
44+
Description == other.Description &&
45+
Example == other.Example &&
46+
Valid?.SequenceEqual(other.Valid) == true &&
47+
Required == other.Required &&
48+
Equals(Value, other.Value);
49+
}
50+
51+
return false;
52+
}
53+
54+
public override int GetHashCode()
55+
{
56+
return HashCode.Combine(Name, Type, DisplayName, Description, Example, Valid, Required, Value);
57+
}
58+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
namespace Rewrite.Core.Config;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
7+
public class RecipeDescriptor
8+
{
9+
// Properties with public getters and private setters to maintain immutability
10+
public string Name { get; }
11+
public string DisplayName { get; }
12+
public string Description { get; }
13+
public HashSet<string> Tags { get; }
14+
public TimeSpan? EstimatedEffortPerOccurrence { get; }
15+
public List<OptionDescriptor> Options { get; }
16+
public List<RecipeDescriptor> RecipeList { get; }
17+
// public List<DataTableDescriptor> DataTables { get; }
18+
// public List<Maintainer> Maintainers { get; }
19+
// public List<Contributor> Contributors { get; }
20+
// public List<RecipeExample> Examples { get; }
21+
public Uri Source { get; }
22+
23+
// Constructor to initialize the properties
24+
public RecipeDescriptor(string name, string displayName, string description, HashSet<string> tags,
25+
TimeSpan? estimatedEffortPerOccurrence, List<OptionDescriptor> options, List<RecipeDescriptor> recipeList,
26+
/*List<DataTableDescriptor> dataTables, List<Maintainer> maintainers, List<Contributor> contributors,
27+
List<RecipeExample> examples,*/ Uri source)
28+
{
29+
Name = name;
30+
DisplayName = displayName;
31+
Description = description;
32+
Tags = tags;
33+
EstimatedEffortPerOccurrence = estimatedEffortPerOccurrence;
34+
Options = options;
35+
RecipeList = recipeList;
36+
// DataTables = dataTables;
37+
// Maintainers = maintainers;
38+
// Contributors = contributors;
39+
// Examples = examples;
40+
Source = source;
41+
}
42+
43+
// Override Equals and GetHashCode to handle equality based on Name and Options
44+
public override bool Equals(object obj)
45+
{
46+
if (obj is RecipeDescriptor other)
47+
{
48+
return Name == other.Name && Options.SequenceEqual(other.Options);
49+
}
50+
51+
return false;
52+
}
53+
54+
public override int GetHashCode()
55+
{
56+
return (Name, Options).GetHashCode();
57+
}
58+
59+
// With pattern for immutability
60+
public RecipeDescriptor With(
61+
string name = null,
62+
string displayName = null,
63+
string description = null,
64+
HashSet<string> tags = null,
65+
TimeSpan? estimatedEffortPerOccurrence = null,
66+
List<OptionDescriptor> options = null,
67+
List<RecipeDescriptor> recipeList = null,
68+
// List<DataTableDescriptor> dataTables = null,
69+
// List<Maintainer> maintainers = null,
70+
// List<Contributor> contributors = null,
71+
// List<RecipeExample> examples = null,
72+
Uri source = null)
73+
{
74+
return new RecipeDescriptor(
75+
name ?? this.Name,
76+
displayName ?? this.DisplayName,
77+
description ?? this.Description,
78+
tags ?? this.Tags,
79+
estimatedEffortPerOccurrence ?? this.EstimatedEffortPerOccurrence,
80+
options ?? this.Options,
81+
recipeList ?? this.RecipeList,
82+
// dataTables ?? this.DataTables,
83+
// maintainers ?? this.Maintainers,
84+
// contributors ?? this.Contributors,
85+
// examples ?? this.Examples,
86+
source ?? this.Source
87+
);
88+
}
89+
}

Rewrite/src/Rewrite.Core/Recipe.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
1+
using System.Runtime.CompilerServices;
2+
using Rewrite.Core.Config;
3+
14
namespace Rewrite.Core;
25

36
public abstract class Recipe
47
{
8+
private RecipeDescriptor? _descriptor;
9+
10+
public RecipeDescriptor Descriptor
11+
{
12+
get
13+
{
14+
return _descriptor ?? (_descriptor = new RecipeDescriptor(GetType().FullName, GetType().FullName, "",
15+
new HashSet<string>(), null,
16+
GetType().GetConstructors()[0].GetParameters().Select(parameterInfo =>
17+
{
18+
return new OptionDescriptor(parameterInfo.Name, parameterInfo.ParameterType.FullName, null,
19+
null,
20+
null,
21+
null,
22+
!(parameterInfo.IsOptional || parameterInfo.HasDefaultValue ||
23+
parameterInfo.CustomAttributes.FirstOrDefault(data =>
24+
data.AttributeType == typeof(NullableAttribute)) != null),
25+
parameterInfo.DefaultValue
26+
);
27+
}).ToList(),
28+
[],
29+
new Uri($"recipe://{GetType().FullName}")
30+
));
31+
}
32+
33+
set => _descriptor = value;
34+
}
35+
536
public static Recipe Noop()
637
{
738
return new NoopRecipe();

Rewrite/tests/Rewrite.CSharp.Tests/Tree/NullSafeExpressionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Rewrite.CSharp.Tests.Tree;
88
[Collection("C# remoting")]
99
public class NullSafeExpressionTests : RewriteTest
1010
{
11-
[Fact(Skip = "NullSafeExpression parsing was disabled due to infiti recursion issue")]
11+
[Fact(Skip = "NullSafeExpression parsing was disabled due to infinite recursion issue")]
1212
public void Space()
1313
{
1414
RewriteRun(

0 commit comments

Comments
 (0)