Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(src/cli/cli.cc): allow async build after script #593

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 54 additions & 25 deletions src/cli/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2505,36 +2505,36 @@ int main (const int argc, const char* argv[]) {
}
}

if (settings.count("build_script") != 0) {
do {
char prefix[4096] = {0};
std::memcpy(
prefix,
pathResourcesRelativeToUserBuild.string().c_str(),
pathResourcesRelativeToUserBuild.string().size()
);
do {
char prefix[4096] = {0};
std::memcpy(
prefix,
pathResourcesRelativeToUserBuild.string().c_str(),
pathResourcesRelativeToUserBuild.string().size()
);

// @TODO(jwerle): use `setEnv()` if #148 is closed
#if _WIN32
String prefix_ = "PREFIX=";
prefix_ += prefix;
setEnv(prefix_.c_str());
#else
setenv("PREFIX", prefix, 1);
#endif
} while (0);
// @TODO(jwerle): use `setEnv()` if #148 is closed
#if _WIN32
String prefix_ = "PREFIX=";
prefix_ += prefix;
setEnv(prefix_.c_str());
#else
setenv("PREFIX", prefix, 1);
#endif
} while (0);

StringStream buildArgs;
buildArgs << " " << pathResourcesRelativeToUserBuild.string();
StringStream buildArgs;
buildArgs << " " << pathResourcesRelativeToUserBuild.string();

if (flagDebugMode) {
buildArgs << " --debug=true";
}
if (flagDebugMode) {
buildArgs << " --debug=true";
}

if (flagBuildTest) {
buildArgs << " --test=true";
}
if (flagBuildTest) {
buildArgs << " --test=true";
}

if (settings.count("build_script") != 0) {
auto scriptArgs = buildArgs.str();
auto buildScript = settings["build_script"];

Expand Down Expand Up @@ -2565,6 +2565,35 @@ int main (const int argc, const char* argv[]) {
log("ran user build command");
}

// runs async, does not block
if (settings.count("build_script_after") != 0) {
auto scriptArgs = buildArgs.str();
auto buildScript = settings["build_script_after"];

// Windows CreateProcess() won't work if the script has an extension other than exe (say .cmd or .bat)
// cmd.exe can handle this translation
if (platform.win) {
scriptArgs = " /c \"" + buildScript + " " + scriptArgs + "\"";
buildScript = "cmd.exe";
}

SSC::Process* process = new SSC::Process(
buildScript,
scriptArgs,
(oldCwd / targetPath).string(),
[](SSC::String const &out) { stdWrite(out, false); },
[](SSC::String const &out) { stdWrite(out, true); },
[process](const auto status) {
const auto exitCode = std::atoi(status.c_str());
if (exitCode != 0) {
log("build after script failed with code: " + status);
}
}
);

process->open();
}

String flags;
String files;

Expand Down
Loading