|
| 1 | +import path from 'node:path' |
| 2 | + |
| 3 | +import { debugFn } from '@socketsecurity/registry/lib/debug' |
| 4 | +import { logger } from '@socketsecurity/registry/lib/logger' |
| 5 | + |
| 6 | +import { convertDotnetToFacts } from './convert-dotnet-to-facts.mts' |
| 7 | +import { parseBuildToolOpts } from './parse-build-tool-opts.mts' |
| 8 | +import constants, { SOCKET_JSON } from '../../constants.mts' |
| 9 | +import { commonFlags } from '../../flags.mts' |
| 10 | +import { checkCommandInput } from '../../utils/check-input.mts' |
| 11 | +import { getOutputKind } from '../../utils/get-output-kind.mts' |
| 12 | +import { meowOrExit } from '../../utils/meow-with-subcommands.mts' |
| 13 | +import { getFlagListOutput } from '../../utils/output-formatting.mts' |
| 14 | +import { readOrDefaultSocketJson } from '../../utils/socket-json.mts' |
| 15 | + |
| 16 | +import type { |
| 17 | + CliCommandConfig, |
| 18 | + CliCommandContext, |
| 19 | +} from '../../utils/meow-with-subcommands.mts' |
| 20 | + |
| 21 | +const config: CliCommandConfig = { |
| 22 | + commandName: 'dotnet', |
| 23 | + description: |
| 24 | + '[beta] Generate a Socket facts file from a .NET project (`.csproj`/`.sln`/etc)', |
| 25 | + hidden: false, |
| 26 | + flags: { |
| 27 | + ...commonFlags, |
| 28 | + bin: { |
| 29 | + type: 'string', |
| 30 | + description: |
| 31 | + 'Location of the dotnet host to use, default: dotnet on PATH', |
| 32 | + }, |
| 33 | + ignoreUnresolved: { |
| 34 | + type: 'boolean', |
| 35 | + description: |
| 36 | + 'Warn on unresolved dependencies instead of failing the run (unresolved deps are not emitted to the facts file)', |
| 37 | + }, |
| 38 | + dotnetOpts: { |
| 39 | + type: 'string', |
| 40 | + description: 'Additional options to pass on to the bundled dotnet tool', |
| 41 | + }, |
| 42 | + verbose: { |
| 43 | + type: 'boolean', |
| 44 | + description: 'Print debug messages', |
| 45 | + }, |
| 46 | + }, |
| 47 | + help: (command, config) => ` |
| 48 | + Usage |
| 49 | + $ ${command} [options] [CWD=.] |
| 50 | +
|
| 51 | + Options |
| 52 | + ${getFlagListOutput(config.flags)} |
| 53 | +
|
| 54 | + Emits a single \`.socket.facts.json\` describing the resolved dependency |
| 55 | + graph of your .NET project, using the \`dotnet\` host on PATH to run a |
| 56 | + bundled NuGet/MSBuild resolver (SDK-style projects and legacy |
| 57 | + \`packages.config\` are both supported). An unresolved dependency is a fatal |
| 58 | + error; pass --ignore-unresolved to warn and continue instead. |
| 59 | +
|
| 60 | + Unlike the JVM generators there are no configuration filters: .NET |
| 61 | + resolution has no equivalent of Gradle/Maven configurations, so |
| 62 | + --include-configs / --exclude-configs do not apply. |
| 63 | +
|
| 64 | + You can specify --bin to override the path to the \`dotnet\` host to invoke, |
| 65 | + and --dotnet-opts to pass extra options through to the bundled tool. |
| 66 | +
|
| 67 | + Support is beta. Please report issues or give us feedback on what's missing. |
| 68 | +
|
| 69 | + Examples |
| 70 | +
|
| 71 | + $ ${command} . |
| 72 | + $ ${command} --bin=/usr/local/share/dotnet/dotnet . |
| 73 | + `, |
| 74 | +} |
| 75 | + |
| 76 | +export const cmdManifestDotnet = { |
| 77 | + description: config.description, |
| 78 | + hidden: config.hidden, |
| 79 | + run, |
| 80 | +} |
| 81 | + |
| 82 | +async function run( |
| 83 | + argv: string[] | readonly string[], |
| 84 | + importMeta: ImportMeta, |
| 85 | + { parentName }: CliCommandContext, |
| 86 | +): Promise<void> { |
| 87 | + const cli = meowOrExit({ |
| 88 | + argv, |
| 89 | + config, |
| 90 | + importMeta, |
| 91 | + parentName, |
| 92 | + }) |
| 93 | + |
| 94 | + const { json = false, markdown = false } = cli.flags |
| 95 | + |
| 96 | + const dryRun = !!cli.flags['dryRun'] |
| 97 | + |
| 98 | + // TODO: Implement json/md further. |
| 99 | + const outputKind = getOutputKind(json, markdown) |
| 100 | + |
| 101 | + let [cwd = '.'] = cli.input |
| 102 | + // Note: path.resolve vs .join: |
| 103 | + // If given path is absolute then cwd should not affect it. |
| 104 | + cwd = path.resolve(process.cwd(), cwd) |
| 105 | + |
| 106 | + const sockJson = readOrDefaultSocketJson(cwd) |
| 107 | + |
| 108 | + debugFn( |
| 109 | + 'inspect', |
| 110 | + `override: ${SOCKET_JSON} dotnet`, |
| 111 | + sockJson?.defaults?.manifest?.dotnet, |
| 112 | + ) |
| 113 | + |
| 114 | + let { bin, dotnetOpts, ignoreUnresolved, verbose } = cli.flags |
| 115 | + |
| 116 | + // Set defaults for any flag/arg that is not given. Check socket.json first. |
| 117 | + if (!bin) { |
| 118 | + if (sockJson.defaults?.manifest?.dotnet?.bin) { |
| 119 | + bin = sockJson.defaults?.manifest?.dotnet?.bin |
| 120 | + logger.info(`Using default --bin from ${SOCKET_JSON}:`, bin) |
| 121 | + } else { |
| 122 | + bin = 'dotnet' |
| 123 | + } |
| 124 | + } |
| 125 | + if (!dotnetOpts) { |
| 126 | + if (sockJson.defaults?.manifest?.dotnet?.dotnetOpts) { |
| 127 | + dotnetOpts = sockJson.defaults?.manifest?.dotnet?.dotnetOpts |
| 128 | + logger.info( |
| 129 | + `Using default --dotnet-opts from ${SOCKET_JSON}:`, |
| 130 | + dotnetOpts, |
| 131 | + ) |
| 132 | + } else { |
| 133 | + dotnetOpts = '' |
| 134 | + } |
| 135 | + } |
| 136 | + if (ignoreUnresolved === undefined) { |
| 137 | + if (sockJson.defaults?.manifest?.dotnet?.ignoreUnresolved !== undefined) { |
| 138 | + ignoreUnresolved = sockJson.defaults?.manifest?.dotnet?.ignoreUnresolved |
| 139 | + logger.info( |
| 140 | + `Using default --ignore-unresolved from ${SOCKET_JSON}:`, |
| 141 | + ignoreUnresolved, |
| 142 | + ) |
| 143 | + } else { |
| 144 | + ignoreUnresolved = false |
| 145 | + } |
| 146 | + } |
| 147 | + if (verbose === undefined) { |
| 148 | + if (sockJson.defaults?.manifest?.dotnet?.verbose !== undefined) { |
| 149 | + verbose = sockJson.defaults?.manifest?.dotnet?.verbose |
| 150 | + logger.info(`Using default --verbose from ${SOCKET_JSON}:`, verbose) |
| 151 | + } else { |
| 152 | + verbose = false |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if (verbose) { |
| 157 | + logger.group('- ', parentName, config.commandName, ':') |
| 158 | + logger.group('- flags:', cli.flags) |
| 159 | + logger.groupEnd() |
| 160 | + logger.log('- input:', cli.input) |
| 161 | + logger.groupEnd() |
| 162 | + } |
| 163 | + |
| 164 | + const wasValidInput = checkCommandInput(outputKind, { |
| 165 | + nook: true, |
| 166 | + test: cli.input.length <= 1, |
| 167 | + message: 'Can only accept one DIR (make sure to escape spaces!)', |
| 168 | + fail: 'received ' + cli.input.length, |
| 169 | + }) |
| 170 | + if (!wasValidInput) { |
| 171 | + return |
| 172 | + } |
| 173 | + |
| 174 | + if (verbose) { |
| 175 | + logger.group() |
| 176 | + logger.info('- cwd:', cwd) |
| 177 | + logger.info('- dotnet bin:', bin) |
| 178 | + logger.groupEnd() |
| 179 | + } |
| 180 | + |
| 181 | + if (dryRun) { |
| 182 | + logger.log(constants.DRY_RUN_BAILING_NOW) |
| 183 | + return |
| 184 | + } |
| 185 | + |
| 186 | + const parsedDotnetOpts = parseBuildToolOpts(String(dotnetOpts || '')) |
| 187 | + |
| 188 | + await convertDotnetToFacts({ |
| 189 | + bin: String(bin), |
| 190 | + cwd, |
| 191 | + dotnetOpts: parsedDotnetOpts, |
| 192 | + ignoreUnresolved: Boolean(ignoreUnresolved), |
| 193 | + verbose: Boolean(verbose), |
| 194 | + }) |
| 195 | +} |
0 commit comments