File tree 2 files changed +24
-10
lines changed
2 files changed +24
-10
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,7 @@ tempdir = "0.3"
52
52
53
53
[build-dependencies ]
54
54
time = " 0.1"
55
+ git2 = " 0.6"
55
56
sass-rs = " 0.0.18"
56
57
57
58
[[bin ]]
Original file line number Diff line number Diff line change 1
1
2
2
extern crate time;
3
3
extern crate sass_rs;
4
+ extern crate git2;
4
5
5
6
use std:: env;
6
7
use std:: path:: Path ;
7
8
use std:: fs:: File ;
8
9
use std:: io:: Write ;
9
- use std:: process:: Command ;
10
+ use git2:: Repository ;
11
+
10
12
11
13
fn main ( ) {
12
- let git_hash = match Command :: new ( "git" )
13
- . args ( & [ "log" , "--pretty=format:%h" , "-n" , "1" ] )
14
- . output ( ) {
15
- Ok ( output) => String :: from_utf8_lossy ( & output. stdout ) . into_owned ( ) ,
16
- Err ( _) => "???????" . to_string ( ) ,
17
- } ;
14
+ write_git_version ( ) ;
15
+ compile_sass ( ) ;
16
+ }
17
+
18
+
19
+ fn write_git_version ( ) {
20
+ let git_hash = get_git_hash ( ) . unwrap_or ( "???????" . to_owned ( ) ) ;
18
21
let build_date = time:: strftime ( "%Y-%m-%d" , & time:: now_utc ( ) ) . unwrap ( ) ;
19
22
let dest_path = Path :: new ( & env:: var ( "OUT_DIR" ) . unwrap ( ) ) . join ( "git_version" ) ;
20
23
let mut file = File :: create ( & dest_path) . unwrap ( ) ;
21
24
write ! ( file, "({} {})" , git_hash, build_date) . unwrap ( ) ;
22
-
23
- // compile style.scss
24
- compile_sass ( ) ;
25
25
}
26
26
27
27
28
+ fn get_git_hash ( ) -> Option < String > {
29
+ let repo = match Repository :: open ( env:: current_dir ( ) . unwrap ( ) ) {
30
+ Ok ( repo) => repo,
31
+ Err ( _) => return None ,
32
+ } ;
33
+ let head = repo. head ( ) . unwrap ( ) ;
34
+ head. target ( ) . map ( |h| {
35
+ let mut h = format ! ( "{}" , h) ;
36
+ h. truncate ( 7 ) ;
37
+ h
38
+ } )
39
+ }
40
+
28
41
29
42
fn compile_sass ( ) {
30
43
use sass_rs:: sass_context:: SassFileContext ;
You can’t perform that action at this time.
0 commit comments