Skip to content

Make manifest auto detect all cases and run any found #579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 47 additions & 75 deletions src/commands/manifest/cmd-manifest-auto.mts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { existsSync } from 'node:fs'
import path from 'node:path'

import meow from 'meow'

import { debugLog } from '@socketsecurity/registry/lib/debug'
import { logger } from '@socketsecurity/registry/lib/logger'

import { cmdManifestConda } from './cmd-manifest-conda.mts'
import { cmdManifestGradle } from './cmd-manifest-gradle.mts'
import { cmdManifestScala } from './cmd-manifest-scala.mts'
import { convertGradleToMaven } from './convert_gradle_to_maven.mts'
import { convertSbtToMaven } from './convert_sbt_to_maven.mts'
import { detectManifestActions } from './detect-manifest-actions.mts'
import { handleManifestConda } from './handle-manifest-conda.mts'
import constants from '../../constants.mts'
import { commonFlags } from '../../flags.mts'
import { getOutputKind } from '../../utils/get-output-kind.mts'
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
import { getFlagListOutput } from '../../utils/output-formatting.mts'

Expand All @@ -32,7 +32,6 @@ const config: CliCommandConfig = {
default: false,
description: 'Enable debug output, may help when running into errors',
},
// TODO: support output flags
},
help: (command, config) => `
Usage
Expand Down Expand Up @@ -64,9 +63,10 @@ async function run(
importMeta,
parentName,
})
const verbose = !!cli.flags['verbose']
const cwd = (cli.flags['cwd'] as string) ?? process.cwd()
// TODO: impl json/md
const { cwd: cwdFlag, json, markdown, verbose: verboseFlag } = cli.flags
const outputKind = getOutputKind(json, markdown) // TODO: impl json/md further
const cwd = String(cwdFlag || process.cwd())
const verbose = !!verboseFlag

if (verbose) {
logger.group('- ', parentName, config.commandName, ':')
Expand All @@ -77,83 +77,55 @@ async function run(
logger.groupEnd()
}

const subArgs = []
if (verbose) {
subArgs.push('--verbose')
}
const result = await detectManifestActions(String(cwd))
debugLog(result)

const dir = cwd

if (existsSync(path.join(dir, 'build.sbt'))) {
logger.log('Detected a Scala sbt build, running default Scala generator...')
if (cwd) {
subArgs.push('--cwd', cwd)
}
subArgs.push(dir)
if (cli.flags['dryRun']) {
logger.log(DRY_RUN_BAILING_NOW)
return
}
await cmdManifestScala.run(subArgs, importMeta, { parentName })
if (cli.flags['dryRun']) {
logger.log(DRY_RUN_BAILING_NOW)
return
}

if (existsSync(path.join(dir, 'gradlew'))) {
logger.log('Detected a gradle build, running default gradle generator...')
if (cwd) {
// This command takes the cwd as first arg.
subArgs.push(cwd)
}
if (cli.flags['dryRun']) {
logger.log(DRY_RUN_BAILING_NOW)
return
}
await cmdManifestGradle.run(subArgs, importMeta, { parentName })
return
}
const found = Object.values(result).reduce(
(sum, now) => (now ? sum + 1 : sum),
0,
)

const envyml = path.join(dir, 'environment.yml')
const hasEnvyml = existsSync(envyml)
const envyaml = path.join(dir, 'environment.yaml')
const hasEnvyaml = !hasEnvyml && existsSync(envyaml)
if (hasEnvyml || hasEnvyaml) {
if (!found) {
logger.fail(
' Was unable to discover any targets for which we can generate manifest files...',
)
logger.log('')
logger.log(
'Detected an environment.yml file, running default Conda generator...',
'- Make sure this script would work with your target build (see `socket manifest --help` for your target).',
)
// This command takes the TARGET as first arg.
subArgs.push(hasEnvyml ? envyml : hasEnvyaml ? envyaml : '')
if (cli.flags['dryRun']) {
logger.log(DRY_RUN_BAILING_NOW)
return
}
await cmdManifestConda.run(subArgs, importMeta, { parentName })
logger.log(
'- Make sure to run it from the correct dir (use --cwd to target another dir)',
)
logger.log('- Make sure the necessary build tools are available (`PATH`)')
process.exitCode = 1
return
}

if (cli.flags['dryRun']) {
logger.log(DRY_RUN_BAILING_NOW)
return
if (result.sbt) {
logger.log('Detected a Scala sbt build, generating pom files with sbt...')
await convertSbtToMaven(cwd, 'sbt', './socket.sbt.pom.xml', verbose, [])
}

// Show new help screen and exit.
meow(
`
$ ${parentName} ${config.commandName}

Unfortunately this script did not discover a supported language in the
current folder.
if (result.gradle) {
logger.log(
'Detected a gradle build (Gradle, Kotlin, Scala), running default gradle generator...',
)
await convertGradleToMaven(cwd, path.join(cwd, 'gradlew'), cwd, verbose, [])
}

- Make sure this script would work with your target build
- Make sure to run it from the correct folder
- Make sure the necessary build tools are available (\`PATH\`)
if (result.conda) {
logger.log(
'Detected an environment.yml file, running default Conda generator...',
)
await handleManifestConda(cwd, '', outputKind, cwd, verbose)
}

If that doesn't work, see \`${parentName} <lang> --help\` for config details for
your target language.
`,
{
argv: [],
description: config.description,
importMeta,
},
).showHelp()
logger.success(
`Finished. Should have attempted to generate manifest files for ${found} targets.`,
)
}
45 changes: 45 additions & 0 deletions src/commands/manifest/detect-manifest-actions.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// The point here is to attempt to detect the various supported manifest files
// the CLI can generate. This would be environments that we can't do server side

import { existsSync } from 'node:fs'
import path from 'node:path'

import { debugLog } from '@socketsecurity/registry/lib/debug'

export async function detectManifestActions(cwd = process.cwd()): Promise<{
cdxgen: boolean
conda: boolean
gradle: boolean
sbt: boolean
}> {
const output = {
cdxgen: false, // TODO
conda: false,
gradle: false,
sbt: false,
}

if (existsSync(path.join(cwd, 'build.sbt'))) {
debugLog('Detected a Scala sbt build, running default Scala generator...')

output.sbt = true
}

if (existsSync(path.join(cwd, 'gradlew'))) {
debugLog('Detected a gradle build, running default gradle generator...')
output.gradle = true
}

const envyml = path.join(cwd, 'environment.yml')
const hasEnvyml = existsSync(envyml)
const envyaml = path.join(cwd, 'environment.yaml')
const hasEnvyaml = !hasEnvyml && existsSync(envyaml)
if (hasEnvyml || hasEnvyaml) {
debugLog(
'Detected an environment.yml file, running default Conda generator...',
)
output.conda = true
}

return output
}
7 changes: 0 additions & 7 deletions src/commands/scan/cmd-scan-create.mts
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,6 @@ async function run(
pass: 'ok',
fail: 'missing (try `socket login`)',
},
{
nook: true,
test: !pendingHead || !tmp,
message: 'Can not use --pendingHead and --tmp at the same time',
pass: 'ok',
fail: 'remove at least one flag',
},
{
nook: true,
test: !pendingHead || !!branchName,
Expand Down
Loading