66 NxJsonProjectConfiguration ,
77 ProjectConfiguration ,
88 ProjectType ,
9+ readProjectConfiguration ,
910 readWorkspaceConfiguration ,
1011 Tree ,
1112} from '@nrwl/devkit' ;
@@ -25,12 +26,13 @@ import {
2526 GetBuildExecutorConfiguration ,
2627 GetLintExecutorConfiguration ,
2728 GetServeExecutorConfig ,
28- GetTestExecutorConfig ,
2929 NxDotnetProjectGeneratorSchema ,
30+ NxDotnetTestGeneratorSchema ,
3031} from '../../models' ;
3132import initSchematic from '../init/generator' ;
33+ import { GenerateTestProject } from './generate-test-project' ;
3234
33- interface NormalizedSchema extends NxDotnetProjectGeneratorSchema {
35+ export interface NormalizedSchema extends NxDotnetProjectGeneratorSchema {
3436 projectName : string ;
3537 projectRoot : string ;
3638 projectDirectory : string ;
@@ -39,13 +41,32 @@ interface NormalizedSchema extends NxDotnetProjectGeneratorSchema {
3941 parsedTags : string [ ] ;
4042 className : string ;
4143 namespaceName : string ;
44+ projectType : ProjectType ;
4245}
4346
44- function normalizeOptions (
47+ export function normalizeOptions (
4548 host : Tree ,
46- options : NxDotnetProjectGeneratorSchema ,
47- projectType : ProjectType ,
49+ options : NxDotnetProjectGeneratorSchema | NxDotnetTestGeneratorSchema ,
50+ projectType ? : ProjectType ,
4851) : NormalizedSchema {
52+ if ( ! ( 'name' in options ) ) {
53+ // Reconstruct the original parameters as if the test project were generated at the same time as the target project.
54+ const project = readProjectConfiguration ( host , options . project ) ;
55+ const projectPaths = project . root . split ( '/' ) ;
56+ const directory = projectPaths . slice ( 1 , - 1 ) . join ( '/' ) ; // The middle portions contain the original path.
57+ const [ name ] = projectPaths . slice ( - 1 ) ; // The final folder contains the original name.
58+
59+ options = {
60+ name,
61+ language : options . language ,
62+ skipOutputPathManipulation : options . skipOutputPathManipulation ,
63+ testTemplate : options . testTemplate ,
64+ directory,
65+ tags : project . tags ?. join ( ',' ) ,
66+ } as NxDotnetProjectGeneratorSchema ;
67+ projectType = project . projectType ;
68+ }
69+
4970 const name = names ( options . name ) . fileName ;
5071 const className = names ( options . name ) . className ;
5172 const projectDirectory = options . directory
@@ -79,61 +100,11 @@ function normalizeOptions(
79100 projectLanguage : options . language ,
80101 projectTemplate : options . template ,
81102 namespaceName,
103+ projectType : projectType ?? 'library' ,
82104 } ;
83105}
84106
85- async function GenerateTestProject (
86- schema : NormalizedSchema ,
87- host : Tree ,
88- dotnetClient : DotNetClient ,
89- projectType : ProjectType ,
90- ) {
91- const testRoot = schema . projectRoot + '-test' ;
92- const testProjectName = schema . projectName + '-test' ;
93-
94- addProjectConfiguration ( host , testProjectName , {
95- root : testRoot ,
96- projectType : projectType ,
97- sourceRoot : `${ testRoot } ` ,
98- targets : {
99- build : GetBuildExecutorConfiguration ( testRoot ) ,
100- test : GetTestExecutorConfig ( ) ,
101- lint : GetLintExecutorConfiguration ( ) ,
102- } ,
103- tags : schema . parsedTags ,
104- } ) ;
105-
106- const newParams : dotnetNewOptions = [
107- {
108- flag : 'language' ,
109- value : schema . language ,
110- } ,
111- {
112- flag : 'name' ,
113- value : schema . namespaceName + '.Test' ,
114- } ,
115- {
116- flag : 'output' ,
117- value : schema . projectRoot + '-test' ,
118- } ,
119- ] ;
120-
121- if ( isDryRun ( ) ) {
122- addDryRunParameter ( newParams ) ;
123- }
124-
125- dotnetClient . new ( schema . testTemplate , newParams ) ;
126-
127- if ( ! isDryRun ( ) && ! schema . skipOutputPathManipulation ) {
128- const testCsProj = await findProjectFileInPath ( testRoot ) ;
129- SetOutputPath ( host , testRoot , testCsProj ) ;
130- const baseCsProj = await findProjectFileInPath ( schema . projectRoot ) ;
131- SetOutputPath ( host , schema . projectRoot , baseCsProj ) ;
132- dotnetClient . addProjectReference ( testCsProj , baseCsProj ) ;
133- }
134- }
135-
136- function SetOutputPath (
107+ export function SetOutputPath (
137108 host : Tree ,
138109 projectRootPath : string ,
139110 projectFilePath : string ,
@@ -227,12 +198,7 @@ export async function GenerateProject(
227198 dotnetClient . new ( normalizedOptions . template , newParams ) ;
228199
229200 if ( options [ 'testTemplate' ] !== 'none' ) {
230- await GenerateTestProject (
231- normalizedOptions ,
232- host ,
233- dotnetClient ,
234- projectType ,
235- ) ;
201+ await GenerateTestProject ( host , normalizedOptions , dotnetClient ) ;
236202 } else if ( ! options . skipOutputPathManipulation ) {
237203 SetOutputPath (
238204 host ,
@@ -244,7 +210,7 @@ export async function GenerateProject(
244210 await formatFiles ( host ) ;
245211}
246212
247- function addDryRunParameter ( parameters : dotnetNewOptions ) : void {
213+ export function addDryRunParameter ( parameters : dotnetNewOptions ) : void {
248214 parameters . push ( {
249215 flag : 'dryRun' ,
250216 value : true ,
0 commit comments