-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension_ctc_devman_both_together.js
147 lines (122 loc) · 5.45 KB
/
extension_ctc_devman_both_together.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
function activate(context) {
console.log('"sss" is now active!');
let disposable = vscode.commands.registerCommand('sss.generateReports', () => {
const devmanRulePath = path.join(vscode.workspace.rootPath, '.vscode', 'devman.json');
const devmanRuleContent = fs.readFileSync(devmanRulePath, 'utf-8');
const devmanRuleJson = JSON.parse(devmanRuleContent);
const ctcRulePath = path.join(vscode.workspace.rootPath, '.vscode', 'ctcrule.json');
const ctcRuleContent = fs.readFileSync(ctcRulePath, 'utf-8');
const ctcRuleJson = JSON.parse(ctcRuleContent);
const packageJsonPath = path.join(vscode.workspace.rootPath, 'package.json');
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf-8');
const packageJson = JSON.parse(packageJsonContent);
const filesToScan = getFilesToScan(vscode.workspace.rootPath);
let devmanViolations = [];
let ctcViolations = [];
filesToScan.forEach(file => {
const content = fs.readFileSync(file, 'utf-8');
const lines = content.split('\n');
// Check for DevMan violations
for (let i = 0; i < lines.length; i++) {
for (const keyword of devmanRuleJson.sensitiveKeywords) {
const regex = new RegExp(keyword, 'g');
if (regex.test(lines[i])) {
devmanViolations.push({ file, lineNumber: i + 1, ruleMatched: keyword });
}
}
}
// Check for CTC violations (assuming JavaScript project for simplicity)
ctcViolations = checkDependencies(packageJson, ctcRuleJson.libraries);
});
const resultFolderPath = path.join(vscode.workspace.rootPath, '.vscode', 'result');
const datetimeSuffix = new Date().toISOString().replace(/[-:]/g, '').replace('T', '-').split('.')[0];
const resultFolderName = `result-${datetimeSuffix}`;
if (!fs.existsSync(resultFolderPath)) {
fs.mkdirSync(resultFolderPath);
}
const resultFolderFullPath = path.join(resultFolderPath, resultFolderName);
if (!fs.existsSync(resultFolderFullPath)) {
fs.mkdirSync(resultFolderFullPath);
}
// Generate reports
const devmanReportFilePath = path.join(resultFolderFullPath, 'devman_report.json');
const ctcReportFilePath = path.join(resultFolderFullPath, 'ctcscan_report.json');
generateReport(devmanViolations, devmanReportFilePath);
generateReport(ctcViolations, ctcReportFilePath);
// Generate HTML report
const htmlReportFilePath = path.join(resultFolderFullPath, 'report.html');
generateHtmlReport(devmanViolations, ctcViolations, packageJson, datetimeSuffix, htmlReportFilePath);
vscode.window.showInformationMessage('Scan reports generated successfully!');
});
context.subscriptions.push(disposable);
}
function getFilesToScan(rootPath) {
const excludeFolders = ['.vscode', 'node_modules', 'target'];
const files = [];
function traverseDirectory(dir) {
fs.readdirSync(dir).forEach(file => {
const filePath = path.join(dir, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
if (!excludeFolders.includes(file)) {
traverseDirectory(filePath);
}
} else {
files.push(filePath);
}
});
}
traverseDirectory(rootPath);
return files;
}
function checkDependencies(packageJson, rules) {
let violations = [];
for (const [dependency, version] of Object.entries(packageJson.devDependencies)) {
const rule = rules.find(r => r.name === dependency && checkVersion(version, r.version));
if (rule && rule.status !== 'allowed') {
violations.push({ dependency, version, status: rule.status });
}
}
return violations;
}
function checkVersion(version, ruleVersion) {
// Add logic to check if version meets the ruleVersion
// For simplicity, let's assume version check is successful
return true;
}
function generateReport(violations, reportFilePath) {
fs.writeFileSync(reportFilePath, JSON.stringify(violations, null, 4));
}
function generateHtmlReport(devmanViolations, ctcViolations, packageJson, datetimeSuffix, htmlReportFilePath) {
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<title>Scan Report</title>
</head>
<body>
<h1>Safety & Soundness Scan Report</h1>
<p>Application Name: ${packageJson.name}</p>
<p>Application Version: ${packageJson.version}</p>
<p>Scan Date: ${datetimeSuffix}</p>
<h2>DevMan Violations:</h2>
<ul>
${devmanViolations.map(violation => `<li>File: ${violation.file}, Line: ${violation.lineNumber}, Rule Matched: ${violation.ruleMatched}</li>`).join('')}
</ul>
<h2>CTC Violations:</h2>
<ul>
${ctcViolations.map(violation => `<li>Dependency: ${violation.dependency}, Version: ${violation.version}, Status: ${violation.status}</li>`).join('')}
</ul>
</body>
</html>
`;
fs.writeFileSync(htmlReportFilePath, htmlContent);
}
function deactivate() {}
module.exports = {
activate,
deactivate
};