99 Tree ,
1010 writeJson ,
1111 NX_VERSION ,
12+ readNxJson ,
1213} from '@nx/devkit' ;
1314
1415import { DotNetClient , dotnetFactory } from '@nx-dotnet/dotnet' ;
@@ -19,38 +20,47 @@ import {
1920 resolve ,
2021} from '@nx-dotnet/utils' ;
2122import * as path from 'path' ;
23+ import { major } from 'semver' ;
2224
2325const noop = ( ) => void 0 ;
2426
2527export async function initGenerator (
2628 host : Tree ,
27- _ : null , // Nx will populate this with options, which are currently unused.
29+ _ : unknown , // Nx will populate this with options, which are currently unused.
2830 dotnetClient = new DotNetClient ( dotnetFactory ( ) ) ,
2931) {
3032 const tasks : GeneratorCallback [ ] = [ ] ;
31- const initialized = host . isFile ( CONFIG_FILE_PATH ) ;
3233
33- const configObject : NxDotnetConfig = initialized
34- ? readJson ( host , CONFIG_FILE_PATH )
35- : {
36- nugetPackages : { } ,
37- } ;
34+ // Prior to Nx 17, nx-dotnet had a custom config file.
35+ if ( major ( NX_VERSION ) < 17 ) {
36+ const configObject : NxDotnetConfig = host . isFile ( CONFIG_FILE_PATH )
37+ ? readJson ( host , CONFIG_FILE_PATH )
38+ : {
39+ nugetPackages : { } ,
40+ } ;
3841
39- configObject . nugetPackages = configObject . nugetPackages || { } ;
42+ configObject . nugetPackages = configObject . nugetPackages || { } ;
4043
41- host . write ( CONFIG_FILE_PATH , JSON . stringify ( configObject , null , 2 ) ) ;
44+ host . write ( CONFIG_FILE_PATH , JSON . stringify ( configObject , null , 2 ) ) ;
45+ }
4246
43- updateNxJson ( host ) ;
47+ const nxJson = readNxJson ( host ) ;
4448
45- if ( ! initialized ) {
46- addPrepareScript ( host ) ;
47- tasks . push ( installNpmPackages ( host ) ) ;
48- }
49+ // Adds a `dotnet restore` operation to the prepare script.
50+ addPrepareScript ( host ) ;
51+
52+ // Adds @nx -dotnet/core to nxJson
53+ updateNxJson ( host , nxJson ) ;
4954
55+ // Setups up the .config/dotnet-tools.json for managing local .NET tools.
5056 initToolManifest ( host , dotnetClient ) ;
5157
58+ // Creates Directory.Build.* to customize default C# builds.
5259 initBuildCustomization ( host ) ;
5360
61+ // Adds @nx /js to package.json
62+ tasks . push ( installNpmPackages ( host ) ) ;
63+
5464 return async ( ) => {
5565 for ( const task of tasks ) {
5666 await task ( ) ;
@@ -74,13 +84,19 @@ function installNpmPackages(host: Tree): GeneratorCallback {
7484 }
7585}
7686
77- function updateNxJson ( host : Tree ) {
78- const nxJson : NxJsonConfiguration = readJson ( host , 'nx.json' ) ;
79- nxJson . plugins = nxJson . plugins || [ ] ;
80- if ( ! nxJson . plugins . some ( ( x ) => x === '@nx-dotnet/core' ) ) {
87+ function hasPluginInNxJson ( nxJson : NxJsonConfiguration | null ) : boolean {
88+ return ! ! nxJson ?. plugins ?. some ( ( x ) => {
89+ const plugin = typeof x === 'string' ? x : x . plugin ;
90+ return plugin === '@nx-dotnet/core' ;
91+ } ) ;
92+ }
93+
94+ function updateNxJson ( host : Tree , nxJson : NxJsonConfiguration | null ) {
95+ if ( nxJson && ! hasPluginInNxJson ( nxJson ) ) {
96+ nxJson . plugins = nxJson . plugins || [ ] ;
8197 nxJson . plugins . push ( '@nx-dotnet/core' ) ;
98+ writeJson ( host , 'nx.json' , nxJson ) ;
8299 }
83- writeJson ( host , 'nx.json' , nxJson ) ;
84100}
85101
86102function initToolManifest ( host : Tree , dotnetClient : DotNetClient ) {
0 commit comments