|
|
const path = require('path'); |
|
|
const fs = require('fs'); |
|
|
const vscode = require('vscode'); |
|
|
const remediate = require('./Remediation'); |
|
|
|
|
|
|
|
|
const execPatchitpy = require('./execPatchitpy'); |
|
|
const delFile = require('./utilities/deleteFile'); |
|
|
const removePythonComments = require('./utilities/removePythonComments'); |
|
|
|
|
|
|
|
|
function runPatchitpyFromText() { |
|
|
return new Promise((resolve, reject) => { |
|
|
const editor = vscode.window.activeTextEditor; |
|
|
if (editor) { |
|
|
const document = editor.document; |
|
|
const selection = editor.selection; |
|
|
let selectedText = document.getText(selection); |
|
|
|
|
|
const filePath = document.uri.fsPath; |
|
|
const fileName = path.basename(filePath); |
|
|
const fileDir = path.dirname(filePath); |
|
|
|
|
|
const tempFilePath = path.join(fileDir, 'codeFrom_' + fileName); |
|
|
|
|
|
|
|
|
if (selectedText.trim() === '') { |
|
|
vscode.window.showErrorMessage('[PatchitPy]: No code selected'); |
|
|
return; |
|
|
} |
|
|
|
|
|
selectedText = "#PatchitPy ADD\n" + selectedText; |
|
|
selectedText = removePythonComments(selectedText); |
|
|
|
|
|
|
|
|
|
|
|
fs.writeFile(tempFilePath, selectedText, (err) => { |
|
|
if (err) { |
|
|
vscode.window.showErrorMessage(`Error writing to file: ${err.message}`); |
|
|
return; |
|
|
} |
|
|
}); |
|
|
|
|
|
execPatchitpy(tempFilePath) |
|
|
.then(() => { |
|
|
|
|
|
delFile(tempFilePath); |
|
|
|
|
|
remediate(fileDir, fileName, editor, selection); |
|
|
|
|
|
|
|
|
|
|
|
resolve(); |
|
|
}) |
|
|
.catch((err) => { |
|
|
vscode.window.showErrorMessage(`[PatchitPy]: Error executing the tool: ${err.message}`); |
|
|
reject(err); |
|
|
}); |
|
|
|
|
|
} else { |
|
|
reject(new Error('No active text editor')); |
|
|
} |
|
|
|
|
|
|
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
module.exports = runPatchitpyFromText; |