Merge pull request #1605 from andre2007/fix_issue_1124
Shebang without .d extension
commit e8c95b2aaeae815439dc50d2228761ae3ad2dba5
2 parents 44c2537 + 441d223
@Nicholas Wilson Nicholas Wilson authored on 5 Dec 2018
GitHub committed on 5 Dec 2018
Showing 4 changed files
View
6
changelog/shebang-without-d-extension.dd 0 → 100644
Shebang without .d extension
 
Dub single-file packages e.g. `app.d` can now be called without .d extension.
In addition to `dub app.d --param` you can call `dub app --param`.
 
Also files without .d extension are supported now as single-file packages.
View
24
source/dub/commandline.d
import dub.package_;
import dub.packagemanager;
import dub.packagesuppliers;
import dub.project;
import dub.internal.utils : getDUBVersion, getClosestMatch;
import dub.internal.utils : getDUBVersion, getClosestMatch, getTempFile;
 
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
import std.typecons : Tuple, tuple;
import std.variant;
 
import std.path: setExtension;
 
/** Retrieves a list of all available commands.
 
Commands are grouped by category.
 
// special stdin syntax
if (args.length >= 2 && args[1] == "-")
{
import dub.internal.utils: getTempFile;
 
auto path = getTempFile("app", ".d");
stdin.byChunk(4096).joiner.toFile(path.toNativeString());
args = args[0] ~ [path.toNativeString()] ~ args[2..$];
}
 
// create the list of all supported commands
CommandGroup[] commands = getCommands();
string[] commandNames = commands.map!(g => g.commands.map!(c => c.name).array).join.array;
 
// Shebang syntax support for files without .d extension
if (args.length >= 2 && !args[1].endsWith(".d") && !args[1].startsWith("-") && !commandNames.canFind(args[1])) {
if (exists(args[1])) {
auto path = getTempFile("app", ".d");
copy(args[1], path.toNativeString());
args[1] = path.toNativeString();
} else if (exists(args[1].setExtension(".d"))) {
args[1] = args[1].setExtension(".d");
}
}
// special single-file package shebang syntax
if (args.length >= 2 && args[1].endsWith(".d")) {
args = args[0] ~ ["run", "-q", "--temp-build", "--single", args[1], "--"] ~ args[2 ..$];
}
import std.path : absolutePath, buildNormalizedPath;
 
options.root_path = options.root_path.absolutePath.buildNormalizedPath;
}
 
// create the list of all supported commands
CommandGroup[] commands = getCommands();
 
// extract the command
string cmdname;
args = common_args.extractRemainingArgs();
View
test/issue103-single-file-package-no-ext 0 → 100755
View
test/issue103-single-file-package.sh