diff --git a/src/main.cpp b/src/main.cpp index 5fbe53f..fadd8ca 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,66 @@ +#include "json/reader.h" +#include "json/value.h" #include #include +#include +#include #include #include +#include +#include +#include -using std::cerr,std::endl,std::cout; +#ifdef _WIN32 + #include +#else + #include "unistd.h" +#endif + +using std::cerr,std::endl,std::cout,std::ifstream,std::string,std::vector,std::queue,std::filesystem::path; #define AS_EQ(a,b){if((a)!=(b)){cerr<<"assert eq failed :"< +T getValueOrPanic(T value); int main(int argc, char* argv[]) { LOG("Zengtudor OI test tools . loading files .. please add a json file in arg"); - LOG("arg[0]") - LOG(argv[0]) + printValue(argv[0]) AS_EQ(argc,2) std::filesystem::path jsonPath(argv[1]); if (!jsonPath.is_absolute()) { @@ -22,6 +69,7 @@ int main(int argc, char* argv[]) { } LOG("getting json files current dir") LOG(jsonPath) + AS_EM(std::filesystem::exists(jsonPath), true, "Please create the json file,the json file was not exist") LOG("getting project path") const auto projectPath = jsonPath.parent_path(); LOG(projectPath) @@ -32,5 +80,71 @@ int main(int argc, char* argv[]) { const auto buildDir = projectPath / "build"; LOG("getting build dir") LOG(buildDir) + LOG("parsing json file") + ifstream jsonFileStream(jsonPath); + Json::Value jsonValue; + Json::Reader jsonReader; + jsonReader.parse(jsonFileStream,jsonValue); + AS_NM(jsonValue["files"], Json::nullValue, "cannot read the json value `files` array") + string projectFilesBuildString; + LOG("parsing files in json values") + for(auto i:jsonValue["files"]){ + projectFilesBuildString = projectFilesBuildString + " " +getProtectPath(getAbsolutePath(projectPath, std::filesystem::path(i.asString()))); + } + printValue(projectFilesBuildString) + LOG("parsing g++ compiler args") + string compileArgs; + for(auto i:jsonValue["args"]){ + compileArgs = compileArgs + " "+i.asString(); + } + printValue(compileArgs) + if(std::filesystem::exists(buildDir)==false){ + LOG("creating building dir") + std::filesystem::create_directory(buildDir); + } + LOG("compiling") + string compileCommands; + const path exePath = (buildDir/"main").string()+(isWindows==true?".exe":""); + printValue(exePath) + compileCommands=compileCommands+"g++ "+"-o "+getProtectPath(exePath)+" "+projectFilesBuildString+" "+compileArgs; + printValue(compileCommands) + AS_EM(system(compileCommands.c_str()), 0, "compile the program failed") + auto cpuNums = getCpuNums(); + printValue(cpuNums) + + if(jsonValue["tests"].size()==0)LOG("cannot find tests"); + LOG("parsing tests") + vector tests; + for(auto i:jsonValue["tests"]){ + tests.push_back({ + .in=getAbsolutePath(projectPath, getValueOrPanic(i["in"]).asString()), + .ans=getAbsolutePath(projectPath,getValueOrPanic(i["ans"]).asString()) + }); + } + queue> tasks; + const string exePathStr = getProtectPath(exePath); + for(auto i:tests){ + tasks.push([i,&exePathStr](){ + string commands; + commands = commands+exePathStr+" < "+getProtectPath(i.in)+" > "; + }); + } +} + +string getProtectPath(const std::filesystem::path &p){ + return "\""+p.string()+"\""; +} + +template +T getValueOrPanic(T value){ + AS_NE(value, Json::nullValue) + return value; +} + +std::filesystem::path getAbsolutePath(std::filesystem::path rootPath,std::filesystem::path filePath){ + if(filePath.is_absolute()==false){ + filePath = rootPath/filePath; + } + return filePath; } \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index 9997319..4fbb3f0 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,6 +1,6 @@ add_rules("mode.debug", "mode.release") add_requires("jsoncpp") -set_languages("c++17") +set_languages("c++23") target("xj") set_rundir("./")