Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
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
145 changes: 107 additions & 38 deletions chrome.go → extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,59 +92,44 @@ func handleRun(w http.ResponseWriter, r *http.Request) {
}
}

type chromeExtInstall struct{}
type installExtHostCmd struct{}

func (c *chromeExtInstall) Spec() cli.CommandSpec {
func (c *installExtHostCmd) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "install-for-chrome-ext",
Desc: `Installs the chrome native message host manifest.
This allows the sail chrome extension to manage sail.`,
Name: "install-ext-host",
Desc: `Installs the native message host manifest into Chrome and Firefox.
This allows the sail extension to manage sail.`,
}
}

func (c *chromeExtInstall) Run(fl *flag.FlagSet) {
nativeHostDirs, err := nativeMessageHostManifestDirectories()
func (c *installExtHostCmd) Run(fl *flag.FlagSet) {
binPath, err := os.Executable()
if err != nil {
flog.Fatal("failed to get native message host manifest directory: %v", err)
flog.Fatal("failed to get sail binary location")
}

for _, dir := range nativeHostDirs {
if dir == "" {
continue
}

err = os.MkdirAll(dir, 0755)
if err != nil {
flog.Fatal("failed to ensure manifest directory exists: %v", err)
}
err = writeNativeHostManifest(dir)
if err != nil {
flog.Fatal("failed to write native messaging host manifest: %v", err)
}
nativeHostDirsChrome, err := nativeMessageHostManifestDirectoriesChrome()
if err != nil {
flog.Fatal("failed to get chrome native message host manifest directory: %v", err)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a more generic message here sincr we're aiming for two vendors

}
}

func writeNativeHostManifest(dir string) error {
binPath, err := os.Executable()
err = installManifests(nativeHostDirsChrome, "com.coder.sail.json", chromeManifest(binPath))
if err != nil {
return err
flog.Fatal("failed to write chrome manifest files: %v", err)
}

manifest := fmt.Sprintf(`{
"name": "com.coder.sail",
"description": "sail message host",
"path": "%v",
"type": "stdio",
"allowed_origins": [
"chrome-extension://deeepphleikpinikcbjplcgojfhkcmna/"
]
}`, binPath)
nativeHostDirsFirefox, err := nativeMessageHostManifestDirectoriesFirefox()
if err != nil {
flog.Fatal("failed to get firefox native message host manifest directory: %v", err)
}
err = installManifests(nativeHostDirsFirefox, "com.coder.sail.json", firefoxManifest(binPath))
if err != nil {
flog.Fatal("failed to write firefox manifest files: %v", err)
}

dst := path.Join(dir, "com.coder.sail.json")
return ioutil.WriteFile(dst, []byte(manifest), 0644)
flog.Info("Successfully installed manifests.")
}

func nativeMessageHostManifestDirectories() ([]string, error) {
func nativeMessageHostManifestDirectoriesChrome() ([]string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, xerrors.Errorf("failed to get user home dir: %w", err)
Expand Down Expand Up @@ -178,3 +163,87 @@ func nativeMessageHostManifestDirectories() ([]string, error) {
chromeCanaryDir,
}, nil
}

func chromeManifest(binPath string) string {
return fmt.Sprintf(`{
"name": "com.coder.sail",
"description": "sail message host",
"path": "%v",
"type": "stdio",
"allowed_origins": [
"chrome-extension://deeepphleikpinikcbjplcgojfhkcmna/"
]
}`, binPath)
}

func nativeMessageHostManifestDirectoriesFirefox() ([]string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, xerrors.Errorf("failed to get user home dir: %w", err)
}

var firefoxDir string

switch runtime.GOOS {
case "linux":
firefoxDir = path.Join(homeDir, ".mozilla", "native-messaging-hosts")
case "darwin":
firefoxDir = path.Join(homeDir, "Library", "Application Support", "Mozilla", "NativeMessagingHosts")
default:
return nil, xerrors.Errorf("unsupported os %q", runtime.GOOS)
}

return []string{
firefoxDir,
}, nil
}

func firefoxManifest(binPath string) string {
return fmt.Sprintf(`{
"name": "com.coder.sail",
"description": "sail message host",
"path": "%v",
"type": "stdio",
"allowed_extensions": [
"sail@coder.com"
]
}`, binPath)
}

func installManifests(nativeHostDirs []string, file string, content string) error {
data := []byte(content)

for _, dir := range nativeHostDirs {
if dir == "" {
continue
}

err := os.MkdirAll(dir, 0755)
if err != nil {
return xerrors.Errorf("failed to ensure manifest directory exists: %w", err)
}

dst := path.Join(dir, file)
err = ioutil.WriteFile(dst, data, 0644)
if err != nil {
return xerrors.Errorf("failed to write native messaging host manifest: %w", err)
}
}

return nil
}

type chromeExtInstallCmd struct{
cmd *installExtHostCmd
}

func (c *chromeExtInstallCmd) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "install-for-chrome-ext",
Desc: "DEPRECATED: alias of install-ext-host.",
}
}

func (c *chromeExtInstallCmd) Run(fl *flag.FlagSet) {
c.cmd.Run(fl)
}
9 changes: 6 additions & 3 deletions extension/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules
out
*.zip
*.xpi
*.zip
node_modules/
out/
packed-extensions/
web-ext-artifacts/
16 changes: 13 additions & 3 deletions extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
"author": "Coder",
"description": "Work in immutable, pre-configured development environments.",

"browser_specific_settings": {
"gecko": {
"id": "sail@coder.com",
"strict_min_version": "55.0"
}
},

"background": {
"scripts": [
"out/background.js"
"background.js"
],
"persistent": false
},
Expand All @@ -18,7 +25,7 @@
"https://*/*"
],
"js": [
"out/content.js"
"content.js"
]
}
],
Expand All @@ -28,7 +35,10 @@
"storage",
"tabs"
],
"options_page": "out/config.html",
"icons": {
"128": "logo128.png"
},
"options_page": "config.html",
"icons": {
"128": "logo128.png"
},
Expand Down
26 changes: 24 additions & 2 deletions extension/pack.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
#!/bin/bash
#!/usr/bin/env bash

zip -R extension manifest.json out/* logo128.png
set -e

cd $(dirname "$0")

VERSION=$(jq -r ".version" ./manifest.json)
SRC_DIR="./out"
OUTPUT_DIR="./packed-extensions"

mkdir -p "$OUTPUT_DIR"

# Firefox extension (done first because web-ext verifies manifest)
if [ -z "$WEB_EXT_API_KEY" ]; then
web-ext build --source-dir="$SRC_DIR" --artifacts-dir="$OUTPUT_DIR" --overwrite-dest
mv "$OUTPUT_DIR/sail-$VERSION.zip" "$OUTPUT_DIR/sail-$VERSION.firefox.zip"
else
# Requires $WEB_EXT_API_KEY and $WEB_EXT_API_SECRET from addons.mozilla.org.
web-ext sign --source-dir="$SRC_DIR" --artifacts-dir="$OUTPUT_DIR" --overwrite-dest
mv "$OUTPUT_DIR/sail-$VERSION.xpi" "$OUTPUT_DIR/sail-$VERSION.firefox.xpi"
fi

# Chrome extension
rm "$OUTPUT_DIR/sail-$VERSION.chrome.zip" || true
zip -R "$OUTPUT_DIR/sail-$VERSION.chrome.zip" "$SRC_DIR/*"
Loading