This commit is contained in:
Zengtudor 2024-08-12 14:04:26 +08:00
parent fcf3378fe4
commit 06d675b08a
13 changed files with 124 additions and 12 deletions

3
src/Builder/Builder.ts Normal file
View File

@ -0,0 +1,3 @@
export default interface Builder{
build:{():void}
}

31
src/Builder/CppBuilder.ts Normal file
View File

@ -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 () =>{
}
}

View File

@ -1,4 +1,5 @@
import getExecutablePathsFromEnv from "../Tools/GetExecutablePathsFromEnv"; import getExecutablePathsFromEnv from "../Tools/GetExecutablePathsFromEnv";
import protectPath from "../Tools/ProtectPath";
import tryGetCompilerVersion from "../Tools/TryGetCompilerVersion"; import tryGetCompilerVersion from "../Tools/TryGetCompilerVersion";
import Compiler from "./Compiler"; import Compiler from "./Compiler";
@ -11,11 +12,11 @@ export default class GppCompiler implements Compiler{
constructor(path:string) constructor(path:string)
constructor(path?:string,version?:string){ constructor(path?:string,version?:string){
if(path){ if(path){
this.compilerPath = `"${path}"` this.compilerPath = protectPath(path)
}else{ }else{
const compilerPaths = getExecutablePathsFromEnv(this.compilerName) const compilerPaths = getExecutablePathsFromEnv(this.compilerName)
if(compilerPaths.length==0)throw Error(`cannot find ${this.compilerName} compiler`) if(compilerPaths.length==0)throw Error(`cannot find ${this.compilerName} compiler`)
this.compilerPath = `"${compilerPaths[0]}"` this.compilerPath = protectPath(compilerPaths[0])
} }
if(version){ if(version){
this.compilerVersion = version this.compilerVersion = version

View File

@ -8,18 +8,39 @@ import SourceFiles from "./interface/SourceFiles";
import Project from "./Project"; import Project from "./Project";
import { getNmakePath } from "../Tools/NmakePath"; import { getNmakePath } from "../Tools/NmakePath";
import { getNmakeDir } from "../Tools/NmakeDir"; 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 { export default class CppProject implements Project , SourceFiles,HaveCompiler,Optimize {
name: string; name: string;
compiler: GppCompiler; compiler: GppCompiler;
sourceFilesPath: string[]=[]; sourceFilesPath: string[]=[];
optimize: "fast" | "fastest" | "normal" = "fast" 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"; typeName: string = "cpp";
addFiles = (...files: string[]):CppProject =>{ addFiles = (...files: string[]):CppProject =>{
printDebug("adding source files "+files) printDebug("adding source files "+files)
files.forEach(v=>{ files.forEach(v=>{
this.sourceFilesPath.push( this.sourceFilesPath.push(
path.join(getNmakeDir(),v) protectPath(path.join(getNmakeDir(),v))
) )
}); });
printDebug("all files in "+this.name+" :"+this.sourceFilesPath) printDebug("all files in "+this.name+" :"+this.sourceFilesPath)

View File

@ -1,3 +1,4 @@
export default interface Optimize{ export default interface Optimize{
optimize:"fast"|"fastest"|"normal" optimize:"fast"|"fastest"|"normal"
optimizeToBuildString:{():string}
} }

View File

@ -5,7 +5,7 @@ import { getGlobalProjects } from "./Projects";
export const addProject = (f:{():Project}) =>{ export const addProject = (f:{():Project}) =>{
const project = f() const project = f()
if(getGlobalProjects()[project.name]){ 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 getGlobalProjects()[project.name] = project
} }

14
src/Tools/BuildDir.ts Normal file
View File

@ -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
}

6
src/Tools/ExecAsync.ts Normal file
View File

@ -0,0 +1,6 @@
import { exec } from 'child_process';
import { promisify } from 'util';
// 使用 promisify 将 exec 转换为返回 Promise 的异步函数
const execAsync = promisify(exec);
export default execAsync

View File

@ -1,9 +1,20 @@
import Project from "../Project/Project" import Project from "../Project/Project"
import { getGlobalNmake } from "./GlobalNmake" import { getGlobalNmake } from "./GlobalNmake"
export const getGlobalProjects = ():{[key:string]:Project}=>{ export const getGlobalProjects = (): { [key: string]: Project } => {
if(getGlobalNmake().projects==undefined){ if (getGlobalNmake().projects == undefined) {
getGlobalNmake().projects = [] 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
} }

5
src/Tools/ProtectPath.ts Normal file
View File

@ -0,0 +1,5 @@
const protectPath = (path:string):string =>{
return `"${path}"`
}
export default protectPath

View File

@ -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

View File

@ -3,6 +3,7 @@ import printDebug from "./Tools/DebugPrint";
import { existsSync } from "fs"; import { existsSync } from "fs";
import printErrorOrDebug from "./Tools/PrintErrorOrDebug"; import printErrorOrDebug from "./Tools/PrintErrorOrDebug";
import { setNmakePath } from "./Tools/NmakePath"; import { setNmakePath } from "./Tools/NmakePath";
import CppBuilder from "./Builder/CppBuilder";
const argv = require('minimist')(process.argv.slice(2)) const argv = require('minimist')(process.argv.slice(2))
@ -30,3 +31,13 @@ printDebug(`running file ${nmakeFilePath}`)
require(nmakeFilePath) require(nmakeFilePath)
printDebug(`run completion!`,nmakeFilePath) printDebug(`run completion!`,nmakeFilePath)
if (argv[0]) {
switch (argv[0]) {
case "b":
new CppBuilder().build()
break;
default:
break;
}
}

View File

@ -6,8 +6,3 @@ import { addProject } from "../../src/Tools/AddProject";
addProject(()=>{ addProject(()=>{
return new CppProject("hello") return new CppProject("hello")
}) })
addProject(()=>{
return new CppProject("hello")
})