Skip to content

Commit

Permalink
feat(core): better configuration options for target inference (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored Oct 25, 2023
1 parent 2d2cb5b commit 8a7b9a1
Show file tree
Hide file tree
Showing 46 changed files with 513 additions and 108 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_CONFIG_PROVENANCE: 'true'
VERBOSE_LOGGING: 'true'

post-release-smoke-tests:
Expand Down
47 changes: 47 additions & 0 deletions apps/docs-site/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ module.exports = {
// Please change this to your repo.
editUrl: 'https://github.com/nx-dotnet/nx-dotnet/edit/master/docs/',
path: '../../docs',
sidebarItemsGenerator: sidebarItemsSorter,
},
blog: {
showReadingTime: true,
Expand All @@ -114,4 +115,50 @@ module.exports = {
},
],
],
// plugins: [
// [
// '@docusaurus/plugin-content-docs',
// {

// path: '../../docs',
// },
// ],
// ],
};

async function sidebarItemsSorter({
defaultSidebarItemsGenerator,
docs,
...args
}) {
function sortCategoriesFirst(items, docsMap) {
const categories = [];
const docs = [];

for (const item of items) {
if (item.type === 'category') {
categories.push(item);
} else {
docs.push(item);
}
}

const index = docs.findIndex((item) => item.id === 'index');
if (index !== -1) {
const [doc] = docs.splice(index, 1);
categories.unshift(doc);
}
const sorted = [...categories, ...docs];

for (const item of sorted) {
if (item.items) {
item.items = sortCategoriesFirst(item.items, docsMap);
}
}

return sorted;
}
const docsMap = new Map(docs.map((doc) => [doc.id, doc]));
const sidebarItems = await defaultSidebarItemsGenerator({ docs, ...args });
return sortCategoriesFirst(sidebarItems, docsMap);
}
6 changes: 0 additions & 6 deletions apps/docs-site/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
"outputPath": "dist/apps/docs-site"
}
},
"prebuild": {
"executor": "nx:run-commands",
"options": {
"commands": ["echo"]
}
},
"serve": {
"executor": "@nx-plus/docusaurus:dev-server",
"options": {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
80 changes: 80 additions & 0 deletions docs/core/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Configuration

`@nx-dotnet/core` can be configured to update the project graph in various ways depending on your needs. Prior to Nx 17, this configuration was stored inside of a bespoke `.nx-dotnet.rc.json` file. As part of the updates made for Nx 17, this configuration is now stored inside of the plugins array inside `nx.json`. More details on the generic formatting of this configuration can be found in the [Nx documentation](https://nx.dev/reference/nx-json#plugins).

## Available Options

The most up to date list of options can be found embedded in @nx-dotnet/core's source code on [GitHub](https://github.com/nx-dotnet/nx-dotnet/blob/master/packages/utils/src/lib/models/nx-dotnet-config.interface.ts).

### `nugetPackages`

A key value map representing installed nuget packages in the workspace. Used by [`@nx-dotnet/core:nuget-reference`](./Generators/nuget-reference.md) to determine what version to install by default, and kept up to date by [`@nx-dotnet/core:sync`](./Generators/sync.md).

### `moduleBoundaries`

Provides an alternative way to define module boundaries for workspaces which do not include an eslint configuration file. `@nx-dotnet/core` adds a prebuild task by default to run module boundaries checks as described in the [nx documentation](https://nx.dev/recipes/enforce-module-boundaries), but the default configuration method of using .eslintrc.json doesn't make sense for C# only workspaces.

The `moduleBoundaries` configuration can be referenced on [Github](https://github.com/nx-dotnet/nx-dotnet/blob/master/packages/utils/src/lib/models/nx.ts).

### `solutionFile`

Describes a default solution file that projects should be added to on generation. This is described in more detail in the [solutions guide](./guides/handling-solutions.md).

### `inferProjects`

A boolean value which determines whether or not `@nx-dotnet/core` should attempt to infer projects from the workspace. If false, `@nx-dotnet/core` will not register any new projects or targets to the workspace. It will only add dependencies to existing projects.

If true, `@nx-dotnet/core` will attempt to infer projects from `.csproj`, `.fsproj`, or `.vbproj` files in the workspace.

### `inferredTargets`

Can be set to either `false` or a key value map describing how to infer targets from projects. If `false`, `@nx-dotnet/core` will not attempt to infer targets from projects. If a key value map, `@nx-dotnet/core` will attempt to infer targets from projects based on the provided configuration.

`@nx-dotnet/core` can infer the following targets:

- build
- test
- lint
- serve

The test target will only be added to the project if the project file mentions "Microsoft.NET.Test.Sdk".

As an example the following configuration will result in the `test` target being named "mstest" and the `serve` target not being added to the project. The `build` and `lint` targets will be added to the project with their default names.

```json
{
"plugins": [
{
"plugin": "@nx-dotnet/core",
"options": {
"inferredTargets": {
"test": "mstest",
"serve": false
}
}
}
]
}
```

### `ignorePaths`

An array of paths to ignore projects within. This is useful for workspaces which contain projects that are not .NET projects, or for workspaces which contain projects that are not intended to be built by Nx.

### `tags`

Defaults to ['nx-dotnet']. Can be used to add tags to all projects with information inferred by `@nx-dotnet/core`.

### Deprecated Options

:::danger

Properties below this line are from previous versions and support for them may vary. They will be removed in a future release, and should be updated to use configuration options described above.

:::

#### `inferProjectTargets`

A boolean value which determines whether or not `@nx-dotnet/core` should attempt to infer targets from projects. If false, `@nx-dotnet/core` will not register any new targets to the workspace. It will only add dependencies to existing targets.

Existing usages should be updated to use the [`inferTargets`](#infertargets) configuration option.
5 changes: 0 additions & 5 deletions docs/core/generators/init.md

This file was deleted.

36 changes: 18 additions & 18 deletions docs/core/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,76 +81,76 @@ If your workspace does not currently contain eslint, do not worry! You do not ha

## Generators

### [library](./generators/library.md)
### [library](./Generators/library.md)

Generate a new C# project as an Nx library

### [application](./generators/application.md)
### [application](./Generators/application.md)

Generate a new C# project as an Nx application

### [project-reference](./generators/project-reference.md)
### [project-reference](./Generators/project-reference.md)

Add a reference from one project to another

### [sync](./generators/sync.md)
### [sync](./Generators/sync.md)

Synchronizes NuGet references for the workspace

### [nuget-reference](./generators/nuget-reference.md)
### [nuget-reference](./Generators/nuget-reference.md)

Add a NuGet reference to a project

### [restore](./generators/restore.md)
### [restore](./Generators/restore.md)

Restores NuGet packages and .NET tools used by the workspace

### [test](./generators/test.md)
### [test](./Generators/test.md)

Generate a .NET test project for an existing application or library

### [import-projects](./generators/import-projects.md)
### [import-projects](./Generators/import-projects.md)

Import existing projects into your Nx workspace

### [add-swagger-target](./generators/add-swagger-target.md)
### [add-swagger-target](./Generators/add-swagger-target.md)

Add a swagger target to a webapi based project to extract swagger.json into a newly generated library project

### [swagger-typescript](./generators/swagger-typescript.md)
### [swagger-typescript](./Generators/swagger-typescript.md)

Generate a typescript library project based on an openapi/swagger specification file

### [move](./generators/move.md)
### [move](./Generators/move.md)

Moves a .NET project (including updating references)

## Executors

### [build](./executors/build.md)
### [build](./Executors/build.md)

Invokes `dotnet build` to build a project with .NET Core CLI

### [serve](./executors/serve.md)
### [serve](./Executors/serve.md)

Invokes `dotnet watch` in combination with `dotnet build` to run a dev-server

### [test](./executors/test.md)
### [test](./Executors/test.md)

Invokes `dotnet test` to execute unit tests via .NET Core CLI

### [publish](./executors/publish.md)
### [publish](./Executors/publish.md)

Invokes `dotnet publish`

### [format](./executors/format.md)
### [format](./Executors/format.md)

Formats and lints a project using the dotnet-format tool

### [update-swagger](./executors/update-swagger.md)
### [update-swagger](./Executors/update-swagger.md)

Generates a swagger document for an API project

### [openapi-codegen](./executors/openapi-codegen.md)
### [openapi-codegen](./Executors/openapi-codegen.md)

Invokes `nx g @nx-dotnet/core:swagger-typescript` with the proper parameters to update a codegen based library
File renamed without changes.
2 changes: 1 addition & 1 deletion docs/nx-ghpages/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ Run `nx test nx-ghpages` to execute the unit tests via [Jest](https://jestjs.io)

## Executors

### [deploy](./executors/deploy.md)
### [deploy](./Executors/deploy.md)

Deploy a site to the gh-pages branch of a specified repository.
File renamed without changes.
2 changes: 1 addition & 1 deletion docs/nxdoc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ npx nx g @nx-dotnet/nxdoc:generate-docs --outputPath docs

## Generators

### [generate-docs](./generators/generate-docs.md)
### [generate-docs](./Generators/generate-docs.md)

Automatic documentation generator for Nx Plugins
4 changes: 2 additions & 2 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
{
"plugin": "@nx-dotnet/core",
"options": {
"inferProjects": false,
"nugetPackages": {
"Swashbuckle.AspNetCore": "6.5.0"
},
"inferProjects": false
}
}
}
],
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
},
"devDependencies": {
"@babel/core": "^7.14.5",
"@babel/plugin-proposal-async-generator-functions": "^7.20.7",
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
"@babel/preset-react": "^7.14.5",
"@commitlint/cli": "^17.6.1",
"@commitlint/config-conventional": "^17.1.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/core/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
"version": "1.24.0",
"description": "Migration for v1.24.0",
"implementation": "./src/migrations/update-1.24.0/update-1.24.0"
},
"update-2.1.0": {
"version": "2.1.0",
"description": "Migration for v2.1.0",
"implementation": "./src/migrations/update-2.1.0/update-2.1.0"
}
}
}
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"semver": "7.5.4",
"tslib": "^2.5.0",
"xmldoc": "^1.1.2",
"yargs-parser": "^21.0.0"
"yargs-parser": "^21.0.0",
"minimatch": "3.1.2"
},
"peerDependencies": {
"@nx/js": ">=16.0.0-beta.1",
Expand Down
31 changes: 18 additions & 13 deletions packages/core/src/graph/create-nodes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { NxDotnetConfig } from '@nx-dotnet/utils';
import type { ResolvedConfig } from '@nx-dotnet/utils';
import type configUtils = require('@nx-dotnet/utils/src/lib/utility-functions/config');

import * as fs from 'fs';

let configValues: NxDotnetConfig = {
nugetPackages: {},
let configValues: Partial<ResolvedConfig> = {
inferProjects: true,
};

jest.mock(
Expand All @@ -18,24 +18,32 @@ jest.mock(
import { registerProjectTargets } from './create-nodes';

describe('infer-project', () => {
beforeEach(() => {
configValues = {
nugetPackages: {},
inferredTargets: {
build: 'build',
lint: 'lint',
serve: 'serve',
test: 'test',
},
ignorePaths: [],
tags: ['nx-dotnet'],
};
});

afterEach(() => {
jest.resetAllMocks();
});

it('should obey inferProjectTargets: false', () => {
configValues = {
nugetPackages: {},
inferProjectTargets: false,
};
configValues.inferredTargets = false;
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce('<project></project>');

expect(registerProjectTargets('libs/api/my.csproj')).toEqual({});
});

it('should generate build, lint, serve targets for projects', () => {
configValues = {
nugetPackages: {},
};
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce('<project></project>');

const targets = registerProjectTargets('libs/api/my.csproj');
Expand All @@ -46,9 +54,6 @@ describe('infer-project', () => {
});

it('should generate test target for test projects', () => {
configValues = {
nugetPackages: {},
};
jest
.spyOn(fs, 'readFileSync')
.mockReturnValueOnce('<project ref=Microsoft.NET.Test.Sdk></project>');
Expand Down
Loading

0 comments on commit 8a7b9a1

Please sign in to comment.