Skip to content

Commit

Permalink
improves the build script with SOURCE_DIR env var, adds Dockerfile …
Browse files Browse the repository at this point in the history
…and example docker-compose.yml
  • Loading branch information
hry-gh committed Nov 12, 2024
1 parent b7a3abd commit 702be41
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions tools/od_ref_bot/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/content.rs
18 changes: 18 additions & 0 deletions tools/od_ref_bot/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# syntax=docker/dockerfile:labs

FROM rust:1.82 AS builder

WORKDIR /usr/src/app
COPY src/ src/
COPY build.rs build.rs
COPY Cargo.toml Cargo.toml

ADD https://github.com/OpenDreamProject/od-dm-reference.git#main /usr/ref

RUN SOURCE_DIR=/usr/ref/content cargo install --path .

FROM debian:bookworm-slim
# RUN apt-get update && apt-get install -y libssl-dev ca-certificates && rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/local/cargo/bin/od_ref_bot /usr/local/bin/od_ref_bot

CMD ["od_ref_bot"]
11 changes: 7 additions & 4 deletions tools/od_ref_bot/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,31 @@ const SOURCE_DIR: &str = "../../content";

fn main() -> Result<(), Box<dyn Error>> {
let dest_path = Path::new("./src/content.rs");
let from_path = std::env::var("SOURCE_DIR").unwrap_or(SOURCE_DIR.to_string());

let mut out_files = File::create(dest_path)?;

writeln!(
&mut out_files,
r##"use std::collections::HashMap;pub fn get_all() -> HashMap<&'static str, &'static str> {{ let mut out = HashMap::new();"##,
)?;

let _ = visit_dir(&mut out_files, SOURCE_DIR);
let _ = visit_dir(&mut out_files, from_path.as_str(), &from_path);

writeln!(&mut out_files, r##"out}}"##,)?;

Ok(())
}

fn visit_dir(file: &mut File, dir: &str) -> Result<(), Box<dyn Error>> {
fn visit_dir(file: &mut File, dir: &str, from_dir: &String) -> Result<(), Box<dyn Error>> {
for inner_file in fs::read_dir(dir)? {
let inner_file = inner_file?;

let file_type = inner_file.file_type()?;

if !file_type.is_file() {
if file_type.is_dir() {
let _ = visit_dir(file, inner_file.path().to_str().unwrap());
let _ = visit_dir(file, inner_file.path().to_str().unwrap(), from_dir);
}

continue;
Expand All @@ -42,7 +44,7 @@ fn visit_dir(file: &mut File, dir: &str) -> Result<(), Box<dyn Error>> {
r##"out.insert("{name}", include_str!(r#"{path}"#));"##,
name = inner_file
.path()
.strip_prefix(SOURCE_DIR)
.strip_prefix(from_dir)
.unwrap()
.to_string_lossy()
.replace("\\", "/"),
Expand All @@ -52,3 +54,4 @@ fn visit_dir(file: &mut File, dir: &str) -> Result<(), Box<dyn Error>> {

Ok(())
}

9 changes: 9 additions & 0 deletions tools/od_ref_bot/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
od_ref_bot:
image: od_ref_bot
build:
context: .
dockerfile: Dockerfile

environment:
GITHUB_TOKEN: "some_token"

0 comments on commit 702be41

Please sign in to comment.