Skip to content

Commit cfa79aa

Browse files
committed
Compile style.scss with sass-rs
1 parent 596d75a commit cfa79aa

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

Cargo.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ tempdir = "0.3"
4141

4242
[build-dependencies]
4343
time = "0.1"
44+
sass-rs = "0.0.18"
4445

4546
[[bin]]
4647
name = "cratesfyi"

build.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11

22
extern crate time;
3+
extern crate sass_rs;
34

45
use std::env;
56
use std::path::Path;
67
use std::fs::File;
78
use std::io::Write;
89
use std::process::Command;
910

10-
1111
fn main() {
1212
let git_hash = match Command::new("git")
13-
.args(&["log", "--pretty=format:%h", "-n", "1"])
14-
.output() {
13+
.args(&["log", "--pretty=format:%h", "-n", "1"])
14+
.output() {
1515
Ok(output) => String::from_utf8_lossy(&output.stdout).into_owned(),
16-
Err(_) => "???????".to_string()
16+
Err(_) => "???????".to_string(),
1717
};
1818
let build_date = time::strftime("%Y-%m-%d", &time::now_utc()).unwrap();
1919
let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join("git_version");
2020
let mut file = File::create(&dest_path).unwrap();
2121
write!(file, "({} {})", git_hash, build_date).unwrap();
22+
23+
// compile style.scss
24+
compile_sass();
25+
}
26+
27+
28+
29+
fn compile_sass() {
30+
use sass_rs::sass_context::SassFileContext;
31+
32+
let mut file_context = SassFileContext::new(concat!(env!("CARGO_MANIFEST_DIR"),
33+
"/templates/style.scss"));
34+
let css = file_context.compile().unwrap();
35+
let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join("style.css");
36+
let mut file = File::create(&dest_path).unwrap();
37+
file.write_all(css.as_bytes()).unwrap();
2238
}

src/web/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::error::Error;
1616
use std::time::Duration;
1717
use std::path::PathBuf;
1818
use iron::prelude::*;
19-
use iron::Handler;
19+
use iron::{Handler, status};
2020
use router::{Router, NoRoute};
2121
use staticfile::Static;
2222
use handlebars_iron::{HandlebarsEngine, DirectorySource};
@@ -30,6 +30,7 @@ use std::collections::BTreeMap;
3030

3131
/// Duration of static files for staticfile and DatabaseFileHandler (in seconds)
3232
const STATIC_FILE_CACHE_DURATION: u64 = 60 * 60 * 24 * 30 * 12; // 12 months
33+
const STYLE_CSS: &'static str = include_str!(concat!(env!("OUT_DIR"), "/style.css"));
3334

3435

3536
struct CratesfyiHandler {
@@ -59,6 +60,7 @@ impl CratesfyiHandler {
5960
pub fn new() -> CratesfyiHandler {
6061
let mut router = Router::new();
6162
router.get("/", releases::home_page);
63+
router.get("/style.css", style_css_handler);
6264
router.get("/about", |_: &mut Request| {
6365
page::Page::new(false).title("About Docs.rs").to_resp("about")
6466
});
@@ -268,6 +270,17 @@ fn duration_to_str(ts: time::Timespec) -> String {
268270

269271

270272

273+
fn style_css_handler(_: &mut Request) -> IronResult<Response> {
274+
use iron::headers::{CacheControl, CacheDirective, ContentType};
275+
let mut response = Response::with((status::Ok, STYLE_CSS));
276+
let cache = vec![CacheDirective::Public,
277+
CacheDirective::MaxAge(STATIC_FILE_CACHE_DURATION as u32)];
278+
response.headers.set(ContentType("text/css".parse().unwrap()));
279+
response.headers.set(CacheControl(cache));
280+
Ok(response)
281+
}
282+
283+
271284
/// MetaData used in header
272285
#[derive(Debug)]
273286
pub struct MetaData {

0 commit comments

Comments
 (0)