private static void handleNameValue(NginxCompileParameters result, String name, String value) { if (name.equals("--conf-path")) { result.setConfigurationPath(value); } else if (name.equals("--pid-path")) { result.setPidPath(value); } else if (name.equals("--prefix")) { result.setPrefix(value); } else if (name.equals("--http-log-path")) { result.setHttpLogPath(value); } else if (name.equals("--error-log-path")) { result.setErrorLogPath(value); } }
/** * Runs file with -V argument and matches the output against OUTPUT_PATTERN. * * @param from executable to be run with -V command line argument * @return Parameters parsed out from process output * @throws PlatformDependentTools.ThisIsNotNginxExecutableException if file could not be run, or * output would not match against expected pattern */ public static NginxCompileParameters extract(VirtualFile from) throws PlatformDependentTools.ThisIsNotNginxExecutableException { NginxCompileParameters result = new NginxCompileParameters(); Executor executor = new DefaultExecutor(); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { executor.setStreamHandler(new PumpStreamHandler(os, os)); executor.execute(CommandLine.parse(from.getPath() + " -V")); } catch (IOException e) { throw new PlatformDependentTools.ThisIsNotNginxExecutableException(e); } String output = os.toString(); Matcher versionMatcher = Pattern.compile("nginx version: nginx/([\\d\\.]+)").matcher(output); Matcher configureArgumentsMatcher = Pattern.compile("configure arguments: (.*)").matcher(output); if (versionMatcher.find() && configureArgumentsMatcher.find()) { String version = versionMatcher.group(1); String params = configureArgumentsMatcher.group(1); result.setVersion(version); Iterable<String> namevalues = StringUtil.split(params, " "); for (String namevalue : namevalues) { int eqPosition = namevalue.indexOf('='); if (eqPosition == -1) { handleFlag(result, namevalue); } else { handleNameValue( result, namevalue.substring(0, eqPosition), namevalue.substring(eqPosition + 1)); } } } else { throw new PlatformDependentTools.ThisIsNotNginxExecutableException( NginxBundle.message("run.configuration.outputwontmatch")); } return result; }