8
8
import java .nio .file .StandardCopyOption ;
9
9
import java .time .LocalDateTime ;
10
10
import java .util .ArrayList ;
11
+ import java .util .HashSet ;
11
12
import java .util .List ;
13
+ import java .util .Set ;
12
14
13
15
/**
14
16
* Created by bjliuyong on 2018/3/19.
15
17
*/
16
18
public class FileChangeListener extends Thread {
17
19
18
- private List <FileEntry > fileEntries = new ArrayList <>() ;
20
+ private Set <FileEntry > fileEntries = new HashSet <>() ;
19
21
private static String projectDir = System .getProperty ("user.dir" ) ;
20
22
private static Path sp , tp ;
21
23
@@ -69,11 +71,19 @@ public FileChangeListener autoConfig(String type) {
69
71
70
72
@ Override public void run () {
71
73
72
- for (int i = 0 ; i < fileEntries .size () ;i ++) {
73
- System .out .println ("listen file change" + fileEntries .get (i ));
74
- }
74
+ List <FileEntry > copies = new ArrayList <>();
75
+ copies .addAll (fileEntries ) ;
76
+ copies .forEach (fileEntry -> System .out .println ("listen file change" + fileEntry ) );
77
+
78
+ copies .forEach (o -> {
79
+ try {
80
+ if (o .type () == FileEntryType .DIR )
81
+ addFileEntries (o );
82
+ } catch (IOException e ) {
83
+ e .printStackTrace ();
84
+ }
85
+ });
75
86
76
- boolean deal = false ;
77
87
boolean isInterrupted = false ;
78
88
while (!isInterrupted ) {
79
89
try {
@@ -83,19 +93,13 @@ public FileChangeListener autoConfig(String type) {
83
93
e .printStackTrace ();
84
94
}
85
95
86
- for ( int i = 0 ; i < fileEntries .size () ; i ++) {
96
+ fileEntries .forEach ( f -> {
87
97
try {
88
- if (fileEntries .get (i ).type () == FileEntryType .DIR ) {
89
- if (deal )
90
- continue ;
91
- addFileEntries (fileEntries .get (i )) ;
92
- }
93
- onChange (fileEntries .get (i )) ;
98
+ onChange (f );
94
99
} catch (Exception e ) {
95
100
e .printStackTrace ();
96
101
}
97
- }
98
- deal = true ;
102
+ });
99
103
}
100
104
101
105
}
@@ -104,22 +108,50 @@ private void addFileEntries(FileEntry fileEntry) throws IOException {
104
108
if (!fileEntry .srcPath .toFile ().exists ())
105
109
return ;
106
110
Files .walk (fileEntry .srcPath , FileVisitOption .FOLLOW_LINKS ).forEach (path -> {
107
- if (path .toFile ().isFile () && path .toString ().endsWith (".html" )) {
111
+ /* if(path.toFile().isFile() && path.toString().endsWith(".html")) {
108
112
Path relativePath = fileEntry.srcPath.relativize(path) ;
109
113
listenAbsolute(path).toAbsolute(Paths.get(fileEntry.targetPath.toString() , relativePath.toString())) ;
110
- }
114
+ }*/
115
+
116
+ Path relativePath = fileEntry .srcPath .relativize (path ) ;
117
+ Path target = Paths .get (fileEntry .targetPath .toString () , relativePath .toString ()) ;
118
+ listenAbsolute (path )
119
+ .toAbsolute (target )
120
+ .type (path .toFile ().isFile () ? FileEntryType .FILE : FileEntryType .DIR ) ;
111
121
});
112
122
}
113
123
114
124
private void onChange (FileEntry fileEntry ) throws Exception {
115
125
if (!fileEntry .isChange ())
116
126
return ;
127
+ if (fileEntry .type () == FileEntryType .DIR ) {
128
+ Files .walk (fileEntry .srcPath , FileVisitOption .FOLLOW_LINKS ).forEach (path -> {
129
+ Path relativePath = fileEntry .srcPath .relativize (path ) ;
130
+ Path target = Paths .get (fileEntry .targetPath .toString () , relativePath .toString ()) ;
131
+ if (!target .toFile ().exists ()) {
132
+ try {
133
+ Files .copy (path , target , StandardCopyOption .REPLACE_EXISTING );
134
+ System .out .println (LocalDateTime .now ().toString () + "#####File Copy " + path );
135
+ } catch (IOException e ) {
136
+ e .printStackTrace ();
137
+ }
138
+ listenAbsolute (path )
139
+ .toAbsolute (target )
140
+ .type (path .toFile ().isFile () ? FileEntryType .FILE : FileEntryType .DIR ) ;
117
141
118
- Files .copy (fileEntry .srcPath , fileEntry .targetPath , StandardCopyOption .REPLACE_EXISTING );
119
- System .out .println (LocalDateTime .now ().toString () + "#####File Copy " + fileEntry );
120
- }
121
-
142
+ }
143
+ });
144
+ } else {
145
+ if (fileEntry .srcPath .toFile ().exists ()) {
146
+ Files .copy (fileEntry .srcPath , fileEntry .targetPath , StandardCopyOption .REPLACE_EXISTING );
147
+ System .out .println (LocalDateTime .now ().toString () + "#####File Copy " + fileEntry );
148
+ } else {
149
+ fileEntry .targetPath .toFile ().delete () ;
150
+ System .out .println (LocalDateTime .now ().toString () + "#####File Delete " + fileEntry .targetPath );
151
+ }
122
152
153
+ }
154
+ }
123
155
124
156
public static void main (String ...args ) {
125
157
FileChangeListener fileChangeListener = new FileChangeListener () ;
@@ -199,6 +231,21 @@ public FileEntryType type() {
199
231
sb .append ('}' );
200
232
return sb .toString ();
201
233
}
234
+
235
+ @ Override public boolean equals (Object o ) {
236
+ if (this == o )
237
+ return true ;
238
+ if (!(o instanceof FileEntry ))
239
+ return false ;
240
+
241
+ FileEntry entry = (FileEntry ) o ;
242
+
243
+ return srcPath .equals (entry .srcPath );
244
+ }
245
+
246
+ @ Override public int hashCode () {
247
+ return srcPath .hashCode ();
248
+ }
202
249
}
203
250
204
251
public enum FileEntryType {
0 commit comments