From 06d675b08aa2be8e13323bec884c0623397d0e94 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Mon, 12 Aug 2024 14:04:26 +0800 Subject: [PATCH] update --- src/Builder/Builder.ts | 3 +++ src/Builder/CppBuilder.ts | 31 +++++++++++++++++++++++++++++++ src/Compiler/GppCompiler.ts | 5 +++-- src/Project/CppProject.ts | 23 ++++++++++++++++++++++- src/Project/interface/Optimize.ts | 1 + src/Tools/AddProject.ts | 2 +- src/Tools/BuildDir.ts | 14 ++++++++++++++ src/Tools/ExecAsync.ts | 6 ++++++ src/Tools/Projects.ts | 17 ++++++++++++++--- src/Tools/ProtectPath.ts | 5 +++++ src/Tools/SourceFilesToString.ts | 13 +++++++++++++ src/index.ts | 11 +++++++++++ tests/test2/nmake.ts | 5 ----- 13 files changed, 124 insertions(+), 12 deletions(-) create mode 100644 src/Builder/Builder.ts create mode 100644 src/Builder/CppBuilder.ts create mode 100644 src/Tools/BuildDir.ts create mode 100644 src/Tools/ExecAsync.ts create mode 100644 src/Tools/ProtectPath.ts create mode 100644 src/Tools/SourceFilesToString.ts diff --git a/src/Builder/Builder.ts b/src/Builder/Builder.ts new file mode 100644 index 0000000..110826e --- /dev/null +++ b/src/Builder/Builder.ts @@ -0,0 +1,3 @@ +export default interface Builder{ + build:{():void} +} \ No newline at end of file diff --git a/src/Builder/CppBuilder.ts b/src/Builder/CppBuilder.ts new file mode 100644 index 0000000..89161c4 --- /dev/null +++ b/src/Builder/CppBuilder.ts @@ -0,0 +1,31 @@ +import path from "path"; +import CppProject from "../Project/CppProject"; +import { getGlobalProjects, getProjectsByTypeName } from "../Tools/Projects"; +import Builder from "./Builder"; +import { getBuildDir } from "../Tools/BuildDir"; +import fs from "fs" +import printDebug from "../Tools/DebugPrint"; +import sourceFilesToString from "../Tools/SourceFilesToString"; +import execAsync from "../Tools/ExecAsync"; + +export default class CppBuilder implements Builder{ + compile = async ()=>{ + const cppProjects = getProjectsByTypeName("cpp") as CppProject[] + const tasks = [] + cppProjects.forEach((e)=>{ + printDebug(`making dir ${path.join(getBuildDir(),e.name)}`) + fs.mkdirSync(path.join(getBuildDir(),e.name),{recursive:true}) + const command = `${e.compiler.compilerPath} -o ${path.join(getBuildDir(),e.name,`${e.name}.o`)} ${e.optimizeToBuildString()} ${sourceFilesToString(e)}` + printDebug(command) + tasks.push(execAsync(command).then(({stdout,stderr})=>{ + + })) + }) + } + link = async()=>{ + + } + build = async () =>{ + + } +} \ No newline at end of file diff --git a/src/Compiler/GppCompiler.ts b/src/Compiler/GppCompiler.ts index 57f392d..c55344c 100644 --- a/src/Compiler/GppCompiler.ts +++ b/src/Compiler/GppCompiler.ts @@ -1,4 +1,5 @@ import getExecutablePathsFromEnv from "../Tools/GetExecutablePathsFromEnv"; +import protectPath from "../Tools/ProtectPath"; import tryGetCompilerVersion from "../Tools/TryGetCompilerVersion"; import Compiler from "./Compiler"; @@ -11,11 +12,11 @@ export default class GppCompiler implements Compiler{ constructor(path:string) constructor(path?:string,version?:string){ if(path){ - this.compilerPath = `"${path}"` + this.compilerPath = protectPath(path) }else{ const compilerPaths = getExecutablePathsFromEnv(this.compilerName) if(compilerPaths.length==0)throw Error(`cannot find ${this.compilerName} compiler`) - this.compilerPath = `"${compilerPaths[0]}"` + this.compilerPath = protectPath(compilerPaths[0]) } if(version){ this.compilerVersion = version diff --git a/src/Project/CppProject.ts b/src/Project/CppProject.ts index 2d4e7eb..29df562 100644 --- a/src/Project/CppProject.ts +++ b/src/Project/CppProject.ts @@ -8,18 +8,39 @@ import SourceFiles from "./interface/SourceFiles"; import Project from "./Project"; import { getNmakePath } from "../Tools/NmakePath"; import { getNmakeDir } from "../Tools/NmakeDir"; +import { getBuildDir } from "../Tools/BuildDir"; +import printErrorOrDebug from "../Tools/PrintErrorOrDebug"; +import protectPath from "../Tools/ProtectPath"; export default class CppProject implements Project , SourceFiles,HaveCompiler,Optimize { name: string; compiler: GppCompiler; sourceFilesPath: string[]=[]; optimize: "fast" | "fastest" | "normal" = "fast" + optimizeToBuildString = ():string=>{ + let ret = "" + switch (this.optimize) { + case "fast": + ret="-O2" + break; + case "fastest": + ret="-O3" + break + case "normal": + ret="-O1" + break + default: + printErrorOrDebug("cannot find optimize") + break; + } + return ret + }; typeName: string = "cpp"; addFiles = (...files: string[]):CppProject =>{ printDebug("adding source files "+files) files.forEach(v=>{ this.sourceFilesPath.push( - path.join(getNmakeDir(),v) + protectPath(path.join(getNmakeDir(),v)) ) }); printDebug("all files in "+this.name+" :"+this.sourceFilesPath) diff --git a/src/Project/interface/Optimize.ts b/src/Project/interface/Optimize.ts index 8a505fc..c2b5cc3 100644 --- a/src/Project/interface/Optimize.ts +++ b/src/Project/interface/Optimize.ts @@ -1,3 +1,4 @@ export default interface Optimize{ optimize:"fast"|"fastest"|"normal" + optimizeToBuildString:{():string} } \ No newline at end of file diff --git a/src/Tools/AddProject.ts b/src/Tools/AddProject.ts index 9a263c3..ce8c012 100644 --- a/src/Tools/AddProject.ts +++ b/src/Tools/AddProject.ts @@ -5,7 +5,7 @@ import { getGlobalProjects } from "./Projects"; export const addProject = (f:{():Project}) =>{ const project = f() if(getGlobalProjects()[project.name]){ - printErrorOrDebug(`the project name ${project.name} was used please rename it`) + printErrorOrDebug(`the project name "${project.name}" was used please rename it`) } getGlobalProjects()[project.name] = project } \ No newline at end of file diff --git a/src/Tools/BuildDir.ts b/src/Tools/BuildDir.ts new file mode 100644 index 0000000..725fe8f --- /dev/null +++ b/src/Tools/BuildDir.ts @@ -0,0 +1,14 @@ +import path from "path" +import { getGlobalNmake } from "./GlobalNmake" +import { getNmakeDir } from "./NmakeDir" + +export const getBuildDir = ():string=>{ + if(getGlobalNmake()["buildDir"]==undefined){ + getGlobalNmake()["buildDir"]=path.join(getNmakeDir(),"build") + } + return getGlobalNmake()["buildDir"] +} + +export const setBuildDir = (path:string)=>{ + getGlobalNmake()["buildDir"]=path +} \ No newline at end of file diff --git a/src/Tools/ExecAsync.ts b/src/Tools/ExecAsync.ts new file mode 100644 index 0000000..3a1572d --- /dev/null +++ b/src/Tools/ExecAsync.ts @@ -0,0 +1,6 @@ +import { exec } from 'child_process'; +import { promisify } from 'util'; + +// 使用 promisify 将 exec 转换为返回 Promise 的异步函数 +const execAsync = promisify(exec); +export default execAsync \ No newline at end of file diff --git a/src/Tools/Projects.ts b/src/Tools/Projects.ts index e613ef5..0b9e587 100644 --- a/src/Tools/Projects.ts +++ b/src/Tools/Projects.ts @@ -1,9 +1,20 @@ import Project from "../Project/Project" import { getGlobalNmake } from "./GlobalNmake" -export const getGlobalProjects = ():{[key:string]:Project}=>{ - if(getGlobalNmake().projects==undefined){ +export const getGlobalProjects = (): { [key: string]: Project } => { + if (getGlobalNmake().projects == undefined) { getGlobalNmake().projects = [] } - return getGlobalNmake().projects as {[key:string]:Project} + return getGlobalNmake().projects as { [key: string]: Project } +} + +export const getProjectsByTypeName = (name: string): Project[] => { + const projects = getGlobalProjects() + const newProjects: Project[] = [] + for (const i in projects) { + if (projects[i].typeName == "cpp") { + newProjects.push(projects[i]) + } + } + return newProjects } diff --git a/src/Tools/ProtectPath.ts b/src/Tools/ProtectPath.ts new file mode 100644 index 0000000..de6b6c8 --- /dev/null +++ b/src/Tools/ProtectPath.ts @@ -0,0 +1,5 @@ +const protectPath = (path:string):string =>{ + return `"${path}"` +} + +export default protectPath \ No newline at end of file diff --git a/src/Tools/SourceFilesToString.ts b/src/Tools/SourceFilesToString.ts new file mode 100644 index 0000000..0c96521 --- /dev/null +++ b/src/Tools/SourceFilesToString.ts @@ -0,0 +1,13 @@ +import SourceFiles from "../Project/interface/SourceFiles"; +import printDebug from "./DebugPrint"; + +const sourceFilesToString = (project:SourceFiles):string=>{ + let ret = "" + project.sourceFilesPath.forEach(v=>{ + ret = `${ret} ${v}` + }) + printDebug(`tostring: ${ret}`) + return ret +} + +export default sourceFilesToString \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 7dc626b..2c9e7e1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import printDebug from "./Tools/DebugPrint"; import { existsSync } from "fs"; import printErrorOrDebug from "./Tools/PrintErrorOrDebug"; import { setNmakePath } from "./Tools/NmakePath"; +import CppBuilder from "./Builder/CppBuilder"; const argv = require('minimist')(process.argv.slice(2)) @@ -30,3 +31,13 @@ printDebug(`running file ${nmakeFilePath}`) require(nmakeFilePath) printDebug(`run completion!`,nmakeFilePath) +if (argv[0]) { + switch (argv[0]) { + case "b": + new CppBuilder().build() + break; + + default: + break; + } +} \ No newline at end of file diff --git a/tests/test2/nmake.ts b/tests/test2/nmake.ts index 9353405..bd3d158 100644 --- a/tests/test2/nmake.ts +++ b/tests/test2/nmake.ts @@ -3,11 +3,6 @@ import { addProject } from "../../src/Tools/AddProject"; // const compiler = new GppCompiler() -addProject(()=>{ - return new CppProject("hello") -}) - - addProject(()=>{ return new CppProject("hello") }) \ No newline at end of file