Skip to content

Commit

Permalink
Merge branch 'hotfix/2.1.1'
Browse files Browse the repository at this point in the history
* hotfix/2.1.1:
  (#728) update omnisharp on every update of cake.bakery
  (#730) added a new script task
  • Loading branch information
gep13 committed Oct 11, 2022
2 parents 7abcee2 + 50d3a50 commit b576a32
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 57 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,10 @@
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"watch": "npm run ensureCi && tsc -watch -p ./",
"test": "npm run compile && node ./out/test/runTest.js",
"depcheck": "depcheck"
"depcheck": "depcheck",
"ensureCi": "node -e \"const fs=require('fs'); if (fs.existsSync('./node_modules/')) { process.exit(123); }\" && npm ci"
},
"dependencies": {
"adm-zip": "^0.5.9",
Expand Down
76 changes: 63 additions & 13 deletions src/bakery/cakeBakery.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
var request = require('request');
var AdmZip = require('adm-zip');
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import { window } from 'vscode';
import {
CAKE_BAKERY_PACKAGE_URL,
DEFAULT_RESPONSE_TIMEOUT
} from '../constants';
import { logger } from '../shared'

export class CakeBakery {
private extensionPath : string;
Expand All @@ -17,35 +20,33 @@ export class CakeBakery {

public getTargetPath(): string {
return path.join(
this.extensionPath,
'Cake.Bakery/tools/Cake.Bakery.dll'
this.getInstallationPath(),
'tools/Cake.Bakery.dll'
);
}

public getNupkgDestinationPath(): string {
public getInstallationPath(): string {
return path.join(
this.extensionPath,
'Cake.Bakery'
);
}

public getToolFolderPath(): string {
return path.join(
this.extensionPath);
}

public downloadAndExtract(): Thenable<boolean> {
const installationPath = this.getInstallationPath();
const extensionPath = this.extensionPath;
return new Promise((resolve, reject) => {
// Download the NuGet Package
let vm = this;
try {
if (!fs.existsSync(vm.getToolFolderPath())) {
fs.mkdirSync(vm.getToolFolderPath());
if (!fs.existsSync(extensionPath)) {
fs.mkdirSync(extensionPath);
}
} catch (error) {
window.showErrorMessage('Unable to create directory');
}

logger.logInfo("Downloading Cake.Bakery to " + extensionPath)

var data: any[] = [],
dataLen = 0;

Expand All @@ -66,12 +67,61 @@ export class CakeBakery {
}

var zip = new AdmZip(buf);
zip.extractAllTo(vm.getNupkgDestinationPath());
zip.extractAllTo(installationPath);
logger.logInfo("Cake.Bakery successfully installed to: " + installationPath)
resolve(true);
})
.on('error', function(e: any) {
reject(`Failed to download Cake Bakery from NuGet: ${e}`);
const err = `Failed to download Cake Bakery from NuGet: ${e}`;
logger.logError(err);
reject(err);
});
});
}

public updateOmnisharpSettings() : Thenable<void> {
return new Promise((resolve, reject) => {
logger.logInfo("Updating Cake.Bakery path in omnisharp.");
try {
const omnisharpUserFolderPath = this.getOmnisharpUserFolderPath();
if (!fs.existsSync(omnisharpUserFolderPath)) {
fs.mkdirSync(omnisharpUserFolderPath);
}

const targetPath = this.getTargetPath();
const omnisharpCakeConfigFile = this.getOmnisharpCakeConfigFile();
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');
} else {
// create file
var newOmnisharpCakeConfig = { cake: { bakeryPath: targetPath }};
fs.writeFileSync(omnisharpCakeConfigFile, JSON.stringify(newOmnisharpCakeConfig));
}
logger.logInfo("Omnisharp setting successfully updated.")
resolve();
} catch (e) {
const err = `Failed to update Omnisharp settings: ${e}`;
logger.logError(err);
reject(err);
}
});
}


private getOmnisharpUserFolderPath() : string {
return path.join(os.homedir(), ".omnisharp");
}

private getOmnisharpCakeConfigFile() : string {
return path.join(this.getOmnisharpUserFolderPath(), "omnisharp.json");
}
}
24 changes: 11 additions & 13 deletions src/bakery/cakeBakeryCommand.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
import { commands, window, workspace } from 'vscode';
import { commands, window } from 'vscode';
import * as fs from 'fs';
import { CakeBakery } from './cakeBakery';
import { logger } from '../shared'

export async function updateCakeBakeryCommand(extensionPath: string) {
// Make sure that we're in the correct place.
if (workspace.rootPath === undefined) {
window.showErrorMessage('You have not yet opened a folder.');
return;
}

// Install Cake Bakery
var result = await installCakeDebug(extensionPath);
var result = await forceInstallBakery(extensionPath);
if (result) {
commands.executeCommand('o.restart');
window.showInformationMessage(
'Intellisense support for Cake files was installed.'
'Intellisense support (Cake.Bakery) for Cake files was installed.'
);
} else {
window.showErrorMessage(
'Error downloading intellisense support for Cake files.'
'Error downloading intellisense support (Cake.Bakery) for Cake files.'
);
}
}

export async function installCakeDebug(extensionPath: string): Promise<boolean> {
export async function forceInstallBakery(extensionPath: string): Promise<boolean> {
let bakery = new CakeBakery(extensionPath);

var targetPath = bakery.getNupkgDestinationPath();
var targetPath = bakery.getInstallationPath();
logger.logInfo("Force-updating Cake.Bakery in " + targetPath);
if (fs.existsSync(targetPath)) {
fs.rmdirSync(targetPath, {recursive: true});
}

return await bakery.downloadAndExtract();
const success = await bakery.downloadAndExtract();
await bakery.updateOmnisharpSettings();
return success;
}
30 changes: 1 addition & 29 deletions src/cakeMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,41 +105,13 @@ export function activate(context: vscode.ExtensionContext): void {
onConfigurationChanged(context, null as unknown as vscode.ConfigurationChangeEvent);
}

function getOmnisharpUserFolderPath() : string {
return path.join(os.homedir(), ".omnisharp");
}

function getOmnisharpCakeConfigFile() : string {
return path.join(getOmnisharpUserFolderPath(), "omnisharp.json");
}

async function _registerCakeBakery(context: vscode.ExtensionContext) {
let bakery = new CakeBakery(context.extensionPath);
var targetPath = bakery.getTargetPath();

if (!fs.existsSync(targetPath)) {
await bakery.downloadAndExtract();

if (!fs.existsSync(getOmnisharpUserFolderPath())) {
fs.mkdirSync(getOmnisharpUserFolderPath());
}

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

// lets force a restart of the Omnisharp server to use new config
vscode.commands.executeCommand('o.restart');
} else {
// create file
var newOmnisharpCakeConfig = { cake: { bakeryPath: targetPath }};
fs.writeFileSync(getOmnisharpCakeConfigFile(), JSON.stringify(newOmnisharpCakeConfig));
}
await bakery.updateOmnisharpSettings();
} else {
logger.logToOutput("Cake.Bakery has already been installed, skipping automated download and extraction.");
}
Expand Down

0 comments on commit b576a32

Please sign in to comment.