forked from swiftlang/vscode-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFolderContext.ts
204 lines (179 loc) · 6.52 KB
/
FolderContext.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2021 the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import * as vscode from "vscode";
import * as path from "path";
import { LinuxMain } from "./LinuxMain";
import { PackageWatcher } from "./PackageWatcher";
import { SwiftPackage, Target, TargetType } from "./SwiftPackage";
import { TestExplorer } from "./TestExplorer/TestExplorer";
import { WorkspaceContext, FolderEvent } from "./WorkspaceContext";
import { BackgroundCompilation } from "./BackgroundCompilation";
import { TaskQueue } from "./tasks/TaskQueue";
import { isPathInsidePath } from "./utilities/filesystem";
export class FolderContext implements vscode.Disposable {
private packageWatcher: PackageWatcher;
public backgroundCompilation: BackgroundCompilation;
public hasResolveErrors = false;
public testExplorer?: TestExplorer;
public taskQueue: TaskQueue;
/**
* FolderContext constructor
* @param folder Workspace Folder
* @param swiftPackage Swift Package inside the folder
* @param workspaceContext Workspace context
*/
private constructor(
public folder: vscode.Uri,
public linuxMain: LinuxMain,
public swiftPackage: SwiftPackage,
public workspaceFolder: vscode.WorkspaceFolder,
public workspaceContext: WorkspaceContext
) {
this.packageWatcher = new PackageWatcher(this, workspaceContext);
this.packageWatcher.install();
this.backgroundCompilation = new BackgroundCompilation(this);
this.taskQueue = new TaskQueue(this);
}
/** dispose of any thing FolderContext holds */
dispose() {
this.linuxMain?.dispose();
this.packageWatcher.dispose();
this.testExplorer?.dispose();
}
/**
* Create FolderContext
* @param folder Folder that Folder Context is being created for
* @param workspaceContext Workspace context for extension
* @returns a new FolderContext
*/
static async create(
folder: vscode.Uri,
workspaceFolder: vscode.WorkspaceFolder,
workspaceContext: WorkspaceContext
): Promise<FolderContext> {
const statusItemText = `Loading Package (${FolderContext.uriName(folder)})`;
workspaceContext.statusItem.start(statusItemText);
const { linuxMain, swiftPackage } =
await workspaceContext.statusItem.showStatusWhileRunning(statusItemText, async () => {
const linuxMain = await LinuxMain.create(folder);
const swiftPackage = await SwiftPackage.create(folder, workspaceContext.toolchain);
return { linuxMain, swiftPackage };
});
workspaceContext.statusItem.end(statusItemText);
const folderContext = new FolderContext(
folder,
linuxMain,
swiftPackage,
workspaceFolder,
workspaceContext
);
const error = swiftPackage.error;
if (error) {
vscode.window.showErrorMessage(
`Failed to load ${folderContext.name}/Package.swift: ${error.message}`
);
workspaceContext.outputChannel.log(
`Failed to load Package.swift: ${error.message}`,
folderContext.name
);
}
return folderContext;
}
get name(): string {
const relativePath = this.relativePath;
if (relativePath.length === 0) {
return this.workspaceFolder.name;
} else {
return `${this.workspaceFolder.name}/${this.relativePath}`;
}
}
get relativePath(): string {
return path.relative(this.workspaceFolder.uri.fsPath, this.folder.fsPath);
}
get isRootFolder(): boolean {
return this.workspaceFolder.uri === this.folder;
}
/** reload swift package for this folder */
async reload() {
await this.swiftPackage.reload(this.workspaceContext.toolchain);
}
/** reload Package.resolved for this folder */
async reloadPackageResolved() {
await this.swiftPackage.reloadPackageResolved();
}
/** Load Swift Plugins and store in Package */
async loadSwiftPlugins() {
const plugins = await SwiftPackage.loadPlugins(
this.folder,
this.workspaceContext.toolchain
);
this.swiftPackage.plugins = plugins;
}
/**
* Fire an event to all folder observers
* @param event event type
*/
async fireEvent(event: FolderEvent) {
this.workspaceContext.fireEvent(this, event);
}
/** Return edited Packages folder */
editedPackageFolder(identifier: string) {
return path.join(this.folder.fsPath, "Packages", identifier);
}
/** Create Test explorer for this folder */
addTestExplorer() {
this.testExplorer = new TestExplorer(this);
}
/** Create Test explorer for this folder */
removeTestExplorer() {
this.testExplorer?.dispose();
this.testExplorer = undefined;
}
/** Refresh the tests in the test explorer for this folder */
refreshTestExplorer() {
if (this.testExplorer?.controller.resolveHandler) {
this.testExplorer.controller.resolveHandler(undefined);
}
}
/** Return if package folder has a test explorer */
hasTestExplorer() {
return this.testExplorer !== undefined;
}
static uriName(uri: vscode.Uri): string {
return path.basename(uri.fsPath);
}
/**
* Find testTarget for URI
* @param uri URI to find target for
* @returns Target
*/
getTestTarget(uri: vscode.Uri, type?: TargetType): Target | undefined {
if (!isPathInsidePath(uri.fsPath, this.folder.fsPath)) {
return undefined;
}
const testTargets = this.swiftPackage.getTargets(type);
const target = testTargets.find(element => {
const relativeUri = path.relative(
path.join(this.folder.fsPath, element.path),
uri.fsPath
);
return element.sources.find(file => file === relativeUri) !== undefined;
});
return target;
}
}
export interface EditedPackage {
name: string;
folder: string;
}