1
1
//! Checks that all Flunt files have messages in alphabetical order
2
2
3
3
use crate :: walk:: { filter_dirs, walk} ;
4
+ use std:: collections:: HashMap ;
4
5
use std:: { fs:: OpenOptions , io:: Write , path:: Path } ;
5
6
6
7
use regex:: Regex ;
@@ -13,11 +14,18 @@ fn filter_fluent(path: &Path) -> bool {
13
14
if let Some ( ext) = path. extension ( ) { ext. to_str ( ) != Some ( "ftl" ) } else { true }
14
15
}
15
16
16
- fn check_alphabetic ( filename : & str , fluent : & str , bad : & mut bool ) {
17
+ fn check_alphabetic (
18
+ filename : & str ,
19
+ fluent : & str ,
20
+ bad : & mut bool ,
21
+ msgs : & mut HashMap < String , String > ,
22
+ ) {
17
23
let mut matches = MESSAGE . captures_iter ( fluent) . peekable ( ) ;
18
24
while let Some ( m) = matches. next ( ) {
25
+ let name = m. get ( 1 ) . unwrap ( ) ;
26
+ msgs. insert ( name. as_str ( ) . to_owned ( ) , filename. to_owned ( ) ) ;
27
+
19
28
if let Some ( next) = matches. peek ( ) {
20
- let name = m. get ( 1 ) . unwrap ( ) ;
21
29
let next = next. get ( 1 ) . unwrap ( ) ;
22
30
if name. as_str ( ) > next. as_str ( ) {
23
31
tidy_error ! (
@@ -34,13 +42,15 @@ run `./x.py test tidy --bless` to sort the file correctly",
34
42
}
35
43
}
36
44
37
- fn sort_messages ( fluent : & str ) -> String {
45
+ fn sort_messages ( filename : & str , fluent : & str , msgs : & mut HashMap < String , String > ) -> String {
38
46
let mut chunks = vec ! [ ] ;
39
47
let mut cur = String :: new ( ) ;
40
48
for line in fluent. lines ( ) {
41
- if MESSAGE . is_match ( line) {
49
+ if let Some ( name) = MESSAGE . find ( line) {
50
+ msgs. insert ( name. as_str ( ) . to_owned ( ) , filename. to_owned ( ) ) ;
42
51
chunks. push ( std:: mem:: take ( & mut cur) ) ;
43
52
}
53
+
44
54
cur += line;
45
55
cur. push ( '\n' ) ;
46
56
}
@@ -53,20 +63,23 @@ fn sort_messages(fluent: &str) -> String {
53
63
}
54
64
55
65
pub fn check ( path : & Path , bless : bool , bad : & mut bool ) {
66
+ let mut msgs = HashMap :: new ( ) ;
56
67
walk (
57
68
path,
58
69
|path, is_dir| filter_dirs ( path) || ( !is_dir && filter_fluent ( path) ) ,
59
70
& mut |ent, contents| {
60
71
if bless {
61
- let sorted = sort_messages ( contents) ;
72
+ let sorted = sort_messages ( ent . path ( ) . to_str ( ) . unwrap ( ) , contents, & mut msgs ) ;
62
73
if sorted != contents {
63
74
let mut f =
64
75
OpenOptions :: new ( ) . write ( true ) . truncate ( true ) . open ( ent. path ( ) ) . unwrap ( ) ;
65
76
f. write ( sorted. as_bytes ( ) ) . unwrap ( ) ;
66
77
}
67
78
} else {
68
- check_alphabetic ( ent. path ( ) . to_str ( ) . unwrap ( ) , contents, bad) ;
79
+ check_alphabetic ( ent. path ( ) . to_str ( ) . unwrap ( ) , contents, bad, & mut msgs ) ;
69
80
}
70
81
} ,
71
82
) ;
83
+
84
+ crate :: fluent_used:: check ( path, & mut msgs, bad) ;
72
85
}
0 commit comments