Skip to content

Commit

Permalink
docs(core): add dotnet recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacplmann authored and juristr committed Aug 28, 2023
1 parent b02a8a0 commit 2343053
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/generated/manifests/menus.json
Original file line number Diff line number Diff line change
Expand Up @@ -4225,6 +4225,14 @@
"children": [],
"disableCollapsible": false
},
{
"name": "Add a .NET Project",
"path": "/showcase/example-repos/add-dotnet",
"id": "add-dotnet",
"isExternal": false,
"children": [],
"disableCollapsible": false
},
{
"name": "Add a Nuxt Project",
"path": "/showcase/example-repos/add-nuxt",
Expand Down Expand Up @@ -4414,6 +4422,14 @@
"children": [],
"disableCollapsible": false
},
{
"name": "Add a .NET Project",
"path": "/showcase/example-repos/add-dotnet",
"id": "add-dotnet",
"isExternal": false,
"children": [],
"disableCollapsible": false
},
{
"name": "Add a Nuxt Project",
"path": "/showcase/example-repos/add-nuxt",
Expand Down Expand Up @@ -4561,6 +4577,14 @@
"children": [],
"disableCollapsible": false
},
{
"name": "Add a .NET Project",
"path": "/showcase/example-repos/add-dotnet",
"id": "add-dotnet",
"isExternal": false,
"children": [],
"disableCollapsible": false
},
{
"name": "Add a Nuxt Project",
"path": "/showcase/example-repos/add-nuxt",
Expand Down
30 changes: 30 additions & 0 deletions docs/generated/manifests/nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -5267,6 +5267,16 @@
"path": "/showcase/example-repos/add-rust",
"tags": []
},
{
"id": "add-dotnet",
"name": "Add a .NET Project",
"description": "Add a .NET project to your repo",
"file": "shared/recipes/add-stack/add-dotnet",
"itemList": [],
"isExternal": false,
"path": "/showcase/example-repos/add-dotnet",
"tags": []
},
{
"id": "add-nuxt",
"name": "Add a Nuxt Project",
Expand Down Expand Up @@ -5504,6 +5514,16 @@
"path": "/showcase/example-repos/add-rust",
"tags": []
},
{
"id": "add-dotnet",
"name": "Add a .NET Project",
"description": "Add a .NET project to your repo",
"file": "shared/recipes/add-stack/add-dotnet",
"itemList": [],
"isExternal": false,
"path": "/showcase/example-repos/add-dotnet",
"tags": []
},
{
"id": "add-nuxt",
"name": "Add a Nuxt Project",
Expand Down Expand Up @@ -5689,6 +5709,16 @@
"path": "/showcase/example-repos/add-rust",
"tags": []
},
"/showcase/example-repos/add-dotnet": {
"id": "add-dotnet",
"name": "Add a .NET Project",
"description": "Add a .NET project to your repo",
"file": "shared/recipes/add-stack/add-dotnet",
"itemList": [],
"isExternal": false,
"path": "/showcase/example-repos/add-dotnet",
"tags": []
},
"/showcase/example-repos/add-nuxt": {
"id": "add-nuxt",
"name": "Add a Nuxt Project",
Expand Down
6 changes: 6 additions & 0 deletions docs/map.json
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,12 @@
"description": "Add a Rust project to your repo",
"file": "shared/recipes/add-stack/add-rust"
},
{
"name": "Add a .NET Project",
"id": "add-dotnet",
"description": "Add a .NET project to your repo",
"file": "shared/recipes/add-stack/add-dotnet"
},
{
"name": "Add a Nuxt Project",
"id": "add-nuxt",
Expand Down
165 changes: 165 additions & 0 deletions docs/shared/recipes/add-stack/add-dotnet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# Add a New .NET Project

**Supported Features**

Because we are using an Nx plugin for .NET, all the features of Nx are available.

{% pill url="/core-features/run-tasks" %}✅ Run Tasks{% /pill %}
{% pill url="/core-features/cache-task-results" %}✅ Cache Task Results{% /pill %}
{% pill url="/core-features/remote-cache" %}✅ Share Your Cache{% /pill %}
{% pill url="/core-features/explore-graph" %}✅ Explore the Graph{% /pill %}
{% pill url="/core-features/distribute-task-execution" %}✅ Distribute Task Execution{% /pill %}
{% pill url="/core-features/integrate-with-editors" %}✅ Integrate with Editors{% /pill %}
{% pill url="/core-features/automate-updating-dependencies" %}✅ Automate Updating Nx{% /pill %}
{% pill url="/core-features/enforce-module-boundaries" %}✅ Enforce Module Boundaries{% /pill %}
{% pill url="/core-features/plugin-features/use-task-executors" %}✅ Use Task Executors{% /pill %}
{% pill url="/core-features/plugin-features/use-code-generators" %}✅ Use Code Generators{% /pill %}
{% pill url="/core-features/automate-updating-dependencies" %}✅ Automate Updating Framework Dependencies{% /pill %}

## Install the @nx-dotnet/core Plugin

{% callout type="warning" title="Have .NET already installed?" %}
Make sure you have .NET installed on your machine. Consult the [.NET docs for more details](https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/install)
{% /callout %}

{% tabs %}
{%tab label="npm"%}

```shell
npm i --save-dev @nx-dotnet/core
```

{% /tab %}
{%tab label="yarn"%}

```shell
yarn add --dev @nx-dotnet/core
```

{% /tab %}
{% /tabs %}

## Set up your workspace

Use the `init` generator to scaffold out some root level configuration files.

```shell
nx g @nx-dotnet/core:init
```

This generates the following files:

```json {% fileName=".config/dotnet-tools.json" %}
{
"version": 1,
"isRoot": true,
"tools": {}
}
```

```json {% fileName=".nx-dotnet.rc.json" %}
{
"nugetPackages": {}
}
```

```xml {% fileName="Directory.Build.props" %}
<!--
This file is imported early in the build order.
Use it to set default property values that can be overridden in specific projects.
-->
<Project>
<PropertyGroup>
<!-- Output path configuration -->
<RepoRoot>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)'))</RepoRoot>
<ProjectRelativePath>$([MSBuild]::MakeRelative($(RepoRoot), $(MSBuildProjectDirectory)))</ProjectRelativePath>
<BaseOutputPath>$(RepoRoot)dist/$(ProjectRelativePath)</BaseOutputPath>
<OutputPath>$(BaseOutputPath)</OutputPath>
<BaseIntermediateOutputPath>$(RepoRoot)dist/intermediates/$(ProjectRelativePath)/obj</BaseIntermediateOutputPath>
<IntermediateOutputPath>$(BaseIntermediateOutputPath)</IntermediateOutputPath>
<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath>
</PropertyGroup>
<PropertyGroup>
<RestorePackagesWithLockFile>false</RestorePackagesWithLockFile>
</PropertyGroup>
</Project>
```

```xml {% fileName="Directory.Build.targets" %}
<!--
This file is imported late in the build order.
Use it to override properties and define dependent properties.
-->
<Project>
<PropertyGroup>
<MSBuildProjectDirRelativePath>$([MSBuild]::MakeRelative($(RepoRoot), $(MSBuildProjectDirectory)))</MSBuildProjectDirRelativePath>
<NodeModulesRelativePath>$([MSBuild]::MakeRelative($(MSBuildProjectDirectory), $(RepoRoot)))</NodeModulesRelativePath>
</PropertyGroup>
<Target Name="CheckNxModuleBoundaries" BeforeTargets="Build">
<Exec Command="node $(NodeModulesRelativePath)/node_modules/@nx-dotnet/core/src/tasks/check-module-boundaries.js --project-root &quot;$(MSBuildProjectDirRelativePath)&quot;"/>
</Target>
</Project>
```

## Create an Application

Use the `app` generator to create a new .NET app. For this demo, use the `nx` path naming convention and the `web-api` project template.

```shell
nx g @nx-dotnet/core:app my-api --test-template nunit --language C#
```

Serve the API by running

```shell
nx serve my-api
```

## Create a Library

To create a new library, run the library generator. Use the `classlib` template.

```shell
nx g @nx-dotnet/core:lib dotnet-lib
```

We also want to add a project reference from `my-api` to `dotnet-lib` using the `project-reference` generator:

```shell
nx generate @nx-dotnet/core:project-reference --project=my-api --reference=dotnet-lib
```

Now we can move the `WeatherForecast.cs` file out of the `my-api` folder and into the `dotnet-lib` folder. We also need to update the namespace for the file like this:

```c# {% fileName="libs/dotnet-lib/WeatherForecast.cs" %}
namespace DotnetLib;

public class WeatherForecast
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
```

Now use the `dotnet-lib` version of `WeatherForecast` in `my-api`:

```c# {% fileName="apps/my-api/Controllers/WeatherForecastController.cs" %}
using Microsoft.AspNetCore.Mvc;
using DotnetLib;

namespace MyApi.Controllers;

// the rest of the file is unchanged
```

Now when you serve your api it will use the class from the library.

## More Documentation

- [nx-dotnet](https://www.nx-dotnet.com/)
- [.NET](https://dotnet.microsoft.com/en-us/)
1 change: 1 addition & 0 deletions docs/shared/reference/sitemap.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
- [Add a Solid Project](/showcase/example-repos/add-solid)
- [Add a Qwik Project](/showcase/example-repos/add-qwik)
- [Add a Rust Project](/showcase/example-repos/add-rust)
- [Add a .NET Project](/showcase/example-repos/add-dotnet)
- [Add a Nuxt Project](/showcase/example-repos/add-nuxt)
- [Add an Astro Project](/showcase/example-repos/add-astro)
- [Add a Vue Project](/showcase/example-repos/add-vue)
Expand Down

1 comment on commit 2343053

@vercel
Copy link

@vercel vercel bot commented on 2343053 Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev
nx-five.vercel.app

Please sign in to comment.