|
| 1 | +import path from 'node:path' |
| 2 | + |
| 3 | +import { debugFn } from '@socketsecurity/registry/lib/debug' |
| 4 | +import { safeStatsSync } from '@socketsecurity/registry/lib/fs' |
| 5 | +import { logger } from '@socketsecurity/registry/lib/logger' |
| 6 | + |
| 7 | +import { convertDotnetToFacts } from './convert-dotnet-to-facts.mts' |
| 8 | +import { resolveEcosystemConfig } from './generate-recursive-manifests.mts' |
| 9 | +import { excludePathsFlag } from './manifest-flags.mts' |
| 10 | +import { parseBuildToolOpts } from './parse-build-tool-opts.mts' |
| 11 | +import constants, { SOCKET_JSON } from '../../constants.mts' |
| 12 | +import { commonFlags } from '../../flags.mts' |
| 13 | +import { checkCommandInput } from '../../utils/check-input.mts' |
| 14 | +import { cmdFlagValueToArray } from '../../utils/cmd.mts' |
| 15 | +import { getOutputKind } from '../../utils/get-output-kind.mts' |
| 16 | +import { meowOrExit } from '../../utils/meow-with-subcommands.mts' |
| 17 | +import { getFlagListOutput } from '../../utils/output-formatting.mts' |
| 18 | +import { readOrDefaultSocketJson } from '../../utils/socket-json.mts' |
| 19 | +import { camelToKebab } from '../../utils/strings.mts' |
| 20 | +import { assertValidExcludePaths } from '../scan/exclude-paths.mts' |
| 21 | + |
| 22 | +import type { |
| 23 | + CliCommandConfig, |
| 24 | + CliCommandContext, |
| 25 | +} from '../../utils/meow-with-subcommands.mts' |
| 26 | + |
| 27 | +const config: CliCommandConfig = { |
| 28 | + commandName: 'dotnet', |
| 29 | + description: |
| 30 | + '[beta] Generate Socket facts files from .NET solutions and projects', |
| 31 | + hidden: false, |
| 32 | + flags: { |
| 33 | + ...commonFlags, |
| 34 | + bin: { |
| 35 | + type: 'string', |
| 36 | + description: |
| 37 | + 'Location of the dotnet binary to use, default: dotnet on PATH', |
| 38 | + }, |
| 39 | + dotnetOpts: { |
| 40 | + type: 'string', |
| 41 | + description: |
| 42 | + 'Additional options to pass on to `dotnet restore`, e.g. `-p:Configuration=Release --configfile nuget.config`. MSBuild properties among them also apply to project evaluation', |
| 43 | + }, |
| 44 | + includeConfigs: { |
| 45 | + type: 'string', |
| 46 | + description: |
| 47 | + 'Comma-separated glob patterns matched against target frameworks (case-sensitive; `*`, `?`, and `[...]` wildcards). Only target frameworks matching at least one pattern are read, e.g. `net8.0`. A runtime-specific target (`net8.0/win-x64`) also matches its framework. Default: every target framework', |
| 48 | + }, |
| 49 | + excludeConfigs: { |
| 50 | + type: 'string', |
| 51 | + description: |
| 52 | + 'Comma-separated glob patterns; target frameworks matching any pattern are skipped (applied after --include-configs)', |
| 53 | + }, |
| 54 | + ...excludePathsFlag, |
| 55 | + ignoreUnresolved: { |
| 56 | + type: 'boolean', |
| 57 | + description: |
| 58 | + 'Warn on unresolved dependencies instead of failing the run (unresolved deps are not emitted to the facts file)', |
| 59 | + }, |
| 60 | + verbose: { |
| 61 | + type: 'boolean', |
| 62 | + description: 'Print debug messages', |
| 63 | + }, |
| 64 | + }, |
| 65 | + help: (command, config) => ` |
| 66 | + Usage |
| 67 | + $ ${command} [options] [CWD=.|FILE] |
| 68 | +
|
| 69 | + Options |
| 70 | + ${getFlagListOutput(config.flags)} |
| 71 | +
|
| 72 | + Emits a \`<file>.socket.facts.json\` per solution or project describing its |
| 73 | + resolved NuGet dependency graph. Given a directory, it builds every |
| 74 | + solution (.sln, .slnx) and project (.csproj, .fsproj, .vbproj) at its top |
| 75 | + level, solutions first, skipping projects a solution already covers; given |
| 76 | + a file, just that one (a .slnf solution filter too). |
| 77 | +
|
| 78 | + Dependencies come from your own \`dotnet restore\` (same SDK, global.json, |
| 79 | + nuget.config and credential providers), so a build that restores for you |
| 80 | + restores here. PackageReference projects (central package management and |
| 81 | + lock files included) and packages.config projects are supported. An |
| 82 | + unresolved dependency is a fatal error unless --ignore-unresolved. |
| 83 | +
|
| 84 | + Support is beta. Please report issues or give us feedback on what's missing. |
| 85 | +
|
| 86 | + Examples |
| 87 | +
|
| 88 | + $ ${command} . |
| 89 | + $ ${command} ./App.sln |
| 90 | + $ ${command} --dotnet-opts="-p:Configuration=Release" . |
| 91 | + `, |
| 92 | +} |
| 93 | + |
| 94 | +export const cmdManifestDotnet = { |
| 95 | + description: config.description, |
| 96 | + hidden: config.hidden, |
| 97 | + run, |
| 98 | +} |
| 99 | + |
| 100 | +async function run( |
| 101 | + argv: string[] | readonly string[], |
| 102 | + importMeta: ImportMeta, |
| 103 | + { parentName }: CliCommandContext, |
| 104 | +): Promise<void> { |
| 105 | + const cli = meowOrExit({ |
| 106 | + argv, |
| 107 | + config, |
| 108 | + importMeta, |
| 109 | + parentName, |
| 110 | + }) |
| 111 | + |
| 112 | + const { json = false, markdown = false } = cli.flags |
| 113 | + |
| 114 | + const dryRun = !!cli.flags['dryRun'] |
| 115 | + |
| 116 | + const outputKind = getOutputKind(json, markdown) |
| 117 | + |
| 118 | + const wasValidInput = checkCommandInput(outputKind, { |
| 119 | + nook: true, |
| 120 | + test: cli.input.length <= 1, |
| 121 | + message: 'Can only accept one DIR or FILE (make sure to escape spaces!)', |
| 122 | + fail: 'received ' + cli.input.length, |
| 123 | + }) |
| 124 | + if (!wasValidInput) { |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + const target = path.resolve(process.cwd(), cli.input[0] ?? '.') |
| 129 | + const isFile = !!safeStatsSync(target)?.isFile() |
| 130 | + const cwd = isFile ? path.dirname(target) : target |
| 131 | + const entryFile = isFile ? path.basename(target) : undefined |
| 132 | + |
| 133 | + const sockJson = readOrDefaultSocketJson(cwd) |
| 134 | + const defaults = sockJson.defaults?.manifest?.dotnet |
| 135 | + |
| 136 | + debugFn('inspect', `override: ${SOCKET_JSON} dotnet`, defaults) |
| 137 | + |
| 138 | + const dotnetConfig = resolveEcosystemConfig('dotnet', cwd, sockJson) |
| 139 | + const given = cli.flags as { |
| 140 | + bin?: string | undefined |
| 141 | + dotnetOpts?: string | undefined |
| 142 | + excludeConfigs?: string | undefined |
| 143 | + ignoreUnresolved?: boolean | undefined |
| 144 | + includeConfigs?: string | undefined |
| 145 | + verbose?: boolean | undefined |
| 146 | + } |
| 147 | + // An empty --bin or --dotnet-opts leaves the default in place. |
| 148 | + const flags = { |
| 149 | + ...given, |
| 150 | + bin: given.bin || undefined, |
| 151 | + dotnetOpts: given.dotnetOpts || undefined, |
| 152 | + } |
| 153 | + for (const key of [ |
| 154 | + 'bin', |
| 155 | + 'dotnetOpts', |
| 156 | + 'excludeConfigs', |
| 157 | + 'ignoreUnresolved', |
| 158 | + 'includeConfigs', |
| 159 | + 'verbose', |
| 160 | + ] as const) { |
| 161 | + const configured = defaults?.[key] |
| 162 | + if ( |
| 163 | + flags[key] === undefined && |
| 164 | + configured !== undefined && |
| 165 | + configured !== null && |
| 166 | + configured !== '' |
| 167 | + ) { |
| 168 | + logger.info( |
| 169 | + `Using default --${camelToKebab(key)} from ${SOCKET_JSON}:`, |
| 170 | + configured, |
| 171 | + ) |
| 172 | + } |
| 173 | + } |
| 174 | + dotnetConfig.bin = flags.bin ?? dotnetConfig.bin |
| 175 | + if (flags.dotnetOpts) { |
| 176 | + dotnetConfig.buildOpts = parseBuildToolOpts(flags.dotnetOpts) |
| 177 | + } |
| 178 | + dotnetConfig.excludeConfigs = |
| 179 | + flags.excludeConfigs ?? dotnetConfig.excludeConfigs |
| 180 | + dotnetConfig.ignoreUnresolved = |
| 181 | + flags.ignoreUnresolved ?? dotnetConfig.ignoreUnresolved |
| 182 | + dotnetConfig.includeConfigs = |
| 183 | + flags.includeConfigs ?? dotnetConfig.includeConfigs |
| 184 | + dotnetConfig.verbose = flags.verbose ?? dotnetConfig.verbose |
| 185 | + const { bin, verbose } = dotnetConfig |
| 186 | + |
| 187 | + if (verbose) { |
| 188 | + logger.group('- ', parentName, config.commandName, ':') |
| 189 | + logger.group('- flags:', cli.flags) |
| 190 | + logger.groupEnd() |
| 191 | + logger.log('- input:', cli.input) |
| 192 | + logger.groupEnd() |
| 193 | + logger.group() |
| 194 | + logger.info('- cwd:', cwd) |
| 195 | + logger.info('- entry:', entryFile ?? '(every solution and project)') |
| 196 | + logger.info('- dotnet bin:', bin) |
| 197 | + logger.groupEnd() |
| 198 | + } |
| 199 | + |
| 200 | + if (dryRun) { |
| 201 | + logger.log(constants.DRY_RUN_BAILING_NOW) |
| 202 | + return |
| 203 | + } |
| 204 | + |
| 205 | + const excludePaths = cmdFlagValueToArray(cli.flags['excludePaths']) |
| 206 | + assertValidExcludePaths(excludePaths) |
| 207 | + |
| 208 | + await convertDotnetToFacts({ |
| 209 | + config: dotnetConfig, |
| 210 | + cwd, |
| 211 | + entryFile, |
| 212 | + excludePaths, |
| 213 | + }) |
| 214 | +} |
0 commit comments