Created
August 4, 2020 17:42
-
-
Save sortofsleepy/ea7270c7e5f39a297118c4c7460f5ed0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as vscode from 'vscode'; | |
import {existsSync,promises,readFileSync} from "fs" | |
import {resolve, posix, normalize} from "path" | |
let getFiles = async(dir) =>{ | |
const dirents = await promises.readdir(dir,{ | |
withFileTypes:true | |
}); | |
let files = await Promise.all(dirents.map(dirent =>{ | |
const res = resolve(dir,dirent.name); | |
return dirent.isDirectory() ? getFiles(res) : res; | |
})) | |
files = Array.prototype.concat(...files); | |
let filetered = files.map(itm =>{ | |
// TODO file extension filtering | |
return itm; | |
}).filter(itm =>{ | |
if(itm !== undefined){ | |
return itm; | |
} | |
}) | |
return filetered; | |
} | |
export function activate(context: vscode.ExtensionContext) { | |
let parsedFiles = getFiles(vscode.workspace.rootPath); | |
parsedFiles.then(files => { | |
let tmpTransformedSources = []; | |
for(let i = 0; i < files.length; ++ i){ | |
let filePath = files[i]; | |
// get the base folder path relative to the current workspace | |
let base = filePath.replace(vscode.workspace.rootPath,""); | |
base = base.split(/[\/\\]+/g); | |
base.pop(); | |
base =base.join(''); | |
// read the file | |
let source = readFileSync(filePath,{ | |
encoding:"utf8" | |
}) | |
// split source by newlines so it's a little simpler to find all include paths | |
let sourceChunks = source.split("\n"); | |
// loop through each line | |
sourceChunks.forEach(chunk => { | |
// if we found an include statement | |
if(chunk.search("#include") !== -1){ | |
// get the filename of the file we want to load. | |
let file = chunk.replace("#include ",""); | |
file = file.replace(/['"]+/g, ''); | |
let joined = vscode.workspace.rootPath + "/" + base + "/" + file; | |
joined = posix.normalize(joined); | |
joined = resolve(joined); | |
if(existsSync(joined)){ | |
console.log("FOUND ", joined); | |
}else{ | |
console.error("Could not find ", joined); | |
} | |
} | |
}) | |
} | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment