feat: support .NET

Signed-off-by: Olli Janatuinen <olli.janatuinen@gmail.com>
This commit is contained in:
Olli Janatuinen 2022-04-03 15:12:15 +02:00
parent f3ac279f73
commit 65db7e9824
15 changed files with 304 additions and 0 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@
*.dylib *.dylib
/cmd/dagger/dagger /cmd/dagger/dagger
/cmd/dagger/dagger-debug /cmd/dagger/dagger-debug
**/obj/*
# Test binary, build with `go test -c` # Test binary, build with `go test -c`
*.test *.test

View File

@ -0,0 +1,31 @@
// .NET operation
package dotnet
import (
"dagger.io/dagger"
"universe.dagger.io/docker"
)
// A standalone dotnet environment to run dotnet command
#Container: {
// Container app name
name: *"dotnet_publisher" | string
// Source code
source: dagger.#FS
// Use dotnet image
_image: #Image
_sourcePath: "/src"
docker.#Run & {
input: *_image.output | docker.#Image
workdir: "/src"
command: name: "dotnet"
mounts: "source": {
dest: _sourcePath
contents: source
}
}
}

View File

@ -0,0 +1,20 @@
package dotnet
import (
"universe.dagger.io/docker"
)
// .NET image default version
_#DefaultVersion: "6.0"
// Pull a dotnet base image
#Image: {
version: *_#DefaultVersion | string
docker.#Build & {
steps: [
docker.#Pull & {
source: "mcr.microsoft.com/dotnet/sdk:\(version)-alpine"
},
]
}
}

View File

@ -0,0 +1,31 @@
package dotnet
import (
"dagger.io/dagger"
)
// Publish a dotnet binary
#Publish: {
// Source code
source: dagger.#FS
// Target package to publish
package: *"." | string
env: [string]: string
container: #Container & {
"source": source
command: {
args: [package]
flags: {
publish: true
"-o": "/output/"
}
}
export: directories: "/output": _
}
// Directory containing the output of the publish
output: container.export.directories."/output"
}

View File

@ -0,0 +1,14 @@
package dotnet
// Test a dotnet package
#Test: {
// Package to test
package: *"." | string
#Container & {
command: {
args: [package]
flags: test: true
}
}
}

View File

@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Greeting\Greeting.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,14 @@
using System;
using Xunit;
public class Greeting_Should
{
[Fact]
public void Greeting_PrintName()
{
var name = "Dagger Test";
var expect = "Hi Dagger Test!";
var value = Greeting.GetMessage(name);
Assert.Equal(expect, value);
}
}

View File

@ -0,0 +1,6 @@
public class Greeting
{
public static string GetMessage(string name) {
return String.Format("Hi {0}!", name);
}
}

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,13 @@
using System;
public class Program
{
public static void Main()
{
var name = Environment.GetEnvironmentVariable("NAME");
if (String.IsNullOrEmpty(name)) {
name = "John Doe";
}
Console.Write(Greeting.GetMessage(name));
}
}

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>linux-musl-x64</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Greeting\Greeting.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,39 @@
package dotnet
import (
"dagger.io/dagger"
"universe.dagger.io/x/olli.janatuinen@gmail.com/dotnet"
"universe.dagger.io/docker"
)
dagger.#Plan & {
actions: test: {
_source: dagger.#Scratch & {}
simple: {
_image: dotnet.#Image & {}
verify: docker.#Run & {
input: _image.output
command: {
name: "/bin/sh"
args: ["-c", "dotnet --list-sdks | grep '6.0'"]
}
}
}
custom: {
_image: dotnet.#Image & {
version: "5.0"
}
verify: docker.#Run & {
input: _image.output
command: {
name: "/bin/sh"
args: ["-c", "dotnet --list-sdks | grep '5.0'"]
}
}
}
}
}

View File

@ -0,0 +1,58 @@
package dotnet
import (
"dagger.io/dagger"
"dagger.io/dagger/core"
"universe.dagger.io/x/olli.janatuinen@gmail.com/dotnet"
"universe.dagger.io/docker"
"universe.dagger.io/alpine"
)
dagger.#Plan & {
client: filesystem: "./data": read: contents: dagger.#FS
actions: test: {
_baseImage: {
build: alpine.#Build & {
packages: {
"ca-certificates": {}
"krb5-libs": {}
libgcc: {}
libintl: {}
"libssl1.1": {}
"libstdc++": {}
zlib: {}
}
}
output: build.output
}
simple: {
publish: dotnet.#Publish & {
source: client.filesystem."./data".read.contents
package: "hello"
}
exec: docker.#Run & {
input: _baseImage.output
command: {
name: "/bin/sh"
args: ["-c", "/app/hello >> /output.txt"]
}
env: NAME: "dagger"
mounts: binary: {
dest: "/app"
contents: publish.output
source: "/"
}
}
verify: core.#ReadFile & {
input: exec.output.rootfs
path: "/output.txt"
} & {
contents: "Hi dagger!"
}
}
}
}

View File

@ -0,0 +1,11 @@
setup() {
load '../../../../bats_helpers'
common_setup
}
@test "dotnet" {
dagger "do" -p ./publish.cue test
dagger "do" -p ./image.cue test
dagger "do" -p ./test.cue test
}

View File

@ -0,0 +1,15 @@
package dotnet
import (
"dagger.io/dagger"
"universe.dagger.io/x/olli.janatuinen@gmail.com/dotnet"
)
dagger.#Plan & {
client: filesystem: "./data": read: contents: dagger.#FS
actions: test: dotnet.#Test & {
source: client.filesystem."./data".read.contents
package: "./Greeting.Tests"
}
}