-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathPackageWatcher.ts
98 lines (89 loc) · 3.72 KB
/
PackageWatcher.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2024 Apple Inc. and 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 { FolderContext } from "./FolderContext";
import { FolderOperation, WorkspaceContext } from "./WorkspaceContext";
/**
* Watches for changes to **Package.swift** and **Package.resolved**.
*
* Any changes to these files will update the context keys, trigger a `resolve` task,
* and update the Package Dependencies view.
*/
export class PackageWatcher {
private packageFileWatcher?: vscode.FileSystemWatcher;
private resolvedFileWatcher?: vscode.FileSystemWatcher;
constructor(
private folderContext: FolderContext,
private workspaceContext: WorkspaceContext
) {}
/**
* Creates and installs {@link vscode.FileSystemWatcher file system watchers} for
* **Package.swift** and **Package.resolved**.
*/
install() {
this.packageFileWatcher = this.createPackageFileWatcher();
this.resolvedFileWatcher = this.createResolvedFileWatcher();
}
/**
* Disposes the {@link vscode.FileSystemWatcher file system watchers}
* when the extension deactivates.
*/
dispose() {
this.packageFileWatcher?.dispose();
this.resolvedFileWatcher?.dispose();
}
private createPackageFileWatcher(): vscode.FileSystemWatcher {
const watcher = vscode.workspace.createFileSystemWatcher(
new vscode.RelativePattern(this.folderContext.folder, "Package.swift")
);
watcher.onDidCreate(async () => await this.handlePackageSwiftChange());
watcher.onDidChange(async () => await this.handlePackageSwiftChange());
watcher.onDidDelete(async () => await this.handlePackageSwiftChange());
return watcher;
}
private createResolvedFileWatcher(): vscode.FileSystemWatcher {
const watcher = vscode.workspace.createFileSystemWatcher(
new vscode.RelativePattern(this.folderContext.folder, "Package.resolved")
);
watcher.onDidCreate(async () => await this.handlePackageResolvedChange());
watcher.onDidChange(async () => await this.handlePackageResolvedChange());
watcher.onDidDelete(async () => await this.handlePackageResolvedChange());
return watcher;
}
/**
* Handles a create or change event for **Package.swift**.
*
* This will reload the swift package description, update the
* launch configuration if required and then resolve the package
* dependencies.
*/
async handlePackageSwiftChange() {
// Load SwiftPM Package.swift description
await this.folderContext.reload();
this.workspaceContext.fireEvent(this.folderContext, FolderOperation.packageUpdated);
}
/**
* Handles a create or change event for **Package.resolved**.
*
* This will resolve any changes in the Package.resolved.
*/
private async handlePackageResolvedChange() {
const packageResolvedHash = this.folderContext.swiftPackage.resolved?.fileHash;
await this.folderContext.reloadPackageResolved();
// if file contents has changed then send resolve updated message
if (this.folderContext.swiftPackage.resolved?.fileHash !== packageResolvedHash) {
this.workspaceContext.fireEvent(this.folderContext, FolderOperation.resolvedUpdated);
}
}
}