Skip to content

Commit

Permalink
feat: move the logic parts in Main program to Entrypoint
Browse files Browse the repository at this point in the history
Signed-off-by: Thanabodee Charoenpiriyakij <wingyminus@gmail.com>
  • Loading branch information
wingyplus committed Jul 1, 2024
1 parent 57614df commit e60576c
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 107 deletions.
44 changes: 22 additions & 22 deletions potato/dagger/Potato.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dagger.SDK", "Dagger.SDK\Dagger.SDK.csproj", "{60030F7C-2580-41B4-B328-9224283D32CA}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dagger.SDK", "Dagger.SDK\Dagger.SDK.csproj", "{9414D589-D65C-4BD1-BB97-3152651E5EAC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dagger.SDK.Mod.SourceGenerator", "Dagger.SDK.Mod.SourceGenerator\Dagger.SDK.Mod.SourceGenerator.csproj", "{C102023A-E0B4-4481-9B4E-E0D57BD60529}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dagger.SDK.Mod.SourceGenerator", "Dagger.SDK.Mod.SourceGenerator\Dagger.SDK.Mod.SourceGenerator.csproj", "{84F48313-FB5C-4310-86A2-EBDC90C3F6B2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Dagger.SDK.SourceGenerator", "Dagger.SDK.SourceGenerator", "{2AB49E96-2B24-42EE-9588-98E6C33C8030}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Dagger.SDK.SourceGenerator", "Dagger.SDK.SourceGenerator", "{EFC0C220-DC6B-4E1D-87B9-BAA0107F9D79}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dagger.SDK.SourceGenerator", "Dagger.SDK.SourceGenerator\Dagger.SDK.SourceGenerator\Dagger.SDK.SourceGenerator.csproj", "{6CCF0154-23CE-4B18-A34E-5E0A62EA7E73}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dagger.SDK.SourceGenerator", "Dagger.SDK.SourceGenerator\Dagger.SDK.SourceGenerator\Dagger.SDK.SourceGenerator.csproj", "{BE53C677-6892-40F2-B649-A5513A90485D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Potato", "Potato\Potato.csproj", "{6175E82C-9BD2-435E-8E1E-61397B10B288}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Potato", "Potato\Potato.csproj", "{4BAD5885-4DD6-470C-B454-1D1F1356D65E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -22,24 +22,24 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{60030F7C-2580-41B4-B328-9224283D32CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{60030F7C-2580-41B4-B328-9224283D32CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60030F7C-2580-41B4-B328-9224283D32CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60030F7C-2580-41B4-B328-9224283D32CA}.Release|Any CPU.Build.0 = Release|Any CPU
{C102023A-E0B4-4481-9B4E-E0D57BD60529}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C102023A-E0B4-4481-9B4E-E0D57BD60529}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C102023A-E0B4-4481-9B4E-E0D57BD60529}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C102023A-E0B4-4481-9B4E-E0D57BD60529}.Release|Any CPU.Build.0 = Release|Any CPU
{6CCF0154-23CE-4B18-A34E-5E0A62EA7E73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6CCF0154-23CE-4B18-A34E-5E0A62EA7E73}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6CCF0154-23CE-4B18-A34E-5E0A62EA7E73}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6CCF0154-23CE-4B18-A34E-5E0A62EA7E73}.Release|Any CPU.Build.0 = Release|Any CPU
{6175E82C-9BD2-435E-8E1E-61397B10B288}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6175E82C-9BD2-435E-8E1E-61397B10B288}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6175E82C-9BD2-435E-8E1E-61397B10B288}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6175E82C-9BD2-435E-8E1E-61397B10B288}.Release|Any CPU.Build.0 = Release|Any CPU
{9414D589-D65C-4BD1-BB97-3152651E5EAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9414D589-D65C-4BD1-BB97-3152651E5EAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9414D589-D65C-4BD1-BB97-3152651E5EAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9414D589-D65C-4BD1-BB97-3152651E5EAC}.Release|Any CPU.Build.0 = Release|Any CPU
{84F48313-FB5C-4310-86A2-EBDC90C3F6B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84F48313-FB5C-4310-86A2-EBDC90C3F6B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84F48313-FB5C-4310-86A2-EBDC90C3F6B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84F48313-FB5C-4310-86A2-EBDC90C3F6B2}.Release|Any CPU.Build.0 = Release|Any CPU
{BE53C677-6892-40F2-B649-A5513A90485D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE53C677-6892-40F2-B649-A5513A90485D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE53C677-6892-40F2-B649-A5513A90485D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE53C677-6892-40F2-B649-A5513A90485D}.Release|Any CPU.Build.0 = Release|Any CPU
{4BAD5885-4DD6-470C-B454-1D1F1356D65E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BAD5885-4DD6-470C-B454-1D1F1356D65E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BAD5885-4DD6-470C-B454-1D1F1356D65E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BAD5885-4DD6-470C-B454-1D1F1356D65E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6CCF0154-23CE-4B18-A34E-5E0A62EA7E73} = {2AB49E96-2B24-42EE-9588-98E6C33C8030}
{BE53C677-6892-40F2-B649-A5513A90485D} = {EFC0C220-DC6B-4E1D-87B9-BAA0107F9D79}
EndGlobalSection
EndGlobal
14 changes: 0 additions & 14 deletions potato/dagger/Potato/Dagger_Mod_Attributes.cs

This file was deleted.

1 change: 1 addition & 0 deletions potato/dagger/Potato/Potato.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Potato;

[Serializable]
[Mod.Entrypoint]
[Mod.Object]
public partial class Potato
{
Expand Down
13 changes: 0 additions & 13 deletions potato/dagger/Potato/Potato_IEntrypoint.cs

This file was deleted.

43 changes: 8 additions & 35 deletions potato/dagger/Potato/Program.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,9 @@
// QUESTION: is it possible to get generate by source generator?

using System.Text.Json;
using Dagger.SDK;
using Dagger.SDK.Mod;

public class Program
{
public static async Task Main(string[] args)
{
var dag = Dagger.SDK.Dagger.Connect();
await Invoke<Potato.Potato>(dag);
}

private static async Task Invoke<T>(Query dag) where T : class, IDagSetter, IEntrypoint, new()
{
T root = new();
var fnCall = dag.CurrentFunctionCall();
var parentName = await fnCall.ParentName();
// TODO: Get module name to check root type name match with it.

var result = parentName switch
{
// TODO: Dagger.SDK should automatic serialize into id.
"" => await root.Register(dag).Id(),
_ => throw new Exception($"{parentName} is not supported at the moment.")
};

await fnCall.ReturnValue(IntoJson(result));
}

private static Json IntoJson(object result)
{
return new Json { Value = JsonSerializer.Serialize(result) };
}
namespace Potato;

public static class Program
{
public static async Task Main(string[] args)
{
await Entrypoint.Invoke(args);
}
}
20 changes: 20 additions & 0 deletions sdk/Dagger.SDK.Mod.SourceGenerator/GenerateSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ public interface IDagSetter
/// <param name="dag">The Dagger client instance.</param>
void SetDag(Query dag);
}

/// <summary>
/// An interface for invoking the module class.
/// </summary>
public interface IEntrypoint
{
/// <summary>
/// Register an object as the root of module.
/// </summary>
/// <param name="dag">Dagger client instance.</param>
/// <param name="module">The empty Dagger module.</param>
/// <returns>The Dagger module with registered object.</returns>
Module Register(Query dag, Module module);
}
""";
return SourceText.From(CSharpSource.Format(sourceText), Encoding.UTF8);
}
Expand All @@ -34,6 +48,12 @@ public static SourceText ModuleAttributesSource()

namespace Dagger.SDK.Mod;

/// <summary>
/// Treat the class as the root of Module.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public sealed class EntrypointAttribute : Attribute;

/// <summary>
/// Expose the class as a Dagger.ObjectTypeDef.
/// </summary>
Expand Down
105 changes: 82 additions & 23 deletions sdk/Dagger.SDK.Mod.SourceGenerator/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Dagger.SDK.Mod.SourceGenerator;
[Generator(LanguageNames.CSharp)]
public class SourceGenerator : IIncrementalGenerator
{
private const string EntrypointAttribute = "Dagger.SDK.Mod.EntrypointAttribute";
private const string ObjectAttribute = "Dagger.SDK.Mod.ObjectAttribute";
private const string FunctionAttribute = "Dagger.SDK.Mod.FunctionAttribute";

Expand All @@ -31,6 +32,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
GenerateSources.ModuleInterfacesSource());
});

var entrypoint = context.SyntaxProvider.ForAttributeWithMetadataName(
fullyQualifiedMetadataName: EntrypointAttribute,
predicate: IsPartialClass,
transform: ToObjectContext
);

context.RegisterSourceOutput(entrypoint, GenerateEntrypoint);

var objects = context.SyntaxProvider.ForAttributeWithMetadataName(
fullyQualifiedMetadataName: ObjectAttribute,
predicate: IsPartialClass,
Expand All @@ -50,6 +59,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterSourceOutput(objectWithFunctions, GenerateObjectTypeDef);
}

private T ToEntrypointContext<T>(GeneratorAttributeSyntaxContext arg1, CancellationToken arg2)
{
throw new NotImplementedException();
}


private ObjectContext GroupIntoObjectContext((ObjectContext Left, ImmutableArray<FunctionContext> Right) tuple,
CancellationToken token)
{
Expand Down Expand Up @@ -90,65 +105,109 @@ private static ObjectContext ToObjectContext(

private static bool IsPartialClass(SyntaxNode node, CancellationToken token)
{
return node is ClassDeclarationSyntax classDecl && classDecl.Modifiers.Any(SyntaxKind.PartialKeyword);
return node is ClassDeclarationSyntax classDecl &&
classDecl.Modifiers.Any(SyntaxKind.PublicKeyword) &&
classDecl.Modifiers.Any(SyntaxKind.PartialKeyword);
}

private static void GenerateObjectTypeDef(SourceProductionContext context,
ObjectContext objectContext)
{
var ns = objectContext.Namespace;
var className = objectContext.Name;

var withFunctionDefs = objectContext
.Functions
.Select(static m => m.Name)
.Select(static methodName => $"""
.WithFunction(
dag.Function("{methodName}", dag.TypeDef().WithKind(TypeDefKind.STRING_KIND))
dag.Function("{methodName}", dag.TypeDef().WithKind(Dagger.SDK.TypeDefKind.STRING_KIND))
)
""");

var source = $$"""
using Dagger.SDK;
namespace {{objectContext.Namespace}};

namespace {{ns}};

public partial class {{className}}
public partial class {{objectContext.Name}}
{
public TypeDef ToObjectTypeDef(Query dag)
public Dagger.SDK.TypeDef ToObjectTypeDef(Dagger.SDK.Query dag)
{
var objTypeDef = dag.TypeDef().WithObject("{{className}}");
var objTypeDef = dag.TypeDef().WithObject("{{objectContext.Name}}");
return objTypeDef
{{string.Join("\n", withFunctionDefs)}};
}
}
""";
context.AddSource($"{className}_ObjectTypeDef.g.cs",
context.AddSource($"{objectContext.Name}_ObjectTypeDef.g.cs",
SourceText.From(CSharpSource.Format(source), Encoding.UTF8));
}

private static void GenerateIDagSetter(SourceProductionContext context, ObjectContext objectContext)
{
var ns = objectContext.Namespace;
var className = objectContext.Name;

var source = $$"""
using Dagger.SDK;
using Dagger.SDK.Mod;

namespace {{ns}};
namespace {{objectContext.Namespace}};

public partial class {{className}} : IDagSetter
public partial class {{objectContext.Name}} : Dagger.SDK.Mod.IDagSetter
{
private Query _dag;
private Dagger.SDK.Query _dag;

public void SetDag(Query dag)
public void SetDag(Dagger.SDK.Query dag)
{
_dag = dag;
}
}
""";

context.AddSource($"{className}.g.cs", SourceText.From(CSharpSource.Format(source), Encoding.UTF8));
context.AddSource($"{objectContext.Name}_IDagSetter.g.cs", SourceText.From(CSharpSource.Format(source), Encoding.UTF8));
}

private static void GenerateEntrypoint(SourceProductionContext context, ObjectContext objectContext)
{
var entrypoint = $$"""
namespace {{objectContext.Namespace}};

public partial class {{objectContext.Name}} : Dagger.SDK.Mod.IEntrypoint
{
public Dagger.SDK.Module Register(Dagger.SDK.Query dag, Dagger.SDK.Module module)
{
return module.WithObject(ToObjectTypeDef(dag));
}
}
""";
context.AddSource($"{objectContext.Name}_IEntrypoint.g.cs", SourceText.From(CSharpSource.Format(entrypoint), Encoding.UTF8));

var main = $$"""
namespace {{objectContext.Namespace}};

public static class Entrypoint
{
public static async Task Invoke(string[] args)
{
var dag = Dagger.SDK.Dagger.Connect();
await Invoke<{{objectContext.Name}}>(dag);
}
private static async Task Invoke<T>(Dagger.SDK.Query dag) where T : class, Dagger.SDK.Mod.IDagSetter, Dagger.SDK.Mod.IEntrypoint, new()
{
T root = new();
var fnCall = dag.CurrentFunctionCall();
var parentName = await fnCall.ParentName();
// TODO: Get module name to check root type name match with it.
var result = parentName switch
{
// TODO: Dagger.SDK should automatic serialize into id.
"" => await root.Register(dag, dag.Module()).Id(),
_ => throw new Exception($"{parentName} is not supported at the moment.")
};

await fnCall.ReturnValue(IntoJson(result));
}

private static Dagger.SDK.Json IntoJson(object result)
{
return new Dagger.SDK.Json { Value = System.Text.Json.JsonSerializer.Serialize(result) };
}
}
""";

context.AddSource("Entrypoint.g.cs", SourceText.From(CSharpSource.Format(main), Encoding.UTF8));
}
}

0 comments on commit e60576c

Please sign in to comment.