diff --git a/source/dub/commandline.d b/source/dub/commandline.d index a00410e..1f3f281 100644 --- a/source/dub/commandline.d +++ b/source/dub/commandline.d @@ -109,6 +109,7 @@ new TestCommand, new GenerateCommand, new DescribeCommand, + new ListImportsCommand, new CleanCommand, new DustmiteCommand ), @@ -781,6 +782,42 @@ } } +class ListImportsCommand : PackageBuildCommand { + this() + { + this.name = "list-import-paths"; + this.argumentsPattern = "[]"; + this.description = "Prints a list of import paths for the project and its dependencies"; + this.helpText = [ + "Prints a list of imports for the root package an all of their dependencies. This can be useful for build tools needing to know where to find the imports." + "All usual options that are also used for build/run/generate apply." + ]; + } + + override void prepare(scope CommandArgs args) + { + super.prepare(args); + } + + override int execute(Dub dub, string[] free_args, string[] app_args) + { + // disable all log output and use "writeln" to output the import paths. + auto ll = getLogLevel(); + setLogLevel(LogLevel.none); + scope (exit) setLogLevel(ll); + + string package_name; + enforceUsage(free_args.length <= 1, "Expected one or zero arguments."); + if (free_args.length >= 1) package_name = free_args[0]; + setupPackage(dub, package_name); + + m_defaultConfig = dub.project.getDefaultConfiguration(m_buildPlatform); + + dub.listImportPaths(m_buildPlatform, m_buildConfig.length ? m_buildConfig : m_defaultConfig); + return 0; + } +} + class CleanCommand : Command { private { bool m_allPackages; diff --git a/source/dub/dub.d b/source/dub/dub.d index c933c83..6a4a813 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -422,6 +422,19 @@ write(dst.toPrettyString()); } + void listImportPaths(BuildPlatform platform, string config) + { + string[] importPaths; + + m_project.listImportPaths(importPaths, platform, config); + + import std.stdio; + + foreach(path; importPaths) { + writeln(path); + } + } + /// Cleans intermediate/cache files of the given package void cleanPackage(Path path) { @@ -1027,3 +1040,4 @@ return null; } } + diff --git a/source/dub/project.d b/source/dub/project.d index 540f6c6..dc7c023 100644 --- a/source/dub/project.d +++ b/source/dub/project.d @@ -181,7 +181,7 @@ logWarn(`WARNING: DUB package names may only contain alphanumeric characters, ` ~ `as well as '-' and '_', please modify the "name" field in %s ` ~ `accordingly. You can use {"targetName": "%s"} to keep the current ` - ~ `executable name.`, + ~ `executable name.`, m_rootPackage.packageInfoFilename.toNativeString(), m_rootPackage.name); } enforce(!m_rootPackage.name.canFind(' '), "Aborting due to the package name containing spaces."); @@ -582,6 +582,29 @@ } } + /// Outputs the import paths for the project, including its dependencies. + void listImportPaths(ref string[] list, BuildPlatform platform, string config) + { + auto configs = getPackageConfigs(platform, config); + + import std.path : buildPath; + + auto fullPackagePaths(Package pack) { + return pack.getBuildSettings(platform, config).importPaths + .map!(importPath => buildPath(pack.path.toString(), importPath)); + } + + foreach(path; fullPackagePaths(m_rootPackage)) { + list ~= path; + } + + foreach (dep; m_dependencies) { + foreach(path; fullPackagePaths(dep)) { + list ~= path; + } + } + } + void saveSelections() { assert(m_selections !is null, "Cannot save selections for non-disk based project (has no selections)."); @@ -972,3 +995,4 @@ m_selections[p] = Selected(dependencyFromJson(v)); } } +