Skip to content

Instantly share code, notes, and snippets.

@Laeeth
Created February 23, 2016 01:16
Show Gist options
  • Save Laeeth/9e62861414329721ca39 to your computer and use it in GitHub Desktop.
Save Laeeth/9e62861414329721ca39 to your computer and use it in GitHub Desktop.
Find all versions of dependencies for dub.sdl and dub.json files off a subdirectory
module kaleidic.util.checkdependencies;
/**
Quick and dirty hack to find all versions referenced by dub.sdl and dub.json files off a subdirectory
Written by Laeeth Isharc 2016
Unlimited rights for use, redistribution, relicensing, republication granted to Sonke Ludwig and Rejected Software
Let the user beware
*/
import std.stdio;
import std.string;
import std.process;
import std.path;
import std.exception;
import std.array:array;
import std.range:repeat;
import std.file;
import std.algorithm:sort,max,canFind;
import std.conv:to;
import dub.package_;
import dub.internal.vibecompat.inet.path;
struct Dependency
{
string filename;
string packageName;
string ver;
}
int main(string[] args)
{
Dependency[] deps;
string baseDir;
if (args.length>2)
{
writefln("syntax: extractdeps [base directory]");
return 0;
}
baseDir=(args.length==1)?getcwd():args[1];
writefln("extracting dependencies from %s",baseDir);
foreach(entry;dirEntries(baseDir, "{dub.json,dub.sdl}",SpanMode.depth))
{
if (!entry.isFile)
continue;
if (entry.name.canFind("/.dub/"))
continue;
if (entry.name.baseName=="dub.sdl" || entry.name.baseName=="dub.json")
{
try
{
deps~=entry.name.extractDependencies();
}
catch(Exception e)
{
writefln("* could not open %s",entry.name);
}
}
}
deps=deps.sort!((a,b)=>(a.packageName<b.packageName && a.ver<b.ver)).array;
foreach(dep;deps)
writefln("%s\t%s\t%s",dep.packageName,
dep.ver,dep.filename);
return 1;
}
auto extractDependencies(string filename)
{
Dependency[] ret;
Package package_;
auto dir=Path(filename.dirName);
//writefln("* processing %s",filename);
if (filename.baseName=="dub.json")
{
package_=new Package(dir,PathAndFormat(Path(filename),PackageFormat.json));
foreach(entry;package_.dependencies.keys)
ret~=Dependency(filename,entry,package_.dependencies[entry].to!string);
return ret;
}
else if (filename.baseName=="dub.sdl")
{
package_=new Package(dir,PathAndFormat(Path(filename),PackageFormat.sdl));
foreach(entry;package_.dependencies.keys)
ret~=Dependency(filename,entry,package_.dependencies[entry].to!string);
return ret;
}
else
throw new Exception("cannot extractDepencies from "~filename~" - must be dub.json or dub.sdl");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment