diff --git a/source/dub/commandline.d b/source/dub/commandline.d index 0ec677b..7c2a578 100644 --- a/source/dub/commandline.d +++ b/source/dub/commandline.d @@ -97,6 +97,7 @@ new TestCommand, new GenerateCommand, new DescribeCommand, + new CleanCommand, new DustmiteCommand ), CommandGroup("Package management", @@ -693,6 +694,48 @@ } } +class CleanCommand : Command { + private { + bool m_allPackages; + } + + this() + { + this.name = "clean"; + this.argumentsPattern = "[]"; + this.description = "Removes intermetiate build files and cached build results"; + this.helpText = [ + "This command removes any cached build files of the given package(s). The final target file, as well as any copyFiles are currently not removed.", + "Without arguments, the package in the current working directory will be cleaned." + ]; + } + + override void prepare(scope CommandArgs args) + { + args.getopt("all-packages", &m_allPackages, [ + "Cleans up *all* known packages (dub list)" + ]); + } + + override int execute(Dub dub, string[] free_args, string[] app_args) + { + enforceUsage(free_args.length <= 1, "Expected one or zero arguments."); + enforceUsage(app_args.length == 0, "Application arguments are not supported for the clean command."); + enforceUsage(!m_allPackages || !free_args.length, "The --all-packages flag may not be used together with an explicit package name."); + + enforce(free_args.length == 0, "Cleaning a specific package isn't possible right now."); + + if (m_allPackages) { + foreach (p; dub.packageManager.getPackageIterator()) + dub.cleanPackage(p.path); + } else { + dub.cleanPackage(dub.rootPath); + } + + return 0; + } +} + /******************************************************************************/ /* FETCH / REMOVE / UPGRADE */ diff --git a/source/dub/dub.d b/source/dub/dub.d index 000f8f8..0d25d15 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -352,6 +352,18 @@ write(dst.toPrettyString()); } + /// Cleans intermediate/cache files of the given package + void cleanPackage(Path path) + { + logInfo("Cleaning package at %s...", path.toNativeString()); + enforce(Package.isPackageAt(path), "No package found.", path.toNativeString()); + + // TODO: clear target files and copy files + + if (existsFile(path ~ ".dub/build")) rmdirRecurse((path ~ ".dub/build").toNativeString()); + if (existsFile(path ~ ".dub/obj")) rmdirRecurse((path ~ ".dub/obj").toNativeString()); + } + /// Returns all cached packages as a "packageId" = "version" associative array string[string] cachedPackages() const { return m_project.cachedPackagesIDs(); }