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 all 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.
106 changes: 105 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,105 @@ class Main {
return version;
}

function why() {
var library = param("Library to check for: ");
var path = sys.FileSystem.exists('haxelib.json') ? paramOpt() : param("Hxml File or Library: ");
var version = paramOpt();
var repository = getRepository();
var scanResults:WhyResult = Failure;
if (path != null) {
if (path.toLowerCase().endsWith(".hxml")) {
var hxmlFile = sys.io.File.getContent(path);
scanResults = scanForDep(hxmlFile, library, repository);
} else {
var dir = getLibrary(path, version).dir;
scanResults = scanForDepFromLib(Data.readData(Path.join([dir, "haxelib.json"]), CheckData), library, [], repository, true);
}

} 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 path = getLibrary(libName, libVersion);
if (path == null)
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 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 getLibrary(name:String, version:Null<String>):Null<{project: String, version:String, dir:String, info:Infos}> {
var results = new List();
checkRec(getRepository(), name, version, results, false);
if (results.isEmpty())
return null;
return results.first();
}
function installFromHxml( rep:String, path:String ) {
var targets = [
'-java ' => 'hxjava',
Expand Down
1 change: 1 addition & 0 deletions test/IntegrationTests.hx
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class IntegrationTests extends TestBase {
runner.add(new tests.integration.TestDev());
runner.add(new tests.integration.TestRun());
runner.add(new tests.integration.TestPath());
runner.add(new tests.integration.TestWhy());
var success = runner.run();

if (!success) {
Expand Down
7 changes: 7 additions & 0 deletions test/libraries/libDep/dep/Dep.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dep;

class Dep {
static function new() {
trace("new Dep");
}
}
13 changes: 13 additions & 0 deletions test/libraries/libDep/haxelib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Dep",
"url" : "http://example.org",
"license": "GPL",
"tags": ["foo", "test"],
"description": "This project is an example of an haxelib project",
"version": "0.1.0-alpha.0",
"releasenote": "Initial release, everything is working correctly",
"dependencies": {
"Dep2": ""
},
"contributors": ["Foo"]
}
7 changes: 7 additions & 0 deletions test/libraries/libDep2/dep2/Dep2.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dep2;

class Dep2 {
static function new() {
trace("new Dep2");
}
}
13 changes: 13 additions & 0 deletions test/libraries/libDep2/haxelib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Dep2",
"url" : "http://example.org",
"license": "GPL",
"tags": ["foo", "test"],
"description": "This project is an example of an haxelib project",
"version": "0.1.0-alpha.0",
"releasenote": "Initial release, everything is working correctly",
"dependencies": {
"Foo": ""
},
"contributors": ["Foo"]
}
36 changes: 36 additions & 0 deletions test/tests/integration/TestWhy.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package tests.integration;

import haxe.io.*;
import haxelib.*;
import IntegrationTests.*;
using IntegrationTests;

class TestWhy extends IntegrationTests {
function test():Void {
{
var r = haxelib(["dev", "Foo", Path.join([IntegrationTests.projectRoot, "test/libraries/libFoo"])]).result();
assertSuccess(r);
}
{
var r = haxelib(["dev", "Dep2", Path.join([IntegrationTests.projectRoot, "test/libraries/libDep2"])]).result();
assertSuccess(r);
}

{
var r = haxelib(["dev", "Dep", Path.join([IntegrationTests.projectRoot, "test/libraries/libDep"])]).result();
assertSuccess(r);
}

{
var r = haxelib(["why", "Bar", "Foo"]).result();
assertSuccess(r);
assertTrue(r.out.indexOf("This is directly required by your project") != -1);
}
{
var r = haxelib(["why", "Foo", "Dep2"]).result();
assertSuccess(r);
assertTrue(r.out.indexOf("Dependency Structure") != -1);
assertTrue(r.out.indexOf("Dep") != -1);
}
}
}