From f9c007b2ef9cb5a87ccd676fdf7240c3c31ce627 Mon Sep 17 00:00:00 2001 From: Gabriele Picco Date: Mon, 22 May 2023 10:51:27 -0500 Subject: [PATCH] :art: Add account optional support (#1) * :art: Add account optional support * :green_heart: Fix CI build * :green_heart: Fix CI * :green_heart: Fix CI * :green_heart: Fix CI --- .config/dotnet-tools.json | 6 +++ .github/workflows/dotnet.yml | 14 +++---- README.md | 22 +++++----- SharedBuildProperties.props | 2 +- Solana.Unity.Anchor.Test/Resources/seq.json | 3 +- Solana.Unity.Anchor.Test/UnitTest1.cs | 13 ++---- Solana.Unity.Anchor/ClientGenerator.cs | 30 ++++++++++--- .../Models/Accounts/IdlAccount.cs | 2 + build.cake | 42 +++++++++++++++++-- 9 files changed, 96 insertions(+), 38 deletions(-) diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json index 9895427..3ec1a07 100644 --- a/.config/dotnet-tools.json +++ b/.config/dotnet-tools.json @@ -13,6 +13,12 @@ "commands": [ "dotnet-format" ] + }, + "solana.unity.anchor.tool": { + "version": "0.2.8", + "commands": [ + "dotnet-anchorgen" + ] } } } \ No newline at end of file diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index cd2469d..1f06831 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -11,10 +11,10 @@ jobs: runs-on: ubuntu-latest steps: - name: Get the sources - uses: actions/checkout@v1 - - name: Run the build script - uses: cake-build/cake-action@v1 - with: - script-path: build.cake - target: Pack - cake-bootstrap: true + uses: actions/checkout@v2 + - name: Run cake + shell: bash + run: | + dotnet tool install Cake.Tool --version 1.1.0 + dotnet tool restore + dotnet cake --target=Test --verbosity=verbose diff --git a/README.md b/README.md index 0cac6e1..6e46a01 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@

- +

@@ -12,7 +12,7 @@ Code License - + Discord

@@ -30,18 +30,16 @@ Solana.Unity.Anchor rely on [Solana.Unity-Core](https://github.com/garbles-labs/ ## Features This repo contains 3 main projects: -- Solnet.Anchor: IDL parsing and code generation -- Solnet.Anchor.Tool: dotnet tool executable that interfaces with the project above. -- Solnet.Anchor.SourceGenerator: Roslyn source generator that depends on the tool above to automatically generate code from IDL in your IDE. +- Solana.Unity.Anchor: IDL parsing and code generation +- Solana.Unity.Anchor.Tool: dotnet tool executable that interfaces with the project above. +- Solana.Unity.Anchor.SourceGenerator: Roslyn source generator that depends on the tool above to automatically generate code from IDL in your IDE. Currently covers all of IDL features with the exception of events and seeds. ## Requirements -Solnet.Anchor and Solnet.Anchor.Tool are compiled and run in net6. Could be easily backported to net5. -Solnet.Anchor.SourceGenerator is compiled in netstandard2.1 to be able to be used as a Roslyn Source Generator. However, machine needs net6 as it just calls Solnet.Anchor.Tool that requires net6. - -Generated code can be run using net5 or net6, and the respective Solnet version >=5.0.3 or >=6.0.3 libraries. +Solana.Unity.Anchor and Solana.Unity.Anchor.Tool are compiled and run in net6. Could be easily backported to net5. +Solana.Unity.Anchor.SourceGenerator is compiled in netstandard2.1 to be able to be used as a Roslyn Source Generator. However, machine needs net6 as it just calls Solnet.Anchor.Tool that requires net6. ## Instructions @@ -74,7 +72,11 @@ Consider supporting us: We encourage everyone to contribute, submit issues, PRs, discuss. Every kind of help is welcome. -## Maintainers +## Solana.Unity Maintainers + +* **Gabriele Picco** - [PiccoGabriele](https://github.com/PiccoGabriele) + +## Solnet Maintainers * **Hugo** - [murlokito](https://github.com/murlokito) * **Tiago** - [tiago](https://github.com/tiago18c) diff --git a/SharedBuildProperties.props b/SharedBuildProperties.props index 0cb31d6..340ef95 100644 --- a/SharedBuildProperties.props +++ b/SharedBuildProperties.props @@ -2,7 +2,7 @@ xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> Solana.Unity.Anchor - 0.2.8 + 0.2.9 Copyright 2022 © Garbles Labs Garbles Labs Garbles Labs diff --git a/Solana.Unity.Anchor.Test/Resources/seq.json b/Solana.Unity.Anchor.Test/Resources/seq.json index f7b28b4..1d29113 100644 --- a/Solana.Unity.Anchor.Test/Resources/seq.json +++ b/Solana.Unity.Anchor.Test/Resources/seq.json @@ -38,7 +38,8 @@ { "name": "sequenceAccount", "isMut": true, - "isSigner": false + "isSigner": false, + "isOptional": true }, { "name": "authority", diff --git a/Solana.Unity.Anchor.Test/UnitTest1.cs b/Solana.Unity.Anchor.Test/UnitTest1.cs index 52701fe..a306a84 100644 --- a/Solana.Unity.Anchor.Test/UnitTest1.cs +++ b/Solana.Unity.Anchor.Test/UnitTest1.cs @@ -1,10 +1,4 @@ -using Microsoft.CodeAnalysis; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Solana.Unity.Anchor; -using System.Collections.Generic; -using System; -using System.Buffers.Binary; -using System.Numerics; namespace Solana.Unity.Anchor.Test { @@ -18,14 +12,13 @@ public void TestMethod1() var res = IdlParser.ParseFile("Resources/seq.json"); Assert.IsNotNull(res); - - - ClientGenerator c = new(); c.GenerateSyntaxTree(res); + Assert.IsNotNull(c); - + var code = c.GenerateCode(res); + Assert.IsNotNull(code); } } } diff --git a/Solana.Unity.Anchor/ClientGenerator.cs b/Solana.Unity.Anchor/ClientGenerator.cs index 914fa16..ec23b19 100644 --- a/Solana.Unity.Anchor/ClientGenerator.cs +++ b/Solana.Unity.Anchor/ClientGenerator.cs @@ -156,8 +156,7 @@ private List GenerateKeysInitExpressions(IIdlAccountItem[] acc } else if (acc is IdlAccount singleAcc) { - - + initExpressions.Add(InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, @@ -167,10 +166,29 @@ private List GenerateKeysInitExpressions(IIdlAccountItem[] acc IdentifierName(singleAcc.IsMut ? "Writable" : "ReadOnly")), ArgumentList(SeparatedList(new ArgumentSyntax[] { - Argument(MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - identifierNameSyntax, - IdentifierName(singleAcc.Name.ToPascalCase()))), + Argument( + singleAcc.IsOptional ? + ConditionalExpression( + BinaryExpression( + SyntaxKind.EqualsExpression, + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + identifierNameSyntax, + IdentifierName(singleAcc.Name.ToPascalCase())), + LiteralExpression(SyntaxKind.NullLiteralExpression) + ), + + IdentifierName("programId"), + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + identifierNameSyntax, + IdentifierName(singleAcc.Name.ToPascalCase())) + ) : + MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, + identifierNameSyntax, + IdentifierName(singleAcc.Name.ToPascalCase()) + ) + ), Argument(LiteralExpression(singleAcc.IsSigner ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression)) })))); diff --git a/Solana.Unity.Anchor/Models/Accounts/IdlAccount.cs b/Solana.Unity.Anchor/Models/Accounts/IdlAccount.cs index e89feba..d61349a 100644 --- a/Solana.Unity.Anchor/Models/Accounts/IdlAccount.cs +++ b/Solana.Unity.Anchor/Models/Accounts/IdlAccount.cs @@ -13,6 +13,8 @@ public class IdlAccount : IIdlAccountItem public bool IsMut { get; set; } public bool IsSigner { get; set; } + + public bool IsOptional { get; set; } public IdlPda Pda { get; set; } diff --git a/build.cake b/build.cake index b2779f5..ffd2dd2 100644 --- a/build.cake +++ b/build.cake @@ -2,6 +2,11 @@ #addin nuget:?package=Cake.Coverlet&version=2.5.4 #tool dotnet:?package=dotnet-reportgenerator-globaltool&version=4.8.7 +var testProjectsRelativePaths = new string[] +{ + "./Solana.Unity.Anchor.Test/Solana.Unity.Anchor.Test.csproj", +}; + var target = Argument("target", "Pack"); var configuration = Argument("configuration", "Release"); @@ -10,9 +15,12 @@ var artifactsDir = MakeAbsolute(Directory("artifacts")); var reportTypes = "HtmlInline"; var coverageFolder = "./code_coverage"; -var coverageFileName = "results.info"; +var coverageFolderIntegration = "./code_coverage_integration"; + +var coberturaFileName = "results"; +var coverageFilePath = Directory(coverageFolder) + File(coberturaFileName + ".info"); +var jsonFilePath = Directory(coverageFolder) + File(coberturaFileName + ".json"); -var coverageFilePath = Directory(coverageFolder) + File(coverageFileName); var packagesDir = artifactsDir.Combine(Directory("packages")); var deliverables = new[] {"Solana.Unity.Anchor.Tool", "Solana.Unity.Anchor.SourceGenerator"}; @@ -20,7 +28,6 @@ var deliverables = new[] {"Solana.Unity.Anchor.Tool", "Solana.Unity.Anchor.Sourc Task("Clean") .Does(() => { CleanDirectory(artifactsDir); - CleanDirectory(coverageFolder); }); Task("Restore") @@ -40,7 +47,36 @@ Task("Build") }); +Task("Test") + .IsDependentOn("Build") + .Does(() => { + + var coverletSettings = new CoverletSettings { + CollectCoverage = true, + CoverletOutputDirectory = coverageFolder, + CoverletOutputName = coberturaFileName + }; + var testSettings = new DotNetCoreTestSettings + { + NoRestore = true, + Configuration = configuration, + NoBuild = true, + ArgumentCustomization = args => args.Append($"--logger trx") + }; + + DotNetCoreTest(testProjectsRelativePaths[0], testSettings, coverletSettings); + + coverletSettings.MergeWithFile = jsonFilePath; + for (int i = 1; i < testProjectsRelativePaths.Length; i++) + { + if (i == testProjectsRelativePaths.Length - 1) + { + coverletSettings.CoverletOutputFormat = CoverletOutputFormat.lcov; + } + DotNetCoreTest(testProjectsRelativePaths[i], testSettings, coverletSettings); + } + }); Task("Publish") .IsDependentOn("Build")