This commit is contained in:
Zengtudor 2024-08-12 00:24:49 +08:00
parent e0d569cadc
commit db5a419af8
13 changed files with 70 additions and 59 deletions

View File

@ -1,13 +0,0 @@
import { cloneDeep } from "lodash"
import NmakeGlobal from "../interface/NmakeGlobal"
import printDebug from "../Tools/DebugPrint"
import { popGlobalStack, pushGlobalStack } from "../Tools/GlobalStack"
const project = (name:string,callback:Function) =>{
pushGlobalStack()
printDebug("adding project :"+name)
callback()
popGlobalStack()
}
export default project

View File

@ -1 +0,0 @@
export {default as project} from "./project"

View File

@ -1,4 +1,5 @@
export default interface Compiler{
compilerPath:string;
compilerVersion:string;
compilerName:string
}

View File

@ -5,10 +5,22 @@ import Compiler from "./Compiler";
export default class GppCompiler implements Compiler{
compilerPath: string;
compilerVersion: string;
constructor(){
const compilerPaths = getExecutablePathsFromEnv("g++")
if(compilerPaths.length==0)throw Error("cannot find g++ compiler")
this.compilerPath=`"${compilerPaths[0]}"`
this.compilerVersion = tryGetCompilerVersion(this.compilerPath,"--version")
compilerName: string = "g++";
// staticCompilerName: string = "g++";
constructor()
constructor(path:string)
constructor(path?:string,version?:string){
if(path){
this.compilerPath = `"${path}"`
}else{
const compilerPaths = getExecutablePathsFromEnv(this.compilerName)
if(compilerPaths.length==0)throw Error(`cannot find ${this.compilerName} compiler`)
this.compilerPath = `"${compilerPaths[0]}"`
}
if(version){
this.compilerVersion = version
}else{
this.compilerVersion = tryGetCompilerVersion(this.compilerPath,"--version")
}
}
}

View File

@ -1,4 +1,7 @@
import GppCompiler from "../Compiler/GppCompiler";
import printDebug from "../Tools/DebugPrint";
import { getDefaultCompiler, setDefaultCompiler } from "../Tools/DefaultCompiler";
import setGetDefaultCompiler from "../Tools/setGetDefaultCompiler";
import HaveCompiler from "./interface/HaveCompiler";
import Optimize from "./interface/Optimize";
import SourceFiles from "./interface/SourceFiles";
@ -9,17 +12,18 @@ export default class CppProject implements Project , SourceFiles,HaveCompiler,Op
compiler: GppCompiler;
sourceFiles: string[]=[];
optimize: "fast" | "fastest" | "normal" = "fast"
typeName: string = "cpp";
constructor(name:string)
constructor(name:string,compiler:GppCompiler)
constructor(name:string,compiler?:GppCompiler){
printDebug(`adding new ${this.typeName} project`)
if(compiler){
this.compiler=compiler
this.compiler = compiler
}else{
this.compiler=new GppCompiler()
this.compiler = setGetDefaultCompiler("g++",()=>{return new GppCompiler()})
}
this.name=name
}
}

View File

@ -1,5 +1,6 @@
import Compiler from "../Compiler/Compiler"
export default interface Project{
name:string
name:string;
typeName:string
}

View File

@ -1,9 +0,0 @@
import NmakeGlobal from "../interface/NmakeGlobal"
import { getGlobalNmake, Global } from "./GlobalStack"
const constructGlobal = ()=>{
Global.nmake = {}
Global.nmake.projects = {} as NmakeGlobal[]
}
export default constructGlobal

View File

@ -0,0 +1,15 @@
import Compiler from "../Compiler/Compiler"
const Global = (global as any)
export const setDefaultCompiler = (name:string,compiler:Compiler)=>{
if(!Global.defaultCompiler)Global.defaultCompiler = {};
const globalCompilers = Global.defaultCompiler as {[key:string]:Compiler}
globalCompilers[name] = compiler
}
export const getDefaultCompiler = (name:string):undefined|Compiler=>{
if(!Global.defaultCompiler)Global.defaultCompiler = {};
const globalCompilers = Global.defaultCompiler as {[key:string]:Compiler}
return globalCompilers[name]
}

View File

@ -1,24 +0,0 @@
import { cloneDeep } from "lodash"
import NmakeGlobal from "../interface/NmakeGlobal"
import printDebug from "./DebugPrint"
export const Global = global as any
let globalNmakeStacks = Global.globalStacks as NmakeGlobal[]
export const pushGlobalStack = () =>{
printDebug("pushing global stack")
if(!Global.globalStacks){Global.globalStacks = [];globalNmakeStacks = Global.globalStacks as NmakeGlobal[]}
globalNmakeStacks.push(Global.nmake)
Global.nmake = cloneDeep(Global.nmake)
}
export const popGlobalStack = ()=>{
printDebug("popping global stack")
Global.nmake = globalNmakeStacks[globalNmakeStacks.length-1]
globalNmakeStacks.pop()
}
export const getGlobalNmake=():NmakeGlobal=>{
printDebug("getting global nmake")
return Global.nmake
}

View File

@ -0,0 +1,21 @@
import Compiler from "../Compiler/Compiler"
import printDebug from "./DebugPrint"
import { getDefaultCompiler, setDefaultCompiler } from "./DefaultCompiler"
const setGetDefaultCompiler = (name: string, newCompiler: { (): Compiler }) => {
let defaultCompiler = getDefaultCompiler("g++")
if (defaultCompiler) {
return defaultCompiler
} else {
printDebug(`setting default compiler for g++`)
setDefaultCompiler("g++", newCompiler())
defaultCompiler = getDefaultCompiler("g++")
if (defaultCompiler) {
return defaultCompiler
} else {
throw Error("cannot set compiler")
}
}
}
export default setGetDefaultCompiler

View File

@ -1,6 +1,5 @@
import { exec, execSync } from "child_process"
import printDebug from "./DebugPrint";
import util from "util";
const tryGetCompilerVersion =(path:string,command:string):string=>{
const exec_command = `${path} ${command}`

View File

@ -2,7 +2,6 @@ import path from "path";
import printDebug from "./Tools/DebugPrint";
import { existsSync } from "fs";
import printErrorOrDebug from "./Tools/PrintErrorOrDebug";
import constructGlobal from "./Tools/ConstructGlobal";
const argv = require('minimist')(process.argv.slice(2))
@ -22,7 +21,6 @@ printDebug("adding ts-node")
require("ts-node").register()
printDebug("adding global values")
constructGlobal();
printDebug(`running file ${nmakeFilePath}`)
require(nmakeFilePath)

View File

@ -1,3 +1,10 @@
import GppCompiler from "../../src/Compiler/GppCompiler";
import CppProject from "../../src/Project/CppProject";
// const compiler = new GppCompiler()
new CppProject("hello");
new CppProject("hello");
new CppProject("hello");