Skip to content

Commit c222d33

Browse files
authored
Merge pull request #99 from Peefy/feat-kcl-dotnet-bindings
feat: kcl dotnet bindings init version
2 parents 5536e1c + bcceea6 commit c222d33

File tree

19 files changed

+18243
-0
lines changed

19 files changed

+18243
-0
lines changed

.github/workflows/dotnet-test.yaml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: dotnet-test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- master
8+
tags:
9+
- '*'
10+
pull_request:
11+
branches:
12+
- main
13+
paths:
14+
- "dotnet/**"
15+
workflow_dispatch:
16+
17+
jobs:
18+
build-and-test:
19+
name: Test .NET Library of KCL
20+
runs-on: ${{ matrix.os }}
21+
defaults:
22+
run:
23+
working-directory: "dotnet"
24+
env:
25+
DevBuild: 'false'
26+
strategy:
27+
fail-fast: false
28+
matrix:
29+
build: [linux-debug, linux-release, macos-debug, macos-release, windows-debug, windows-release]
30+
include:
31+
- build: linux-debug
32+
os: ubuntu-latest
33+
config: debug
34+
- build: linux-release
35+
os: ubuntu-latest
36+
config: release
37+
- build: macos-debug
38+
os: macos-latest
39+
config: debug
40+
- build: macos-release
41+
os: macos-latest
42+
config: release
43+
- build: windows-debug
44+
os: windows-2019
45+
config: debug
46+
- build: windows-release
47+
os: windows-2019
48+
config: release
49+
steps:
50+
- uses: actions/checkout@v4
51+
- uses: actions/setup-dotnet@v4
52+
with:
53+
dotnet-version: '8.0.x'
54+
- name: Install Rust
55+
uses: actions-rs/toolchain@v1
56+
with:
57+
toolchain: 1.77
58+
override: true
59+
components: clippy, rustfmt
60+
- name: Build KCL Library
61+
run: cargo build --release
62+
- name: Clear package cache
63+
run: dotnet clean KclLib.sln && dotnet nuget locals all --clear
64+
- name: Enable development builds for the main branch
65+
if: github.ref == 'refs/heads/main' || github.base_ref == 'main'
66+
shell: bash
67+
run: |
68+
echo "DevBuild=true" >> $GITHUB_ENV
69+
- name: Restore packages
70+
run: dotnet restore KclLib.sln
71+
- name: Build
72+
run: dotnet build KclLib.sln -c ${{ matrix.config }} --no-restore
73+
- name: Test
74+
run: dotnet test KclLib.sln -c ${{ matrix.config }}
75+
- name: Create package
76+
run: |
77+
cd KclLib
78+
dotnet pack -c ${{ matrix.config }} /p:Packing=true

.github/workflows/go-test.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ on:
88
tags:
99
- '*'
1010
pull_request:
11+
branches:
12+
- main
13+
paths:
14+
- "lib/**"
15+
- "*.go"
16+
- "go.mod"
17+
- "go.sum"
1118
workflow_dispatch:
1219

1320
permissions:

.github/workflows/rust-test.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ on:
88
tags:
99
- '*'
1010
pull_request:
11+
branches:
12+
- main
13+
paths:
14+
- "Cargo.toml"
15+
- "src/**"
1116
workflow_dispatch:
1217

1318
permissions:

dotnet/.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*.swp
2+
*.*~
3+
.DS_Store
4+
5+
.vs/
6+
.vscode
7+
8+
bin/
9+
obj/
10+
11+
BenchmarkDotNet.Artifacts/

dotnet/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "kcl-lib-dotnet"
3+
publish = false
4+
version = "0.1.0"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
doc = false
9+
10+
[dependencies]
11+
kclvm-api = { git = "https://github.com/kcl-lang/kcl", version = "0.9.0" }

dotnet/KclLib.Tests/APITest.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
namespace KclLib.Tests;
2+
3+
using KclLib.API;
4+
5+
[TestClass]
6+
public class APITest
7+
{
8+
static string parentDirectory = FindCsprojInParentDirectory(Environment.CurrentDirectory);
9+
10+
[TestMethod]
11+
public void TestExecProgramAPI()
12+
{
13+
var api = new API();
14+
var execArgs = new ExecProgram_Args();
15+
var path = Path.Combine(parentDirectory, "test_data", "schema.k");
16+
execArgs.KFilenameList.Add(path);
17+
var result = api.ExecProgram(execArgs);
18+
Assert.AreEqual("app:\n replicas: 2", result.YamlResult, result.ToString());
19+
}
20+
21+
[TestMethod]
22+
public void TestExecProgramAPIFileNotFound()
23+
{
24+
var api = new API();
25+
var execArgs = new ExecProgram_Args();
26+
var path = Path.Combine(parentDirectory, "test_data", "file_not_found.k");
27+
execArgs.KFilenameList.Add(path);
28+
try
29+
{
30+
var result = api.ExecProgram(execArgs);
31+
Assert.Fail("No exception was thrown for non-existent file path");
32+
}
33+
catch (Exception ex)
34+
{
35+
Assert.AreEqual(true, ex.Message.Contains("ERROR"));
36+
Assert.AreEqual(true, ex.Message.Contains("Cannot find the kcl file"));
37+
}
38+
}
39+
40+
[TestMethod]
41+
public void TestListVariablesAPI()
42+
{
43+
var api = new API();
44+
var args = new ListVariables_Args();
45+
var path = Path.Combine(parentDirectory, "test_data", "schema.k");
46+
args.Files.Add(path);
47+
var result = api.ListVariables(args);
48+
Assert.AreEqual("AppConfig {replicas = 2}", result.Variables["app"].Variables[0].Value, result.ToString());
49+
}
50+
51+
static string FindCsprojInParentDirectory(string directory)
52+
{
53+
string parentDirectory = Directory.GetParent(directory).FullName;
54+
string csprojFilePath = Directory.GetFiles(parentDirectory, "*.csproj").FirstOrDefault();
55+
56+
if (csprojFilePath != null)
57+
{
58+
return parentDirectory;
59+
}
60+
else if (parentDirectory == directory)
61+
{
62+
return null;
63+
}
64+
else
65+
{
66+
return FindCsprojInParentDirectory(parentDirectory);
67+
}
68+
}
69+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="coverlet.collector" Version="6.0.0" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
15+
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
16+
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="../KclLib/KclLib.csproj" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<None Include="../target/release/libkcl_lib_dotnet.*">
29+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
30+
</None>
31+
<None Include="../target/release/kcl_lib_dotnet.*">
32+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
33+
</None>
34+
</ItemGroup>
35+
36+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
schema AppConfig:
3+
replicas: int = 1
4+
5+
app: AppConfig {
6+
replicas = 2
7+
}

dotnet/KclLib.sln

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.002.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "kcl_lib", "./KclLib/KclLib.csproj", "{925059E1-CFF5-4A12-A293-88344BAF33D9}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "kcl_lib_test", "./KclLib.Tests/KclLib.Tests.csproj", "{925059E1-CFF5-4A12-A293-88344BAF33D9}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{925059E1-CFF5-4A12-A293-88344BAF33D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{925059E1-CFF5-4A12-A293-88344BAF33D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{925059E1-CFF5-4A12-A293-88344BAF33D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{925059E1-CFF5-4A12-A293-88344BAF33D9}.Release|Any CPU.Build.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
GlobalSection(ExtensibilityGlobals) = postSolution
25+
SolutionGuid = {13F982F0-938D-4D8B-AB03-9D13D0F1D80F}
26+
EndGlobalSection
27+
EndGlobal

dotnet/KclLib/KclLib.csproj

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Google.Protobuf" Version="3.27.2" />
11+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Include="../target/release/libkcl_lib_dotnet.*">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Include="../target/release/kcl_lib_dotnet.*">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
23+
</Project>

0 commit comments

Comments
 (0)