diff --git a/build-files.txt b/build-files.txt index 2ef281a..d8f0be5 100644 --- a/build-files.txt +++ b/build-files.txt @@ -6,7 +6,6 @@ source/dub/package_.d source/dub/platform.d source/dub/project.d -source/dub/utils.d source/dub/compilers/compiler.d source/dub/compilers/dmd.d source/dub/compilers/gdc.d @@ -18,6 +17,7 @@ source/dub/generators/visuald.d source/dub/internal/std/process.d source/dub/internal/std/processcompat.d +source/dub/internal/utils.d source/dub/internal/vibecompat/core/file.d source/dub/internal/vibecompat/core/log.d source/dub/internal/vibecompat/data/json.d diff --git a/source/dub/compilers/dmd.d b/source/dub/compilers/dmd.d index 5b25e8e..2ac08ab 100644 --- a/source/dub/compilers/dmd.d +++ b/source/dub/compilers/dmd.d @@ -1,7 +1,7 @@ /** DMD compiler support. - Copyright: © 2013 rejectedsoftware e.K. + Copyright: © 2013-2013 rejectedsoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ @@ -9,10 +9,10 @@ import dub.compilers.compiler; import dub.internal.std.process; +import dub.internal.utils; import dub.internal.vibecompat.core.log; import dub.internal.vibecompat.inet.path; import dub.platform; -import dub.utils; import std.algorithm; import std.array; diff --git a/source/dub/compilers/gdc.d b/source/dub/compilers/gdc.d index 45221ed..59f55dc 100644 --- a/source/dub/compilers/gdc.d +++ b/source/dub/compilers/gdc.d @@ -1,7 +1,7 @@ /** GDC compiler support. - Copyright: © 2013 rejectedsoftware e.K. + Copyright: © 2013-2013 rejectedsoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ @@ -9,10 +9,10 @@ import dub.compilers.compiler; import dub.internal.std.process; +import dub.internal.utils; import dub.internal.vibecompat.core.log; import dub.internal.vibecompat.inet.path; import dub.platform; -import dub.utils; import std.algorithm; import std.array; diff --git a/source/dub/compilers/ldc.d b/source/dub/compilers/ldc.d index 61d6749..91ef466 100644 --- a/source/dub/compilers/ldc.d +++ b/source/dub/compilers/ldc.d @@ -1,7 +1,7 @@ /** LDC compiler support. - Copyright: © 2013 rejectedsoftware e.K. + Copyright: © 2013-2013 rejectedsoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ @@ -9,10 +9,10 @@ import dub.compilers.compiler; import dub.internal.std.process; +import dub.internal.utils; import dub.internal.vibecompat.core.log; import dub.internal.vibecompat.inet.path; import dub.platform; -import dub.utils; import std.algorithm; import std.array; diff --git a/source/dub/dependency.d b/source/dub/dependency.d index 3fbf473..b17280c 100644 --- a/source/dub/dependency.d +++ b/source/dub/dependency.d @@ -1,18 +1,18 @@ /** Stuff with dependencies. - Copyright: © 2012 Matthias Dondorff + Copyright: © 2012-2013 Matthias Dondorff License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Matthias Dondorff, Sönke Ludwig */ module dub.dependency; +import dub.internal.utils; import dub.internal.vibecompat.core.log; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.data.json; import dub.internal.vibecompat.inet.url; import dub.package_; -import dub.utils; import std.algorithm; import std.array; diff --git a/source/dub/dub.d b/source/dub/dub.d index 31749dd..ac08d7f 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -1,7 +1,7 @@ /** A package manager. - Copyright: © 2012 Matthias Dondorff + Copyright: © 2012-2013 Matthias Dondorff License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Matthias Dondorff, Sönke Ludwig */ @@ -10,11 +10,11 @@ import dub.compilers.compiler; import dub.dependency; import dub.internal.std.process; +import dub.internal.utils; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.core.log; import dub.internal.vibecompat.data.json; import dub.internal.vibecompat.inet.url; -import dub.utils; import dub.package_; import dub.packagemanager; import dub.packagesupplier; diff --git a/source/dub/generators/build.d b/source/dub/generators/build.d index 5a47dbb..13b9e43 100644 --- a/source/dub/generators/build.d +++ b/source/dub/generators/build.d @@ -1,7 +1,7 @@ /** Generator for direct compiler builds. - Copyright: © 2013 rejectedsoftware e.K. + Copyright: © 2013-2013 rejectedsoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ @@ -10,13 +10,13 @@ import dub.compilers.compiler; import dub.generators.generator; import dub.internal.std.process; +import dub.internal.utils; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.core.log; import dub.internal.vibecompat.inet.path; import dub.package_; import dub.packagemanager; import dub.project; -import dub.utils; import std.algorithm; import std.array; diff --git a/source/dub/generators/generator.d b/source/dub/generators/generator.d index ded08eb..02673e2 100644 --- a/source/dub/generators/generator.d +++ b/source/dub/generators/generator.d @@ -1,7 +1,7 @@ /** Generator for project files - Copyright: © 2012 Matthias Dondorff + Copyright: © 2012-2013 Matthias Dondorff License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Matthias Dondorff */ @@ -119,7 +119,7 @@ void runBuildCommands(string[] commands, in BuildSettings build_settings) { import dub.internal.std.process; - import dub.utils; + import dub.internal.utils; string[string] env = environment.toAA(); // TODO: do more elaborate things here diff --git a/source/dub/generators/monod.d b/source/dub/generators/monod.d index 1fa89a0..7d9aa8d 100644 --- a/source/dub/generators/monod.d +++ b/source/dub/generators/monod.d @@ -1,7 +1,7 @@ /** Generator for MonoD project files - Copyright: © 2013 rejectedsoftware e.K. + Copyright: © 2013-2013 rejectedsoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ @@ -9,12 +9,12 @@ import dub.compilers.compiler; import dub.generators.generator; +import dub.internal.utils; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.core.log; import dub.package_; import dub.packagemanager; import dub.project; -import dub.utils; import std.algorithm; import std.array; diff --git a/source/dub/generators/rdmd.d b/source/dub/generators/rdmd.d index 01602c6..47dcf3d 100644 --- a/source/dub/generators/rdmd.d +++ b/source/dub/generators/rdmd.d @@ -1,7 +1,7 @@ /** Generator for direct RDMD builds. - Copyright: © 2013 rejectedsoftware e.K. + Copyright: © 2013-2013 rejectedsoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ @@ -10,13 +10,13 @@ import dub.compilers.compiler; import dub.generators.generator; import dub.internal.std.process; +import dub.internal.utils; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.core.log; import dub.internal.vibecompat.inet.path; import dub.package_; import dub.packagemanager; import dub.project; -import dub.utils; import std.algorithm; import std.array; @@ -61,13 +61,7 @@ if( settings.run ){ import std.random; auto rnd = to!string(uniform(uint.min, uint.max)) ~ "-"; - auto tmp = environment.get("TEMP"); - if( !tmp.length ) tmp = environment.get("TMP"); - if( !tmp.length ){ - version(Posix) tmp = "/tmp"; - else tmp = "."; - } - buildsettings.targetPath = (Path(tmp)~".rdmd/source/").toNativeString(); + buildsettings.targetPath = (getTempDir()~".rdmd/source/").toNativeString(); buildsettings.targetName = rnd ~ buildsettings.targetName; run_exe_file = Path(buildsettings.targetPath) ~ getTargetFileName(buildsettings, settings.platform); } diff --git a/source/dub/generators/visuald.d b/source/dub/generators/visuald.d index 245f981..c2ec4f6 100644 --- a/source/dub/generators/visuald.d +++ b/source/dub/generators/visuald.d @@ -1,7 +1,7 @@ /** Generator for VisualD project files - Copyright: © 2012 Matthias Dondorff + Copyright: © 2012-2013 Matthias Dondorff License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Matthias Dondorff */ @@ -9,12 +9,12 @@ import dub.compilers.compiler; import dub.generators.generator; +import dub.internal.utils; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.core.log; import dub.package_; import dub.packagemanager; import dub.project; -import dub.utils; import std.algorithm; import std.array; diff --git a/source/dub/internal/utils.d b/source/dub/internal/utils.d new file mode 100644 index 0000000..28281c4 --- /dev/null +++ b/source/dub/internal/utils.d @@ -0,0 +1,145 @@ +/** + ... + + Copyright: © 2012 Matthias Dondorff + License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. + Authors: Matthias Dondorff +*/ +module dub.internal.utils; + +import dub.internal.std.process; +import dub.internal.vibecompat.core.file; +import dub.internal.vibecompat.core.log; +import dub.internal.vibecompat.data.json; +import dub.internal.vibecompat.inet.url; +import dub.version_; + +// todo: cleanup imports. +import std.algorithm : startsWith; +import std.array; +import std.conv; +import std.exception; +import std.file; +import std.net.curl; +import std.string; +import std.typecons; +import std.zip; + + +Path getTempDir() +{ + auto tmp = environment.get("TEMP"); + if( !tmp.length ) tmp = environment.get("TMP"); + if( !tmp.length ){ + version(Posix) tmp = "/tmp/"; + else tmp = "./"; + } + return Path(tmp); +} + +bool isEmptyDir(Path p) { + foreach(DirEntry e; dirEntries(p.toNativeString(), SpanMode.shallow)) + return false; + return true; +} + +Json jsonFromFile(Path file, bool silent_fail = false) { + if( silent_fail && !existsFile(file) ) return Json.EmptyObject; + auto f = openFile(file.toNativeString(), FileMode.Read); + scope(exit) f.close(); + auto text = stripUTF8Bom(cast(string)f.readAll()); + return parseJson(text); +} + +Json jsonFromZip(Path zip, string filename) { + auto f = openFile(zip, FileMode.Read); + ubyte[] b = new ubyte[cast(size_t)f.size]; + f.rawRead(b); + f.close(); + auto archive = new ZipArchive(b); + auto text = stripUTF8Bom(cast(string)archive.expand(archive.directory[filename])); + return parseJson(text); +} + +void writeJsonFile(Path path, Json json) +{ + auto f = openFile(path, FileMode.CreateTrunc); + scope(exit) f.close(); + f.writePrettyJsonString(json); +} + +bool isPathFromZip(string p) { + enforce(p.length > 0); + return p[$-1] == '/'; +} + +bool existsDirectory(Path path) { + if( !existsFile(path) ) return false; + auto fi = getFileInfo(path); + return fi.isDirectory; +} + +void runCommands(string[] commands, string[string] env = null) +{ + foreach(cmd; commands){ + logDiagnostic("Running %s", cmd); + Pid pid; + if( env !is null ) pid = spawnShell(cmd, env); + else pid = spawnShell(cmd); + auto exitcode = pid.wait(); + enforce(exitcode == 0, "Command failed with exit code "~to!string(exitcode)); + } +} + +/** + Downloads a file from the specified URL. + + Any redirects will be followed until the actual file resource is reached or if the redirection + limit of 10 is reached. Note that only HTTP(S) is currently supported. +*/ +void download(string url, string filename) +{ + auto conn = setupHTTPClient(); + logDebug("Storing %s...", url); + std.net.curl.download(url, filename, conn); +} +/// ditto +void download(Url url, Path filename) +{ + download(url.toString(), filename.toNativeString()); +} +/// ditto +char[] download(string url) +{ + auto conn = setupHTTPClient(); + logDebug("Getting %s...", url); + return get(url, conn); +} +/// ditto +char[] download(Url url) +{ + return download(url.toString()); +} + +private HTTP setupHTTPClient() +{ + auto conn = HTTP(); + static if( is(typeof(&conn.verifyPeer)) ) + conn.verifyPeer = false; + + // convert version string to valid SemVer format + auto verstr = dubVersion; + if (verstr.startsWith("v")) verstr = verstr[1 .. $]; + auto idx = verstr.indexOf("-"); + if (idx >= 0) verstr = verstr[0 .. idx] ~ "+" ~ verstr[idx+1 .. $].split("-").join("."); + + conn.addRequestHeader("User-Agent", "dub/"~verstr~" (std.net.curl; +https://github.com/rejectedsoftware/dub)"); + return conn; +} + +private string stripUTF8Bom(string str) +{ + if( str.length >= 3 && str[0 .. 3] == [0xEF, 0xBB, 0xBF] ) + return str[3 ..$]; + return str; +} diff --git a/source/dub/package_.d b/source/dub/package_.d index 39cc81d..34315e9 100644 --- a/source/dub/package_.d +++ b/source/dub/package_.d @@ -1,7 +1,7 @@ /** Stuff with dependencies. - Copyright: © 2012 Matthias Dondorff + Copyright: © 2012-2013 Matthias Dondorff License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Matthias Dondorff */ @@ -9,11 +9,11 @@ import dub.compilers.compiler; import dub.dependency; +import dub.internal.utils; import dub.internal.vibecompat.core.log; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.data.json; import dub.internal.vibecompat.inet.url; -import dub.utils; import std.algorithm; import std.array; diff --git a/source/dub/packagemanager.d b/source/dub/packagemanager.d index 9fccb1a..de829b2 100644 --- a/source/dub/packagemanager.d +++ b/source/dub/packagemanager.d @@ -1,19 +1,19 @@ /** Management of packages on the local computer. - Copyright: © 2012 rejectedsoftware e.K. + Copyright: © 2012-2013 rejectedsoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig, Matthias Dondorff */ module dub.packagemanager; import dub.dependency; +import dub.internal.utils; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.core.log; import dub.internal.vibecompat.data.json; import dub.internal.vibecompat.inet.path; import dub.package_; -import dub.utils; import std.algorithm : countUntil, filter, sort, canFind; import std.array; diff --git a/source/dub/packagesupplier.d b/source/dub/packagesupplier.d index 604d53f..53e593c 100644 --- a/source/dub/packagesupplier.d +++ b/source/dub/packagesupplier.d @@ -1,18 +1,18 @@ /** A package supplier, able to get some packages to the local FS. - Copyright: © 2012 Matthias Dondorff + Copyright: © 2012-2013 Matthias Dondorff License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Matthias Dondorff */ module dub.packagesupplier; import dub.dependency; +import dub.internal.utils; import dub.internal.vibecompat.core.log; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.data.json; import dub.internal.vibecompat.inet.url; -import dub.utils; import std.file; import std.exception; diff --git a/source/dub/project.d b/source/dub/project.d index 4ad11ef..908c5c9 100644 --- a/source/dub/project.d +++ b/source/dub/project.d @@ -1,7 +1,7 @@ /** Representing a full project, with a root Package and several dependencies. - Copyright: © 2012 Matthias Dondorff + Copyright: © 2012-2013 Matthias Dondorff License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Matthias Dondorff, Sönke Ludwig */ @@ -9,12 +9,12 @@ import dub.compilers.compiler; import dub.dependency; +import dub.internal.utils; import dub.internal.std.process; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.core.log; import dub.internal.vibecompat.data.json; import dub.internal.vibecompat.inet.url; -import dub.utils; import dub.package_; import dub.packagemanager; import dub.packagesupplier; diff --git a/source/dub/utils.d b/source/dub/utils.d deleted file mode 100644 index 11e983c..0000000 --- a/source/dub/utils.d +++ /dev/null @@ -1,145 +0,0 @@ -/** - ... - - Copyright: © 2012 Matthias Dondorff - License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. - Authors: Matthias Dondorff -*/ -module dub.utils; - -import dub.internal.std.process; -import dub.internal.vibecompat.core.file; -import dub.internal.vibecompat.core.log; -import dub.internal.vibecompat.data.json; -import dub.internal.vibecompat.inet.url; -import dub.version_; - -// todo: cleanup imports. -import std.algorithm : startsWith; -import std.array; -import std.conv; -import std.exception; -import std.file; -import std.net.curl; -import std.string; -import std.typecons; -import std.zip; - - -Path getTempDir() -{ - auto tmp = environment.get("TEMP"); - if( !tmp.length ) tmp = environment.get("TMP"); - if( !tmp.length ){ - version(Posix) tmp = "/tmp/"; - else tmp = "./"; - } - return Path(tmp); -} - -package bool isEmptyDir(Path p) { - foreach(DirEntry e; dirEntries(p.toNativeString(), SpanMode.shallow)) - return false; - return true; -} - -package Json jsonFromFile(Path file, bool silent_fail = false) { - if( silent_fail && !existsFile(file) ) return Json.EmptyObject; - auto f = openFile(file.toNativeString(), FileMode.Read); - scope(exit) f.close(); - auto text = stripUTF8Bom(cast(string)f.readAll()); - return parseJson(text); -} - -package Json jsonFromZip(Path zip, string filename) { - auto f = openFile(zip, FileMode.Read); - ubyte[] b = new ubyte[cast(size_t)f.size]; - f.rawRead(b); - f.close(); - auto archive = new ZipArchive(b); - auto text = stripUTF8Bom(cast(string)archive.expand(archive.directory[filename])); - return parseJson(text); -} - -package void writeJsonFile(Path path, Json json) -{ - auto f = openFile(path, FileMode.CreateTrunc); - scope(exit) f.close(); - f.writePrettyJsonString(json); -} - -package bool isPathFromZip(string p) { - enforce(p.length > 0); - return p[$-1] == '/'; -} - -package bool existsDirectory(Path path) { - if( !existsFile(path) ) return false; - auto fi = getFileInfo(path); - return fi.isDirectory; -} - -private string stripUTF8Bom(string str) -{ - if( str.length >= 3 && str[0 .. 3] == [0xEF, 0xBB, 0xBF] ) - return str[3 ..$]; - return str; -} - -void runCommands(string[] commands, string[string] env = null) -{ - foreach(cmd; commands){ - logDiagnostic("Running %s", cmd); - Pid pid; - if( env !is null ) pid = spawnShell(cmd, env); - else pid = spawnShell(cmd); - auto exitcode = pid.wait(); - enforce(exitcode == 0, "Command failed with exit code "~to!string(exitcode)); - } -} - -/** - Downloads a file from the specified URL. - - Any redirects will be followed until the actual file resource is reached or if the redirection - limit of 10 is reached. Note that only HTTP(S) is currently supported. -*/ -void download(string url, string filename) -{ - auto conn = setupHTTPClient(); - logDebug("Storing %s...", url); - std.net.curl.download(url, filename, conn); -} -/// ditto -void download(Url url, Path filename) -{ - download(url.toString(), filename.toNativeString()); -} -/// ditto -char[] download(string url) -{ - auto conn = setupHTTPClient(); - logDebug("Getting %s...", url); - return get(url, conn); -} -/// ditto -char[] download(Url url) -{ - return download(url.toString()); -} - -private HTTP setupHTTPClient() -{ - auto conn = HTTP(); - static if( is(typeof(&conn.verifyPeer)) ) - conn.verifyPeer = false; - - // convert version string to valid SemVer format - auto verstr = dubVersion; - if (verstr.startsWith("v")) verstr = verstr[1 .. $]; - auto idx = verstr.indexOf("-"); - if (idx >= 0) verstr = verstr[0 .. idx] ~ "+" ~ verstr[idx+1 .. $].split("-").join("."); - - conn.addRequestHeader("User-Agent", "dub/"~verstr~" (std.net.curl; +https://github.com/rejectedsoftware/dub)"); - return conn; -} \ No newline at end of file