|
1 | 1 | import * as yaml from 'js-yaml'; |
2 | 2 | import * as path from 'path'; |
3 | 3 | import * as vscode from 'vscode'; |
| 4 | +import Ajv from 'ajv'; |
| 5 | + |
| 6 | +import * as scgSchema from '../../schemas/scg_config.schema.json'; |
| 7 | + |
| 8 | +const ajv = new Ajv(); |
| 9 | +const validate_scg = ajv.compile<ScgConfigSchema>(scgSchema); |
4 | 10 |
|
5 | 11 | export interface ScgConfigSchema { |
6 | 12 | outputfile?: string; |
@@ -153,90 +159,29 @@ export async function readScgConfig(filePath: string): Promise<ScgConfigSchema | |
153 | 159 | const uri = vscode.Uri.file(filePath); |
154 | 160 | const document = await vscode.workspace.openTextDocument(uri); |
155 | 161 | const fileContents = document.getText(); |
156 | | - const data = yaml.load(fileContents) as ScgConfigSchema; |
157 | | - if (!validateScgConfig(data)) { |
158 | | - return undefined; |
| 162 | + const data = yaml.load(fileContents); |
| 163 | + |
| 164 | + if (validate_scg(data)) { |
| 165 | + return data; |
159 | 166 | } |
160 | | - return data; |
| 167 | + return undefined; |
161 | 168 | } catch { |
162 | 169 | return undefined; |
163 | 170 | } |
164 | 171 | } |
165 | 172 |
|
166 | | -export function validateScgConfig(data: ScgConfigSchema): boolean { |
167 | | - if (!data.templatepath || typeof data.templatepath !== 'string') return false; |
168 | | - if (data.adjustspacing && typeof data.adjustspacing !== 'boolean') return false; |
169 | | - if (data.verifycontent && typeof data.verifycontent !== 'boolean') return false; |
170 | | - |
171 | | - // Validate optional fields |
172 | | - if (data.outputfile && typeof data.outputfile !== 'string') return false; |
173 | | - |
174 | | - // Validate counters |
175 | | - if (data.counters) { |
176 | | - for (const counter of data.counters) { |
177 | | - if (typeof counter.name !== 'string') return false; |
178 | | - if (counter.value && typeof counter.value !== 'number') return false; |
179 | | - } |
180 | | - } |
181 | | - |
182 | | - // Validate sources |
183 | | - for (const source of data.sources) { |
184 | | - if (typeof source.filename !== 'string' || typeof source.id !== 'string') return false; |
185 | | - if (source.sheet && typeof source.sheet !== 'string') return false; |
186 | | - if (source.delimiter && typeof source.delimiter !== 'string') return false; |
187 | | - } |
188 | | - |
189 | | - // Validate layout |
190 | | - for (const layout of data.layout) { |
191 | | - if (typeof layout.name !== 'string') return false; |
192 | | - if (layout.source && typeof layout.source !== 'string') return false; |
193 | | - if (layout.include && typeof layout.include !== 'object') return false; |
194 | | - } |
195 | | - |
196 | | - return true; |
197 | | -} |
198 | | - |
199 | 173 | export async function isScgConfig(filePath: string): Promise<boolean> { |
200 | 174 | try { |
201 | 175 | const uri = vscode.Uri.file(filePath); |
202 | 176 | const document = await vscode.workspace.openTextDocument(uri); |
203 | 177 | const fileContents = document.getText(); |
204 | | - const data = yaml.load(fileContents) as ScgConfigSchema; |
205 | | - return validateScgConfig(data); |
| 178 | + const data = yaml.load(fileContents); |
| 179 | + return validate_scg(data); |
206 | 180 | } catch { |
207 | 181 | return false; |
208 | 182 | } |
209 | 183 | } |
210 | 184 |
|
211 | | -export async function findScgConfigFiles(): Promise<vscode.Uri[]> { |
212 | | - try { |
213 | | - const activeEditor = vscode.window.activeTextEditor; |
214 | | - if (!activeEditor) { |
215 | | - return []; |
216 | | - } |
217 | | - let currentFolder = vscode.Uri.file(activeEditor.document.uri.fsPath).with({ path: vscode.Uri.file(activeEditor.document.uri.fsPath).path.replace(/\/[^/]+$/, '') }); |
218 | | - const workspaceFolder = vscode.workspace.workspaceFolders?.[0].uri.fsPath; |
219 | | - |
220 | | - while (currentFolder.fsPath.startsWith(workspaceFolder || '') && currentFolder.fsPath !== workspaceFolder) { |
221 | | - const files = await vscode.workspace.findFiles(new vscode.RelativePattern(currentFolder, '*.yaml')); |
222 | | - const scgConfigFiles: vscode.Uri[] = []; |
223 | | - for (const file of files) { |
224 | | - if (await isScgConfig(file.fsPath)) { |
225 | | - scgConfigFiles.push(file); |
226 | | - } |
227 | | - } |
228 | | - if (scgConfigFiles.length > 0) { |
229 | | - return scgConfigFiles; |
230 | | - } |
231 | | - currentFolder = vscode.Uri.file(currentFolder.fsPath).with({ path: vscode.Uri.file(currentFolder.fsPath).path.replace(/\/[^/]+$/, '') }); |
232 | | - } |
233 | | - return []; |
234 | | - } catch (error) { |
235 | | - console.error('Error finding SCG config files:', error); |
236 | | - return []; |
237 | | - } |
238 | | -} |
239 | | - |
240 | 185 |
|
241 | 186 | export class ScgSource { |
242 | 187 | id: string; |
|
0 commit comments