@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
90 lines • 4.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
/* eslint-disable @typescript-eslint/no-unused-vars */
const devkit_1 = require("@nx/devkit");
const semver_1 = require("semver");
const xmldoc_1 = require("xmldoc");
async function update(host) {
const projects = (0, devkit_1.getProjects)(host);
const directoryBuildPropsExists = host.exists('Directory.Build.props');
const directoryBuildPropsUpdated = directoryBuildPropsExists && updateDirectoryBuildProps(host);
for (const [project, configuration] of projects) {
const changed = updateTargetOutputs(directoryBuildPropsUpdated, configuration);
if (changed) {
(0, devkit_1.updateProjectConfiguration)(host, project, configuration);
}
}
updateTargetDefaults(host, directoryBuildPropsUpdated);
await (0, devkit_1.formatFiles)(host);
}
function updateDirectoryBuildProps(host) {
const contents = host.read('Directory.Build.props', 'utf-8');
if (!contents) {
devkit_1.logger.warn('Unable to read "Directory.Build.props"');
return false;
}
const xml = new xmldoc_1.XmlDocument(contents);
const propertyGroups = xml.childrenNamed('PropertyGroup');
const outputManipulationGroup = propertyGroups.find((group) => group.childNamed('OutputPath'));
if (!outputManipulationGroup) {
devkit_1.logger.warn('Unable to find property group containing output manipulation in Directory.Build.props');
return false;
}
outputManipulationGroup.children.push(new xmldoc_1.XmlDocument(`<BaseIntermediateOutputPath>$(RepoRoot)dist/intermediates/$(ProjectRelativePath)/obj</BaseIntermediateOutputPath>`));
outputManipulationGroup.children.push(new xmldoc_1.XmlDocument(`<IntermediateOutputPath>$(BaseIntermediateOutputPath)</IntermediateOutputPath>`));
host.write('Directory.Build.props', xml.toString());
return true;
}
function updateTargetOutputs(directoryBuildPropsUpdated, configuration) {
let changed = false;
const targets = Object.values(configuration.targets ?? {}).filter((x) => x.executor === '@nx-dotnet/core:build');
for (const target of targets) {
if (directoryBuildPropsUpdated) {
if (!target.outputs?.some((x) => x.includes('intermediates'))) {
const prefix = getWorkspaceRootPrefix();
target.outputs?.push(prefix + `dist/intermediates/${configuration.root}`);
changed = true;
}
}
else if (!target.outputs?.some((x) => x.includes('obj'))) {
const prefix = getProjectRootPrefix(configuration);
target.outputs?.push(prefix + `obj`);
changed = true;
}
}
return changed;
}
function getProjectRootPrefix(configuration) {
return (0, semver_1.gt)(devkit_1.NX_VERSION, '15.0.0-beta.0')
? '{projectRoot}/'
: `${configuration.root}/`;
}
function getWorkspaceRootPrefix() {
return (0, semver_1.gt)(devkit_1.NX_VERSION, '15.0.0-beta.0') ? '{workspaceRoot}/' : '';
}
function updateTargetDefaults(host, directoryBuildPropsUpdated) {
let changed = false;
const nxJson = (0, devkit_1.readNxJson)(host);
const targetDefaults = nxJson?.targetDefaults;
if (!targetDefaults) {
return;
}
const configuration = targetDefaults['@nx-dotnet/core:build'];
if (configuration) {
if (directoryBuildPropsUpdated) {
if (!configuration.outputs?.some((x) => x.includes('intermediates'))) {
configuration.outputs?.push(`{workspaceRoot}/dist/intermediates/{projectRoot}`);
changed = true;
}
}
else if (!configuration.outputs?.some((x) => x.includes('obj'))) {
configuration.outputs?.push(`{projectRoot}/obj`);
changed = true;
}
}
if (changed && nxJson) {
(0, devkit_1.writeJson)(host, 'nx.json', nxJson);
}
}
//# sourceMappingURL=add-intermediate-outputs.js.map