Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "why" command #525

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified run.n
Binary file not shown.
103 changes: 102 additions & 1 deletion src/haxelib/client/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ private enum CommandCategory {
Miscellaneous;
Deprecated(msg:String);
}

private enum WhyResult {
DirectlyRequired;
Failure;
Success(path:Array<String>);
}
class SiteProxy extends haxe.remoting.Proxy<haxelib.SiteApi> {
}

Expand Down Expand Up @@ -199,6 +203,7 @@ class Main {
addCommand("libpath", libpath, "returns the root path of a library", Information, false);
addCommand("version", version, "print the currently used haxelib version", Information, false);
addCommand("help", usage, "display this list of options", Information, false);
addCommand("why", why, "list why a lib is required", Information, false);

addCommand("submit", submit, "submit or update a library package", Development);
addCommand("register", register, "register a new user", Development);
Expand Down Expand Up @@ -767,6 +772,102 @@ class Main {
return version;
}

function why() {
var library = param("Library to check for: ");
var hxmlPath = sys.FileSystem.exists('haxelib.json') ? paramOpt() : param("Hxml File: ");
var repository = getRepository();
var scanResults:WhyResult = Failure;
if (hxmlPath != null) {
var hxmlFile = sys.io.File.getContent(hxmlPath);
scanResults = scanForDep(hxmlFile, library, repository);
} else {
// If it's null that means it has to exist
var hxlibJson = sys.io.File.getContent('haxelib.json');
scanResults = scanForDepFromLib(Data.readData(hxlibJson, CheckData), library, [], repository, true);

}
switch (scanResults) {
case Failure:
print('Couldn\'t find a reason or ${library} isn\'t required by the project.');
case DirectlyRequired:
print('This is directly required by your project.');
case Success(depPath):
print('Dependency Structure: ${depPath.join('> ')}');
}
}
function scanForDep(hxml:String, lib:String, rep:String):WhyResult {
for (line in hxml.split('\n')) {
line = line.trim();

var libraryFlagEReg = ~/^(-lib|-L|--library)\b/;
if (libraryFlagEReg.match(line)) {
var key = libraryFlagEReg.matchedRight().trim();
var parts = ~/:/.split(key);
var libName = parts[0];
var libVersion:String = null;
var branch:String = null;
var url:String = null;
var type:String = "haxelib";
if (parts.length > 1) {
if (parts[1].startsWith("git:")) {
type = "git";
var urlParts = parts[1].substr(4).split("#");
url = urlParts[0];
branch = urlParts.length > 1 ? urlParts[1] : null;
} else
{
type = "haxelib";
libVersion = parts[1];
}
}
if (libName == lib) {
// Directly required, return that it's directly required
return DirectlyRequired;
}
var results = new List();
checkRec(rep, libName, type == "git" ? "git" : libVersion, results, false);
var path = '';
if (!results.isEmpty()) {
path = results.first().dir;
}
if (path == '')
continue;
var haxelibData = Data.readData(sys.io.File.getContent(path + '/haxelib.json'), CheckData);
var haxelibResults = scanForDepFromLib(haxelibData, lib, [libName], rep);

if (haxelibResults != Failure)
return haxelibResults;
} else if (line.toLowerCase().endsWith('.hxml')) {
var hxmlOutput = scanForDep(sys.io.File.getContent(line.trim()), lib, rep);
if (hxmlOutput != Failure)
return hxmlOutput;
}
}
return Failure;
}

function scanForDepFromLib(libData:Infos, scanFor:String, path:Array<String>, rep:String, direct:Bool = false):WhyResult {
for (dep in libData.dependencies) {
var newPath = path.copy();
newPath.push(dep.name);
if (dep.name == scanFor) {
return direct ? DirectlyRequired : Success(newPath);
}
var results = new List();
checkRec(rep, dep.name, null, results, false);
var dir = '';
if (!results.isEmpty())
dir = results.first().dir;
else
continue;
var haxelibData = Data.readData(sys.io.File.getContent(path + '/haxelib.json'), CheckData);
var scanResult = scanForDepFromLib(haxelibData, scanFor, newPath, rep);
if (scanResult != Failure) {
return scanResult;
}
}
return Failure;
}
function installFromHxml( rep:String, path:String ) {
var targets = [
'-java ' => 'hxjava',
Expand Down