char/sorcery
static-files based git repo viewer
git clone https://git.t4t.associates/char/sorcery
b45076d
main
12use std :: env ; 3use std :: fs ; 4use std :: path :: { Path , PathBuf } ; 56use sha2 :: { Digest , Sha256 } ; 78fn files ( root : & Path , dir : & Path , output : & mut Vec < PathBuf > ) { 9let mut entries = fs :: read_dir ( dir ) 10. unwrap_or_else ( | error | panic ! ( "reading {}: {error}" , dir . display ())) 11. map ( | entry | entry . unwrap () . path ()) 12. collect :: < Vec < _ >> () ; 13entries . sort () ; 14for path in entries { 15if path . is_dir () { 16files ( root , & path , output ) ; 17} else { 18output . push ( path . strip_prefix ( root ) . unwrap () . to_owned ()) ; 19} 20} 21} 2223fn hash ( bytes : & [ u8 ] , length : usize ) -> String { 24format ! ( "{:x}" , Sha256 :: digest ( bytes ))[ .. length ] . to_owned () 25} 2627fn main () { 28for path in [ 29"src/style.css" , 30"assets/fonts" , 31"web/dist" , 32".glisten/dist/manifest.json" , 33] { 34println ! ( "cargo::rerun-if-changed={path}" ) ; 35} 36let manifest = fs :: read ( ".glisten/dist/manifest.json" ) 37. expect ( "missing .glisten/dist/manifest.json; run deno task glisten first" ) ; 38let entries = fs :: read_dir ( "web/dist" ) 39. expect ( "missing web/dist; run deno task build" ) 40. map ( | entry | entry . expect ( "reading web/dist entry" )) 41. filter ( | entry | entry . file_type () . expect ( "reading web asset type" ) . is_file ()) 42. map ( | entry | { 43entry 44. file_name () 45. into_string () 46. expect ( "non-UTF-8 web asset" ) 47}) 48. collect :: < Vec < _ >> () ; 49let entry = | prefix : & str | { 50let matches = entries 51. iter () 52. filter ( | name | { 53name . starts_with ( prefix ) && name . ends_with ( ".js" ) && name . len () > prefix . len () + 3 54}) 55. collect :: < Vec < _ >> () ; 56assert ! ( 57matches . len () == 1 , 58"expected exactly one {prefix}*.js in web/dist; run deno task build" 59) ; 60matches [ 0 ] . as_str () 61} ; 62let main_js = entry ( "main-" ) ; 63let worker_js = entry ( "highlight-worker-" ) ; 6465let mut digest = Sha256 :: new () ; 66let root = Path :: new ( "assets/fonts" ) ; 67let mut paths = Vec :: new () ; 68files ( root , root , & mut paths ) ; 69paths . sort () ; 70let fonts = paths 71. iter () 72. map ( | path | { 73let source = path . to_str () . expect ( "non-UTF-8 font path" ) ; 74let bytes = fs :: read ( root . join ( path )) . expect ( "reading font" ) ; 75let content_hash = Sha256 :: digest ( & bytes ) ; 76digest . update (( source . len () as u64 ) . to_be_bytes ()) ; 77digest . update ( source . as_bytes ()) ; 78digest . update ( content_hash ) ; 79let staged = if path . extension () . is_some_and ( | ext | ext == "woff2" ) { 80let stem = path . file_stem () . unwrap () . to_str () . unwrap () ; 81let hash = format ! ( "{content_hash:x}" ) ; 82path . with_file_name ( format ! ( "{stem}-{}.woff2" , & hash [ .. 12 ])) 83. to_str () 84. unwrap () 85. to_owned () 86} else { 87source . to_owned () 88} ; 89( source . to_owned () , staged ) 90}) 91. collect :: < Vec < _ >> () ; 9293let css = fs :: read_to_string ( "src/style.css" ) . expect ( "reading stylesheet" ) ; 94let root = css 95. split_once ( ":root {" ) 96. expect ( "stylesheet has a :root block" ) 97. 1 98. split_once ( '}' ) 99. expect ( "stylesheet :root block is closed" ) 100. 0 ; 101let mut declarations = root 102. lines () 103. filter_map ( | line | { 104let trimmed = line . trim () ; 105if trimmed . is_empty () { 106Some ( "" ) 107} else if [ 108"color-scheme:" , 109"--bg-" , 110"--fg-" , 111"--accent-" , 112"--selection-col:" , 113"--hl-" , 114] 115. iter () 116. any ( | prefix | trimmed . starts_with ( prefix )) 117{ 118Some ( line ) 119} else { 120None 121} 122}) 123. collect :: < Vec < _ >> () ; 124declarations . dedup_by ( | a , b | a . is_empty () && b . is_empty ()) ; 125let declarations = declarations . join ( " \n " ) ; 126let declarations = declarations . trim_matches ( ' \n ' ) ; 127let theme = format ! ( "/* \n :root {{ \n {declarations} \n }} \n */ \n " ) ; 128let mut rewritten = String :: new () ; 129let mut rest = css . as_str () ; 130while let Some ( index ) = rest . find ( "/fonts/" ) { 131rewritten . push_str ( & rest [ .. index ]) ; 132rest = & rest [ index + "/fonts/" . len () .. ] ; 133let end = rest 134. find ( | c : char | matches ! ( c , ' \' ' | '"' | ')' | '(' ) || c . is_whitespace ()) 135. unwrap_or ( rest . len ()) ; 136let source = & rest [ .. end ] ; 137let staged = fonts 138. iter () 139. find ( | ( path , _ ) | path == source && path . ends_with ( ".woff2" )) 140. unwrap_or_else ( || panic ! ( "unknown stylesheet font: /fonts/{source}" )) ; 141rewritten . push_str ( "/fonts/" ) ; 142rewritten . push_str ( & staged . 1 ) ; 143rest = & rest [ end .. ] ; 144} 145rewritten . push_str ( rest ) ; 146let style = format ! ( "style-{}.css" , hash ( rewritten . as_bytes () , 12 )) ; 147for bytes in [ 148style . as_bytes () , 149main_js . as_bytes () , 150worker_js . as_bytes () , 151manifest . as_slice () , 152] { 153digest . update (( bytes . len () as u64 ) . to_be_bytes ()) ; 154digest . update ( bytes ) ; 155} 156let root = Path :: new ( "web/dist" ) ; 157let mut paths = Vec :: new () ; 158files ( root , root , & mut paths ) ; 159paths . sort () ; 160for path in paths { 161let name = path . to_str () . expect ( "non-UTF-8 web asset path" ) ; 162let bytes = fs :: read ( root . join ( & path )) . expect ( "reading web asset" ) ; 163digest . update (( name . len () as u64 ) . to_be_bytes ()) ; 164digest . update ( name . as_bytes ()) ; 165digest . update (( bytes . len () as u64 ) . to_be_bytes ()) ; 166digest . update ( bytes ) ; 167} 168let digest = format ! ( "{:x}" , digest . finalize ()) ; 169let generated = format ! ( 170"pub const MAIN_JS_FILE: &str = {main_js:?}; \n\ 171pub const WORKER_JS_FILE: &str = {worker_js:?}; \n\ 172pub const STYLE_CSS_FILE: &str = {style:?}; \n\ 173pub const STYLESHEET: &str = {rewritten:?}; \n\ 174pub const THEME_STYLESHEET: &str = {theme:?}; \n\ 175pub const FONT_FILES: &[(&str, &str)] = &{fonts:?}; \n\ 176pub const ASSET_DIGEST: &str = {:?}; \n " , 177& digest [ .. 16 ] 178) ; 179fs :: write ( 180Path :: new ( & env :: var_os ( "OUT_DIR" ) . unwrap ()) . join ( "assets.rs" ) , 181generated , 182) 183. expect ( "writing asset manifest" ) ; }