Skip to content

Commit

Permalink
Merge branch 'master' into dd/gh-net-release
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisInSky committed Nov 26, 2024
2 parents 040b4ad + 36ee739 commit 0c0d4e2
Show file tree
Hide file tree
Showing 41 changed files with 1,073 additions and 831 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/net-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
build_test:
name: Build & Test
runs-on: ubuntu-latest
permissions:
packages: read
defaults:
run:
shell: bash
Expand All @@ -33,7 +35,14 @@ jobs:
dotnet-version: 8.0.x

- name: Restore Solution
run: >-
run: |
NUGET_SOURCE_URL="https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json"
dotnet nuget remove source ${{ github.repository_owner }}-github || true
dotnet nuget add source $NUGET_SOURCE_URL \
--name ${{ github.repository_owner }}-github \
--username "${{ github.workflow }}" \
--password ${{ secrets.GITHUB_TOKEN }} \
--store-password-in-clear-text
dotnet restore
- name: Build Solution
Expand Down
16 changes: 16 additions & 0 deletions Sails.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Substrate.Gear.Client.Tests
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sails.Client.Tests", "net\tests\Sails.Client.Tests\Sails.Client.Tests.csproj", "{BC248D44-14CD-4BB0-9AF9-4E0750574492}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{4D457454-ED79-4BC3-8B6B-AC6934AAF048}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sails.DemoClient", "net\examples\Sails.DemoClient\Sails.DemoClient.csproj", "{C9D87AD3-612B-43A0-89BD-9211FE2FB310}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sails.DemoClient.Tests", "net\tests\Sails.DemoClient.Tests\Sails.DemoClient.Tests.csproj", "{5ED8CDB7-F896-4ACE-8B7B-A128CCBBCFA4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -83,6 +89,14 @@ Global
{BC248D44-14CD-4BB0-9AF9-4E0750574492}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC248D44-14CD-4BB0-9AF9-4E0750574492}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC248D44-14CD-4BB0-9AF9-4E0750574492}.Release|Any CPU.Build.0 = Release|Any CPU
{C9D87AD3-612B-43A0-89BD-9211FE2FB310}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C9D87AD3-612B-43A0-89BD-9211FE2FB310}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9D87AD3-612B-43A0-89BD-9211FE2FB310}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9D87AD3-612B-43A0-89BD-9211FE2FB310}.Release|Any CPU.Build.0 = Release|Any CPU
{5ED8CDB7-F896-4ACE-8B7B-A128CCBBCFA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5ED8CDB7-F896-4ACE-8B7B-A128CCBBCFA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5ED8CDB7-F896-4ACE-8B7B-A128CCBBCFA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5ED8CDB7-F896-4ACE-8B7B-A128CCBBCFA4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -93,6 +107,8 @@ Global
{A6A2172B-8F8F-4BDC-B519-E7299FFCCA5F} = {19594BCA-94DB-44AD-ACBC-2ACFED242E9F}
{42B621CE-C2B4-4911-961C-5B087A514AF5} = {19594BCA-94DB-44AD-ACBC-2ACFED242E9F}
{BC248D44-14CD-4BB0-9AF9-4E0750574492} = {19594BCA-94DB-44AD-ACBC-2ACFED242E9F}
{C9D87AD3-612B-43A0-89BD-9211FE2FB310} = {4D457454-ED79-4BC3-8B6B-AC6934AAF048}
{5ED8CDB7-F896-4ACE-8B7B-A128CCBBCFA4} = {19594BCA-94DB-44AD-ACBC-2ACFED242E9F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0894C08B-8BB9-401D-8471-26F5EB5A4EA2}
Expand Down
10 changes: 7 additions & 3 deletions net/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,29 @@
<PackageVersion Include="Substrate.NET.API" Version="0.9.23" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Threading.Channels" Version="9.0.0" />
<!-- For Tests -->
<!-- Tests -->
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="FluentAssertions" Version="6.12.2" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Nito.AsyncEx" Version="5.1.2" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
<PackageVersion Include="Polly" Version="8.5.0" />
<PackageVersion Include="Testcontainers" Version="4.0.0" />
<PackageVersion Include="Verify.SourceGenerators" Version="2.5.0" />
<PackageVersion Include="Verify.Xunit" Version="28.3.2" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<!-- For Code Analysis -->
<!-- Code Analysis -->
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.17" />
<PackageVersion Include="Roslynator.Analyzers" Version="4.12.9" />
<PackageVersion Include="Roslynator.Formatting.Analyzers" Version="4.12.9" />
<!-- Due to Vulnarabilties -->
<!-- Vulnarabilties -->
<PackageVersion Include="MessagePack" Version="2.5.192" />
<PackageVersion Include="System.Text.Json" Version="9.0.0" />
<!-- Examples -->
<PackageVersion Include="Sails.ClientGenerator" Version="0.0.1-beta.1" />
<PackageVersion Include="Sails.Net" Version="0.0.1-beta.1" />
</ItemGroup>
</Project>
16 changes: 16 additions & 0 deletions net/examples/Sails.DemoClient/Sails.DemoClient.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<AdditionalFiles Include="demo.idl" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Sails.ClientGenerator" />
<PackageReference Include="Sails.Net" />
</ItemGroup>

</Project>
91 changes: 91 additions & 0 deletions net/examples/Sails.DemoClient/demo.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
type ReferenceCount = struct {
u32,
};

type DoThatParam = struct {
p1: nat32,
p2: actor_id,
p3: ManyVariants,
};

type ManyVariants = enum {
One,
Two: u32,
Three: opt u256,
Four: struct { a: u32, b: opt u16 },
Five: struct { str, h256 },
Six: struct { u32 },
};

type TupleStruct = struct {
bool,
};

constructor {
/// Program constructor (called once at the very beginning of the program lifetime)
Default : ();
/// Another program constructor (called once at the very beginning of the program lifetime)
New : (counter: opt u32, dog_position: opt struct { i32, i32 });
};

service Counter {
/// Add a value to the counter
Add : (value: u32) -> u32;
/// Substract a value from the counter
Sub : (value: u32) -> u32;
/// Get the current value
query Value : () -> u32;

events {
/// Emitted when a new value is added to the counter
Added: u32;
/// Emitted when a value is subtracted from the counter
Subtracted: u32;
}
};

service Dog {
MakeSound : () -> str;
Walk : (dx: i32, dy: i32) -> null;
query AvgWeight : () -> u32;
query Position : () -> struct { i32, i32 };

events {
Barked;
Walked: struct { from: struct { i32, i32 }, to: struct { i32, i32 } };
}
};

service PingPong {
Ping : (input: str) -> result (str, str);
};

service References {
Add : (v: u32) -> u32;
AddByte : (byte: u8) -> vec u8;
GuessNum : (number: u8) -> result (str, str);
Incr : () -> ReferenceCount;
SetNum : (number: u8) -> result (null, str);
query Baked : () -> str;
query LastByte : () -> opt u8;
query Message : () -> opt str;
};

service ThisThat {
DoThat : (param: DoThatParam) -> result (struct { actor_id, nat32 }, struct { str });
DoThis : (p1: u32, p2: str, p3: struct { opt h160, nat8 }, p4: TupleStruct) -> struct { str, u32 };
Noop : () -> null;
query That : () -> result (str, str);
query This : () -> u32;
};

service ValueFee {
/// Return flag if fee taken and remain value,
/// using special type `CommandReply<T>`
DoSomethingAndTakeFee : () -> bool;

events {
Withheld: u128;
}
};

24 changes: 4 additions & 20 deletions net/rs/client-gen/src/ctor_generators.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
helpers::*,
type_generators::{primitive_type_to_dotnet, TypeDeclGenerator},
};
use crate::{helpers::*, type_decl_generators::*};
use convert_case::{Case, Casing};
use csharp::Tokens;
use genco::prelude::*;
Expand Down Expand Up @@ -63,19 +60,9 @@ impl<'a> Visitor<'a> for CtorFactoryGenerator<'a> {
self.interface_tokens.push();

let route_bytes = &path_bytes(func.name()).0;
let args = &encoded_fn_args(func.params());
let args = &encoded_fn_args_comma_prefixed(func.params());
let args_with_type = &self.type_generator.fn_params_with_types(func.params());

let type_decls = func
.params()
.iter()
.map(|p| p.type_decl())
.collect::<Vec<_>>();
let tuple_arg_type = if type_decls.is_empty() {
primitive_type_to_dotnet(PrimitiveType::Null).to_string()
} else {
self.type_generator.generate_types_as_tuple(type_decls)
};
let void_type = primitive_type_to_dotnet(PrimitiveType::Null);

let activation = &csharp::import("global::Sails.Remoting.Abstractions", "IActivation");
let action = &csharp::import("global::Sails.Remoting", "RemotingAction");
Expand All @@ -88,10 +75,7 @@ impl<'a> Visitor<'a> for CtorFactoryGenerator<'a> {
$(inheritdoc())
public $activation $func_name_pascal($args_with_type)
{
return new $action<$(&tuple_arg_type)>(
this.remoting,
[$route_bytes],
new $(&tuple_arg_type)($args));
return new $action<$(void_type)>(this.remoting, [$route_bytes]$args);
}
$['\n']
};
Expand Down
3 changes: 1 addition & 2 deletions net/rs/client-gen/src/events_generator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::helpers::*;
use crate::type_generators::{primitive_type_to_dotnet, TypeDeclGenerator};
use crate::{helpers::*, type_decl_generators::*};
use convert_case::{Case, Casing};
use csharp::Tokens;
use genco::prelude::*;
Expand Down
11 changes: 8 additions & 3 deletions net/rs/client-gen/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ pub(crate) fn path_bytes(path: &str) -> (String, usize) {
}
}

pub(crate) fn encoded_fn_args(params: &[FuncParam]) -> String {
pub(crate) fn encoded_fn_args_comma_prefixed(params: &[FuncParam]) -> String {
params
.iter()
.map(|p| escape_keywords(p.name().to_case(convert_case::Case::Camel)))
.map(|p| {
format!(
", {}",
escape_keywords(p.name().to_case(convert_case::Case::Camel))
)
})
.collect::<Vec<_>>()
.join(", ")
.join("")
}

pub fn summary_comment<T>(comment: T) -> SummaryComment<T>
Expand Down
6 changes: 4 additions & 2 deletions net/rs/client-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ mod events_generator;
mod helpers;
mod root_generator;
mod service_generators;
mod type_generators;
mod tol_level_type_generators;
mod type_decl_generators;

pub struct IdlPath<'a>(&'a Path);

Expand Down Expand Up @@ -69,7 +70,8 @@ impl<'a> ClientGenerator<'a, IdlString<'a>> {
RootGenerator::new(anonymous_service_name, namespace, self.external_types);
visitor::accept_program(&program, &mut generator);
let tokens = generator.finalize();
let code = tokens.to_file_string()?;
let mut code = "// <auto-generated />\n".to_owned();
code.push_str(&tokens.to_file_string()?);
Ok(code)
}

Expand Down
7 changes: 6 additions & 1 deletion net/rs/client-gen/src/root_generator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{ctor_generators::*, events_generator::*, service_generators::*, type_generators::*};
use crate::{
ctor_generators::*, events_generator::*, service_generators::*, tol_level_type_generators::*,
type_decl_generators::*,
};
use convert_case::{Case, Casing};
use csharp::Tokens;
use genco::{prelude::*, tokens::ItemStr};
Expand All @@ -19,6 +22,8 @@ impl<'a> RootGenerator<'a> {
external_types: HashMap<&'a str, &'a str>,
) -> Self {
let mut tokens = Tokens::new();
tokens.append(ItemStr::Static("#nullable enable"));
tokens.line();
tokens.append(ItemStr::Static(
"#pragma warning disable RCS0056 // A line is too long",
));
Expand Down
12 changes: 3 additions & 9 deletions net/rs/client-gen/src/service_generators.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::{helpers::*, type_generators::TypeDeclGenerator};
use crate::{helpers::*, type_decl_generators::*};
use convert_case::{Case, Casing};
use csharp::Tokens;
use genco::prelude::*;
use sails_idl_parser::{ast::visitor, ast::visitor::Visitor, ast::*};

const BASE_TUPLE_RUST: &str = "global::Substrate.NetApi.Model.Types.Base.BaseTupleRust";

/// Generates a client that implements service trait
pub(crate) struct ServiceClientGenerator<'a> {
service_name: String,
Expand Down Expand Up @@ -63,7 +61,7 @@ impl<'a> Visitor<'a> for ServiceClientGenerator<'a> {
let func_route_bytes = path_bytes(func.name()).0;
let route_bytes = [service_route_bytes, func_route_bytes].join(", ");

let args = encoded_fn_args(func.params());
let args = &encoded_fn_args_comma_prefixed(func.params());
let args_with_type = &self.type_generator.fn_params_with_types(func.params());
let func_return_type = &self.type_generator.generate_type_decl(func.output());

Expand All @@ -80,11 +78,7 @@ impl<'a> Visitor<'a> for ServiceClientGenerator<'a> {
$(inheritdoc())
public $return_type<$func_return_type> $func_name_pascal($args_with_type)
{
return new $action<$func_return_type>(
this.remoting,
[$(&route_bytes)],
new $(BASE_TUPLE_RUST)($(&args))
);
return new $action<$func_return_type>(this.remoting, [$(&route_bytes)]$args);
}
};
}
Expand Down
Loading

0 comments on commit 0c0d4e2

Please sign in to comment.