Skip to content

Commit

Permalink
(cake-build#733) use existing properties inside omnisharp.json
Browse files Browse the repository at this point in the history
instead of enforcing properties "cake" and "bakeryPath"
case sensitive.
  • Loading branch information
nils-a committed Oct 13, 2022
1 parent 4bc6337 commit fc99053
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
48 changes: 37 additions & 11 deletions src/bakery/cakeBakery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,36 @@ export class CakeBakery {

const targetPath = this.getTargetPath();
const omnisharpCakeConfigFile = this.getOmnisharpCakeConfigFile();
let omnisharpCakeConfig = {};
if (fs.existsSync(omnisharpCakeConfigFile)) {
// Read in file
//import omnisharpCakeConfig from omnisharpCakeConfigFile;
var omnisharpCakeConfig = JSON.parse(fs.readFileSync(omnisharpCakeConfigFile, 'utf-8'))
logger.logInfo(`existing bakery-path: ${omnisharpCakeConfig.cake.bakeryPath}`);
omnisharpCakeConfig.cake.bakeryPath = targetPath;
logger.logInfo(`new bakery-path: ${omnisharpCakeConfig.cake.bakeryPath}`);
fs.writeFileSync(omnisharpCakeConfigFile, JSON.stringify(omnisharpCakeConfig, null, 2));

// lets force a restart of the Omnisharp server to use new config
vscode.commands.executeCommand('o.restart');
omnisharpCakeConfig = JSON.parse(fs.readFileSync(omnisharpCakeConfigFile, 'utf-8'))
} else {
// create file
var newOmnisharpCakeConfig = { cake: { bakeryPath: targetPath }};
fs.writeFileSync(omnisharpCakeConfigFile, JSON.stringify(newOmnisharpCakeConfig));
logger.logInfo(`${omnisharpCakeConfigFile} will be created.`);
}

let cakePropName = this.getPropertyNameCaseInsensitive(omnisharpCakeConfig, "cake");
if(!cakePropName) {
cakePropName = "cake"
omnisharpCakeConfig[cakePropName] = {};
}

let bakeryPathPropName = this.getPropertyNameCaseInsensitive(omnisharpCakeConfig[cakePropName], "bakeryPath");
if(!bakeryPathPropName) {
bakeryPathPropName = "bakeryPath"
omnisharpCakeConfig[cakePropName][bakeryPathPropName] = "";
}

logger.logInfo(`existing bakery-path: ${omnisharpCakeConfig[cakePropName][bakeryPathPropName]}`);
omnisharpCakeConfig[cakePropName][bakeryPathPropName] = targetPath;
logger.logInfo(`new bakery-path: ${omnisharpCakeConfig[cakePropName][bakeryPathPropName]}`);


fs.writeFileSync(omnisharpCakeConfigFile, JSON.stringify(omnisharpCakeConfig, null, 2));

// lets force a restart of the Omnisharp server to use new config
vscode.commands.executeCommand('o.restart');
logger.logInfo("Omnisharp setting successfully updated.")
resolve();
} catch (e) {
Expand All @@ -124,4 +138,16 @@ export class CakeBakery {
private getOmnisharpCakeConfigFile() : string {
return path.join(this.getOmnisharpUserFolderPath(), "omnisharp.json");
}

private getPropertyNameCaseInsensitive(obj: any, propName: string) : string|null {
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if(propName.toLowerCase() == key.toLowerCase()) {
return key;
}
}
}

return null;
}
}
24 changes: 14 additions & 10 deletions src/bakery/cakeBakeryCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import { logger } from '../shared'

export async function updateCakeBakeryCommand(extensionPath: string) {
// Install Cake Bakery
var result = await forceInstallBakery(extensionPath);
if (result) {
commands.executeCommand('o.restart');
window.showInformationMessage(
'Intellisense support (Cake.Bakery) for Cake files was installed.'
);
} else {
window.showErrorMessage(
'Error downloading intellisense support (Cake.Bakery) for Cake files.'
);
try {
var result = await forceInstallBakery(extensionPath);
if (result) {
commands.executeCommand('o.restart');
window.showInformationMessage(
'Intellisense support (Cake.Bakery) for Cake files was installed.'
);
} else {
window.showErrorMessage(
'Error downloading intellisense support (Cake.Bakery) for Cake files.'
);
}
} catch (e: unknown) {
logger.logError("Intellisense support (Cake.Bakery) for Cake files NOT installed!\r\n> "+e, false)
}
}

Expand Down

0 comments on commit fc99053

Please sign in to comment.