diff --git a/source/dub/generators/build.d b/source/dub/generators/build.d index 26a4388..c893fb0 100644 --- a/source/dub/generators/build.d +++ b/source/dub/generators/build.d @@ -321,7 +321,7 @@ const dbPathStr = dbPath.toNativeString(); Json db; if (exists(dbPathStr)) { - const text = stripUTF8Bom(cast(string)readFile(dbPath)); + const text = readText(dbPath); db = parseJsonString(text, dbPathStr); enforce(db.type == Json.Type.array, "Expected a JSON array in " ~ dbPathStr); } diff --git a/source/dub/internal/utils.d b/source/dub/internal/utils.d index 23e7221..e352afe 100644 --- a/source/dub/internal/utils.d +++ b/source/dub/internal/utils.d @@ -92,7 +92,7 @@ Json jsonFromFile(NativePath file, bool silent_fail = false) { if( silent_fail && !existsFile(file) ) return Json.emptyObject; - auto text = stripUTF8Bom(cast(string)readFile(file)); + auto text = readText(file); return parseJsonString(text, file.toNativeString()); } @@ -451,7 +451,7 @@ } } -string stripUTF8Bom(string str) +private string stripUTF8Bom(string str) { if( str.length >= 3 && str[0 .. 3] == [0xEF, 0xBB, 0xBF] ) return str[3 ..$]; diff --git a/source/dub/internal/vibecompat/core/file.d b/source/dub/internal/vibecompat/core/file.d index e3fc1c1..98fd1ce 100644 --- a/source/dub/internal/vibecompat/core/file.d +++ b/source/dub/internal/vibecompat/core/file.d @@ -34,6 +34,12 @@ return cast(ubyte[]) std.file.read(path.toNativeString()); } +/// Returns the content of a file as text +public string readText(NativePath path) +{ + return std.file.readText(path.toNativeString()); +} + /** Moves or renames a file. */ diff --git a/source/dub/recipe/io.d b/source/dub/recipe/io.d index 5b9b1e0..f12fdb5 100644 --- a/source/dub/recipe/io.d +++ b/source/dub/recipe/io.d @@ -46,9 +46,7 @@ PackageRecipe readPackageRecipe(NativePath filename, in PackageName parent = PackageName.init, StrictMode mode = StrictMode.Ignore) { - import dub.internal.utils : stripUTF8Bom; - - string text = stripUTF8Bom(cast(string)readFile(filename)); + string text = readText(filename); return parsePackageRecipe(text, filename.toNativeString(), parent, null, mode); } diff --git a/source/dub/test/base.d b/source/dub/test/base.d index c1c6a31..b9407c7 100644 --- a/source/dub/test/base.d +++ b/source/dub/test/base.d @@ -310,7 +310,7 @@ SelectionsFile selected; try { - const content = cast(string) this.fs.readFile(selverfile); + const content = this.fs.readText(selverfile); selected = parseConfigString!SelectionsFile( content, selverfile.toNativeString()); } @@ -380,7 +380,6 @@ Package parent = null, string version_ = null, StrictMode mode = StrictMode.Ignore) { - import dub.internal.utils : stripUTF8Bom; if (recipe.empty) recipe = this.findPackageFile(path); @@ -392,7 +391,7 @@ const PackageName parent_name = parent ? PackageName(parent.name) : PackageName.init; - string text = stripUTF8Bom(cast(string)this.fs.readFile(recipe)); + string text = this.fs.readText(recipe); auto content = parsePackageRecipe(text, recipe.toNativeString(), parent_name, null, mode); @@ -748,6 +747,18 @@ return entry.content.dup; } + /// Reads a file, returns the content as text + public string readText (NativePath path) + { + import std.utf : validate; + + auto entry = this.lookup(path); + enforce(entry.type == Type.File, "Trying to read a directory"); + // Ignore BOM: If it's needed for a test, add support for it. + validate(cast(const(char[])) entry.content); + return cast(string) entry.content.idup(); + } + /// Write to this file public void writeFile (NativePath path, const(char)[] data) {