Skip to content

Commit

Permalink
fix: source/dest are now relative to config
Browse files Browse the repository at this point in the history
This is a step towards #284

BREAKING CHANGE: source/dest are now relative to your project root, as
defined by your config file.
  • Loading branch information
epage committed Oct 23, 2017
1 parent c1cf01c commit ce95b39
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
18 changes: 9 additions & 9 deletions src/bin/cobalt/build.rs
Expand Up @@ -22,24 +22,26 @@ pub fn build_command(config: cobalt::Config, matches: &clap::ArgMatches) -> Resu
}

pub fn build(config: &cobalt::Config) -> Result<()> {
info!("Building from {} into {}", config.source, config.dest);
info!("Building from {:?} into {:?}", config.source, config.dest);
cobalt::build(config)?;

Ok(())
}

pub fn clean_command(config: cobalt::Config, _matches: &clap::ArgMatches) -> Result<()> {
let cwd = env::current_dir().unwrap_or_else(|_| path::PathBuf::new());
let destdir = path::PathBuf::from(&config.dest);
let destdir = fs::canonicalize(destdir).unwrap_or_else(|_| path::PathBuf::new());
let destdir = config
.dest
.canonicalize()
.unwrap_or_else(|_| path::PathBuf::new());
if cwd == destdir {
bail!("Destination directory is same as current directory. \
Cancelling the operation");
}

fs::remove_dir_all(&config.dest)?;

info!("directory \"{}\" removed", &config.dest);
info!("directory \"{:?}\" removed", &config.dest);

Ok(())
}
Expand All @@ -53,12 +55,10 @@ pub fn import_command(config: cobalt::Config, matches: &clap::ArgMatches) -> Res
}

fn import(config: &cobalt::Config, branch: &str, message: &str) -> Result<()> {
info!("Importing {} to {}", config.dest, branch);
info!("Importing {:?} to {}", config.dest, branch);

let meta = fs::metadata(&config.dest)?;

if !meta.is_dir() {
bail!("`{}` is not a directory", config.dest);
if !config.dest.is_dir() {
bail!("`{:?}` is not a directory", config.dest);
}
ghp::import_dir(&config.dest, branch, message)?;

Expand Down
9 changes: 5 additions & 4 deletions src/bin/cobalt/main.rs
Expand Up @@ -74,7 +74,7 @@ mod new;
use std::env;

use clap::{Arg, App, SubCommand, AppSettings};
use cobalt::{Config, ConfigBuilder, Dump, jekyll};
use cobalt::{ConfigBuilder, Dump, jekyll};
use cobalt::{list_syntaxes, list_syntax_themes};
use env_logger::LogBuilder;
use log::{LogRecord, LogLevelFilter};
Expand Down Expand Up @@ -264,13 +264,12 @@ fn run() -> Result<()> {
.or_else(|| global_matches.value_of("config"));

// Fetch config information if available
let mut config: Config = if let Some(config_path) = config_path {
let mut config = if let Some(config_path) = config_path {
ConfigBuilder::from_file(config_path)
.chain_err(|| format!("Error reading config file {:?}", config_path))?
.build()?
} else {
let cwd = env::current_dir().expect("How does this fail?");
ConfigBuilder::from_cwd(cwd)?.build()?
ConfigBuilder::from_cwd(cwd)?
};

config.dest = matches
Expand All @@ -292,6 +291,8 @@ fn run() -> Result<()> {
info!("Setting: {:?}", config.dump);
}

let config = config.build()?;

match command {
"init" => new::init_command(config, matches),
"new" => new::new_command(config, matches),
Expand Down
4 changes: 2 additions & 2 deletions src/cobalt.rs
Expand Up @@ -68,8 +68,8 @@ fn deep_insert(data_map: &mut HashMap<String, Value>,
pub fn build(config: &Config) -> Result<()> {
trace!("Build configuration: {:?}", config);

let source = Path::new(&config.source);
let dest = Path::new(&config.dest);
let source = config.source.as_path();
let dest = config.dest.as_path();

let template_extensions: Vec<&OsStr> =
config.template_extensions.iter().map(OsStr::new).collect();
Expand Down
10 changes: 4 additions & 6 deletions src/config.rs
Expand Up @@ -238,9 +238,8 @@ impl ConfigBuilder {
result?;

let config = Config {
root,
source,
dest,
source: root.join(source),
dest: root.join(dest),
layouts,
drafts,
data,
Expand Down Expand Up @@ -269,9 +268,8 @@ impl ConfigBuilder {
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
pub root: path::PathBuf,
pub source: String,
pub dest: String,
pub source: path::PathBuf,
pub dest: path::PathBuf,
pub layouts: &'static str,
pub drafts: String,
pub data: &'static str,
Expand Down
10 changes: 5 additions & 5 deletions tests/mod.rs
Expand Up @@ -76,25 +76,25 @@ fn assert_dirs_eq(expected: &Path, actual: &Path) {
fn run_test(name: &str) -> Result<(), cobalt::Error> {
let target = format!("tests/target/{}/", name);
let target: PathBuf = target.into();
let mut config = ConfigBuilder::from_file(format!("tests/fixtures/{}/.cobalt.yml", name))
.unwrap_or_default()
.build()?;
let mut config = ConfigBuilder::from_cwd(format!("tests/fixtures/{}", name))?;
let destdir = TempDir::new(name).expect("Tempdir not created");

config.source = format!("tests/fixtures/{}/", name);
config.source = "./".to_owned();
config.dest = destdir
.path()
.to_str()
.expect("Can't convert destdir to str")
.to_owned();

let config = config.build()?;

// try to create the target directory, ignore errors
fs::create_dir_all(&config.dest).is_ok();

let result = cobalt::build(&config);

if result.is_ok() {
assert_dirs_eq(Path::new(config.dest.as_str()), &target);
assert_dirs_eq(&config.dest, &target);
}

// clean up
Expand Down

0 comments on commit ce95b39

Please sign in to comment.