Skip to content

Commit

Permalink
fix(core): app generation should work in encapuslated Nx workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Feb 24, 2023
1 parent d4fca4d commit 13c0a2c
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 81 deletions.
4 changes: 0 additions & 4 deletions e2e/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { tmpProjPath } from '@nrwl/nx-plugin/testing';
import { ChildProcess, exec, execSync } from 'child_process';
import {
copySync,
createFileSync,
ensureDirSync,
moveSync,
readdirSync,
readFileSync,
removeSync,
Expand All @@ -13,10 +11,8 @@ import {
writeFileSync,
} from 'fs-extra';
import * as path from 'path';
import { dirSync } from 'tmp';

import * as isCI from 'is-ci';
import { workspaceConfigName } from 'nx/src/config/workspaces';
import { detectPackageManager } from '@nrwl/devkit';
interface RunCmdOpts {
silenceError?: boolean;
Expand Down
13 changes: 10 additions & 3 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
"dependsOn": ["^build", "prebuild", "^prebuild", "^codegen", "codegen"],
"inputs": ["production", "^production"]
},
"@nx-dotnet/core:build": {
"dependsOn": ["^build", "prebuild", "^prebuild", "^codegen", "codegen"],
"inputs": [
"production",
"^production",
"{workspaceRoot}/Directory.Build.props",
"{workspaceRoot}/Directory.Build.targets"
]
},
"codegen": {
"dependsOn": ["^swagger", "swagger"]
},
Expand All @@ -63,9 +72,7 @@
"{workspaceRoot}/tsconfig.base.json",
"{workspaceRoot}/tslint.json",
"{workspaceRoot}/nx.json",
"{workspaceRoot}/babel.config.json",
"{workspaceRoot}/Directory.Build.props",
"{workspaceRoot}/Directory.Build.targets"
"{workspaceRoot}/babel.config.json"
],
"production": [
"default",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@nrwl/js": "15.8.0-beta.2",
"@nrwl/linter": "15.8.0-beta.2",
"@nrwl/node": "15.8.0-beta.2",
"@nrwl/nx-cloud": "15.1.0",
"@nrwl/nx-cloud": "15.1.1",
"@nrwl/nx-plugin": "15.8.0-beta.2",
"@nrwl/react": "15.8.0-beta.2",
"@nrwl/web": "15.8.0-beta.2",
Expand Down
151 changes: 87 additions & 64 deletions packages/core/src/generators/add-swagger-target/add-swagger-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
updateNxJson,
addDependenciesToPackageJson,
} from '@nrwl/devkit';
import { libraryGenerator } from '@nrwl/js/src/generators/library/library';
import type JsLibraryGenerator = require('@nrwl/js/src/generators/library/library');

import type NxPluginOpenAPILibGenerator = require('@trumbitta/nx-plugin-openapi/src/generators/api-lib/generator');
import type NxPluginOpenAPIInitGenerator = require('@trumbitta/nx-plugin-openapi/src/generators/init/generator');
Expand Down Expand Up @@ -121,76 +121,99 @@ async function generateCodegenProject(
const tasks: GeneratorCallback[] = [];
const nameWithDirectory = `generated-${options.codegenProject}`;
if (options.useNxPluginOpenAPI) {
ensurePackage(host, '@trumbitta/nx-plugin-openapi', '^1.12.1');
tasks.push(
addDependenciesToPackageJson(
host,
{},
{ '@trumbitta/nx-plugin-openapi': '^1.12.1' },
),
);
const {
default: nxPluginOpenAPIGenerator,
}: // eslint-disable-next-line @typescript-eslint/no-var-requires
typeof NxPluginOpenAPILibGenerator = require('@trumbitta/nx-plugin-openapi/src/generators/api-lib/generator');
const {
default: nxPluginOpenAPIInitGenerator,
}: // eslint-disable-next-line @typescript-eslint/no-var-requires
typeof NxPluginOpenAPIInitGenerator = require('@trumbitta/nx-plugin-openapi/src/generators/init/generator');

tasks.push(await nxPluginOpenAPIInitGenerator(host));

tasks.push(
await nxPluginOpenAPIGenerator(host, {
isRemoteSpec: false,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
name: options.codegenProject!,
directory: 'generated',
generator: 'typescript-fetch',
sourceSpecLib: options.swaggerProject,
}),
);

const configuration = readProjectConfiguration(host, nameWithDirectory);
configuration.targets ??= {};
const targetConfiguration = configuration.targets?.['generate-sources'];
targetConfiguration.options['sourceSpecPathOrUrl'] = joinPathFragments(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
readProjectConfiguration(host, options.swaggerProject!).root,
'swagger.json',
);
targetConfiguration.dependsOn = ['^swagger'];
configuration.targets['codegen'] = targetConfiguration;
delete configuration.targets['generate-sources'];
updateProjectConfiguration(host, nameWithDirectory, configuration);
await setupOpenAPICodegen(host, tasks, options, nameWithDirectory);
} else {
tasks.push(
await libraryGenerator(host, {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
name: options.codegenProject!,
directory: 'generated',
buildable: true,
}),
);
const codegenProjectConfiguration = readProjectConfiguration(
host,
nameWithDirectory,
);
codegenProjectConfiguration.implicitDependencies ??= [];
codegenProjectConfiguration.implicitDependencies.push(
options.swaggerProject ? options.swaggerProject : options.project,
);
updateProjectConfiguration(
host,
nameWithDirectory,
codegenProjectConfiguration,
);
await setupNxNETCodegen(tasks, host, options, nameWithDirectory);
}

updateNxJsonForCodegenTargets(host, options);

return tasks;
}

async function setupOpenAPICodegen(
host: Tree,
tasks: GeneratorCallback[],
options: AddSwaggerJsonExecutorSchema,
nameWithDirectory: string,
) {
ensurePackage(host, '@trumbitta/nx-plugin-openapi', '^1.12.1');
tasks.push(
addDependenciesToPackageJson(
host,
{},
{ '@trumbitta/nx-plugin-openapi': '^1.12.1' },
),
);
const {
default: nxPluginOpenAPIGenerator,
}: // eslint-disable-next-line @typescript-eslint/no-var-requires
typeof NxPluginOpenAPILibGenerator = require('@trumbitta/nx-plugin-openapi/src/generators/api-lib/generator');
const {
default: nxPluginOpenAPIInitGenerator,
}: // eslint-disable-next-line @typescript-eslint/no-var-requires
typeof NxPluginOpenAPIInitGenerator = require('@trumbitta/nx-plugin-openapi/src/generators/init/generator');

tasks.push(await nxPluginOpenAPIInitGenerator(host));

tasks.push(
await nxPluginOpenAPIGenerator(host, {
isRemoteSpec: false,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
name: options.codegenProject!,
directory: 'generated',
generator: 'typescript-fetch',
sourceSpecLib: options.swaggerProject,
}),
);

const configuration = readProjectConfiguration(host, nameWithDirectory);
configuration.targets ??= {};
const targetConfiguration = configuration.targets?.['generate-sources'];
targetConfiguration.options['sourceSpecPathOrUrl'] = joinPathFragments(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
readProjectConfiguration(host, options.swaggerProject!).root,
'swagger.json',
);
targetConfiguration.dependsOn = ['^swagger'];
configuration.targets['codegen'] = targetConfiguration;
delete configuration.targets['generate-sources'];
updateProjectConfiguration(host, nameWithDirectory, configuration);
}

async function setupNxNETCodegen(
tasks: GeneratorCallback[],
host: Tree,
options: AddSwaggerJsonExecutorSchema,
nameWithDirectory: string,
) {
const {
libraryGenerator,
}: // eslint-disable-next-line @typescript-eslint/no-var-requires
typeof JsLibraryGenerator = require('@nrwl/js/src/generators/library/library');
tasks.push(
await libraryGenerator(host, {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
name: options.codegenProject!,
directory: 'generated',
buildable: true,
}),
);
const codegenProjectConfiguration = readProjectConfiguration(
host,
nameWithDirectory,
);
codegenProjectConfiguration.implicitDependencies ??= [];
codegenProjectConfiguration.implicitDependencies.push(
options.swaggerProject ? options.swaggerProject : options.project,
);
updateProjectConfiguration(
host,
nameWithDirectory,
codegenProjectConfiguration,
);
}

function updateNxJsonForCodegenTargets(
host: Tree,
options: AddSwaggerJsonExecutorSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { Answers, prompt } from 'inquirer';

import { DotNetClient, mockDotnetFactory } from '@nx-dotnet/dotnet';
import { updateConfig } from '@nx-dotnet/utils';
import { updateConfig, getProjectFileForNxProject } from '@nx-dotnet/utils';

import generator from './generator';
import { NugetReferenceGeneratorSchema } from './schema';
Expand Down Expand Up @@ -54,8 +54,6 @@ describe('nuget-reference generator', () => {
});

it('provides resolved version to dotnet add package reference', async () => {
const { getProjectFileForNxProject } = await import('@nx-dotnet/utils');

const projectFilePath = 'libs/test/Test.csproj';

(getProjectFileForNxProject as jest.MockedFunction<() => Promise<string>>)
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/generators/utils/generate-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ export async function GenerateProject(

if (
normalizedOptions.projectTemplate === 'webapi' &&
!normalizedOptions.skipSwaggerLib
!normalizedOptions.skipSwaggerLib &&
packageIsInstalled('@nrwl/js')
) {
tasks.push(
await generateSwaggerSetup(host, {
Expand Down Expand Up @@ -275,3 +276,12 @@ export function addPrebuildMsbuildTask(

xml.children.push(fragment);
}

function packageIsInstalled(pkg: string) {
try {
require.resolve(pkg);
return true;
} catch {
return false;
}
}
3 changes: 2 additions & 1 deletion packages/core/src/graph/infer-project.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { NxDotnetConfig } from '@nx-dotnet/utils';
import type configUtils = require('@nx-dotnet/utils/src/lib/utility-functions/config');

import * as fs from 'fs';

Expand All @@ -11,7 +12,7 @@ jest.mock(
() =>
({
readConfig: () => configValues,
} as typeof import('@nx-dotnet/utils/src/lib/utility-functions/config')),
} as typeof configUtils),
);

import { registerProjectTargets } from './infer-project';
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4472,10 +4472,10 @@
"@nrwl/workspace" "15.8.0-beta.2"
tslib "^2.3.0"

"@nrwl/nx-cloud@15.1.0":
version "15.1.0"
resolved "https://registry.yarnpkg.com/@nrwl/nx-cloud/-/nx-cloud-15.1.0.tgz#455404f6006a2fb2247ecb4b4c0e30665704eecd"
integrity sha512-QcMlPZ+DBzTWcoWSziyrENYhBLfE5MSns3M5qJB8EClg9/vomepA+NLOLWB+/O0sfdeBGFKlvFyrjf+CnVaB2A==
"@nrwl/nx-cloud@15.1.1":
version "15.1.1"
resolved "https://registry.yarnpkg.com/@nrwl/nx-cloud/-/nx-cloud-15.1.1.tgz#4f730e04c43ba863df8ecf0a5e7c927a5e5d14eb"
integrity sha512-aIVb87PQpAjwdEaFksQ3rYgFq9MDaBG2KvYZcj/k+z2uWeWH84ha/opO37aCFLf5VXNiyJZGRb/+7vW7BFpNOw==
dependencies:
axios "^0.21.2"
chalk "4.1.0"
Expand Down

0 comments on commit 13c0a2c

Please sign in to comment.