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

View File

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

View File

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

View File

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

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

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

View File

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