<commit_before>/*---------------------------------------------------------------------------------------------
 *  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-extra-promise';
import * as path from 'path';
import * as vscode from 'vscode';
import * as tasks from 'vscode-tasks';
import {OmnisharpServer} from './omnisharp/server';
import * as serverUtils from './omnisharp/utils';
import * as protocol from './omnisharp/protocol.ts'

interface DebugConfiguration {
    name: string,
    type: string,
    request: string,
    sourceFileMap?: any,
}

interface ConsoleLaunchConfiguration extends DebugConfiguration {
    preLaunchTask: string,
    program: string,
    args: string[],
    cwd: string,
    stopAtEntry: boolean,
    env?: any,
    externalConsole?: boolean
}

interface CommandLine {
    command: string,
    args?: string
}

interface LaunchBrowserConfiguration {
    enabled: boolean,
    args: string,
    windows?: CommandLine,
    osx: CommandLine,
    linux: CommandLine
}

interface WebLaunchConfiguration extends ConsoleLaunchConfiguration {
    launchBrowser: LaunchBrowserConfiguration
}

interface AttachConfiguration extends DebugConfiguration {
    processId: string
}

interface Paths {
    vscodeFolder: string;
    tasksJsonPath: string;
    launchJsonPath: string;
}

function getPaths(): Paths {
    const vscodeFolder = path.join(vscode.workspace.rootPath, '.vscode');
    
    return {
        vscodeFolder: vscodeFolder,
        tasksJsonPath: path.join(vscodeFolder, 'tasks.json'),
        launchJsonPath: path.join(vscodeFolder, 'launch.json')
    }
}

interface Operations {
    addTasksJson?: boolean,
    updateTasksJson?: boolean,
    addLaunchJson?: boolean
}

function hasOperations(operations: Operations) {
    return operations.addLaunchJson ||
           operations.updateTasksJson ||
           operations.addLaunchJson;
}

function getOperations() {
    const paths = getPaths();

    return getBuildOperations(paths.tasksJsonPath).then(operations => 
           getLaunchOperations(paths.launchJsonPath, operations));
}

function getBuildOperations(tasksJsonPath: string) {
    return new Promise<Operations>((resolve, reject) => {
        return fs.existsAsync(tasksJsonPath).then(exists => {
            if (exists) {
                fs.readFileAsync(tasksJsonPath).then(buffer => {
                    const text = buffer.toString();
                    const tasksJson: tasks.TaskConfiguration = JSON.parse(text);
                    const buildTask = tasksJson.tasks.find(td => td.taskName === 'build');
                    
                    resolve({ updateTasksJson: (buildTask === undefined) });
                });
            }
            else {
                resolve({ addTasksJson: true });
            }
        });
    });
}

function getLaunchOperations(launchJsonPath: string, operations: Operations) {
    return new Promise<Operations>((resolve, reject) => {
        return fs.existsAsync(launchJsonPath).then(exists => {
            if (exists) {
                resolve(operations);
            }
            else {
                operations.addLaunchJson = true;
                resolve(operations);
            }
        });
    });
}

function promptToAddAssets() {
    return new Promise<boolean>((resolve, reject) => {
        const item = { title: 'Yes' }
        
        vscode.window.showInformationMessage('Required assets to build and debug are missing from your project. Add them?', item).then(selection => {
            return selection
                ? resolve(true)
                : resolve(false);
        });
    });
}

function computeProgramPath(projectData: TargetProjectData) {
    if (!projectData) {
        // If there's no target project data, use a placeholder for the path.
        return '${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>'
    }

    let result = '${workspaceRoot}';
    
    if (projectData.projectPath) {
        result = path.join(result, path.relative(vscode.workspace.rootPath, projectData.projectPath.fsPath));
    }

    result = path.join(result, `bin/${projectData.configurationName}/${projectData.targetFramework}/${projectData.executableName}`);

    return result;
}

function createLaunchConfiguration(projectData: TargetProjectData): ConsoleLaunchConfiguration {
    return {
        name: '.NET Core Launch (console)',
        type: 'coreclr',
        request: 'launch',
        preLaunchTask: 'build',
        program: computeProgramPath(projectData),
        args: [],
        cwd: '${workspaceRoot}',
        externalConsole: false,
        stopAtEntry: false
    }
}

function createWebLaunchConfiguration(projectData: TargetProjectData): WebLaunchConfiguration {
    return {
        name: '.NET Core Launch (web)',
        type: 'coreclr',
        request: 'launch',
        preLaunchTask: 'build',
        program: computeProgramPath(projectData),
        args: [],
        cwd: '${workspaceRoot}',
        stopAtEntry: false,
        launchBrowser: {
            enabled: true,
            args: '${auto-detect-url}',
            windows: {
                command: 'cmd.exe',
                args: '/C start ${auto-detect-url}'
            },<commit_msg>Re-enable the debug console window by default (#692)<commit_after>/*---------------------------------------------------------------------------------------------
 *  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-extra-promise';
import * as path from 'path';
import * as vscode from 'vscode';
import * as tasks from 'vscode-tasks';
import {OmnisharpServer} from './omnisharp/server';
import * as serverUtils from './omnisharp/utils';
import * as protocol from './omnisharp/protocol.ts'

interface DebugConfiguration {
    name: string,
    type: string,
    request: string,
    internalConsoleOptions?: string,
    sourceFileMap?: any,
}

interface ConsoleLaunchConfiguration extends DebugConfiguration {
    preLaunchTask: string,
    program: string,
    args: string[],
    cwd: string,
    stopAtEntry: boolean,
    env?: any,
    externalConsole?: boolean
}

interface CommandLine {
    command: string,
    args?: string
}

interface LaunchBrowserConfiguration {
    enabled: boolean,
    args: string,
    windows?: CommandLine,
    osx: CommandLine,
    linux: CommandLine
}

interface WebLaunchConfiguration extends ConsoleLaunchConfiguration {
    launchBrowser: LaunchBrowserConfiguration
}

interface AttachConfiguration extends DebugConfiguration {
    processId: string
}

interface Paths {
    vscodeFolder: string;
    tasksJsonPath: string;
    launchJsonPath: string;
}

function getPaths(): Paths {
    const vscodeFolder = path.join(vscode.workspace.rootPath, '.vscode');
    
    return {
        vscodeFolder: vscodeFolder,
        tasksJsonPath: path.join(vscodeFolder, 'tasks.json'),
        launchJsonPath: path.join(vscodeFolder, 'launch.json')
    }
}

interface Operations {
    addTasksJson?: boolean,
    updateTasksJson?: boolean,
    addLaunchJson?: boolean
}

function hasOperations(operations: Operations) {
    return operations.addLaunchJson ||
           operations.updateTasksJson ||
           operations.addLaunchJson;
}

function getOperations() {
    const paths = getPaths();

    return getBuildOperations(paths.tasksJsonPath).then(operations => 
           getLaunchOperations(paths.launchJsonPath, operations));
}

function getBuildOperations(tasksJsonPath: string) {
    return new Promise<Operations>((resolve, reject) => {
        return fs.existsAsync(tasksJsonPath).then(exists => {
            if (exists) {
                fs.readFileAsync(tasksJsonPath).then(buffer => {
                    const text = buffer.toString();
                    const tasksJson: tasks.TaskConfiguration = JSON.parse(text);
                    const buildTask = tasksJson.tasks.find(td => td.taskName === 'build');
                    
                    resolve({ updateTasksJson: (buildTask === undefined) });
                });
            }
            else {
                resolve({ addTasksJson: true });
            }
        });
    });
}

function getLaunchOperations(launchJsonPath: string, operations: Operations) {
    return new Promise<Operations>((resolve, reject) => {
        return fs.existsAsync(launchJsonPath).then(exists => {
            if (exists) {
                resolve(operations);
            }
            else {
                operations.addLaunchJson = true;
                resolve(operations);
            }
        });
    });
}

function promptToAddAssets() {
    return new Promise<boolean>((resolve, reject) => {
        const item = { title: 'Yes' }
        
        vscode.window.showInformationMessage('Required assets to build and debug are missing from your project. Add them?', item).then(selection => {
            return selection
                ? resolve(true)
                : resolve(false);
        });
    });
}

function computeProgramPath(projectData: TargetProjectData) {
    if (!projectData) {
        // If there's no target project data, use a placeholder for the path.
        return '${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>'
    }

    let result = '${workspaceRoot}';
    
    if (projectData.projectPath) {
        result = path.join(result, path.relative(vscode.workspace.rootPath, projectData.projectPath.fsPath));
    }

    result = path.join(result, `bin/${projectData.configurationName}/${projectData.targetFramework}/${projectData.executableName}`);

    return result;
}

function createLaunchConfiguration(projectData: TargetProjectData): ConsoleLaunchConfiguration {
    return {
        name: '.NET Core Launch (console)',
        type: 'coreclr',
        request: 'launch',
        preLaunchTask: 'build',
        program: computeProgramPath(projectData),
        args: [],
        cwd: '${workspaceRoot}',
        externalConsole: false,
        stopAtEntry: false,
        internalConsoleOptions: "openOnSessionStart"
    }
}

function createWebLaunchConfiguration(projectData: TargetProjectData): WebLaunchConfiguration {
    return {
        name: '.NET Core Launch (web)',
        type: 'coreclr',
        request: 'launch',
        preLaunchTask: 'build',
        program: computeProgramPath(projectData),
        args: [],
        cwd: '${workspaceRoot}',
        stopAtEntry: false,
        internalConsoleOptions: "openOnSessionStart",
        launchBrowser: {
            enabled: true,
            args: '${auto-detect-url}',
            windows: {
                command: 'cmd.exe',
                args: '/C start ${auto-detect-url}'
            },
{'fraction_non_alphanumeric': 0.13937376282166636, 'fraction_numerical': 0.00026992981824725573, 'mean_word_length': 2.3368357850495345, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2429745', 'n_tokens_mistral': 2884, 'n_tokens_neox': 2841, 'n_words': 683}