-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.ts
101 lines (71 loc) · 3.83 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as vsc from "vscode";
import Configuration from "./types/Configuration";
import CSharpConfiguration from "./types/CSharpConfiguration";
import Task from "./types/Task";
import Solution from "./types/Solution";
const selectActiveProjectCommandString = `csharp-active-project.selectProject`;
export async function activate({subscriptions}: vsc.ExtensionContext)
{
let workspace = vsc.workspace.workspaceFolders?.[0].uri;
if (workspace == null) return;
if (await Solution.find(workspace) == null) return;
let csharpLaunchConfiguration = readCSharpActiveProjectLaunchConfiguration();
if (csharpLaunchConfiguration == null) return;
let activeProjectName = csharpLaunchConfiguration.program.match(/.*\/(.*?)\.dll/)![1];
let activeProjectStatusBarItem = vsc.window.createStatusBarItem(vsc.StatusBarAlignment.Left);
activeProjectStatusBarItem.command = selectActiveProjectCommandString;
activeProjectStatusBarItem.text = `$(breakpoints-view-icon) ${activeProjectName}`;
activeProjectStatusBarItem.show();
subscriptions.push(activeProjectStatusBarItem);
let selectActiveProjectCommand = vsc.commands.registerCommand(selectActiveProjectCommandString, async () => {
let solutionFilePath = (await Solution.find(workspace!))!;
let solution = await Solution.read(vsc.Uri.joinPath(workspace!, solutionFilePath));
let projects = solution.projects.filter(project => project.outputType != null
&& project.outputType !== `Library`);
let selectedProjectName = await vsc.window.showQuickPick(projects.map(project => project.name));
if (selectedProjectName == null) return;
let selectedProject = projects.find(project => (project.name === selectedProjectName))!;
let launchConfiguration = readCSharpActiveProjectLaunchConfiguration()!;
let buildTask = readCSharpActiveProjectBuildTask()!;
launchConfiguration.program = `${selectedProject.directory.replace(workspace!.fsPath, `\${workspaceFolder}`)}/bin/Debug/${selectedProject.targetFramework}/${selectedProject.name}.dll`;
await writeCSharpActiveProjectLaunchConfiguration(launchConfiguration);
buildTask.args[1] = selectedProject.path.replace(workspace!.fsPath, `\${workspaceFolder}`);
await writeCSharpActiveProjectBuildTask(buildTask);
let activeProjectName = selectedProject.name;
activeProjectStatusBarItem.text = `$(breakpoints-view-icon) ${activeProjectName}`;
});
subscriptions.push(selectActiveProjectCommand);
}
function readCSharpActiveProjectLaunchConfiguration()
{
let launch = vsc.workspace.getConfiguration(`launch`);
let launchConfigurations = launch.get<Configuration[]>(`configurations`);
if (launchConfigurations == null) return null;
if (launchConfigurations.length === 0) return null;
let launchConfiguration = launchConfigurations[0];
let csharpLaunchConfiguration = CSharpConfiguration.tryConvert(launchConfiguration);
return csharpLaunchConfiguration;
}
function writeCSharpActiveProjectLaunchConfiguration(csharpActiveProjectLaunchConfiguration: Configuration)
{
let launch = vsc.workspace.getConfiguration(`launch`);
let launchConfigurations = launch.get<Configuration[]>(`configurations`)!;
launchConfigurations[0] = csharpActiveProjectLaunchConfiguration;
return launch.update(`configurations`, launchConfigurations);
}
function readCSharpActiveProjectBuildTask()
{
let tasks = vsc.workspace.getConfiguration(`tasks`);
let tasksTasks = tasks.get<Task[]>(`tasks`);
if (tasksTasks == null) return null;
if (tasksTasks.length === 0) return null;
let task = tasksTasks[0];
return task;
}
function writeCSharpActiveProjectBuildTask(csharpActiveProjectBuildTask: Task)
{
let tasks = vsc.workspace.getConfiguration(`tasks`);
let tasksTasks = tasks.get<Task[]>(`tasks`)!;
tasksTasks[0] = csharpActiveProjectBuildTask;
return tasks.update(`tasks`, tasksTasks);
}