Search in sources :

Example 1 with InputReader

use of de.undercouch.underline.InputReader in project georocket by georocket.

the class ImportCommand method doRun.

@Override
public void doRun(String[] remainingArgs, InputReader in, PrintWriter out, Handler<Integer> handler) throws OptionParserException, IOException {
    long start = System.currentTimeMillis();
    // resolve file patterns
    Queue<String> queue = new ArrayDeque<>();
    for (String p : patterns) {
        // convert Windows backslashes to slashes (necessary for Files.newDirectoryStream())
        if (SystemUtils.IS_OS_WINDOWS) {
            p = FilenameUtils.separatorsToUnix(p);
        }
        // collect paths and glob patterns
        List<String> roots = new ArrayList<>();
        List<String> globs = new ArrayList<>();
        String[] parts = p.split("/");
        boolean rootParsed = false;
        for (String part : parts) {
            if (!rootParsed) {
                if (hasGlobCharacter(part)) {
                    globs.add(part);
                    rootParsed = true;
                } else {
                    roots.add(part);
                }
            } else {
                globs.add(part);
            }
        }
        if (globs.isEmpty()) {
            // string does not contain a glob pattern at all
            queue.add(p);
        } else {
            // string contains a glob pattern
            if (roots.isEmpty()) {
                // there are not paths in the string. start from the current
                // working directory
                roots.add(".");
            }
            // add all files matching the pattern
            String root = String.join("/", roots);
            String glob = String.join("/", globs);
            Project project = new Project();
            FileSet fs = new FileSet();
            fs.setDir(new File(root));
            fs.setIncludes(glob);
            DirectoryScanner ds = fs.getDirectoryScanner(project);
            Arrays.stream(ds.getIncludedFiles()).map(path -> Paths.get(root, path).toString()).forEach(queue::add);
        }
    }
    if (queue.isEmpty()) {
        error("given pattern didn't match any files");
        return;
    }
    Vertx vertx = new Vertx(this.vertx);
    GeoRocketClient client = createClient();
    int queueSize = queue.size();
    doImport(queue, client, vertx, exitCode -> {
        client.close();
        if (exitCode == 0) {
            String m = "file";
            if (queueSize > 1) {
                m += "s";
            }
            System.out.println("Successfully imported " + queueSize + " " + m + " in " + DurationFormat.formatUntilNow(start));
        }
        handler.handle(exitCode);
    });
}
Also used : AsyncFile(io.vertx.core.file.AsyncFile) Arrays(java.util.Arrays) OptionParserException(de.undercouch.underline.OptionParserException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) Observable(rx.Observable) Pair(org.apache.commons.lang3.tuple.Pair) FileSet(org.apache.tools.ant.types.FileSet) WriteStream(io.vertx.core.streams.WriteStream) FileSystem(io.vertx.rxjava.core.file.FileSystem) Project(org.apache.tools.ant.Project) UnknownAttributes(de.undercouch.underline.UnknownAttributes) Pump(io.vertx.core.streams.Pump) AsyncResult(io.vertx.core.AsyncResult) Splitter(com.google.common.base.Splitter) OptionDesc(de.undercouch.underline.OptionDesc) PrintWriter(java.io.PrintWriter) OpenOptions(io.vertx.core.file.OpenOptions) ObservableFuture(io.vertx.rx.java.ObservableFuture) SystemUtils(org.apache.commons.lang3.SystemUtils) InputReader(de.undercouch.underline.InputReader) DurationFormat(io.georocket.util.DurationFormat) IOException(java.io.IOException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) GeoRocketClient(io.georocket.client.GeoRocketClient) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) File(java.io.File) List(java.util.List) Stream(java.util.stream.Stream) Buffer(io.vertx.core.buffer.Buffer) Paths(java.nio.file.Paths) RxHelper(io.vertx.rx.java.RxHelper) Optional(java.util.Optional) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) ArgumentType(de.undercouch.underline.Option.ArgumentType) Handler(io.vertx.core.Handler) FilenameUtils(org.apache.commons.io.FilenameUtils) Vertx(io.vertx.rxjava.core.Vertx) FileSet(org.apache.tools.ant.types.FileSet) ArrayList(java.util.ArrayList) Vertx(io.vertx.rxjava.core.Vertx) GeoRocketClient(io.georocket.client.GeoRocketClient) ArrayDeque(java.util.ArrayDeque) Project(org.apache.tools.ant.Project) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) AsyncFile(io.vertx.core.file.AsyncFile) File(java.io.File)

Aggregations

Splitter (com.google.common.base.Splitter)1 InputReader (de.undercouch.underline.InputReader)1 ArgumentType (de.undercouch.underline.Option.ArgumentType)1 OptionDesc (de.undercouch.underline.OptionDesc)1 OptionParserException (de.undercouch.underline.OptionParserException)1 UnknownAttributes (de.undercouch.underline.UnknownAttributes)1 GeoRocketClient (io.georocket.client.GeoRocketClient)1 DurationFormat (io.georocket.util.DurationFormat)1 AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Buffer (io.vertx.core.buffer.Buffer)1 AsyncFile (io.vertx.core.file.AsyncFile)1 OpenOptions (io.vertx.core.file.OpenOptions)1 Pump (io.vertx.core.streams.Pump)1 WriteStream (io.vertx.core.streams.WriteStream)1 ObservableFuture (io.vertx.rx.java.ObservableFuture)1 RxHelper (io.vertx.rx.java.RxHelper)1 Vertx (io.vertx.rxjava.core.Vertx)1 FileSystem (io.vertx.rxjava.core.file.FileSystem)1