Skip to content

Commit 84f5550

Browse files
committed
(GH-480) Automaticaly find ruby version from PDK
This commit changes the hardcoded paths for Ruby and Gem versions provided by the PDK to methods that match based on `latest` versions. This is rather naive sort approach, and it is kinda what got us to hardcode things in 7b9083e However, the logic works in that the versions are sorted in descending order this time around instead of assuming the one we want is the last in the list. The sorted 'first' is our latest, and the one we should use both for ruby version as well as for the cached version. This sort can fail if there are weird version numbers that don't match semver, but ruby generally follows these and Puppet doesn't vendor interim versions so PDK doesn't either.
1 parent b74acdb commit 84f5550

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
1111
- ([GH-476](https://github.com/lingua-pupuli/puppet-vscode/issues/476)) Add Bolt commands
1212
- ([GH-477](https://github.com/lingua-pupuli/puppet-vscode/issues/477)) Add Bolt yaml snippets
1313

14+
### Fixed
15+
16+
- ([GH-480](https://github.com/lingua-pupuli/puppet-vscode/issues/480)) Automatically find Ruby version from PDK
17+
1418
## [0.16.0] - 2019-01-25
1519

1620
### Added

src/configuration.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import * as vscode from 'vscode';
44
import * as path from 'path';
5+
import * as fs from 'fs';
56

67
import { IConnectionConfiguration, ConnectionType, ProtocolType, PuppetInstallType } from './interfaces';
78
import { PathResolver } from './configuration/pathResolver';
@@ -13,8 +14,6 @@ export class ConnectionConfiguration implements IConnectionConfiguration {
1314
public timeout: number;
1415
public debugFilePath: string;
1516
private settings: ISettings;
16-
private gemRubyVersionDir: string;
17-
private rubyVersionDir: string;
1817

1918
constructor() {
2019
this.settings = settingsFromWorkspace();
@@ -25,9 +24,6 @@ export class ConnectionConfiguration implements IConnectionConfiguration {
2524
this.debugFilePath = this.settings.editorService.debugFilePath;
2625

2726
this._puppetInstallType = this.settings.installType;
28-
29-
this.rubyVersionDir = '2.5.1';
30-
this.gemRubyVersionDir = '2.5.0';
3127
}
3228

3329
private _puppetInstallType: PuppetInstallType;
@@ -207,35 +203,35 @@ export class ConnectionConfiguration implements IConnectionConfiguration {
207203

208204
get pdkRubyLib(): string {
209205
var lib = path.join(this.puppetBaseDir, 'lib');
210-
if (process.platform === 'win32') {
211-
// Translate all slashes to / style to avoid puppet/ruby issue #11930
212-
lib = lib.replace(/\\/g, '/');
213-
}
206+
lib = this.replaceSlashes(lib);
214207
return lib;
215208
}
216209

217210
get pdkRubyVerDir(): string {
218211
var rootDir = path.join(this.puppetBaseDir, 'private', 'puppet', 'ruby');
219212

220-
return path.join(rootDir, this.gemRubyVersionDir);
213+
return this.findFirstDirectory(rootDir);
221214
}
222215

223216
get pdkGemDir(): string {
224-
// GEM_HOME=C:\Users\user\AppData\Local/PDK/cache/ruby/2.4.0
217+
// bundler cache - C:\Users\user\AppData\Local/PDK/cache/ruby
218+
// pdk source - C:\Program Files\Puppet Labs\DevelopmentKit\share\cache\ruby
225219
var rootDir = path.join(this.puppetBaseDir, 'share', 'cache', 'ruby');
226-
var directory = path.join(rootDir, this.gemRubyVersionDir);
227220

228-
if (process.platform === 'win32') {
229-
// Translate all slashes to / style to avoid puppet/ruby issue #11930
230-
directory = directory.replace(/\\/g, '/');
231-
}
221+
// bundler cache - C:\Users\user\AppData\Local/PDK/cache/ruby/2.4.0
222+
// pdk source - C:\Program Files\Puppet Labs\DevelopmentKit\share\cache\ruby\2.4.0
223+
var directory = this.findFirstDirectory(rootDir);
224+
225+
directory = this.replaceSlashes(directory);
232226
return directory;
233227
}
234228

235229
get pdkRubyDir(): string {
230+
// /Puppet Labs/DevelopmentKit/private/ruby
236231
var rootDir = path.join(this.puppetBaseDir, 'private', 'ruby');
237232

238-
return path.join(rootDir, this.rubyVersionDir);;
233+
// /Puppet Labs/DevelopmentKit/private/ruby/2.5.3
234+
return this.findFirstDirectory(rootDir);
239235
}
240236

241237
get pdkRubyBinDir(): string {
@@ -245,7 +241,21 @@ export class ConnectionConfiguration implements IConnectionConfiguration {
245241
get pdkGemVerDir(): string {
246242
var rootDir = path.join(this.pdkRubyDir, 'lib', 'ruby', 'gems');
247243

248-
return path.join(rootDir, this.gemRubyVersionDir);;
244+
return this.findFirstDirectory(rootDir);
245+
}
246+
247+
private findFirstDirectory(rootDir: string) {
248+
var files = fs.readdirSync(rootDir);
249+
let result = files.sort( (a, b) => a.localeCompare(b, undefined, { numeric:true }) ).reverse()[0];
250+
return path.join(rootDir, result);
251+
}
252+
253+
private replaceSlashes(path: string): string {
254+
if (process.platform === 'win32') {
255+
// Translate all slashes to / style to avoid puppet/ruby issue #11930
256+
path = path.replace(/\\/g, '/');
257+
}
258+
return path;
249259
}
250260

251261
// GEM_PATH=C:/Program Files/Puppet Labs/DevelopmentKit/private/ruby/2.4.4/lib/ruby/gems/2.4.0;C:/Program Files/Puppet Labs/DevelopmentKit/share/cache/ruby/2.4.0;C:/Program Files/Puppet Labs/DevelopmentKit/private/puppet/ruby/2.4.0

0 commit comments

Comments
 (0)