Skip to content

Commit 7105ff6

Browse files
committed
Add devserver_loader beginning
1 parent a00b094 commit 7105ff6

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

devserver/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ filegroup(
22
name = "main",
33
srcs = glob(["main.cc", "httplib/httplib.h", "argparse/argparse.h"]),
44
visibility = ["//visibility:public"],
5+
)
6+
7+
filegroup(
8+
name = "devserver_loader",
9+
srcs = glob(["devserver_loader.js"]),
10+
visibility = ["//visibility:public"],
511
)

devserver/defs.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def devserver(name, port, static_file, workspace_name, data = []):
1111
"--workspace_name=%s" % workspace_name,
1212
"--package_name=%s" % native.package_name(),
1313
],
14-
data = data,
14+
data = data + [Label("@rules_devserver//devserver:devserver_loader")],
1515
deps = ["@bazel_tools//tools/cpp/runfiles"],
1616
linkopts = ["-lpthread"],
1717
)

devserver/devserver_loader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("inside devserver_loader.js");

devserver/main.cc

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include <iostream>
22

3-
#include "external/rules_devserver/devserver/argparse/argparse.h"
4-
#include "external/rules_devserver/devserver/httplib/httplib.h"
3+
// #include "external/rules_devserver/devserver/argparse/argparse.h"
4+
// #include "external/rules_devserver/devserver/httplib/httplib.h"
5+
6+
#include "devserver/argparse/argparse.h"
7+
#include "devserver/httplib/httplib.h"
58
#include "tools/cpp/runfiles/runfiles.h"
69

710
using bazel::tools::cpp::runfiles::Runfiles;
@@ -63,20 +66,39 @@ int main(int argc, char **argv) {
6366
DEBUG_LOG("package_name: " << args::get(package_name));
6467
}
6568

66-
const std::string path = runfiles->Rlocation(workspace_name + "/");
69+
const std::string workspace_root = runfiles->Rlocation(workspace_name + "/");
70+
DEBUG_LOG("workspace_root: " << workspace_root << "\n\n");
6771

6872
const std::string static_file_path =
69-
path + args::get(package_name) + "/" + args::get(static_file);
73+
workspace_root + args::get(package_name) + "/" + args::get(static_file);
7074
DEBUG_LOG("static_file_path: " << static_file_path);
7175

7276
std::string static_file_contents;
7377
static_file_contents = GetFileContents(static_file_path);
74-
DEBUG_LOG("static_file_contents: " << static_file_contents)
7578

79+
const std::regex re("<\\/head>");
80+
const std::string replacement =
81+
"<script src=\"/devserver_loader.js\"></script></head>";
82+
static_file_contents =
83+
std::regex_replace(static_file_contents, re, replacement);
84+
85+
DEBUG_LOG("static_file_contents: " << static_file_contents);
7686
svr.Get("/", [&static_file_contents](const httplib::Request &req,
7787
httplib::Response &res) {
7888
res.set_content(static_file_contents, "text/html");
7989
});
8090

91+
svr.Get("/devserver_loader.js", [&workspace_root](const httplib::Request &req,
92+
httplib::Response &res) {
93+
const std::string devserver_loader_path =
94+
workspace_root + "devserver/devserver_loader.js";
95+
DEBUG_LOG("devserver_loader_path: " << devserver_loader_path);
96+
97+
std::string devserver_loader_contents;
98+
devserver_loader_contents = GetFileContents(devserver_loader_path);
99+
100+
res.set_content(devserver_loader_contents, "text/javascript");
101+
});
102+
81103
svr.listen(kHost, args::get(port));
82104
}

0 commit comments

Comments
 (0)