From 2a2b8169e4269e2ca2c76af2f1f406b50fb378e3 Mon Sep 17 00:00:00 2001 From: matthijscox-asml Date: Fri, 10 Feb 2023 09:55:03 +0100 Subject: [PATCH] Refactor: using the do-block with cd --- src/write.jl | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/write.jl b/src/write.jl index ed0ac53..5dfe095 100644 --- a/src/write.jl +++ b/src/write.jl @@ -141,10 +141,8 @@ function Base.write( end end - origin = pwd() - try - mktempdir() do tmpdir - cd(tmpdir) + mktempdir() do tmpdir + cd(tmpdir) do cp(template_path, template_name) unzipped_dir = template_name if template_isfile @@ -152,22 +150,16 @@ function Base.write( unzipped_dir = first(splitext(template_name)) # remove .pptx end ppt_dir = joinpath(unzipped_dir, "ppt") - cd(ppt_dir) - write_relationships!(p) - write_presentation!(p) - write_slides!(p) - write_shapes!(p) - update_table_style!() - cd(tmpdir) + cd(ppt_dir) do + write_relationships!(p) + write_presentation!(p) + write_slides!(p) + write_shapes!(p) + update_table_style!() + end zip(unzipped_dir, filename) cp(filename, filepath) - # need to cd out of folder, else mktempdir cannot cleanup - cd(origin) end - catch e - rethrow(e) - finally - cd(origin) end if open_ppt try @@ -197,12 +189,13 @@ end function zip(folder::String, filename::String) zip_ext_filename = split(filename, ".")[begin] * ".zip" origin = pwd() - cd(folder) - for f in readdir(".") - run_silent_pipeline(`$(exe7z()) a $zip_ext_filename $f`) + cd(folder) do + for f in readdir(".") + run_silent_pipeline(`$(exe7z()) a $zip_ext_filename $f`) + end + mv(zip_ext_filename, joinpath(origin, filename)) end - mv(zip_ext_filename, joinpath(origin, filename)) - return cd(origin) + return nothing end # silent, unless we error