snyk-nuget-plugin
Version:
Snyk CLI NuGet plugin
114 lines • 4.69 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildFrameworksParser = buildFrameworksParser;
exports.parseTargetFramework = parseTargetFramework;
exports.sdkInfo = sdkInfo;
exports.listRuntimes = listRuntimes;
exports.reset = reset;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const dotnet = __importStar(require("./dotnet"));
const nugetFrameworksParser = __importStar(require("../csharp/nugetframeworks_parser"));
const runtime_assembly_v2_1 = require("../runtime-assembly-v2");
// Process-lifetime memoisation for the dotnet calls the runtime resolution flow makes per
// project. None of their answers depend on the project being scanned: the helper app and the
// TargetFramework it parses only depend on the SDK, and `--info`/`--list-runtimes` only on the
// global.json that pins the SDK for the working directory. Without this an
// `--all-projects` scan rebuilt and re-ran the helper for every project, see OSM-3983.
//
// Promises are stored rather than values so concurrent callers share one spawn, and a
// rejection is dropped from the cache so a later call can retry.
const helperBuilds = new Map();
const targetFrameworkInfos = new Map();
const sdkInfos = new Map();
const runtimeLists = new Map();
function memoise(cache, key, compute) {
const cached = cache.get(key);
if (cached) {
return cached;
}
const pending = compute().catch((error) => {
cache.delete(key);
throw error;
});
cache.set(key, pending);
return pending;
}
// Writes the NuGet.Frameworks helper app for this SDK and builds it once, returning its location.
function buildFrameworksParser(sdkVersion) {
const location = nugetFrameworksParser.generate(sdkVersion);
return memoise(helperBuilds, path.resolve(location), async () => {
await dotnet.build(location);
return location;
});
}
function parseTargetFramework(parserLocation, targetFramework) {
const key = `${path.resolve(parserLocation)}\0${targetFramework}`;
return memoise(targetFrameworkInfos, key, async () => {
const response = await dotnet.run(parserLocation, [targetFramework]);
return JSON.parse(response);
});
}
// Which SDK answers in a folder is decided by the nearest global.json above it, or by the
// dotnet on the PATH when there is none, so folders sharing that file share the answer.
function sdkSelectionKey(projectFolder) {
let dir = path.resolve(projectFolder);
for (;;) {
const globalJson = path.join(dir, 'global.json');
if (fs.existsSync(globalJson)) {
return globalJson;
}
const parent = path.dirname(dir);
if (parent === dir) {
return '';
}
dir = parent;
}
}
function sdkInfo(projectFolder) {
return memoise(sdkInfos, sdkSelectionKey(projectFolder), () => (0, runtime_assembly_v2_1.extractSdkInfo)(projectFolder));
}
function listRuntimes(projectFolder) {
return memoise(runtimeLists, sdkSelectionKey(projectFolder), () => dotnet.execute(['--list-runtimes'], projectFolder));
}
function reset() {
helperBuilds.clear();
targetFrameworkInfos.clear();
sdkInfos.clear();
runtimeLists.clear();
dotnet.resetVersionCache();
}
//# sourceMappingURL=dotnet-cache.js.map