Skip to content

Commit

Permalink
fix(format): Don't touch formatted files if they haven't changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Apr 14, 2018
1 parent 89c7e4a commit dabc96d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
38 changes: 22 additions & 16 deletions repl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern crate gluon_format;
#[macro_use]
extern crate gluon_vm;

use std::fs;
use std::io::{self, Write};
use std::ffi::OsStr;
use std::path::Path;
Expand Down Expand Up @@ -63,37 +64,41 @@ fn init_env_logger() {
#[cfg(not(feature = "env_logger"))]
fn init_env_logger() {}

fn format(writer: &mut Write, file: &str, buffer: &str) -> Result<usize> {
fn format(file: &str, buffer: &str) -> Result<String> {
use gluon_format::format_expr;

let mut compiler = Compiler::new();
let thread = new_vm();

let output = format_expr(&mut compiler, &thread, file, buffer)?;
writer.write_all(output.as_bytes())?;
Ok(output.len())
Ok(format_expr(&mut compiler, &thread, file, buffer)?)
}

fn fmt_file(name: &Path) -> Result<()> {
use std::io::{Read, Seek, SeekFrom};
use std::fs::{File, OpenOptions};
use std::io::Read;
use std::fs::File;

let mut input_file = OpenOptions::new().read(true).write(true).open(name)?;

let mut buffer = String::new();
input_file.read_to_string(&mut buffer)?;

{
let mut backup = File::create(name.with_extension("glu.bk"))?;
backup.write_all(buffer.as_bytes())?;
let mut input_file = File::open(name)?;
input_file.read_to_string(&mut buffer)?;
}

input_file.seek(SeekFrom::Start(0))?;

let module_name = filename_to_module(&name.display().to_string());
let written = format(&mut input_file, &module_name, &buffer)?;
// Truncate the file to remove any data that were there before
input_file.set_len(written as u64)?;
let formatted = format(&module_name, &buffer)?;

// Avoid touching the .glu file if it did not change
if buffer != formatted {
let bk_name = name.with_extension("glu.bk");
let tmp_name = name.with_extension("tmp");
{
let mut backup = File::create(&*bk_name)?;
backup.write_all(formatted.as_bytes())?;
}
fs::rename(name, tmp_name)?;
fs::rename(bk_name, name)?;
}
Ok(())
}

Expand All @@ -103,7 +108,8 @@ fn fmt_stdio() -> Result<()> {
let mut buffer = String::new();
stdin().read_to_string(&mut buffer)?;

format(&mut stdout(), "<stdin>", &buffer)?;
let formatted = format("<stdin>", &buffer)?;
stdout().write_all(formatted.as_bytes())?;
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions repl/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn fmt_repl() {

let status = Command::new("../target/debug/gluon")
.args(&["fmt", source])
.env("GLUON_PATH", "..")
.spawn()
.expect("Could not find gluon executable")
.wait()
Expand Down

0 comments on commit dabc96d

Please sign in to comment.