1
+ use anyhow:: { Context as _, Error , Result } ;
1
2
use git2:: Repository ;
2
- use std:: { env, error:: Error , fs:: File , io:: Write , path:: Path } ;
3
-
4
- fn main ( ) {
5
- write_git_version ( ) ;
6
- if let Err ( sass_err) = compile_sass ( ) {
7
- panic ! ( "Error compiling sass: {}" , sass_err) ;
8
- }
9
- write_known_targets ( ) . unwrap ( ) ;
3
+ use std:: { env, fs:: File , io:: Write , path:: Path } ;
4
+
5
+ fn main ( ) -> Result < ( ) > {
6
+ let out_dir = env:: var ( "OUT_DIR" ) . context ( "missing OUT_DIR" ) ?;
7
+ let out_dir = Path :: new ( & out_dir) ;
8
+ write_git_version ( out_dir) ?;
9
+ compile_sass ( out_dir) ?;
10
+ write_known_targets ( out_dir) ?;
11
+ Ok ( ( ) )
10
12
}
11
13
12
- fn write_git_version ( ) {
13
- let maybe_hash = get_git_hash ( ) ;
14
+ fn write_git_version ( out_dir : & Path ) -> Result < ( ) > {
15
+ let maybe_hash = get_git_hash ( ) ? ;
14
16
let git_hash = maybe_hash. as_deref ( ) . unwrap_or ( "???????" ) ;
15
17
16
18
let build_date = time:: OffsetDateTime :: now_utc ( ) . date ( ) ;
17
- let dest_path = Path :: new ( & env :: var ( "OUT_DIR" ) . unwrap ( ) ) . join ( "git_version" ) ;
19
+ let dest_path = out_dir . join ( "git_version" ) ;
18
20
19
- let mut file = File :: create ( & dest_path) . unwrap ( ) ;
20
- write ! ( file, "({} {})" , git_hash, build_date) . unwrap ( ) ;
21
+ let mut file = File :: create ( & dest_path) ? ;
22
+ write ! ( file, "({} {})" , git_hash, build_date) ? ;
21
23
22
24
// TODO: are these right?
23
25
println ! ( "cargo:rerun-if-changed=.git/HEAD" ) ;
24
26
println ! ( "cargo:rerun-if-changed=.git/index" ) ;
27
+
28
+ Ok ( ( ) )
25
29
}
26
30
27
- fn get_git_hash ( ) -> Option < String > {
28
- let repo = Repository :: open ( env:: current_dir ( ) . unwrap ( ) ) . ok ( ) ?;
29
- let head = repo. head ( ) . unwrap ( ) ;
31
+ fn get_git_hash ( ) -> Result < Option < String > > {
32
+ match Repository :: open ( env:: current_dir ( ) ?) {
33
+ Ok ( repo) => {
34
+ let head = repo. head ( ) ?;
30
35
31
- head. target ( ) . map ( |h| {
32
- let mut h = format ! ( "{}" , h) ;
33
- h. truncate ( 7 ) ;
34
- h
35
- } )
36
+ Ok ( head. target ( ) . map ( |h| {
37
+ let mut h = format ! ( "{}" , h) ;
38
+ h. truncate ( 7 ) ;
39
+ h
40
+ } ) )
41
+ }
42
+ Err ( err) => {
43
+ eprintln ! ( "failed to get git repo: {err}" ) ;
44
+ Ok ( None )
45
+ }
46
+ }
36
47
}
37
48
38
49
fn compile_sass_file (
50
+ out_dir : & Path ,
39
51
name : & str ,
40
52
target : & str ,
41
53
include_paths : & [ String ] ,
42
- ) -> Result < ( ) , Box < dyn Error > > {
54
+ ) -> Result < ( ) > {
43
55
use sass_rs:: { Context , Options , OutputStyle } ;
44
56
45
57
const STYLE_DIR : & str = concat ! ( env!( "CARGO_MANIFEST_DIR" ) , "/templates/style" ) ;
@@ -56,31 +68,33 @@ fn compile_sass_file(
56
68
}
57
69
}
58
70
59
- let mut context = Context :: new_file ( format ! ( "{}/{}.scss" , STYLE_DIR , name) ) ?;
71
+ let mut context =
72
+ Context :: new_file ( format ! ( "{}/{}.scss" , STYLE_DIR , name) ) . map_err ( Error :: msg) ?;
60
73
context. set_options ( Options {
61
74
output_style : OutputStyle :: Compressed ,
62
75
include_paths,
63
76
..Default :: default ( )
64
77
} ) ;
65
78
66
- let css = context. compile ( ) ?;
67
- let dest_path = Path :: new ( & env :: var ( "OUT_DIR" ) ? ) . join ( format ! ( "{}.css" , target) ) ;
79
+ let css = context. compile ( ) . map_err ( Error :: msg ) ?;
80
+ let dest_path = out_dir . join ( format ! ( "{}.css" , target) ) ;
68
81
let mut file = File :: create ( & dest_path) ?;
69
82
file. write_all ( css. as_bytes ( ) ) ?;
70
83
71
84
Ok ( ( ) )
72
85
}
73
86
74
- fn compile_sass ( ) -> Result < ( ) , Box < dyn Error > > {
87
+ fn compile_sass ( out_dir : & Path ) -> Result < ( ) > {
75
88
// Compile base.scss -> style.css
76
- compile_sass_file ( "base" , "style" , & [ ] ) ?;
89
+ compile_sass_file ( out_dir , "base" , "style" , & [ ] ) ?;
77
90
78
91
// Compile rustdoc.scss -> rustdoc.css
79
- compile_sass_file ( "rustdoc" , "rustdoc" , & [ ] ) ?;
80
- compile_sass_file ( "rustdoc-2021-12-05" , "rustdoc-2021-12-05" , & [ ] ) ?;
92
+ compile_sass_file ( out_dir , "rustdoc" , "rustdoc" , & [ ] ) ?;
93
+ compile_sass_file ( out_dir , "rustdoc-2021-12-05" , "rustdoc-2021-12-05" , & [ ] ) ?;
81
94
82
95
// Compile vendored.scss -> vendored.css
83
96
compile_sass_file (
97
+ out_dir,
84
98
"vendored" ,
85
99
"vendored" ,
86
100
& [ concat ! ( env!( "CARGO_MANIFEST_DIR" ) , "/vendor/pure-css/css" ) . to_owned ( ) ] ,
@@ -89,7 +103,7 @@ fn compile_sass() -> Result<(), Box<dyn Error>> {
89
103
Ok ( ( ) )
90
104
}
91
105
92
- fn write_known_targets ( ) -> std :: io :: Result < ( ) > {
106
+ fn write_known_targets ( out_dir : & Path ) -> Result < ( ) > {
93
107
use std:: io:: BufRead ;
94
108
95
109
let targets: Vec < String > = std:: process:: Command :: new ( "rustc" )
@@ -102,7 +116,7 @@ fn write_known_targets() -> std::io::Result<()> {
102
116
103
117
string_cache_codegen:: AtomType :: new ( "target::TargetAtom" , "target_atom!" )
104
118
. atoms ( & targets)
105
- . write_to_file ( & Path :: new ( & env :: var ( "OUT_DIR" ) . unwrap ( ) ) . join ( "target_atom.rs" ) ) ?;
119
+ . write_to_file ( & out_dir . join ( "target_atom.rs" ) ) ?;
106
120
107
121
Ok ( ( ) )
108
122
}
0 commit comments