Skip to content

Instantly share code, notes, and snippets.

@JakobOvrum
Last active December 19, 2015 06:48
Show Gist options
  • Save JakobOvrum/5913596 to your computer and use it in GitHub Desktop.
Save JakobOvrum/5913596 to your computer and use it in GitHub Desktop.
Helper script for writing makefiles
#!/usr/bin/env rdmd
/**
* Output a makefile-compatible list of source files
* given a list of directories containing the files.
* Example:
* ./list_sources.d src util >> Makefile
*/
import std.algorithm, std.path;
// returns range of file paths
auto findSources(string dir)
{
import std.array : array, replace;
import std.file : dirEntries, SpanMode;
return dir.dirEntries(SpanMode.depth)
.filter!(entry => entry.isFile && entry.name.extension == ".d")
.map!(entry => entry.name.replace(`\`, `/`)) // Don't use backward slashes on Windows
.array // Needs to be forward range to be sortable
.sort;
}
// e.g. std/net => STD_NET_SOURCES
string toVariableName(string directoryName)
{
import std.array : replace;
import std.string : toUpper;
return directoryName.toUpper().replace("/", "_") ~ "_SOURCES";
}
void main(string[] args)
{
import std.range, std.stdio;
auto directories = args[1 .. $].sort;
directories.map!(toVariableName, findSources)
.map!(tup => [tup[0] ~ " ="].chain(tup[1])) // 0 = varName, 1 = sources
.map!(filePath => filePath.joiner(" \\\n\t"))
.joiner("\n\n") // Separate directories by a blank line
.chain("\n") // Trailing newline
.copy(stdout.lockingTextWriter);
writefln("\nSOURCES = %-($(%s) %|%) ", directories.map!toVariableName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment