forked from dart-lang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.dart
More file actions
108 lines (97 loc) · 3.43 KB
/
Copy pathevent.dart
File metadata and controls
108 lines (97 loc) · 3.43 KB
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
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
/// Extension type replacing [FileSystemEvent] for `package:watcher` internal
/// use.
///
/// The [FileSystemDeleteEvent] subclass of [FileSystemEvent] does something
/// surprising for `isDirectory`: it always returns `false`. The constructor
/// accepts a boolean called `isDirectory` but discards it.
///
/// So, this extension type hides `isDirectory` and instead provides an
/// [EventType] enum with the seven types of event actually used.
extension type Event._(FileSystemEvent event) {
/// Converts [event] to an [Event].
///
/// Returns `null` and asserts `false` if [event] is unexpected on this
/// platform. So, it will cause tests to fail but real code can continue
/// ignoring the event.
static Event? checkAndConvert(FileSystemEvent event) {
var result = Event._(event);
if (Platform.isMacOS) {
if (result.type.isNeverReceivedOnMacOS) {
assert(false);
return null;
}
}
return result;
}
/// A create event for a file at [path].
static Event createFile(String path) =>
Event._(FileSystemCreateEvent(path, false));
/// A create event for a directory at [path].
static Event createDirectory(String path) =>
Event._(FileSystemCreateEvent(path, true));
/// A delete event for [path].
///
/// Delete events do not specify whether they are for files or directories.
static Event delete(String path) => Event._(FileSystemDeleteEvent(
path,
// `FileSystemDeleteEvent` just discards `isDirectory`.
false /* isDirectory */));
/// A modify event for the file at [path].
static Event modifyFile(String path) => Event._(FileSystemModifyEvent(
path,
false /* isDirectory */,
// Don't set `contentChanged`, even pass through from the OS, as
// `package:watcher` never reads it.
false /* contentChanged */));
/// See [FileSystemEvent.path].
String get path => event.path;
EventType get type {
switch (event.type) {
case FileSystemEvent.create:
return event.isDirectory
? EventType.createDirectory
: EventType.createFile;
case FileSystemEvent.delete:
return EventType.delete;
case FileSystemEvent.modify:
return event.isDirectory
? EventType.modifyDirectory
: EventType.modifyFile;
case FileSystemEvent.move:
return event.isDirectory ? EventType.moveDirectory : EventType.moveFile;
default:
throw StateError('Invalid event type ${event.type}.');
}
}
/// See [FileSystemMoveEvent.destination].
///
/// For other types of event, always `null`.
String? get destination => event.type == FileSystemEvent.move
? (event as FileSystemMoveEvent).destination
: null;
}
/// See [FileSystemEvent.type].
///
/// This additionally encodes [FileSystemEvent.isDirectory], which is specified
/// for all event types except deletes.
enum EventType {
delete,
createFile,
createDirectory,
modifyFile,
modifyDirectory,
moveFile,
moveDirectory;
bool get isNeverReceivedOnMacOS {
// See https://github.com/dart-lang/sdk/issues/14806.
if (this == moveFile || this == moveDirectory) {
return true;
}
if (this == modifyDirectory) return true;
return false;
}
}