15
15
import reactor .core .publisher .Flux ;
16
16
import reactor .core .publisher .Mono ;
17
17
import reactor .util .function .Tuple2 ;
18
+ import reactor .util .function .Tuples ;
18
19
19
20
import java .nio .file .Path ;
20
21
import java .util .Collection ;
@@ -49,17 +50,29 @@ public Mono<Void> run() {
49
50
return profilePropertiesParser .parseFromFile (profilePath )
50
51
.map (changeAdapter ::profileToConstants )
51
52
.flatMap (wantedConstants ->
52
- prepareChanges (filesPath , wantedConstants )
53
+ prepareChanges (wantedConstants )
53
54
.flatMap (changes -> printChanges (changes )
54
55
.then (printUnusedConstants (changes , wantedConstants ))
55
56
.then (doSave ? checkIfUserAgree ().then (applyAndSaveChanges (changes )) : Mono .empty ()))
56
57
);
57
58
}
58
59
60
+ public Mono <Map <Path , List <LineChange >>> prepareChanges (final Map <String , Constant > wantedConstants ) {
61
+ return fileHelper .listFiles (filesPath )
62
+ .flatMap (filePath -> fileHelper .lines (filePath )
63
+ .index ()
64
+ .concatMap (line -> lineChangeManager .prepareChange (line .getT2 (), line .getT1 ().intValue (), wantedConstants ))
65
+ .collectList ()
66
+ .filter (changes -> changes .stream ().anyMatch (LineChange ::isConstant ))
67
+ .map (changes -> Tuples .of (filePath , changes )))
68
+ .collectMap (Tuple2 ::getT1 , Tuple2 ::getT2 );
69
+ }
70
+
59
71
private Mono <Void > printChanges (final Map <Path , List <LineChange >> changes ) {
60
72
return Flux .fromIterable (changes .entrySet ())
61
73
.concatMap (fileChanges -> Flux .concat (
62
- Flux .just (String .format ("%s change(s) for file %s:" , fileChanges .getValue ().size (), fileChanges .getKey ())),
74
+ Flux .just (String .format ("%s change(s) to apply for file %s:" , fileChanges .getValue ().stream ().filter (this ::isModifyingChange ).count (), fileChanges
75
+ .getKey ())),
63
76
Flux .fromIterable (fileChanges .getValue ()).filter (LineChange ::isConstant ).map (lineChangeFormatter ::format ),
64
77
Flux .just ("" )
65
78
))
@@ -72,7 +85,7 @@ private Mono<Void> printUnusedConstants(final Map<Path, List<LineChange>> change
72
85
return lineChangeManager .getUnusedWantedConstants (changes .values ().stream ().flatMap (List ::stream ).collect (Collectors .toList ()), wantedConstants )
73
86
.collectList ()
74
87
.filter (Predicate .not (List ::isEmpty ))
75
- .doOnNext (unusedConstants -> consoleHelper .writeLine (String .format ("Still some unused constants: %s%n " , unusedConstants )))
88
+ .doOnNext (unusedConstants -> consoleHelper .writeLine (String .format ("Still some unused constants: %s" , unusedConstants )))
76
89
.then ();
77
90
}
78
91
@@ -87,23 +100,22 @@ private Mono<Void> checkIfUserAgree() {
87
100
.then ();
88
101
}
89
102
90
- public Mono <Map <Path , List <LineChange >>> prepareChanges (final List <Path > filesPath , final Map <String , Constant > wantedConstants ) {
91
- return fileHelper .listFiles (filesPath )
92
- .flatMap (filePath -> fileHelper .lines (filePath )
93
- .index ()
94
- .concatMap (line -> lineChangeManager .prepareChange (line .getT2 (), line .getT1 ().intValue (), wantedConstants ))
95
- .collectList ()
96
- .zipWith (Mono .just (filePath )))
97
- .collectMap (Tuple2 ::getT2 , Tuple2 ::getT1 );
98
- }
99
-
100
103
public Mono <Void > applyAndSaveChanges (final Map <Path , List <LineChange >> changes ) {
101
104
return Flux .fromIterable (changes .entrySet ())
105
+ .filter (e -> onlyChangedFile (e .getValue ()))
102
106
.groupBy (Map .Entry ::getKey , Map .Entry ::getValue )
103
107
.flatMap (fileChanges -> fileHelper .write (fileChanges .key (), true , fileChanges .flatMap (this ::applyChanges )))
104
108
.then ();
105
109
}
106
110
111
+ private boolean onlyChangedFile (final List <LineChange > changes ) {
112
+ return changes .stream ().anyMatch (this ::isModifyingChange );
113
+ }
114
+
115
+ private boolean isModifyingChange (final LineChange change ) {
116
+ return !LineChange .DiffEnum .DO_NOTHING .equals (change .getDiff ());
117
+ }
118
+
107
119
public Flux <String > applyChanges (final Collection <LineChange > changes ) {
108
120
return Flux .fromIterable (changes ).flatMap (lineChangeManager ::applyChange );
109
121
}
0 commit comments