@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
72 lines • 3.4 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDependencies = void 0;
exports.resolveReferenceToProject = resolveReferenceToProject;
const devkit_1 = require("@nx/devkit");
const node_path_1 = require("node:path");
const dotnet_1 = require("@nx-dotnet/dotnet");
const dotnetClient = new dotnet_1.DotNetClient((0, dotnet_1.dotnetFactory)(), devkit_1.workspaceRoot);
const createDependencies = async (ctxOrOpts, maybeCtx) => {
// In Nx version 16.8 - 16.10, CreateDependencies had a single option - the context.
// In v17, the signature was updated to pass options first, and context second.
const ctx = maybeCtx ?? ctxOrOpts;
const rootMap = createProjectRootMappings(ctx.projects);
const parseProject = async (source) => {
const changed = ctx.filesToProcess.projectFileMap[source];
const getProjectReferences = async (file) => {
const newDeps = [];
const { ext } = (0, node_path_1.parse)(file.file);
if (['.csproj', '.fsproj', '.vbproj'].includes(ext)) {
const references = await dotnetClient.getProjectReferencesAsync(file.file);
for (const reference of references) {
const project = resolveReferenceToProject((0, devkit_1.normalizePath)(reference), file.file, rootMap, ctx);
if (project) {
newDeps.push({
source,
target: project,
type: devkit_1.DependencyType.static,
sourceFile: file.file,
});
}
else {
console.warn(`Unable to resolve project for reference ${reference} in ${file.file}`);
}
}
}
return newDeps;
};
const getAllProjectReferences = changed.map(getProjectReferences);
return Promise.all(getAllProjectReferences).then((d) => d.flat());
};
const parseAllProjects = Object.keys(ctx.filesToProcess.projectFileMap).map(parseProject);
return Promise.all(parseAllProjects).then((d) => d.flat());
};
exports.createDependencies = createDependencies;
function createProjectRootMappings(projects) {
const rootMap = {};
for (const [, project] of Object.entries(projects)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
rootMap[project.root] = project.name;
}
return rootMap;
}
function findProjectForPath(filePath, rootMap) {
/**
* Project Mappings are in UNIX-style file paths
* Windows may pass Win-style file paths
* Ensure filePath is in UNIX-style
*/
let currentPath = (0, devkit_1.normalizePath)(filePath);
for (; currentPath !== (0, node_path_1.dirname)(currentPath); currentPath = (0, node_path_1.dirname)(currentPath)) {
const p = rootMap[currentPath];
if (p) {
return p;
}
}
return rootMap[currentPath];
}
function resolveReferenceToProject(reference, source, rootMap, context) {
const resolved = (0, node_path_1.resolve)(context.workspaceRoot, (0, node_path_1.dirname)(source), reference);
return findProjectForPath((0, node_path_1.relative)(context.workspaceRoot, resolved), rootMap);
}
//# sourceMappingURL=create-dependencies.js.map
;