public static boolean testCommand(
     final Context context,
     final String command,
     final String contains,
     final List<String> arguments) {
   try {
     final List<String> commandAndArgs = new ArrayList<String>();
     commandAndArgs.add(command);
     commandAndArgs.addAll(arguments);
     final ProcessBuilder pb = new ProcessBuilder(commandAndArgs);
     logCommand(context, pb);
     final Process compilation = pb.start();
     final ConsumeStream result = ConsumeStream.start(compilation.getInputStream(), null);
     final ConsumeStream error = ConsumeStream.start(compilation.getErrorStream(), null);
     compilation.waitFor();
     result.join();
     error.join();
     return error.output.toString().contains(contains)
         || result.output.toString().contains(contains);
   } catch (IOException ex) {
     context.log(ex.getMessage());
     return false;
   } catch (InterruptedException ex) {
     context.log(ex.getMessage());
     return false;
   }
 }
Esempio n. 2
0
 public static void main(final String[] args) {
   final Context context = new Context();
   final List<CompileParameter> parameters = initializeParameters(context, ".");
   final int returnCode =
       parse(args, context, parameters) ? (processContext(context, parameters) ? 0 : 1) : 2;
   context.close();
   System.exit(returnCode);
 }
 public static List<File> findFiles(
     final Context context, final File path, final List<String> extensions) {
   context.log("Searching for files...");
   for (final String ext : extensions) {
     context.log("Matching: " + ext);
   }
   final List<File> foundFiles = new LinkedList<File>();
   findFiles(context, path, foundFiles, extensions);
   return foundFiles;
 }
Esempio n. 4
0
 @Override
 public void run(final Context context) throws ExitException {
   if (context.contains(INSTANCE)) {
     context.show("Validating DSL ...");
     final Either<Boolean> result = DslCompiler.parse(context, DslPath.getDslPaths(context));
     if (result.isSuccess()) {
       context.show("Parse successful.");
     } else {
       context.error(result.whyNot());
       throw new ExitException();
     }
   }
 }
Esempio n. 5
0
 @Override
 public boolean check(final Context context) throws ExitException {
   if (context.contains(INSTANCE)) {
     final Map<String, String> dslMap = DslPath.getCurrentDsl(context);
     if (dslMap.size() == 0) {
       context.error(
           "DSL files not found in: "
               + context.get(DslPath.INSTANCE)
               + ". At least one DSL file required.");
       return false;
     }
   }
   return true;
 }
Esempio n. 6
0
 private static void showHelpAndExit(
     final Context context, final boolean headers, final List<CompileParameter> parameters) {
   if (headers) {
     context.show("DSL Platform - Command-Line Client (" + Main.getVersion() + ")");
     context.show(
         "This tool allows you to compile provided DSL to various languages such as Java, Scala, PHP, C#, etc... or create an SQL migration between two DSL models.");
   }
   context.show();
   context.show();
   context.show("Command parameters:");
   int max = 0;
   for (final CompileParameter cp : parameters) {
     if (cp.getShortDescription() == null) {
       continue;
     }
     int width = cp.getAlias().length();
     if (cp.getUsage() != null) {
       width += 1 + cp.getUsage().length();
     }
     if (max < width) {
       max = width;
     }
   }
   max += 2;
   for (final CompileParameter cp : parameters) {
     if (cp.getShortDescription() == null) {
       continue;
     }
     final StringBuilder sb = new StringBuilder();
     sb.append(" -").append(cp.getAlias());
     int len = max - cp.getAlias().length();
     if (cp.getUsage() != null) {
       sb.append("=").append(cp.getUsage());
       len -= cp.getUsage().length() + 1;
     }
     for (; len >= 0; len--) {
       sb.append(' ');
     }
     sb.append(cp.getShortDescription());
     context.show(sb.toString());
   }
   context.show();
   context.show("Example usages:");
   context.show("\ttarget=java_client,revenj.java postgres=localhost/Database?user=postgres");
   context.show(
       "\tjava_client=model.jar revenj.net=Model.dll postgres=localhost/Database?user=postgres");
   context.show("\tproperties=development.props download");
 }
 private static void logCommand(final Context context, final ProcessBuilder builder) {
   final StringBuilder description = new StringBuilder("Running: ");
   for (final String arg : builder.command()) {
     description.append(arg).append(" ");
   }
   context.log(description.toString());
 }
 private static void unpackZip(
     final Context context,
     final File path,
     final URL remoteUrl,
     final ArrayList<File> unpackedFiles,
     final int retry)
     throws IOException {
   try {
     final InputStream response = remoteUrl.openConnection().getInputStream();
     final ZipInputStream zip = new ZipInputStream(new BufferedInputStream(response));
     ZipEntry entry;
     final byte[] buffer = new byte[8192];
     while ((entry = zip.getNextEntry()) != null) {
       long size = 0;
       final File file = new File(path, entry.getName());
       unpackedFiles.add(file);
       final FileOutputStream fos = new FileOutputStream(file);
       int len;
       while ((len = zip.read(buffer)) != -1) {
         fos.write(buffer, 0, len);
         size += len;
       }
       fos.close();
       context.log("Unpacked: " + entry.getName() + ". Size: " + (size / 1024) + "kB");
       zip.closeEntry();
     }
     zip.close();
   } catch (IOException io) {
     context.error(io);
     for (final File f : unpackedFiles) {
       if (f.delete()) {
         context.log("Cleaned up: " + f);
       } else {
         context.log("Failed to clean up: " + f);
       }
     }
     if (retry > 0) {
       context.log("Retrying download... from " + remoteUrl);
       unpackZip(context, path, remoteUrl, new ArrayList<File>(), retry - 1);
     } else throw io;
   }
 }
 public static void saveFile(final Context context, final File file, final String content)
     throws IOException {
   context.log("Saving file: " + file.getAbsolutePath());
   final FileOutputStream fos = new FileOutputStream(file);
   try {
     final Writer writer = new OutputStreamWriter(fos, "UTF-8");
     writer.write(content);
     writer.close();
   } finally {
     fos.close();
   }
 }
Esempio n. 10
0
 public static boolean processContext(
     final Context context, final List<CompileParameter> parameters) {
   try {
     context.notify("PROCESS", parameters);
     for (final CompileParameter cp : parameters) {
       if (!cp.check(context)) {
         if (cp.getDetailedDescription() != null) {
           context.show();
           context.show();
           context.show(cp.getDetailedDescription());
         }
         return false;
       }
     }
     for (final CompileParameter cp : parameters) {
       cp.run(context);
     }
     return true;
   } catch (ExitException ex) {
     return false;
   }
 }
 public static Either<String> findCommand(
     final Context context, final String path, final String name, final String contains) {
   final String simple = path != null ? new File(path, name).getAbsolutePath() : name;
   if (testCommand(context, simple, contains, new ArrayList<String>())) {
     context.log("Found " + name + " in " + simple);
     return Either.success(simple);
   }
   if (isWindows()) {
     final String bat =
         path != null ? new File(path, name + ".bat").getAbsolutePath() : name + ".bat";
     if (testCommand(context, bat, contains, new ArrayList<String>())) {
       context.log("Found " + name + " in " + bat);
       return Either.success(bat);
     }
     final String cmd =
         path != null ? new File(path, name + ".cmd").getAbsolutePath() : name + ".cmd";
     if (testCommand(context, cmd, contains, new ArrayList<String>())) {
       context.log("Found " + name + " in " + cmd);
       return Either.success(cmd);
     }
   }
   return Either.fail("File not found: " + name);
 }
Esempio n. 12
0
 public static List<CompileParameter> initializeParameters(
     final Context context, final String path) {
   final List<CompileParameter> parameters =
       new ArrayList<CompileParameter>(DEFAULT_PARAMETERS.length + 2);
   parameters.add(new Help(parameters));
   parameters.add(new PropertiesFile(parameters));
   Collections.addAll(parameters, DEFAULT_PARAMETERS);
   final File loc = new File(path);
   final File[] jars =
       loc.listFiles(
           new FileFilter() {
             public boolean accept(File file) {
               return file.getPath().toLowerCase().endsWith(".jar");
             }
           });
   final List<URL> urls = new ArrayList<URL>(jars != null ? jars.length : 0);
   if (jars != null) {
     for (final File j : jars) {
       try {
         urls.add(j.toURI().toURL());
       } catch (MalformedURLException ex) {
         context.error(ex);
       }
     }
   }
   final URLClassLoader ucl = new URLClassLoader(urls.toArray(new URL[urls.size()]));
   final ServiceLoader<CompileParameter> plugins = ServiceLoader.load(CompileParameter.class, ucl);
   for (final CompileParameter cp : plugins) {
     parameters.add(cp);
   }
   // HACK: service loader will lock jars on Windows
   // close is not available in Java6, so use reflection to invoke it
   if (Utils.isWindows()) {
     try {
       final Method close = ucl.getClass().getMethod("close");
       if (close != null) {
         close.invoke(ucl);
       }
     } catch (NoSuchMethodError ignore) {
     } catch (Exception ignore) {
     }
   }
   return parameters;
 }
 @Override
 public void run() {
   if (reader == null) {
     return;
   }
   final char[] buffer = new char[8192];
   int len;
   try {
     while ((len = reader.read(buffer)) != -1) {
       output.append(buffer, 0, len);
       if (context != null) {
         context.log(buffer, len);
       }
     }
     reader.close();
   } catch (IOException ex) {
     exception = ex;
   }
 }
 private static void findFiles(
     final Context context,
     final File path,
     final List<File> foundFiles,
     final List<String> extensions) {
   for (final String fn : path.list()) {
     final File f = new File(path, fn);
     if (f.isDirectory()) {
       findFiles(context, f, foundFiles, extensions);
     } else {
       for (final String e : extensions) {
         if (f.getName().endsWith(e)) {
           context.log("Found: " + f.getAbsolutePath());
           foundFiles.add(f);
           break;
         }
       }
     }
   }
 }
Esempio n. 15
0
 private static boolean parse(
     final String[] args, final Context context, final List<CompileParameter> parameters) {
   if (args.length == 1 && ("/?".equals(args[0]) || "-?".equals(args[0]) || "?".equals(args[0]))) {
     showHelpAndExit(context, true, parameters);
     return false;
   }
   final List<ParameterParser> customParsers = new ArrayList<ParameterParser>();
   for (final CompileParameter cp : parameters) {
     if (cp instanceof ParameterParser) {
       customParsers.add((ParameterParser) cp);
     }
   }
   final List<String> errors = new ArrayList<String>();
   for (String a : args) {
     if (a.startsWith("-") || a.startsWith("/")) a = a.substring(1);
     final int eq = a.indexOf('=');
     final String name = a.substring(0, eq != -1 ? eq : a.length());
     final String value = eq == -1 ? null : a.substring(eq + 1);
     final CompileParameter cp = from(name, parameters);
     if (cp == null) {
       boolean matched = false;
       for (final ParameterParser parser : customParsers) {
         final Either<Boolean> tryParse = parser.tryParse(name, value, context);
         if (!tryParse.isSuccess()) {
           errors.add(tryParse.explainError());
           matched = true;
           break;
         } else if (tryParse.get()) {
           matched = true;
           break;
         }
       }
       if (!matched) {
         errors.add("Unknown parameter: " + name);
       }
     } else {
       if (eq == -1 && cp.getUsage() != null) {
         if (cp instanceof ParameterParser) {
           Either<Boolean> tryParse = ((ParameterParser) cp).tryParse(name, null, context);
           if (tryParse.isSuccess() && tryParse.get()) {
             context.put(cp, null);
           } else {
             errors.add("Expecting " + cp.getUsage() + " after = for " + a);
           }
         } else {
           errors.add("Expecting " + cp.getUsage() + " after = for " + a);
         }
       } else {
         context.put(cp, value);
       }
     }
   }
   if (args.length == 0 || errors.size() > 0) {
     for (final String err : errors) {
       context.error(err);
     }
     showHelpAndExit(context, args.length == errors.size(), parameters);
     return false;
   }
   return true;
 }