Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement configuration for automatic local bundle loading. #211

Merged
merged 3 commits into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ use std::io::{Read, Write};
use std::io::ErrorKind as IoErrorKind;
use std::fs::File;
use std::path::PathBuf;
use std::ffi::OsStr;

use app_dirs::{app_dir, app_root, get_app_root, sanitized, AppDataType};
use toml;

use errors::{ErrorKind, Result};
use io::Bundle;
use io::itarbundle::{HttpITarIoFactory, ITarBundle};
use io::local_cache::LocalCache;
use io::zipbundle::ZipBundle;
use io::Bundle;
use status::StatusBackend;


Expand All @@ -40,7 +42,6 @@ pub struct BundleInfo {
url: String,
}


impl PersistentConfig {
pub fn open(auto_create_config_file: bool) -> Result<PersistentConfig> {
let mut cfg_path = if auto_create_config_file {
Expand Down Expand Up @@ -74,28 +75,56 @@ impl PersistentConfig {
Ok(config)
}

pub fn make_cached_url_provider(&self, url: &str, only_cached: bool, status: &mut StatusBackend) -> Result<LocalCache<ITarBundle<HttpITarIoFactory>>> {
pub fn make_cached_url_provider(&self, url: &str, only_cached: bool, status: &mut StatusBackend) -> Result<Box<Bundle>> {
let itb = ITarBundle::<HttpITarIoFactory>::new(url);

let mut url2digest_path = app_dir(AppDataType::UserCache, &::APP_INFO, "urls")?;
url2digest_path.push(sanitized(url));

LocalCache::<ITarBundle<HttpITarIoFactory>>::new(
let bundle = LocalCache::<ITarBundle<HttpITarIoFactory>>::new(
itb,
&url2digest_path,
&app_dir(AppDataType::UserCache, &::APP_INFO, "manifests")?,
&app_dir(AppDataType::UserCache, &::APP_INFO, "files")?,
only_cached,
status
)
status,
)?;

Ok(Box::new(bundle) as _)
}

pub fn make_local_file_provider(
&self,
file_path: &OsStr,
_status: &mut StatusBackend,
) -> Result<Box<Bundle>> {
use std::path::Path;

let zip_bundle = ZipBundle::<File>::open(Path::new(file_path))?;

Ok(Box::new(zip_bundle) as _)
}

pub fn default_bundle(&self, only_cached: bool, status: &mut StatusBackend) -> Result<Box<Bundle>> {
use std::io;
use hyper::Url;

if self.default_bundles.len() != 1 {
return Err(ErrorKind::Msg("exactly one default_bundle item must be specified (for now)".to_owned()).into());
}

Ok(Box::new(self.make_cached_url_provider(&self.default_bundles[0].url, only_cached, status)?))
let url = Url::parse(&self.default_bundles[0].url)
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "failed to parse url"))?;
if url.scheme() == "file" {
// load the local zip file.
let file_path = url.to_file_path()
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "failed to parse local path"))?;
let zip_bundle = self.make_local_file_provider(file_path.as_os_str(), status)?;

return Ok(Box::new(zip_bundle) as _);
}
let bundle = self.make_cached_url_provider(&self.default_bundles[0].url, only_cached, status)?;
return Ok(Box::new(bundle) as _);
}

pub fn format_cache_path(&self) -> Result<PathBuf> {
Expand Down
3 changes: 3 additions & 0 deletions tests/tex-outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ fn md5_of_hello() { TestCase::new("md5_of_hello").check_pdf(true).go() }
#[test]
fn negative_roman_numeral() { TestCase::new("negative_roman_numeral").go() }

#[test]
fn tex_logo() { TestCase::new("tex_logo").go() }

#[test]
fn pdfoutput() { TestCase::new("pdfoutput").go() }

Expand Down
3 changes: 3 additions & 0 deletions tests/tex-outputs/tex_logo.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**
(tex_logo.tex [1] )
Output written on tex_logo.xdv (1 page, 228 bytes).
Binary file added tests/tex-outputs/tex_logo.pdf
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/tex-outputs/tex_logo.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
\def\makeatletter{\catcode `\@ 11\relax}%
\makeatletter%
\def\@{\spacefactor \@m {}}%
\def\TeXlogo{T\kern -.1667em\lower .5ex\hbox {E}\kern -.125em X\@}%
\TeXlogo%
\bye
Binary file added tests/tex-outputs/tex_logo.xdv
Binary file not shown.