Skip to content

Commit 3e9c85e

Browse files
committed
(GH-100) Refactor ruby environment detection in the Extension
The Debug Adapter needs to share the ruby detection code with the Extensions proper however it imported the vscode namespace which is not available out-of- process. This commit extracts the ruby environment detection method to a separate class which can be consumed by the extension or Debug Adapter.
1 parent a06b9d3 commit 3e9c85e

File tree

2 files changed

+197
-191
lines changed

2 files changed

+197
-191
lines changed

client/src/connection.ts

Lines changed: 4 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { setupPDKCommands } from '../src/commands/pdkcommands';
1111
import { reporter } from './telemetry/telemetry';
1212
import * as messages from '../src/messages';
1313
import fs = require('fs');
14+
import { RubyHelper } from './rubyHelper';
1415

1516
const langID = 'puppet'; // don't change this
1617

@@ -178,203 +179,15 @@ export class ConnectionManager implements IConnectionManager {
178179
callback(proc);
179180
}
180181

181-
private getDirectories(parent) {
182-
return fs.readdirSync(parent).filter(function (file) {
183-
return fs.statSync(path.join(parent, file)).isDirectory();
184-
});
185-
}
186-
187-
private pathEnvSeparator() {
188-
if (process.platform == 'win32') {
189-
return ";";
190-
} else {
191-
return ":";
192-
}
193-
}
194-
195-
private getLanguageServerFromPuppetAgent(serverExe) {
196-
let logPrefix: string = '[getLanguageServerFromPuppetAgent] ';
197-
// setup defaults
198-
let spawn_options: cp.SpawnOptions = {}
199-
spawn_options.env = process.env;
200-
let result = {
201-
command: 'ruby',
202-
args: [serverExe],
203-
options: spawn_options,
204-
}
205-
let puppetAgentDir: string = null;
206-
207-
// type Platform = 'aix'
208-
// | 'android'
209-
// | 'darwin'
210-
// | 'freebsd'
211-
// | 'linux'
212-
// | 'openbsd'
213-
// | 'sunos'
214-
// | 'win32';
215-
switch (process.platform) {
216-
case 'win32':
217-
let comspec: string = process.env["COMSPEC"];
218-
let programFiles = process.env["ProgramFiles"];
219-
if (process.env["PROCESSOR_ARCHITEW6432"] == "AMD64") {
220-
// VSCode is running as 32bit process on a 64bit Operating System. Need to break out
221-
// of the 32bit using the sysnative redirection and environment variables
222-
comspec = path.join(process.env["WINDIR"],"sysnative","cmd.exe");
223-
programFiles = process.env["ProgramW6432"];
224-
}
225-
226-
if (this.connectionConfiguration.puppetAgentDir == undefined) {
227-
puppetAgentDir = path.join(programFiles, "Puppet Labs", "Puppet");
228-
} else {
229-
puppetAgentDir = this.connectionConfiguration.puppetAgentDir;
230-
}
231-
232-
result.options.stdio = 'pipe';
233-
break;
234-
default:
235-
if (this.connectionConfiguration.puppetAgentDir == undefined) {
236-
puppetAgentDir = '/opt/puppetlabs/puppet';
237-
} else {
238-
puppetAgentDir = this.connectionConfiguration.puppetAgentDir;
239-
}
240-
241-
result.options.stdio = 'pipe';
242-
result.options.shell = true;
243-
break;
244-
}
245-
246-
let puppetDir = path.join(puppetAgentDir,"puppet");
247-
let facterDir = path.join(puppetAgentDir,"facter");
248-
let hieraDir = path.join(puppetAgentDir,"hiera");
249-
let mcoDir = path.join(puppetAgentDir,"mcollective");
250-
let rubydir = path.join(puppetAgentDir,"sys","ruby");
251-
let rubylib = path.join(puppetDir,"lib") + this.pathEnvSeparator() + path.join(facterDir,"lib") + this.pathEnvSeparator() + path.join(hieraDir,"lib") + this.pathEnvSeparator() + path.join(mcoDir,"lib")
252-
253-
if (process.platform == 'win32') {
254-
// Translate all slashes to / style to avoid puppet/ruby issue #11930
255-
rubylib = rubylib.replace(/\\/g,"/");
256-
}
257-
258-
// Setup the process environment variables
259-
if (result.options.env.PATH == undefined) { result.options.env.PATH = ''; }
260-
if (result.options.env.RUBYLIB == undefined) { result.options.env.RUBYLIB = ''; }
261-
result.options.env.RUBY_DIR = rubydir;
262-
result.options.env.PATH = path.join(puppetDir,"bin") + this.pathEnvSeparator() + path.join(facterDir,"bin") + this.pathEnvSeparator() + path.join(hieraDir,"bin") + this.pathEnvSeparator() + path.join(mcoDir,"bin") +
263-
this.pathEnvSeparator() + path.join(puppetAgentDir,"bin") + this.pathEnvSeparator() + path.join(rubydir,"bin") + this.pathEnvSeparator() + path.join(puppetAgentDir,"sys","tools","bin") +
264-
this.pathEnvSeparator() + result.options.env.PATH;
265-
result.options.env.RUBYLIB = rubylib + this.pathEnvSeparator() + result.options.env.RUBYLIB;
266-
result.options.env.RUBYOPT = 'rubygems';
267-
result.options.env.SSL_CERT_FILE = path.join(puppetDir,"ssl","cert.pem");
268-
result.options.env.SSL_CERT_DIR = path.join(puppetDir,"ssl","certs");
269-
270-
this.logger.debug(logPrefix + "Using environment variable RUBY_DIR=" + result.options.env.RUBY_DIR);
271-
this.logger.debug(logPrefix + "Using environment variable PATH=" + result.options.env.PATH);
272-
this.logger.debug(logPrefix + "Using environment variable RUBYLIB=" + result.options.env.RUBYLIB);
273-
this.logger.debug(logPrefix + "Using environment variable RUBYOPT=" + result.options.env.RUBYOPT);
274-
this.logger.debug(logPrefix + "Using environment variable SSL_CERT_FILE=" + result.options.env.SSL_CERT_FILE);
275-
this.logger.debug(logPrefix + "Using environment variable SSL_CERT_DIR=" + result.options.env.SSL_CERT_DIR);
276-
277-
return result;
278-
}
279-
280-
// Commented out for the moment. This will be enabled once the configuration and
281-
// exact user story is figured out.
282-
//
283-
// private getLanguageServerFromPDK(serverExe) {
284-
// let logPrefix: string = '[getLanguageServerFromPDK] ';
285-
// // setup defaults
286-
// let spawn_options: cp.SpawnOptions = {}
287-
// spawn_options.env = process.env;
288-
// let result = {
289-
// command: 'ruby',
290-
// args: [serverExe],
291-
// options: spawn_options,
292-
// }
293-
// let pdkDir: string = null;
294-
295-
// // type Platform = 'aix'
296-
// // | 'android'
297-
// // | 'darwin'
298-
// // | 'freebsd'
299-
// // | 'linux'
300-
// // | 'openbsd'
301-
// // | 'sunos'
302-
// // | 'win32';
303-
// switch (process.platform) {
304-
// case 'win32':
305-
// let comspec: string = process.env["COMSPEC"];
306-
// let programFiles = process.env["ProgramFiles"];
307-
// if (process.env["PROCESSOR_ARCHITEW6432"] == "AMD64") {
308-
// // VSCode is running as 32bit process on a 64bit Operating System. Need to break out
309-
// // of the 32bit using the sysnative redirection and environment variables
310-
// comspec = path.join(process.env["WINDIR"],"sysnative","cmd.exe");
311-
// programFiles = process.env["ProgramW6432"];
312-
// }
313-
314-
// pdkDir = path.join(programFiles, "Puppet Labs", "DevelopmentKit");
315-
316-
// result.options.stdio = 'pipe';
317-
// break;
318-
// default:
319-
// pdkDir = '/opt/puppetlabs/pdk';
320-
321-
// result.options.stdio = 'pipe';
322-
// result.options.shell = true;
323-
// break;
324-
// }
325-
// // Check if this really is a PDK installation
326-
// if (!fs.existsSync(path.join(pdkDir, "PDK_VERSION"))) {
327-
// this.logger.debug(logPrefix + "Could not find a valid PDK installation at " + pdkDir);
328-
// return null;
329-
// } else {
330-
// this.logger.debug(logPrefix + "Found a valid PDK installation at " + pdkDir);
331-
// }
332-
333-
// // Now to detect ruby versions
334-
// let subdirs = this.getDirectories(path.join(pdkDir,"private", "ruby"));
335-
// if (subdirs.length == 0) { return null; }
336-
// let rubyDir = path.join(pdkDir,"private", "ruby",subdirs[0]);
337-
338-
// subdirs = this.getDirectories(path.join(pdkDir,"share","cache","ruby"));
339-
// if (subdirs.length == 0) { return null; }
340-
// let gemDir = path.join(pdkDir,"share","cache","ruby",subdirs[0]);
341-
342-
// let rubylib = path.join(pdkDir,'lib')
343-
// if (process.platform == 'win32') {
344-
// // Translate all slashes to / style to avoid puppet/ruby issue #11930
345-
// rubylib = rubylib.replace(/\\/g,"/");
346-
// gemDir = gemDir.replace(/\\/g,"/");
347-
// }
348-
349-
// // Setup the process environment variables
350-
// if (result.options.env.PATH == undefined) { result.options.env.PATH = '' }
351-
// if (result.options.env.RUBYLIB == undefined) { result.options.env.RUBYLIB = '' }
352-
353-
// result.options.env.RUBY_DIR = rubyDir;
354-
// result.options.env.PATH = path.join(pdkDir,'bin') + this.pathEnvSeparator() + path.join(rubyDir,'bin') + this.pathEnvSeparator() + result.options.env.PATH;
355-
// result.options.env.RUBYLIB = path.join(pdkDir,'lib') + this.pathEnvSeparator() + result.options.env.RUBYLIB;
356-
// result.options.env.GEM_PATH = gemDir;
357-
// result.options.env.GEM_HOME = gemDir;
358-
// result.options.env.RUBYOPT = 'rubygems';
359-
360-
// this.logger.debug(logPrefix + "Using environment variable RUBY_DIR=" + result.options.env.RUBY_DIR);
361-
// this.logger.debug(logPrefix + "Using environment variable PATH=" + result.options.env.PATH);
362-
// this.logger.debug(logPrefix + "Using environment variable RUBYLIB=" + result.options.env.RUBYLIB);
363-
// this.logger.debug(logPrefix + "Using environment variable GEM_PATH=" + result.options.env.GEM_PATH);
364-
// this.logger.debug(logPrefix + "Using environment variable GEM_HOME=" + result.options.env.GEM_HOME);
365-
// this.logger.debug(logPrefix + "Using environment variable RUBYOPT=" + result.options.env.RUBYOPT);
366-
367-
// return result;
368-
// }
369-
370182
private createLanguageServerProcess(serverExe: string, callback : Function) {
371183
let logPrefix: string = '[createLanguageServerProcess] ';
372184
this.logger.debug(logPrefix + 'Language server found at: ' + serverExe)
373185

374186
let localServer = null
375187

376-
if (localServer == null) { localServer = this.getLanguageServerFromPuppetAgent(serverExe); }
377-
// if (localServer == null) { localServer = this.getLanguageServerFromPDK(serverExe); }
188+
if (localServer == null) { localServer = RubyHelper.getRubyEnvFromPuppetAgent(serverExe, this.connectionConfiguration, this.logger); }
189+
// Commented out for the moment. This will be enabled once the configuration and exact user story is figured out.
190+
//if (localServer == null) { localServer = RubyHelper.getRubyEnvFromPDK(serverExe, this.connectionConfiguration, this.logger); }
378191

379192
if (localServer == null) {
380193
this.logger.warning(logPrefix + "Could not find a valid Puppet Agent installation");

0 commit comments

Comments
 (0)