diff --git a/source/dub/commandline.d b/source/dub/commandline.d index b345d04..8a83c70 100644 --- a/source/dub/commandline.d +++ b/source/dub/commandline.d @@ -68,6 +68,7 @@ new RemoveOverrideCommand, new ListOverridesCommand, new CleanCachesCommand, + new ConvertCommand, ) ]; } @@ -316,6 +317,33 @@ abstract void prepare(scope CommandArgs args); abstract int execute(Dub dub, string[] free_args, string[] app_args); + + private bool loadCwdPackage(Dub dub, bool warn_missing_package) + { + bool found = existsFile(dub.rootPath ~ "source/app.d"); + if (!found) + foreach (f; packageInfoFiles) + if (existsFile(dub.rootPath ~ f.filename)) { + found = true; + break; + } + + if (!found) { + if (warn_missing_package) { + logInfo(""); + logInfo("Neither a package description file, nor source/app.d was found in"); + logInfo(dub.rootPath.toNativeString()); + logInfo("Please run DUB from the root directory of an existing package, or run"); + logInfo("\"dub init --help\" to get information on creating a new package."); + logInfo(""); + } + return false; + } + + dub.loadPackageFromCwd(); + + return true; + } } struct CommandGroup { @@ -499,33 +527,6 @@ dub.loadPackage(pack); return true; } - - private bool loadCwdPackage(Dub dub, bool warn_missing_package) - { - bool found = existsFile(dub.rootPath ~ "source/app.d"); - if (!found) - foreach (f; packageInfoFiles) - if (existsFile(dub.rootPath ~ f.filename)) { - found = true; - break; - } - - if (!found) { - if (warn_missing_package) { - logInfo(""); - logInfo("Neither a package description file, nor source/app.d was found in"); - logInfo(dub.rootPath.toNativeString()); - logInfo("Please run DUB from the root directory of an existing package, or run"); - logInfo("\"dub init --help\" to get information on creating a new package."); - logInfo(""); - } - return false; - } - - dub.loadPackageFromCwd(); - - return true; - } } class GenerateCommand : PackageBuildCommand { @@ -1643,6 +1644,44 @@ /******************************************************************************/ +/* CONVERT command */ +/******************************************************************************/ + +class ConvertCommand : Command { + private { + string m_format; + } + + this() + { + this.name = "convert"; + this.argumentsPattern = ""; + this.description = "Converts the file format of the package recipe."; + this.helpText = [ + "This command will convert between JSON and SDLang formatted package recipe files." + "", + "Warning: Beware that any formatting and comments within the package recipe will get lost in the conversion process." + ]; + } + + override void prepare(scope CommandArgs args) + { + args.getopt("f|format", &m_format, ["Specifies the target package recipe format. Possible values:", " json, sdl"]); + } + + override int execute(Dub dub, string[] free_args, string[] app_args) + { + enforceUsage(app_args.length == 0, "Unexpected application arguments."); + enforceUsage(free_args.length == 0, "Unexpected arguments: "~free_args.join(" ")); + enforceUsage(m_format.length > 0, "Missing target format file extension (--format=...)."); + if (!loadCwdPackage(dub, true)) return 1; + dub.convertRecipe(m_format); + return 0; + } +} + + +/******************************************************************************/ /* HELP */ /******************************************************************************/ diff --git a/source/dub/dub.d b/source/dub/dub.d index 849538d..dd6be06 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -733,6 +733,28 @@ logInfo("Successfully created an empty project in '%s'.", path.toNativeString()); } + /** Converts the package recipe to the given format. + + Params: + destination_file_ext = The file extension matching the desired + format. Possible values are "json" or "sdl". + */ + void convertRecipe(string destination_file_ext) + { + import std.path : extension; + import dub.recipe.io : writePackageRecipe; + + auto srcfile = m_project.rootPackage.packageInfoFilename; + auto srcext = srcfile[$-1].toString().extension; + if (srcext == "."~destination_file_ext) { + logInfo("Package format is already %s.", destination_file_ext); + return; + } + + writePackageRecipe(srcfile[0 .. $-1] ~ ("dub."~destination_file_ext), m_project.rootPackage.info); + removeFile(srcfile); + } + void runDdox(bool run) { if (m_dryRun) return;