Skip to content

Commit

Permalink
Fix key duplication/handle camelcase properties
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianWilczynski committed Feb 9, 2020
1 parent 0636dea commit 80b2d41
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/VisualStudioCode/package/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 23 additions & 12 deletions src/VisualStudioCode/package/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs';
import * as json5 from 'json5';
import { OmnisharpSettings } from './omnisharpSettings';
import { OmnisharpSettingsKey } from './omnisharpSettings';
import { Context } from './context';

export function activate(context: vscode.ExtensionContext) {
Expand All @@ -29,7 +29,7 @@ export function ensureConfigurationUpdated(context: Context) {

const omnisharpJsonPath = path.join(omnisharpDirectoryPath, 'omnisharp.json');

let omnisharpSettings: OmnisharpSettings;
let omnisharpSettings: any;
let settingsUpdated = false;

if (fs.existsSync(omnisharpJsonPath)) {
Expand All @@ -39,17 +39,23 @@ export function ensureConfigurationUpdated(context: Context) {
omnisharpSettings = {};
}

if (!omnisharpSettings.RoslynExtensionsOptions || typeof omnisharpSettings.RoslynExtensionsOptions !== 'object') {
omnisharpSettings.RoslynExtensionsOptions = {};
const roslynExtensionsOptionsKey = findKey(omnisharpSettings, OmnisharpSettingsKey.RoslynExtensionsOptions);

if (!omnisharpSettings[roslynExtensionsOptionsKey] || typeof omnisharpSettings[roslynExtensionsOptionsKey] !== 'object') {
omnisharpSettings[roslynExtensionsOptionsKey] = {};
}

if (omnisharpSettings.RoslynExtensionsOptions.EnableAnalyzersSupport !== true) {
omnisharpSettings.RoslynExtensionsOptions.EnableAnalyzersSupport = true;
const enableAnalyzersSupportKey = findKey(omnisharpSettings[roslynExtensionsOptionsKey], OmnisharpSettingsKey.EnableAnalyzersSupport);

if (omnisharpSettings[roslynExtensionsOptionsKey][enableAnalyzersSupportKey] !== true) {
omnisharpSettings[roslynExtensionsOptionsKey][enableAnalyzersSupportKey] = true;
settingsUpdated = true;
}

if (!Array.isArray(omnisharpSettings.RoslynExtensionsOptions.LocationPaths)) {
omnisharpSettings.RoslynExtensionsOptions.LocationPaths = [];
const locationPathsKey = findKey(omnisharpSettings[roslynExtensionsOptionsKey], OmnisharpSettingsKey.LocationPaths);

if (!Array.isArray(omnisharpSettings[roslynExtensionsOptionsKey][locationPathsKey])) {
omnisharpSettings[roslynExtensionsOptionsKey][locationPathsKey] = [];
}

const roslynPath = path.join(context.extensionDirectoryPath, 'roslyn');
Expand All @@ -61,13 +67,14 @@ export function ensureConfigurationUpdated(context: Context) {
].map(p => p.replace(/\\/g, '/'));

const containsPaths = locationPaths.every(
p => omnisharpSettings.RoslynExtensionsOptions!.LocationPaths!.includes(p));
p => (omnisharpSettings[roslynExtensionsOptionsKey][locationPathsKey] as any[]).includes(p));

if (!containsPaths) {
const unrelatedPaths = omnisharpSettings.RoslynExtensionsOptions.LocationPaths
.filter(p => !p.includes('josefpihrt-vscode.roslynator') && !locationPaths.includes(p));
const unrelatedPaths: string[] = (omnisharpSettings[roslynExtensionsOptionsKey][locationPathsKey] as any[])
.filter(p => typeof p === 'string'
&& !p.includes('josefpihrt-vscode.roslynator') && !locationPaths.includes(p));

omnisharpSettings.RoslynExtensionsOptions.LocationPaths = [
omnisharpSettings[roslynExtensionsOptionsKey][locationPathsKey] = [
...unrelatedPaths,
...locationPaths
];
Expand All @@ -80,4 +87,8 @@ export function ensureConfigurationUpdated(context: Context) {

vscode.window.showInformationMessage('omnisharp.json has been updated with Roslynator configuration.');
}
}

function findKey(settings: any, key: OmnisharpSettingsKey) {
return Object.keys(settings).find(k => k.toLowerCase() === key.toLowerCase()) ?? key;
}
6 changes: 6 additions & 0 deletions src/VisualStudioCode/package/src/omnisharpSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ export interface OmnisharpSettings {
EnableAnalyzersSupport?: boolean;
LocationPaths?: string[];
};
}

export enum OmnisharpSettingsKey {
RoslynExtensionsOptions = 'RoslynExtensionsOptions',
EnableAnalyzersSupport = 'EnableAnalyzersSupport',
LocationPaths = 'LocationPaths'
}
40 changes: 36 additions & 4 deletions src/VisualStudioCode/package/src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ suite('Auto update omnisharp.json', () => {
RoslynExtensionsOptions: {
EnableAnalyzersSupport: true,
LocationPaths: [
"/temp/home/.vscode/extensions/josefpihrt-vscode.roslynator-1.0.1/roslyn/common",
"/temp/home/.vscode/extensions/josefpihrt-vscode.roslynator-1.0.1/roslyn/analyzers",
"/temp/home/.vscode/extensions/josefpihrt-vscode.roslynator-1.0.1/roslyn/refactorings",
"/temp/home/.vscode/extensions/josefpihrt-vscode.roslynator-1.0.1/roslyn/fixes"
'/temp/home/.vscode/extensions/josefpihrt-vscode.roslynator-1.0.1/roslyn/common',
'/temp/home/.vscode/extensions/josefpihrt-vscode.roslynator-1.0.1/roslyn/analyzers',
'/temp/home/.vscode/extensions/josefpihrt-vscode.roslynator-1.0.1/roslyn/refactorings',
'/temp/home/.vscode/extensions/josefpihrt-vscode.roslynator-1.0.1/roslyn/fixes'
]
}
};
Expand All @@ -66,4 +66,36 @@ suite('Auto update omnisharp.json', () => {
assert.ok(omnisharpSettings.RoslynExtensionsOptions?.LocationPaths?.every(
p => !p.includes('josefpihrt-vscode.roslynator-1.0.1')));
});

test('Handle camel cased properties', () => {
const oldOmnisharpSettings = {
RoslynExtensionsOptions: {
enableAnalyzersSupport: true,
locationPaths: [
'/path/to/custom/analyzers/'
]
}
};

fs.mkdirSync(omnisharpPath);
fs.writeJSONSync(omnisharpJsonPath, oldOmnisharpSettings);

roslynator.ensureConfigurationUpdated({
extensionDirectoryPath: path.join(extensionsPath, 'josefpihrt-vscode.roslynator-1.0.1'),
homeDirectoryPath: homePath
});

const omnisharpSettings = fs.readJSONSync(omnisharpJsonPath);

assert.strictEqual(omnisharpSettings.RoslynExtensionsOptions.LocationPaths, undefined);
assert.strictEqual(omnisharpSettings.RoslynExtensionsOptions.EnableAnalyzersSupport, undefined);

assert.ok(omnisharpSettings.RoslynExtensionsOptions.enableAnalyzersSupport);

assert.ok((omnisharpSettings.RoslynExtensionsOptions.locationPaths as string[])
.includes('/path/to/custom/analyzers/'));

assert.ok((omnisharpSettings.RoslynExtensionsOptions.locationPaths as string[])
.some(p => p.includes('josefpihrt-vscode.roslynator-1.0.1')));
});
});

0 comments on commit 80b2d41

Please sign in to comment.