From 4b0cceb91acc3179126aaa5fb546c5fe87b3b32c Mon Sep 17 00:00:00 2001 From: Asher Date: Tue, 16 Jul 2019 19:26:05 -0500 Subject: [PATCH] Extract ripgrep when inside binary --- scripts/nbin-shim.js | 9 +++++++++ src/cli.ts | 7 ++++--- src/util.ts | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/scripts/nbin-shim.js b/scripts/nbin-shim.js index 1fbd6e33..99f788ad 100644 --- a/scripts/nbin-shim.js +++ b/scripts/nbin-shim.js @@ -1,8 +1,17 @@ +/* global require, global, process, __dirname */ if (!global.NBIN_LOADED) { try { const nbin = require("nbin"); nbin.shimNativeFs("{{ROOT_PATH}}"); global.NBIN_LOADED = true; + + const path = require("path"); + const rg = require("vscode-ripgrep"); + rg.binaryRgPath = rg.rgPath; + rg.rgPath = path.join( + require("os").tmpdir(), + `code-server/${path.basename(rg.binaryRgPath)}`, + ); } catch (error) { // Not in the binary. } diff --git a/src/cli.ts b/src/cli.ts index 1bf52df6..412cb0fc 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -10,7 +10,7 @@ import product from "vs/platform/product/node/product"; import { MainServer, WebviewServer } from "vs/server/src/server"; import "vs/server/src/tar"; -import { generateCertificate, generatePassword, open } from "vs/server/src/util"; +import { generateCertificate, generatePassword, open, unpackExecutables } from "vs/server/src/util"; interface Args extends ParsedArgs { "allow-http"?: boolean; @@ -167,9 +167,10 @@ const main = async (): Promise => { socket: args.socket, }, webviewServer, args); - const [webviewAddress, serverAddress] = await Promise.all([ + const [webviewAddress, serverAddress, /* ignore */] = await Promise.all([ webviewServer.listen(), - server.listen() + server.listen(), + unpackExecutables(), ]); console.log(`Main server listening on ${serverAddress}`); console.log(`Webview server listening on ${webviewAddress}`); diff --git a/src/util.ts b/src/util.ts index a4995030..e1aca6fa 100644 --- a/src/util.ts +++ b/src/util.ts @@ -4,6 +4,7 @@ import * as fs from "fs"; import * as os from "os"; import * as path from "path"; import * as util from "util"; +import * as rg from "vscode-ripgrep"; import { getPathFromAmdModule } from "vs/base/common/amd"; import { getMediaMime as vsGetMediaMime } from "vs/base/common/mime"; @@ -114,3 +115,17 @@ export const open = async (url: string): Promise => { }); }); }; + +/** + * Extract executables to the temporary directory. This is required since we + * can't execute binaries stored within our binary. + */ +export const unpackExecutables = async (): Promise => { + const rgPath = (rg as any).binaryRgPath; + if (rgPath) { + await mkdirp(tmpdir); + const destination = path.join(tmpdir, path.basename(rgPath)); + await util.promisify(fs.copyFile)(rgPath, destination); + await util.promisify(fs.chmod)(destination, "755"); + } +};