Skip to content

Commit

Permalink
Overwrite the existing files when deploying
Browse files Browse the repository at this point in the history
  • Loading branch information
koute committed Jul 31, 2019
1 parent f80dbe3 commit 9e35d3b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
26 changes: 26 additions & 0 deletions integration-tests/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,29 @@ fn depends_on_cdylibs() {

assert_file_contains( js_path, "depends_on_cdylibs.wasm" );
}

#[test]
fn deploy_with_output_overwrites_files_but_does_not_delete_the_directory() {
let template_dir = crate_path( "static-files" );
let cwd = REPOSITORY_ROOT.join( "target" ).join( "tmp" ).join( "static-files-output-test" );

let _ = fs::remove_dir_all( &cwd );
fs::create_dir_all( &cwd ).unwrap();
fs::create_dir_all( cwd.join( "static" ) ).unwrap();
fs::create_dir_all( cwd.join( "src" ) ).unwrap();

fs::copy( template_dir.join( "Cargo.toml" ), cwd.join( "Cargo.toml" ) ).unwrap();
fs::copy( template_dir.join( "src" ).join( "main.rs" ), cwd.join( "src" ).join( "main.rs" ) ).unwrap();

fs::write( cwd.join( "static" ).join( "test1.txt" ), "A" ).unwrap();
fs::write( cwd.join( "static" ).join( "test2.txt" ), "B" ).unwrap();
run( &cwd, &*CARGO_WEB, &["deploy", "--target", "wasm32-unknown-unknown", "--output", "custom-output"] ).assert_success();

assert_file_contains( cwd.join( "custom-output" ).join( "test1.txt" ), "A" );

fs::write( cwd.join( "static" ).join( "test1.txt" ), "C" ).unwrap();
run( &cwd, &*CARGO_WEB, &["deploy", "--target", "wasm32-unknown-unknown", "--output", "custom-output"] ).assert_success();

assert_file_contains( cwd.join( "custom-output" ).join( "test1.txt" ), "C" );
assert_file_exists( cwd.join( "custom-output" ).join( "test2.txt" ) );
}
16 changes: 13 additions & 3 deletions src/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use cargo_shim::{
};

use error::Error;
use utils::read_bytes;
use utils::{get_sha1sum, read_bytes};

// Note: newlines before the DOCTYPE break GitHub pages
const DEFAULT_INDEX_HTML_TEMPLATE: &'static str = r#"<!DOCTYPE html>
Expand Down Expand Up @@ -54,6 +54,12 @@ fn generate_index_html( filename: &str ) -> String {
handlebars.render_template( DEFAULT_INDEX_HTML_TEMPLATE, &template_data ).unwrap()
}

fn are_the_same( a: &Path, b: &Path ) -> bool {
let a_sum = get_sha1sum( a ).ok();
let b_sum = get_sha1sum( b ).ok();
a_sum.is_some() && b_sum.is_some() && a_sum == b_sum
}

enum RouteKind {
Blob( Vec< u8 > ),
StaticDirectory( PathBuf )
Expand Down Expand Up @@ -240,7 +246,11 @@ impl Deployment {
}

if target_path.exists() {
continue;
if let Ok( existing_bytes ) = read_bytes( &target_path ) {
if *bytes == existing_bytes {
continue;
}
}
}

let target_dir = target_path.parent().unwrap();
Expand All @@ -265,7 +275,7 @@ impl Deployment {
let source_path = entry.path();
let relative_path = source_path.strip_prefix( source_dir ).unwrap();
let target_path = root_directory.join( relative_path );
if target_path.exists() {
if target_path.exists() && are_the_same( &source_path, &target_path ) {
continue;
}

Expand Down

0 comments on commit 9e35d3b

Please sign in to comment.