Merge "Add a script to simplify testing a plugin in the local testsite"
diff --git a/polygerrit-ui/server.go b/polygerrit-ui/server.go
index e849469..faf28ad 100644
--- a/polygerrit-ui/server.go
+++ b/polygerrit-ui/server.go
@@ -103,7 +103,7 @@
func handleIndex(writer http.ResponseWriter, originalRequest *http.Request) {
fakeRequest := &http.Request{
URL: &url.URL{
- Path: "/",
+ Path: "/",
RawQuery: originalRequest.URL.RawQuery,
},
}
@@ -170,7 +170,7 @@
func patchResponse(req *http.Request, res *http.Response) io.Reader {
switch req.URL.EscapedPath() {
case "/":
- return replaceCdn(res.Body)
+ return rewriteHostPage(res.Body)
case "/config/server/info":
return injectLocalPlugins(res.Body)
default:
@@ -178,13 +178,39 @@
}
}
-func replaceCdn(reader io.Reader) io.Reader {
+func rewriteHostPage(reader io.Reader) io.Reader {
buf := new(bytes.Buffer)
buf.ReadFrom(reader)
original := buf.String()
+ // Simply remove all CDN references, so files are loaded from the local file system or the proxy
+ // server instead.
replaced := cdnPattern.ReplaceAllString(original, "")
+ // Modify window.INITIAL_DATA so that it has the same effect as injectLocalPlugins. To achieve
+ // this let's add JavaScript lines at the end of the <script>...</script> snippet that also
+ // contains window.INITIAL_DATA=...
+ // Here we rely on the fact that the <script> snippet that we want to append to is the first one.
+ if len(*plugins) > 0 {
+ insertionPoint := strings.Index(replaced, "</script>")
+ builder := new(strings.Builder)
+ builder.WriteString(
+ "window.INITIAL_DATA['/config/server/info'].plugin.html_resource_paths = []; ")
+ builder.WriteString(
+ "window.INITIAL_DATA['/config/server/info'].plugin.js_resource_paths = []; ")
+ for _, p := range strings.Split(*plugins, ",") {
+ if filepath.Ext(p) == ".html" {
+ builder.WriteString(
+ "window.INITIAL_DATA['/config/server/info'].plugin.html_resource_paths.push('" + p + "'); ")
+ }
+ if filepath.Ext(p) == ".js" {
+ builder.WriteString(
+ "window.INITIAL_DATA['/config/server/info'].plugin.js_resource_paths.push('" + p + "'); ")
+ }
+ }
+ replaced = replaced[:insertionPoint] + builder.String() + replaced[insertionPoint:]
+ }
+
return strings.NewReader(replaced)
}
@@ -209,11 +235,11 @@
jsResources := getJsonPropByPath(response, jsPluginsPath).([]interface{})
for _, p := range strings.Split(*plugins, ",") {
- if strings.HasSuffix(p, ".html") {
+ if filepath.Ext(p) == ".html" {
htmlResources = append(htmlResources, p)
}
- if strings.HasSuffix(p, ".js") {
+ if filepath.Ext(p) == ".js" {
jsResources = append(jsResources, p)
}
}