-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server should serve vscode for web (#11)
* Update our server so we can serve vscode for web * We need to check in workbench.html and turn it into a template which we then render. * We also need to properly load all the extensions.
- Loading branch information
Showing
5 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/go-logr/zapr" | ||
"github.com/pkg/errors" | ||
"go.uber.org/zap" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// findExtensionsInDir returns a list of all the extensions | ||
func findExtensionsInDir(extDir string) ([]string, error) { | ||
log := zapr.NewLogger(zap.L()) | ||
if extDir == "" { | ||
return nil, errors.New("extensions dir is empty") | ||
} | ||
entries, err := os.ReadDir(extDir) | ||
|
||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to read directory %s", extDir) | ||
} | ||
|
||
extLocations := make([]string, 0, len(entries)) | ||
|
||
for _, entry := range entries { | ||
if !entry.IsDir() { | ||
continue | ||
} | ||
|
||
// Extensions should contain a package.json file | ||
pkgFile := filepath.Join(extDir, entry.Name(), "package.json") | ||
|
||
_, err := os.Stat(pkgFile) | ||
if err != nil && os.IsNotExist(err) { | ||
log.Info("dir does not contain a package.json file; skipping it as an extension", "dir", entry.Name()) | ||
continue | ||
} | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Failed to stat %s", pkgFile) | ||
} | ||
|
||
extPath := filepath.Join(extDir, entry.Name()) | ||
log.Info("Found extension", "dir", extPath) | ||
extLocations = append(extLocations, extPath) | ||
} | ||
return extLocations, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package server | ||
|
||
import _ "embed" | ||
|
||
//go:embed templates/workbench.html | ||
var workbenchTemplateStr string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<!-- | ||
This html template is based on https://github.com/microsoft/vscode/blob/main/src/vs/code/browser/workbench/workbench.html | ||
I changed the file to use GoTemplates for variable substitution | ||
--> | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script> | ||
performance.mark('code/didStartRenderer'); | ||
</script> | ||
<meta charset="utf-8" /> | ||
|
||
<!-- Mobile tweaks --> | ||
<meta name="mobile-web-app-capable" content="yes" /> | ||
<meta name="apple-mobile-web-app-capable" content="yes" /> | ||
<meta name="apple-mobile-web-app-title" content="Code"> | ||
<link rel="apple-touch-icon" href="{{ .WorkbenchWebBaseUrl }}/resources/server/code-192.png" /> | ||
|
||
<!-- Disable pinch zooming --> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"> | ||
|
||
<!-- Workbench Configuration --> | ||
<meta id="vscode-workbench-web-configuration" data-settings="{{ .WorkbenchWebConfiguration}}"> | ||
|
||
<!-- Workbench Auth Session --> | ||
<meta id="vscode-workbench-auth-session" data-settings="{{ .WorkbenchAuthSession}}"> | ||
|
||
<!-- Workbench Icon/Manifest/CSS --> | ||
<link rel="icon" href="{{ .WorkbenchWebBaseUrl }}/resources/server/favicon.ico" type="image/x-icon" /> | ||
<link rel="manifest" href="{{ .WorkbenchWebBaseUrl }}/resources/server/manifest.json" crossorigin="use-credentials" /> | ||
<link data-name="vs/workbench/workbench.web.main" rel="stylesheet" href="{{ .WorkbenchWebBaseUrl }}/out/vs/workbench/workbench.web.main.css"> | ||
|
||
</head> | ||
|
||
<body aria-label=""> | ||
</body> | ||
|
||
<!-- Startup (do not modify order of script tags!) --> | ||
<script src="{{ .WorkbenchWebBaseUrl }}/out/vs/loader.js"></script> | ||
<script src="{{ .WorkbenchWebBaseUrl }}/out/vs/webPackagePaths.js"></script> | ||
<script> | ||
baseUrl = new URL('{{ .WorkbenchWebBaseUrl }}', window.location.origin).toString(); | ||
// Strip the trailing slash from baseUrl because if we don't then we will get "//" | ||
// If we don't do this than we get 404s trying to start webWorkerExtensionHostIframe.html | ||
baseUrl = baseUrl.replace(/\/$/, ''); | ||
Object.keys(self.webPackagePaths).map(function (key, index) { | ||
self.webPackagePaths[key] = `${baseUrl}/node_modules/${key}/${self.webPackagePaths[key]}`; | ||
}); | ||
|
||
// Set up nls if the user is not using the default language (English) | ||
const nlsConfig = {}; | ||
// Normalize locale to lowercase because translationServiceUrl is case-sensitive. | ||
// ref: https://github.com/microsoft/vscode/issues/187795 | ||
const locale = localStorage.getItem('vscode.nls.locale') || navigator.language.toLowerCase(); | ||
if (!locale.startsWith('en')) { | ||
nlsConfig['vs/nls'] = { | ||
availableLanguages: { | ||
'*': locale | ||
}, | ||
translationServiceUrl: '{{ .WorkbenchNLSBaseURL }}' | ||
}; | ||
} | ||
|
||
require.config({ | ||
// TODO(jeremy): Should we switch to useing "vscode" rather than out? Should ew make that a template | ||
// parameter | ||
baseUrl: `${baseUrl}/out`, | ||
recordStats: true, | ||
trustedTypesPolicy: window.trustedTypes?.createPolicy('amdLoader', { | ||
createScriptURL(value) { | ||
if(value.startsWith(window.location.origin)) { | ||
return value; | ||
} | ||
throw new Error(`Invalid script url: ${value}`) | ||
} | ||
}), | ||
paths: self.webPackagePaths, | ||
...nlsConfig | ||
}); | ||
</script> | ||
<script> | ||
performance.mark('code/willLoadWorkbenchMain'); | ||
</script> | ||
<script src="{{ .WorkbenchWebBaseUrl }}/out/vs/workbench/workbench.web.main.nls.js"></script> | ||
<script src="{{ .WorkbenchWebBaseUrl }}/out/vs/workbench/workbench.web.main.js"></script> | ||
<script src="{{ .WorkbenchWebBaseUrl }}/out/vs/code/browser/workbench/workbench.js"></script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package server | ||
|
||
// IWorkbenchConstructionOptions contains the configuration for the vscode workbench | ||
// Keep it in sync with IWorkbenchConstructionOptions | ||
// https://github.com/microsoft/vscode/blob/d5d14242969257ffff1815ef3bec45d1f2eb0e81/src/vs/workbench/browser/web.api.ts#L134 | ||
// | ||
// This struct contains the options passed to the workbench.html template. | ||
type IWorkbenchConstructionOptions struct { | ||
AdditionalBuiltinExtensions []VSCodeUriComponents `json:"additionalBuiltinExtensions,omitempty"` | ||
} | ||
|
||
type VSCodeUriComponents struct { | ||
Scheme string `json:"scheme,omitempty"` | ||
Authority string `json:"authority,omitempty"` | ||
Path string `json:"path,omitempty"` | ||
Query string `json:"query,omitempty"` | ||
Fragment string `json:"fragment,omitempty"` | ||
} |