diff --git a/build-files.txt b/build-files.txt index 1944066..3a9ec14 100644 --- a/build-files.txt +++ b/build-files.txt @@ -58,6 +58,7 @@ source/dub/internal/sdlang/util.d source/dub/internal/tinyendian.d source/dub/internal/undead/xml.d +source/dub/internal/temp_files.d source/dub/internal/utils.d source/dub/internal/vibecompat/core/file.d source/dub/internal/vibecompat/data/json.d diff --git a/source/dub/internal/temp_files.d b/source/dub/internal/temp_files.d new file mode 100644 index 0000000..ddfaf34 --- /dev/null +++ b/source/dub/internal/temp_files.d @@ -0,0 +1,45 @@ +/** + Provides methods to generate temporary file names and folders and + automatically clean them up on program exit. + + Copyright: © 2012 Matthias Dondorff, © 2012-2023 Sönke Ludwig + License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. + Authors: Matthias Dondorff, Sönke Ludwig, Jan Jurzitza +*/ +module dub.internal.temp_files; + +import std.file; + +import dub.internal.vibecompat.core.file; + +NativePath getTempDir() +{ + return NativePath(std.file.tempDir()); +} + +NativePath getTempFile(string prefix, string extension = null) +{ + import std.uuid : randomUUID; + import std.array: replace; + + string fileName = prefix ~ "-" ~ randomUUID.toString() ~ extension; + + if (extension !is null && extension == ".d") + fileName = fileName.replace("-", "_"); + + auto path = getTempDir() ~ fileName; + temporary_files ~= path; + return path; +} + +private NativePath[] temporary_files; + +static ~this() +{ + foreach (path; temporary_files) + { + auto spath = path.toNativeString(); + if (spath.exists) + std.file.remove(spath); + } +} diff --git a/source/dub/internal/utils.d b/source/dub/internal/utils.d index 538f034..fe3b4e7 100644 --- a/source/dub/internal/utils.d +++ b/source/dub/internal/utils.d @@ -30,28 +30,7 @@ public import std.net.curl : HTTPStatusException; } - -private NativePath[] temporary_files; - -NativePath getTempDir() -{ - return NativePath(std.file.tempDir()); -} - -NativePath getTempFile(string prefix, string extension = null) -{ - import std.uuid : randomUUID; - import std.array: replace; - - string fileName = prefix ~ "-" ~ randomUUID.toString() ~ extension; - - if (extension !is null && extension == ".d") - fileName = fileName.replace("-", "_"); - - auto path = getTempDir() ~ fileName; - temporary_files ~= path; - return path; -} +public import dub.internal.temp_files; /** * Obtain a lock for a file at the given path. @@ -99,16 +78,6 @@ } } -static ~this() -{ - foreach (path; temporary_files) - { - auto spath = path.toNativeString(); - if (spath.exists) - std.file.remove(spath); - } -} - bool isWritableDir(NativePath p, bool create_if_missing = false) { import std.random;