Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with 'Add tasks.json' command #50

Merged
merged 1 commit into from
Mar 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 43 additions & 56 deletions src/features/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as proto from '../protocol';
import {OmnisharpServer} from '../omnisharpServer';
import findLaunchTargets from '../launchTargetFinder';
import * as pathHelpers from '../pathHelpers';
import {runInTerminal} from 'run-in-terminal';
import * as fs from 'fs';
import * as path from 'path';
Expand Down Expand Up @@ -173,39 +174,24 @@ export function dnxRestoreForProject(server: OmnisharpServer, fileName: string)
});
}

function exists(path: string) {
return new Promise<boolean>((resolve, reject) => {
fs.exists(path, exists => {
if (exists) {
resolve(true);
}
else {
resolve(false);
}
})
})
}

function mkdir(directoryPath: string) {
return new Promise<boolean>((resolve, reject) => {
fs.mkdir(directoryPath, err => {
if (!err) {
resolve(true);
}
else {
reject(err);
}
})
})
}

function ensureDirectoryCreated(directoryPath: string) {
return exists(directoryPath).then(e => {
return pathHelpers.exists(directoryPath).then(e => {
if (e) {
return true;
}
else {
return mkdir(directoryPath);
return pathHelpers.mkdir(directoryPath);
}
});
}

function getExpectedVsCodeFolderPath(solutionPathOrFolder: string): Promise<string> {
return pathHelpers.getPathKind(solutionPathOrFolder).then(kind => {
if (kind === pathHelpers.PathKind.File) {
return path.join(path.dirname(solutionPathOrFolder), '.vscode');
}
else {
return path.join(solutionPathOrFolder, '.vscode');
}
});
}
Expand All @@ -222,37 +208,38 @@ export function addTasksJson(server: OmnisharpServer, extensionPath: string) {
return reject('No solution or folder open.');
}

let vscodeFolderPath = path.join(path.dirname(solutionPathOrFolder), '.vscode');
let tasksJsonPath = path.join(vscodeFolderPath, 'tasks.json');

return exists(tasksJsonPath).then(e => {
if (e) {
return vscode.window.showInformationMessage(`${tasksJsonPath} already exists.`).then(_ => {
return resolve(tasksJsonPath);
});
}
else {
let templatePath = path.join(extensionPath, 'template-tasks.json');

return exists(templatePath).then(e => {
if (!e) {
return reject('Could not find template-tasks.json file in extension.');
}
return getExpectedVsCodeFolderPath(solutionPathOrFolder).then(vscodeFolderPath => {
let tasksJsonPath = path.join(vscodeFolderPath, 'tasks.json');

return pathHelpers.exists(tasksJsonPath).then(e => {
if (e) {
return vscode.window.showInformationMessage(`${tasksJsonPath} already exists.`).then(_ => {
return resolve(tasksJsonPath);
});
}
else {
let templatePath = path.join(extensionPath, 'template-tasks.json');

return ensureDirectoryCreated(vscodeFolderPath).then(ok => {
if (ok) {
let oldFile = fs.createReadStream(templatePath);
let newFile = fs.createWriteStream(tasksJsonPath);
oldFile.pipe(newFile);

return resolve(tasksJsonPath);
}
else {
return reject(`Could not create ${vscodeFolderPath} directory.`);
return pathHelpers.exists(templatePath).then(e => {
if (!e) {
return reject('Could not find template-tasks.json file in extension.');
}

return ensureDirectoryCreated(vscodeFolderPath).then(ok => {
if (ok) {
let oldFile = fs.createReadStream(templatePath);
let newFile = fs.createWriteStream(tasksJsonPath);
oldFile.pipe(newFile);

return resolve(tasksJsonPath);
}
else {
return reject(`Could not create ${vscodeFolderPath} directory.`);
}
});
});
});
}
}
});
});
});
}
60 changes: 60 additions & 0 deletions src/pathHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

'use strict';

import * as fs from 'fs';
import * as path from 'path';

export enum PathKind {
File,
Directory
}

export function getPathKind(path: string): Promise<PathKind> {
return new Promise<PathKind>((resolve, reject) =>
{
fs.lstat(path, (err, stats) => {
if (err) {
reject(err);
}
else if (stats.isFile()) {
resolve(PathKind.File);
}
else if (stats.isDirectory()) {
resolve(PathKind.Directory);
}
else {
reject(Error(`Path is not file or directory: ${path}`));
}
});
});
}

export function exists(path: string) {
return new Promise<boolean>((resolve, reject) => {
fs.exists(path, exists => {
if (exists) {
resolve(true);
}
else {
resolve(false);
}
})
})
}

export function mkdir(directoryPath: string) {
return new Promise<boolean>((resolve, reject) => {
fs.mkdir(directoryPath, err => {
if (!err) {
resolve(true);
}
else {
reject(err);
}
})
})
}