Skip to content

Commit

Permalink
feature(core/api): add ability to pass env to {spawn,exec,execSync}
Browse files Browse the repository at this point in the history
  • Loading branch information
heapwolf committed Apr 16, 2024
1 parent f57b6cf commit eb335ff
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 172 deletions.
1 change: 1 addition & 0 deletions api/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ export function execSync (command, options) {
id: rand64(),
args: command,
cwd: options?.cwd ?? '',
env: Object.entries(options.env).map(pair => pair.join('=')).join('\u0001'),
stdin: options?.stdin !== false,
stdout: options?.stdout !== false,
stderr: options?.stderr !== false,
Expand Down
1 change: 1 addition & 0 deletions api/child_process/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ parentPort.onmessage = async ({ data: { id, method, args } }) => {

const params = {
args: [command, ...Array.from(argv ?? [])].join('\u0001'),
env: Object.entries(opts.env).map(pair => pair.join('=')).join('\u0001'),
id,
cwd: opts?.cwd ?? '',
stdin: opts?.stdin !== false,
Expand Down
36 changes: 24 additions & 12 deletions src/cli/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ static std::atomic<int> appStatus = -1;
static std::mutex appMutex;

#if defined(__APPLE__)
void pollOSLogStream (bool isForDesktop, String bundleIdentifier, int processIdentifier) {
/* void pollOSLogStream (bool isForDesktop, String bundleIdentifier, int processIdentifier) {
if (Env::get("SSC_NO_OSLOG_POLLING").size() > 0) return;
// It appears there is a bug with `:predicateWithFormat:` as the
// following does not appear to work:
//
Expand All @@ -319,22 +320,22 @@ void pollOSLogStream (bool isForDesktop, String bundleIdentifier, int processIde
// ];
//
// We can build the predicate query string manually, instead.
auto queryStream = StringStream {};
auto pid = std::to_string(processIdentifier);
auto bid = bundleIdentifier.c_str();
queryStream
<< "("
<< (isForDesktop
? "category == 'socket.runtime.desktop'"
: "category == 'socket.runtime.mobile'")
<< " OR category == 'socket.runtime.debug'"
<< ") AND ";
StringStream queryStream;
queryStream << "(category == 'socket.runtime')";
if (settings.count("logs_root") == 0) {
queryStream << " AND ";
if (processIdentifier > 0) {
queryStream << "processIdentifier == " << pid << " AND ";
}
queryStream << "subsystem == '" << bid << "'";
queryStream << "subsystem == '" << bid << "'";
}
// log store query and predicate for filtering logs based on the currently
// running application that was just launched and those of a subsystem
// directly related to the application's bundle identifier which allows us
Expand Down Expand Up @@ -410,7 +411,7 @@ void pollOSLogStream (bool isForDesktop, String bundleIdentifier, int processIde
}
// this is may be set to `true` from a `SIGUSR1` signal
checkLogStore = false;
checkLogStore = settings.count("logs_root") > 0;
@autoreleasepool {
// We need a new `OSLogStore` in each so we can keep enumeratoring
// the logs until the application terminates. This is required because
Expand Down Expand Up @@ -496,7 +497,12 @@ void pollOSLogStream (bool isForDesktop, String bundleIdentifier, int processIde
}
appMutex.unlock();
} */

void pollOSLogStream(bool isDesktop, const std::string &bid, int pid) {

}

#endif

void handleBuildPhaseForUser (
Expand Down Expand Up @@ -537,10 +543,13 @@ void handleBuildPhaseForUser (
buildScript = "cmd.exe";
}

auto scriptPath = (cwd / targetPath).string();
log("running build script (cmd='" + buildScript + "', args='" + scriptArgs + "', pwd='" + scriptPath + "')");

auto process = new SSC::Process(
buildScript,
scriptArgs,
(cwd / targetPath).string(),
scriptPath,
[](SSC::String const &out) { IO::write(out, false); },
[](SSC::String const &out) { IO::write(out, true); }
);
Expand Down Expand Up @@ -986,6 +995,9 @@ int runApp (const Path& path, const String& args, bool headless) {

for (auto const &envKey : parseStringList(settings["build_env"])) {
auto cleanKey = trim(envKey);
// TODO(@heapwolf): this is wrong, ',' should be cleared from lists like this
cleanKey.erase(0, cleanKey.find_first_not_of(","));
cleanKey.erase(cleanKey.find_last_not_of(",") + 1);
auto envValue = Env::get(cleanKey.c_str());
auto key = [NSString stringWithUTF8String: cleanKey.c_str()];
auto value = [NSString stringWithUTF8String: envValue.c_str()];
Expand Down
2 changes: 2 additions & 0 deletions src/core/child_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ namespace SSC {
process = new Process(
command,
argv,
options.env,
options.cwd,
onStdout,
onStderr,
Expand Down Expand Up @@ -313,6 +314,7 @@ namespace SSC {
process = new Process(
command,
argv,
options.env,
options.cwd,
onStdout,
onStderr,
Expand Down
2 changes: 2 additions & 0 deletions src/core/core.hh
Original file line number Diff line number Diff line change
Expand Up @@ -695,13 +695,15 @@ namespace SSC {
using Handles = std::map<uint64_t, Process*>;
struct SpawnOptions {
String cwd;
const Vector<String> env;
bool allowStdin = true;
bool allowStdout = true;
bool allowStderr = true;
};

struct ExecOptions {
String cwd;
const Vector<String> env;
bool allowStdout = true;
bool allowStderr = true;
uint64_t timeout = 0;
Expand Down
6 changes: 1 addition & 5 deletions src/desktop/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -679,11 +679,7 @@ MAIN {
#if defined(__APPLE__)
static auto SSC_OS_LOG_BUNDLE = os_log_create(
bundleIdentifier.c_str(),
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
"socket.runtime.mobile"
#else
"socket.runtime.desktop"
#endif
"socket.runtime"
);
#endif

Expand Down
8 changes: 1 addition & 7 deletions src/extension/extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -613,13 +613,7 @@ void sapi_log (const sapi_context_t* ctx, const char* message) {
#if defined(__APPLE__)
static auto userConfig = SSC::getUserConfig();
static auto bundleIdentifier = userConfig["meta_bundle_identifier"];
static auto SSC_OS_LOG_BUNDLE = os_log_create(bundleIdentifier.c_str(),
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
"socket.runtime.mobile"
#else
"socket.runtime.desktop"
#endif
);
static auto SSC_OS_LOG_BUNDLE = os_log_create(bundleIdentifier.c_str(), "socket.runtime");
os_log_with_type(SSC_OS_LOG_BUNDLE, OS_LOG_TYPE_INFO, "%{public}s", output.c_str());
#endif
}
Expand Down
Loading

0 comments on commit eb335ff

Please sign in to comment.