From fcec818c9e509f2d144303d0dbb35d781a429633 Mon Sep 17 00:00:00 2001 From: Christian Meier Date: Tue, 16 Jun 2015 14:13:26 +0200 Subject: [PATCH] refactored runBinScript to use JRubyFile.createResource this treats the jruby-complete the same way as a filesystem installation of jruby. the implementation uses the new LoadService. Sponsored by Lookout Inc. --- .../org/jruby/util/cli/ArgumentProcessor.java | 72 ++++++++----------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/core/src/main/java/org/jruby/util/cli/ArgumentProcessor.java b/core/src/main/java/org/jruby/util/cli/ArgumentProcessor.java index 31da8fcce7e..d3609317cba 100644 --- a/core/src/main/java/org/jruby/util/cli/ArgumentProcessor.java +++ b/core/src/main/java/org/jruby/util/cli/ArgumentProcessor.java @@ -34,6 +34,7 @@ import org.jruby.exceptions.MainExitException; import org.jruby.runtime.profile.builtin.ProfileOutput; import org.jruby.util.JRubyFile; +import org.jruby.util.FileResource; import org.jruby.util.KCode; import org.jruby.util.SafePropertyAccessor; @@ -531,59 +532,48 @@ private void runBinScript() { endOfArguments = true; } - private String resolveScript(String scriptName) { - // These try/catches are to allow failing over to the "commands" logic - // when running from within a jruby-complete jar file, which has - // jruby.home = a jar file URL that does not resolve correctly with - // JRubyFile.create. - File fullName = null; - try { - // try cwd first - fullName = JRubyFile.create(config.getCurrentDirectory(), scriptName); - if (fullName.exists() && fullName.isFile()) { - if (RubyInstanceConfig.DEBUG_SCRIPT_RESOLUTION) { - config.getError().println("Found: " + fullName.getAbsolutePath()); - } - return scriptName; - } - } catch (Exception e) { - // keep going, try bin/#{scriptName} + private String resolve(String path, String scriptName) { + if (RubyInstanceConfig.DEBUG_SCRIPT_RESOLUTION) { + config.getError().println("trying path: " + path); } try { - fullName = JRubyFile.create(config.getJRubyHome(), "bin/" + scriptName); + FileResource fullName = JRubyFile.createResource(null, path, scriptName); if (fullName.exists() && fullName.isFile()) { if (RubyInstanceConfig.DEBUG_SCRIPT_RESOLUTION) { - config.getError().println("Found: " + fullName.getAbsolutePath()); + config.getError().println("Found: " + fullName.absolutePath()); } - return fullName.getAbsolutePath(); + return fullName.absolutePath(); } } catch (Exception e) { - // keep going, try PATH - } - if(Ruby.getClassLoader().getResourceAsStream("bin/" + scriptName) != null){ - return "classpath:bin/" + scriptName; + // keep going } - try { - Object pathObj = config.getEnvironment().get("PATH"); - String path = pathObj.toString(); - if (path != null) { - String[] paths = path.split(System.getProperty("path.separator")); - for (int i = 0; i < paths.length; i++) { - fullName = JRubyFile.create(new File(paths[i]).getAbsolutePath(), scriptName); - if (fullName.exists() && fullName.isFile()) { - if (RubyInstanceConfig.DEBUG_SCRIPT_RESOLUTION) { - config.getError().println("Found: " + fullName.getAbsolutePath()); - } - return fullName.getAbsolutePath(); - } - } + return null; + } + + private String resolveScript(String scriptName) { + // These try/catches are to allow failing over to the "commands" logic + // when running from within a jruby-complete jar file, which has + // jruby.home = a jar file URL that does not resolve correctly with + // JRubyFile.create. + String result = resolve(config.getCurrentDirectory(), scriptName); + if (result != null) return result; + result = resolve(config.getJRubyHome() + "/bin", scriptName); + if (result != null) return result; + result = resolve("uri:classloader:/bin", scriptName); + if (result != null) return result; + + String path = config.getEnvironment().get("PATH").toString(); + if (path != null) { + String[] paths = path.split(System.getProperty("path.separator")); + for (int i = 0; i < paths.length; i++) { + result = resolve(paths[i], scriptName); + if (result != null) return result; } - } catch (Exception e) { - // will fall back to JRuby::Commands } if (config.isDebug()) { - config.getError().println("warning: could not resolve -S script on filesystem: " + scriptName); + config.getError().println("warning: could not resolve -S script: " + scriptName); } + // fall back to JRuby::Commands return null; }