Skip to content

Commit

Permalink
Reimplement choose_postgresql_version script in rust; also allow conf…
Browse files Browse the repository at this point in the history
…iguration via env var
  • Loading branch information
Marshall Pierce committed Nov 23, 2016
1 parent e82bca2 commit 33b4405
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
48 changes: 45 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,49 @@
use std::process::Command;
use std::env;
use std::fs;
use std::os::unix::fs::symlink;

fn main() {
let output = Command::new("util/choose_postgresql_version").output().unwrap_or_else(|e| {
panic!("Failed to execute process: {}", e)
});

// allow configuration via env var. Set to the version number itself, like "9.5.3".
let env_var = env::var("RPGFFI_PG_VERSION");
let pg_vers = if env_var.is_ok() {
env_var.unwrap()
} else {
let cmd_vers = Command::new("pg_config").arg("--version").output().unwrap_or_else(|e| {
panic!("Failed to execute pg_config: {}", e)
});

if !cmd_vers.status.success() {
panic!("pg_config failed with {}", cmd_vers.status.code().unwrap());
}

// transform "PostgreSQL 9.6.1" to "9.6.1"
String::from_utf8(cmd_vers.stdout).unwrap().chars()
.skip_while(|c| *c != ' ')
.skip(1)
.collect::<String>()
};

// transform "9.6.1" to "96"
let short_vers = pg_vers.chars()
.filter(|c| *c != '.')
.take(2)
.collect::<String>();

match fs::remove_file("src/lib.rs") {
Ok(_) => (),
Err(e) => panic!("couldn't remove lib.rs: {}", e)
}

let dest_filename = format!("pg{}.rs", short_vers);

if let Err(_) = fs::File::open(format!("src/{}", dest_filename)) {
panic!("Destination file src/{} does not exist", dest_filename);
}

if let Err(e) = symlink(dest_filename, "src/lib.rs") {
panic!("Symlink failed: {}", e);
}

}
7 changes: 0 additions & 7 deletions util/choose_postgresql_version

This file was deleted.

0 comments on commit 33b4405

Please sign in to comment.