Skip to content

Commit 9e35d3b

Browse files
committed
Overwrite the existing files when deploying
1 parent f80dbe3 commit 9e35d3b

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

integration-tests/src/tests.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,29 @@ fn depends_on_cdylibs() {
556556

557557
assert_file_contains( js_path, "depends_on_cdylibs.wasm" );
558558
}
559+
560+
#[test]
561+
fn deploy_with_output_overwrites_files_but_does_not_delete_the_directory() {
562+
let template_dir = crate_path( "static-files" );
563+
let cwd = REPOSITORY_ROOT.join( "target" ).join( "tmp" ).join( "static-files-output-test" );
564+
565+
let _ = fs::remove_dir_all( &cwd );
566+
fs::create_dir_all( &cwd ).unwrap();
567+
fs::create_dir_all( cwd.join( "static" ) ).unwrap();
568+
fs::create_dir_all( cwd.join( "src" ) ).unwrap();
569+
570+
fs::copy( template_dir.join( "Cargo.toml" ), cwd.join( "Cargo.toml" ) ).unwrap();
571+
fs::copy( template_dir.join( "src" ).join( "main.rs" ), cwd.join( "src" ).join( "main.rs" ) ).unwrap();
572+
573+
fs::write( cwd.join( "static" ).join( "test1.txt" ), "A" ).unwrap();
574+
fs::write( cwd.join( "static" ).join( "test2.txt" ), "B" ).unwrap();
575+
run( &cwd, &*CARGO_WEB, &["deploy", "--target", "wasm32-unknown-unknown", "--output", "custom-output"] ).assert_success();
576+
577+
assert_file_contains( cwd.join( "custom-output" ).join( "test1.txt" ), "A" );
578+
579+
fs::write( cwd.join( "static" ).join( "test1.txt" ), "C" ).unwrap();
580+
run( &cwd, &*CARGO_WEB, &["deploy", "--target", "wasm32-unknown-unknown", "--output", "custom-output"] ).assert_success();
581+
582+
assert_file_contains( cwd.join( "custom-output" ).join( "test1.txt" ), "C" );
583+
assert_file_exists( cwd.join( "custom-output" ).join( "test2.txt" ) );
584+
}

src/deployment.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use cargo_shim::{
1515
};
1616

1717
use error::Error;
18-
use utils::read_bytes;
18+
use utils::{get_sha1sum, read_bytes};
1919

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

57+
fn are_the_same( a: &Path, b: &Path ) -> bool {
58+
let a_sum = get_sha1sum( a ).ok();
59+
let b_sum = get_sha1sum( b ).ok();
60+
a_sum.is_some() && b_sum.is_some() && a_sum == b_sum
61+
}
62+
5763
enum RouteKind {
5864
Blob( Vec< u8 > ),
5965
StaticDirectory( PathBuf )
@@ -240,7 +246,11 @@ impl Deployment {
240246
}
241247

242248
if target_path.exists() {
243-
continue;
249+
if let Ok( existing_bytes ) = read_bytes( &target_path ) {
250+
if *bytes == existing_bytes {
251+
continue;
252+
}
253+
}
244254
}
245255

246256
let target_dir = target_path.parent().unwrap();
@@ -265,7 +275,7 @@ impl Deployment {
265275
let source_path = entry.path();
266276
let relative_path = source_path.strip_prefix( source_dir ).unwrap();
267277
let target_path = root_directory.join( relative_path );
268-
if target_path.exists() {
278+
if target_path.exists() && are_the_same( &source_path, &target_path ) {
269279
continue;
270280
}
271281

0 commit comments

Comments
 (0)