Skip to content

Commit 29768a3

Browse files
author
David Hasani
committed
address comments
1 parent 3403c49 commit 29768a3

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

packages/core/src/codewhisperer/commands/startTransformByQ.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ import { makeTemporaryToolkitFolder } from '../../shared'
8282
import globals from '../../shared/extensionGlobals'
8383
import { convertDateToTimestamp } from '../../shared/datetime'
8484
import { isWin } from '../../shared/vscode/env'
85-
import { ChildProcess } from '../../shared/utilities/processUtils'
85+
import { findStringInDirectory } from '../../shared/utilities/workspaceUtils'
8686

8787
function getFeedbackCommentData() {
8888
const jobId = transformByQState.getJobId()
@@ -745,31 +745,19 @@ export async function getValidSQLConversionCandidateProjects() {
745745
})
746746
const openProjects = await getOpenProjects()
747747
const javaProjects = await getJavaProjects(openProjects)
748-
let errorLog = ''
749-
const isWindows = isWin()
750-
const command = isWindows ? 'findstr' : 'grep'
748+
let resultLog = ''
751749
for (const project of javaProjects) {
752750
// as long as at least one of these strings is found, project contains embedded SQL statements
753751
const searchStrings = ['oracle.jdbc.OracleDriver', 'jdbc:oracle:thin:@//', 'jdbc:oracle:oci:@//']
754752
for (const str of searchStrings) {
755-
// case-insensitive, recursive search
756-
const args = isWindows ? ['/i', '/s', str, '*.*'] : ['-i', '-r', str]
757-
const spawnResult = await new ChildProcess(command, args).run({
758-
spawnOptions: { cwd: project.path, shell: false },
759-
})
753+
const spawnResult = await findStringInDirectory(str, project.path)
760754
// just for telemetry purposes
761755
if (spawnResult.error || spawnResult.stderr) {
762-
errorLog += `${JSON.stringify(spawnResult)}---`
756+
resultLog += `search failed: ${JSON.stringify(spawnResult)}`
763757
} else {
764-
errorLog += `${command} succeeded: ${spawnResult.exitCode}`
758+
resultLog += `search succeeded: ${spawnResult.exitCode}`
765759
}
766-
/*
767-
note:
768-
error is set when command fails to spawn; stderr is set when command itself fails.
769-
status of 0 means search string was detected (will be printed to stdout).
770-
status will be non-zero and stdout / stderr / error will be empty when search string is not detected.
771-
*/
772-
getLogger().info(`CodeTransformation: searching for ${str} in ${project.path}, result = ${errorLog}`)
760+
getLogger().info(`CodeTransformation: searching for ${str} in ${project.path}, result = ${resultLog}`)
773761
if (spawnResult.exitCode === 0) {
774762
embeddedSQLProjects.push(project)
775763
break
@@ -780,7 +768,7 @@ export async function getValidSQLConversionCandidateProjects() {
780768
`CodeTransformation: found ${embeddedSQLProjects.length} projects with embedded SQL statements`
781769
)
782770
telemetry.record({
783-
codeTransformMetadata: errorLog,
771+
codeTransformMetadata: resultLog,
784772
})
785773
})
786774
return embeddedSQLProjects

packages/core/src/shared/utilities/workspaceUtils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { sanitizeFilename } from './textUtilities'
1717
import { GitIgnoreAcceptor } from '@gerhobbelt/gitignore-parser'
1818
import * as parser from '@gerhobbelt/gitignore-parser'
1919
import fs from '../fs/fs'
20+
import { ChildProcess } from './processUtils'
21+
import { isWin } from '../vscode/env'
2022

2123
type GitIgnoreRelativeAcceptor = {
2224
folderPath: string
@@ -610,3 +612,24 @@ export async function collectFilesForIndex(
610612
// pick top 100k files below size limit
611613
return storage.slice(0, Math.min(100000, i))
612614
}
615+
616+
/**
617+
* Performs a case-insensitive, recursive search for a string in a directory.
618+
* note:
619+
* 'error' is set when command fails to spawn; 'stderr' is set when command itself fails.
620+
* 'exitCode' will be 0 when searchStr is detected (each line where it's found will be printed to 'stdout').
621+
* 'exitCode' will be non-zero and 'stdout' / 'stderr' / 'error' will all be empty/undefined when searchStr is not detected.
622+
* @param searchStr the string to search for
623+
* @param dirPath the path of the directory to search in
624+
* @returns the result of the search
625+
*/
626+
export async function findStringInDirectory(searchStr: string, dirPath: string) {
627+
const isWindows = isWin()
628+
const command = isWindows ? 'findstr' : 'grep'
629+
// case-insensitive, recursive search
630+
const args = isWindows ? ['/i', '/s', searchStr, '*.*'] : ['-i', '-r', searchStr]
631+
const spawnResult = await new ChildProcess(command, args).run({
632+
spawnOptions: { cwd: dirPath },
633+
})
634+
return spawnResult
635+
}

packages/core/src/testInteg/shared/utilities/workspaceUtils.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
collectFiles,
1111
collectFilesForIndex,
1212
findParentProjectFile,
13+
findStringInDirectory,
1314
getWorkspaceFoldersByPrefixes,
1415
getWorkspaceRelativePath,
1516
} from '../../../shared/utilities/workspaceUtils'
@@ -509,4 +510,15 @@ describe('workspaceUtils', () => {
509510
)
510511
})
511512
})
513+
514+
describe('findStringInDirectory', function () {
515+
it('prints the line with the detected string to stdout', async () => {
516+
const fileAmount = 1
517+
const searchStr = 'oracle.jdbc.OracleDriver'
518+
const fileContent = `test content ${searchStr} more test content`
519+
const workspaceFolder = await createTestWorkspace(fileAmount, { fileContent: fileContent })
520+
const spawnResult = await findStringInDirectory(searchStr, workspaceFolder.uri.fsPath)
521+
assert.equal(spawnResult.stdout.includes(searchStr), true)
522+
})
523+
})
512524
})

0 commit comments

Comments
 (0)