@nx-dotnet/core
Version:
- Have an existing nx workspace. For creating this, see [nrwl's documentation](https://nx.dev/latest/angular/getting-started/nx-setup). - .NET SDK is installed, and `dotnet` is available on the path. For help on this, see [Microsoft's documentation](https
198 lines • 8.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FILTERED_PATH_PARTS = void 0;
exports.normalizeOptions = normalizeOptions;
exports.getNamespaceFromRoot = getNamespaceFromRoot;
exports.getProjectTagsFromSchema = getProjectTagsFromSchema;
exports.getProjectRootFromSchema = getProjectRootFromSchema;
exports.GenerateProject = GenerateProject;
exports.addPrebuildMsbuildTask = addPrebuildMsbuildTask;
const devkit_1 = require("@nx/devkit");
// Files generated via `dotnet` are not available in the virtual fs
const path_1 = require("path");
const xmldoc_1 = require("xmldoc");
const utils_1 = require("@nx-dotnet/utils");
const models_1 = require("../../models");
const add_swagger_target_1 = require("../add-swagger-target/add-swagger-target");
const generator_1 = require("../init/generator");
const add_to_sln_1 = require("./add-to-sln");
const generator_2 = require("../test/generator");
const prompt_for_template_1 = require("./prompt-for-template");
const get_scope_1 = require("./get-scope");
const dotnet_new_1 = require("./dotnet-new");
const try_read_json_1 = require("./try-read-json");
async function normalizeOptions(host, options, client, projectType) {
const name = getNameFromSchema(options);
const className = (0, devkit_1.names)(options.name).className;
const projectDirectory = options.directory
? `${(0, devkit_1.names)(options.directory).fileName}/${name}`
: name;
const projectName = getProjectNameFromSchema(options, projectDirectory);
const projectRoot = getProjectRootFromSchema(host, options, projectDirectory, projectType);
const parsedTags = getProjectTagsFromSchema(options);
const template = await getTemplate(options, client);
const namespaceName = options.namespaceName ?? getNamespaceFromRoot(host, projectDirectory);
const nxProjectName = (0, devkit_1.names)(options.name).fileName;
const __unparsed__ = options.__unparsed__ ?? [];
const args = options.args ?? [];
return {
...options,
name,
className,
projectName,
projectRoot,
projectDirectory,
parsedTags,
projectLanguage: options.language,
projectTemplate: template,
namespaceName,
nxProjectName,
args,
__unparsed__,
projectType: projectType ?? options.projectType ?? 'library',
};
}
function getNameFromSchema(options) {
return options.pathScheme === 'nx'
? (0, devkit_1.names)(options.name).fileName
: options.name;
}
/**
* Filter out common directory names that are not typically part of the namespace.
*/
exports.FILTERED_PATH_PARTS = new Set([
'src',
'apps',
'libs',
'packages',
'projects',
]);
function getNamespaceFromRoot(host, projectDirectory) {
const scope = (0, get_scope_1.getWorkspaceScope)((0, devkit_1.readNxJson)(host), (0, try_read_json_1.tryReadJson)(host, 'package.json'));
const namespaceParts = projectDirectory
// not sure why eslint complains here, testing in devtools shows different results without the escape character.
// eslint-disable-next-line no-useless-escape
.split(/[\/\\]/gm) // Without the unnecessary parentheses, the separator is excluded from the result array.
.filter((part) => !exports.FILTERED_PATH_PARTS.has(part))
.flatMap((part) => part.split('.').map((subpart) => (0, devkit_1.names)(subpart).className));
if (scope) {
namespaceParts.unshift((0, devkit_1.names)(scope).className);
}
return namespaceParts.join('.');
}
async function getTemplate(options, client) {
let template = options.template ?? '';
if (client) {
template = await (0, prompt_for_template_1.promptForTemplate)(client, options.template, options.language);
}
return template;
}
function getProjectTagsFromSchema(options) {
return options.tags ? options.tags.split(',').map((s) => s.trim()) : [];
}
function getProjectRootFromSchema(host, options, projectDirectory, projectType) {
const workspaceLayoutRoot = (projectType || options.projectType) === 'application'
? (0, devkit_1.getWorkspaceLayout)(host).appsDir
: (0, devkit_1.getWorkspaceLayout)(host).libsDir;
if (workspaceLayoutRoot) {
if (projectDirectory.startsWith(workspaceLayoutRoot)) {
return projectDirectory;
}
return (0, devkit_1.joinPathFragments)(workspaceLayoutRoot, projectDirectory);
}
return projectDirectory;
}
function getProjectNameFromSchema(options, projectDirectory) {
if (options.pathScheme === 'dotnet') {
return options.directory
? `${(0, devkit_1.names)(options.directory).className}.${options.name}`
: options.name;
}
return projectDirectory.replace(/\//g, '-');
}
async function GenerateProject(host, options, dotnetClient, projectType) {
const tasks = [
await (0, generator_1.initGenerator)(host, null, dotnetClient),
];
options.testTemplate = options.testTemplate ?? 'none';
const normalizedOptions = await normalizeOptions(host, options, dotnetClient, projectType);
(0, devkit_1.addProjectConfiguration)(host, normalizedOptions.projectName, {
root: normalizedOptions.projectRoot,
projectType: projectType,
sourceRoot: `${normalizedOptions.projectRoot}`,
targets: (0, utils_1.isNxCrystalEnabled)(host)
? {}
: {
build: (0, models_1.GetBuildExecutorConfiguration)(normalizedOptions.projectRoot),
...(projectType === 'application'
? { serve: (0, models_1.GetServeExecutorConfig)() }
: {}),
lint: (0, models_1.GetLintExecutorConfiguration)(),
},
tags: normalizedOptions.parsedTags,
});
const newParams = {
language: normalizedOptions.language,
name: normalizedOptions.namespaceName,
output: normalizedOptions.projectRoot,
};
const additionalArguments = normalizedOptions.args.concat(normalizedOptions.__unparsed__);
(0, dotnet_new_1.runDotnetNew)(host, dotnetClient, normalizedOptions.projectTemplate, newParams, additionalArguments);
(0, add_to_sln_1.addToSolutionFile)(host, normalizedOptions.projectRoot, dotnetClient, normalizedOptions.solutionFile);
const testTemplate = normalizedOptions.testTemplate;
if (testTemplate !== 'none') {
await (0, generator_2.default)(host, {
language: normalizedOptions.language,
targetProject: normalizedOptions.projectName,
pathScheme: normalizedOptions.pathScheme,
tags: normalizedOptions.tags,
suffix: normalizedOptions.testProjectNameSuffix,
testTemplate,
solutionFile: normalizedOptions.solutionFile,
}, dotnetClient);
}
if (normalizedOptions.projectTemplate === 'webapi' &&
!normalizedOptions.skipSwaggerLib &&
packageIsInstalled('@nx/js')) {
tasks.push(await (0, add_swagger_target_1.default)(host, {
project: normalizedOptions.projectName,
projectRoot: normalizedOptions.projectRoot,
swaggerProject: `${normalizedOptions.nxProjectName}-swagger`,
codegenProject: `${normalizedOptions.nxProjectName}-types`,
useOpenApiGenerator: normalizedOptions.useOpenApiGenerator,
skipFormat: normalizedOptions.skipFormat,
}));
}
createGitIgnore(host, normalizedOptions.projectRoot);
if (!normalizedOptions.skipFormat) {
await (0, devkit_1.formatFiles)(host);
}
return async () => {
for (const task of tasks) {
await task();
}
};
}
function createGitIgnore(host, projectRoot) {
const gitIgnorePath = (0, devkit_1.normalizePath)((0, devkit_1.joinPathFragments)(projectRoot, '.gitignore'));
host.write(gitIgnorePath, `[Bb]in/\n[Oo]bj/`);
}
function addPrebuildMsbuildTask(host, options, xml) {
const scriptPath = (0, devkit_1.normalizePath)((0, path_1.relative)(options.projectRoot, (0, utils_1.resolve)('@nx-dotnet/core/src/tasks/check-module-boundaries')));
const fragment = new xmldoc_1.XmlDocument(`
<Target Name="CheckNxModuleBoundaries" BeforeTargets="Build">
<Exec Command="node ${scriptPath} -p ${options.projectName}"/>
</Target>
`);
xml.children.push(fragment);
}
function packageIsInstalled(pkg) {
try {
require.resolve(pkg);
return true;
}
catch {
return false;
}
}
//# sourceMappingURL=generate-project.js.map