diff --git a/src/CliFunc/project.ts b/src/CliFunc/project.ts deleted file mode 100644 index da607fd..0000000 --- a/src/CliFunc/project.ts +++ /dev/null @@ -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 \ No newline at end of file diff --git a/src/CliFunc/tools.ts b/src/CliFunc/tools.ts deleted file mode 100644 index 49efb49..0000000 --- a/src/CliFunc/tools.ts +++ /dev/null @@ -1 +0,0 @@ -export {default as project} from "./project" \ No newline at end of file diff --git a/src/Compiler/Compiler.ts b/src/Compiler/Compiler.ts index 58f7f86..7e52cbf 100644 --- a/src/Compiler/Compiler.ts +++ b/src/Compiler/Compiler.ts @@ -1,4 +1,5 @@ export default interface Compiler{ compilerPath:string; compilerVersion:string; + compilerName:string } \ No newline at end of file diff --git a/src/Compiler/GppCompiler.ts b/src/Compiler/GppCompiler.ts index 8dc2152..57f392d 100644 --- a/src/Compiler/GppCompiler.ts +++ b/src/Compiler/GppCompiler.ts @@ -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") + } } } \ No newline at end of file diff --git a/src/Project/CppProject.ts b/src/Project/CppProject.ts index 0808190..cffc044 100644 --- a/src/Project/CppProject.ts +++ b/src/Project/CppProject.ts @@ -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 - } } \ No newline at end of file diff --git a/src/Project/Project.ts b/src/Project/Project.ts index 3195a0a..0d84f92 100644 --- a/src/Project/Project.ts +++ b/src/Project/Project.ts @@ -1,5 +1,6 @@ import Compiler from "../Compiler/Compiler" export default interface Project{ - name:string + name:string; + typeName:string } \ No newline at end of file diff --git a/src/Tools/ConstructGlobal.ts b/src/Tools/ConstructGlobal.ts deleted file mode 100644 index 2d3cdc6..0000000 --- a/src/Tools/ConstructGlobal.ts +++ /dev/null @@ -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 \ No newline at end of file diff --git a/src/Tools/DefaultCompiler.ts b/src/Tools/DefaultCompiler.ts new file mode 100644 index 0000000..803cb4e --- /dev/null +++ b/src/Tools/DefaultCompiler.ts @@ -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] +} \ No newline at end of file diff --git a/src/Tools/GlobalStack.ts b/src/Tools/GlobalStack.ts deleted file mode 100644 index 3c7737d..0000000 --- a/src/Tools/GlobalStack.ts +++ /dev/null @@ -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 -} \ No newline at end of file diff --git a/src/Tools/SetGetDefaultCompiler.ts b/src/Tools/SetGetDefaultCompiler.ts new file mode 100644 index 0000000..bc2ef9e --- /dev/null +++ b/src/Tools/SetGetDefaultCompiler.ts @@ -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 \ No newline at end of file diff --git a/src/Tools/TryGetCompilerVersion.ts b/src/Tools/TryGetCompilerVersion.ts index e5f91dc..0bb20d3 100644 --- a/src/Tools/TryGetCompilerVersion.ts +++ b/src/Tools/TryGetCompilerVersion.ts @@ -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}` diff --git a/src/index.ts b/src/index.ts index 7136cea..cd6bd99 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) diff --git a/tests/test2/nmake.ts b/tests/test2/nmake.ts index 749c880..c4e216d 100644 --- a/tests/test2/nmake.ts +++ b/tests/test2/nmake.ts @@ -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");