@@ -10,9 +10,11 @@ import {
1010 Tree ,
1111} from '@nrwl/devkit' ;
1212
13+ // Files generated via `dotnet` are not available in the virtual fs
1314import { readFileSync , writeFileSync } from 'fs' ;
15+
1416import { dirname , relative } from 'path' ;
15- import { XmlDocument , XmlNode , XmlTextNode } from 'xmldoc' ;
17+ import { XmlDocument } from 'xmldoc' ;
1618
1719import { DotNetClient , dotnetNewOptions } from '@nx-dotnet/dotnet' ;
1820import {
@@ -84,41 +86,18 @@ export function normalizeOptions(
8486 } ;
8587}
8688
87- export function SetOutputPath (
89+ export async function manipulateXmlProjectFile (
8890 host : Tree ,
89- projectRootPath : string ,
90- projectFilePath : string ,
91- ) : void {
91+ options : NormalizedSchema ,
92+ ) : Promise < void > {
93+ const projectFilePath = await findProjectFileInPath ( options . projectRoot ) ;
94+
9295 const xml : XmlDocument = new XmlDocument (
9396 readFileSync ( projectFilePath ) . toString ( ) ,
9497 ) ;
9598
96- let outputPath = `${ relative (
97- dirname ( projectFilePath ) ,
98- process . cwd ( ) ,
99- ) } /dist/${ projectRootPath } `;
100- outputPath = outputPath . replace ( '\\' , '/' ) ; // Forward slash works on windows, backslash does not work on mac/linux
101-
102- const textNode : Partial < XmlTextNode > = {
103- text : outputPath ,
104- type : 'text' ,
105- } ;
106- textNode . toString = ( ) => textNode . text ?? '' ;
107- textNode . toStringWithIndent = ( ) => textNode . text ?? '' ;
108-
109- const el : Partial < XmlNode > = {
110- name : 'OutputPath' ,
111- attr : { } ,
112- type : 'element' ,
113- children : [ textNode as XmlTextNode ] ,
114- firstChild : null ,
115- lastChild : null ,
116- } ;
117-
118- el . toStringWithIndent = xml . toStringWithIndent . bind ( el ) ;
119- el . toString = xml . toString . bind ( el ) ;
120-
121- xml . childNamed ( 'PropertyGroup' ) ?. children . push ( el as XmlNode ) ;
99+ setOutputPath ( xml , options . projectRoot , projectFilePath ) ;
100+ addPrebuildMsbuildTask ( host , options , xml ) ;
122101
123102 writeFileSync ( projectFilePath , xml . toString ( ) ) ;
124103}
@@ -180,12 +159,10 @@ export async function GenerateProject(
180159
181160 if ( options [ 'testTemplate' ] !== 'none' ) {
182161 await GenerateTestProject ( host , normalizedOptions , dotnetClient ) ;
183- } else if ( ! options . skipOutputPathManipulation ) {
184- SetOutputPath (
185- host ,
186- normalizedOptions . projectRoot ,
187- await findProjectFileInPath ( normalizedOptions . projectRoot ) ,
188- ) ;
162+ }
163+
164+ if ( ! options . skipOutputPathManipulation && ! isDryRun ( ) ) {
165+ await manipulateXmlProjectFile ( host , normalizedOptions ) ;
189166 }
190167
191168 await formatFiles ( host ) ;
@@ -197,3 +174,37 @@ export function addDryRunParameter(parameters: dotnetNewOptions): void {
197174 value : true ,
198175 } ) ;
199176}
177+
178+ export function setOutputPath (
179+ xml : XmlDocument ,
180+ projectRootPath : string ,
181+ projectFilePath : string ,
182+ ) {
183+ let outputPath = `${ relative (
184+ dirname ( projectFilePath ) ,
185+ process . cwd ( ) ,
186+ ) } /dist/${ projectRootPath } `;
187+ outputPath = outputPath . replace ( '\\' , '/' ) ; // Forward slash works on windows, backslash does not work on mac/linux
188+
189+ const fragment = new XmlDocument ( `<OutputPath>${ outputPath } </OutputPath>` ) ;
190+ xml . childNamed ( 'PropertyGroup' ) ?. children . push ( fragment ) ;
191+ }
192+
193+ export function addPrebuildMsbuildTask (
194+ host : Tree ,
195+ options : { projectRoot : string ; name : string } ,
196+ xml : XmlDocument ,
197+ ) {
198+ const scriptPath = relative (
199+ options . projectRoot ,
200+ require . resolve ( '@nx-dotnet/core/src/tasks/check-module-boundaries' ) ,
201+ ) ;
202+
203+ const fragment = new XmlDocument ( `
204+ <Target Name="CheckNxModuleBoundaries" BeforeTargets="Build">
205+ <Exec Command="node ${ scriptPath } -p ${ options . name } "/>
206+ </Target>
207+ ` ) ;
208+
209+ xml . children . push ( fragment ) ;
210+ }
0 commit comments