snyk-nuget-plugin
Version:
Snyk CLI NuGet plugin
161 lines (157 loc) • 5.95 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.generate = generate;
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const generator = __importStar(require("./generator"));
function targetFrameworkFromSdkVersion(sdkVersion) {
const major = parseInt(sdkVersion.split('.')[0], 10);
return `net${major}.0`;
}
// Escape a filesystem path so it can be safely embedded in a double-quoted XML
// attribute value (Windows user paths can legitimately contain '&', etc.).
function xmlAttributeEscape(value) {
return value
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
// The bundled `nupkgs` folder normally lives on disk next to this module. But when this
// plugin runs inside the Snyk CLI's packaged binary, `pkg` mounts everything under `dist`
// into a virtual snapshot filesystem, so `path.join(__dirname, 'nupkgs')` resolves to a
// snapshot-only path (e.g. `/snapshot/...`) that only the packaged Node process itself can
// read. `dotnet restore` runs as a separate OS process and can't see into that snapshot at
// all, so instead of pointing nuget.config there directly, we copy the offline packages
// into the same real, on-disk directory generator.generate() already created below for
// Parse.csproj/Program.cs, and point nuget.config at that.
function writeOfflinePackagesInto(tempDir) {
const nugetConfigPath = path.join(tempDir, 'nuget.config');
if (fs.existsSync(nugetConfigPath)) {
// Already populated by a previous call that hit generator.generate()'s content cache
// and got back this same tempDir.
return;
}
const bundledNupkgsDir = path.join(__dirname, 'nupkgs');
fs.mkdirSync(path.join(tempDir, 'nupkgs'));
for (const fileName of fs.readdirSync(bundledNupkgsDir)) {
fs.writeFileSync(path.join(tempDir, 'nupkgs', fileName), fs.readFileSync(path.join(bundledNupkgsDir, fileName)));
}
fs.writeFileSync(nugetConfigPath, `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="snyk-nuget-plugin-offline" value="${xmlAttributeEscape(tempDir)}/nupkgs" />
</packageSources>
</configuration>
`);
}
function generate(sdkVersion) {
const targetFramework = targetFrameworkFromSdkVersion(sdkVersion);
const files = [
{
name: 'Parse.csproj',
contents: `
<Project Sdk='Microsoft.NET.Sdk'>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>${targetFramework}</TargetFramework>
<Nullable>enable</Nullable>
<RootNamespace>ShortNameToLongName</RootNamespace>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<RollForward>LatestMajor</RollForward>
</PropertyGroup>
<ItemGroup>
<PackageReference Include='NuGet.Frameworks' Version='6.14.3' />
</ItemGroup>
</Project>
`,
},
{
name: 'Program.cs',
contents: `
using System;
using System.Text.Json;
using NuGet.Frameworks;
class Program
{
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: dotnet run <shortTargetFramework>");
return;
}
string shortName = args[0];
try
{
NuGetFramework framework = NuGetFramework.Parse(shortName);
string json = JsonSerializer.Serialize(new
{
framework.Framework,
Version = framework.Version.ToString(),
framework.Platform,
PlatformVersion = framework.PlatformVersion?.ToString(),
framework.HasPlatform,
framework.HasProfile,
framework.Profile,
framework.DotNetFrameworkName,
framework.DotNetPlatformName,
framework.IsPCL,
framework.IsPackageBased,
framework.AllFrameworkVersions,
framework.IsUnsupported,
framework.IsAgnostic,
framework.IsAny,
framework.IsSpecificFramework,
ShortName = shortName
});
Console.Write(json);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
`,
},
];
const tempDir = generator.generate('csharp', files);
writeOfflinePackagesInto(tempDir);
return tempDir;
}
//# sourceMappingURL=nugetframeworks_parser.js.map