This commit is contained in:
Zengtudor 2024-08-11 15:23:31 +08:00
parent b7ae7cc27b
commit 28624c15c0
10 changed files with 84 additions and 1 deletions

View File

@ -3,6 +3,7 @@
"version": "0.0.1",
"description": "一个用Nodejs作为框架编译C/C++的代码库,未来可能会支持更多语言",
"main": "./src/index.ts",
"type": "commonjs",
"scripts": {
"dev": "tsc && node ./build/index.js"
},

4
src/Compiler/Compiler.ts Normal file
View File

@ -0,0 +1,4 @@
export default abstract class Compiler{
abstract compilerPath:string;
abstract compilerVersion:string;
}

View File

@ -0,0 +1,17 @@
import { execSync } from "child_process";
import getExecutablePathsFromEnv from "../Tools/GetExecutablePathsFromEnv";
import getFilesFromPath from "../Tools/GetPathsFromEnv";
import tryGetCompilerVersion from "../Tools/TryGetCompilerVersion";
import Compiler from "./Compiler";
export default class GppCompiler extends Compiler{
compilerPath: string;
compilerVersion: string;
constructor(){
super()
const compilerPaths = getExecutablePathsFromEnv("g++")
if(compilerPaths.length==0)throw Error("cannot find g++ compiler")
this.compilerPath=`"${compilerPaths[0]}"`
this.compilerVersion = tryGetCompilerVersion(this.compilerPath,"--version")
}
}

6
src/Project/Project.ts Normal file
View File

@ -0,0 +1,6 @@
import Compiler from "../Compiler/Compiler"
export default abstract class Project{
abstract name:string
abstract compiler:Compiler
}

7
src/Tools/DebugPrint.ts Normal file
View File

@ -0,0 +1,7 @@
const printDebug = (m:string)=>{
if((global as any).isDebug){
console.log(`[Debug] ${m}`)
}
}
export default printDebug

View File

@ -0,0 +1,9 @@
import getPathsFromEnv from "./GetPathsFromEnv";
const getExecutablePathsFromEnv = (name:string):string[]=>{
const ret:string[]=[];
getPathsFromEnv(name).forEach(e=>ret.push(e))
getPathsFromEnv(`${name}.exe`).forEach(e=>ret.push(e))
return ret
}
export default getExecutablePathsFromEnv

View File

@ -0,0 +1,20 @@
import { exec } from "child_process";
import { existsSync } from "fs";
import path from "path";
import { env } from "process";
const getPathsFromEnv = (name:string):[]|string[]=>{
const envPath:string = env["PATH"] || env["Path"] as string
if(envPath == undefined)return [];
const paths = envPath.split(path.delimiter)
const retPaths= [];
for (const p of paths){
const filePath = path.join(p,name)
if(existsSync(filePath)){
retPaths.push(path.join(p,name))
}
}
return retPaths
}
export default getPathsFromEnv

View File

@ -0,0 +1,12 @@
import { exec, execSync } from "child_process"
import printDebug from "./DebugPrint";
import util from "util";
const execPromise =util.promisify(exec);
const tryGetCompilerVersion =(path:string,command:string):string=>{
const exec_command = `${path} ${command}`
printDebug(exec_command)
return execSync(exec_command).toString().trim()
}
export default tryGetCompilerVersion

7
tests/test1/index.ts Normal file
View File

@ -0,0 +1,7 @@
import GppCompiler from "../../src/Compiler/GppCompiler";
(global as any).isDebug=true;
const a = new GppCompiler()
console.log(a.compilerVersion)
// console.log(execSync("g++ --version").toString().trim())

View File

@ -25,7 +25,7 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
"module": "CommonJS", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */