Skip to content

Commit

Permalink
template: build.rs dont write file if no change made
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsella committed Dec 11, 2023
1 parent dd0c5e0 commit 9375c9b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 2023/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ members = [
"day9",
"day10",
"day11",
# "day12",
"day12",
# "day13",
# "day14",
# "day15",
Expand Down
19 changes: 19 additions & 0 deletions 2023/day12/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
input.txt
flamegraph.svg
perf.data*
### Rust
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

10 changes: 10 additions & 0 deletions 2023/day12/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "day12"
authors = ["mirsella <[email protected]>"]
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

28 changes: 28 additions & 0 deletions 2023/day12/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::fs::File;
use std::io::{self, Read};
use std::path::PathBuf;
use std::{env, fs};

fn replace_in_file(file_path: &PathBuf, old: &str, new: &str) -> io::Result<()> {
let mut contents = String::new();
File::open(file_path)?.read_to_string(&mut contents)?;
let new_contents = contents.replace(old, new);
if contents != new_contents {
println!("Updating {}", file_path.display());
fs::write(file_path, new_contents)?;
}
Ok(())
}

fn main() -> io::Result<()> {
let pkg_name = env::var("CARGO_PKG_NAME").unwrap();
replace_in_file(
&"../Cargo.toml".into(),
&format!("# \"{pkg_name}\""),
&format!("\"{pkg_name}\""),
)?;

replace_in_file(&"./Cargo.toml".into(), "\n[workspace]", "")?;

Ok(())
}
24 changes: 24 additions & 0 deletions 2023/day12/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
fn part1(input: &str) -> usize {
todo!()
}
fn part2(input: &str) -> usize {
todo!()
}
fn main() {
let input = include_str!("../input.txt");
println!("Part 1: {}", part1(input));
println!("Part 2: {}", part2(input));
}

#[cfg(test)]
mod tests {
const INPUT: &str = "";
#[test]
fn part1() {
assert_eq!(super::part1(INPUT), todo!());
}
#[test]
fn part2() {
assert_eq!(super::part2(INPUT), todo!());
}
}
5 changes: 4 additions & 1 deletion template/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ fn replace_in_file(file_path: &PathBuf, old: &str, new: &str) -> io::Result<()>
let mut contents = String::new();
File::open(file_path)?.read_to_string(&mut contents)?;
let new_contents = contents.replace(old, new);
fs::write(file_path, new_contents)?;
if contents != new_contents {
println!("Updating {}", file_path.display());
fs::write(file_path, new_contents)?;
}
Ok(())
}

Expand Down

0 comments on commit 9375c9b

Please sign in to comment.