All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.committed.speedy.hook.executable.DefaultExecutable Maven / Gradle / Ivy

There is a newer version: 0.1.3
Show newest version
package io.committed.speedy.hook.executable;

import static java.util.Objects.requireNonNull;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Collections;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.logging.Log;

/**
 * Created on 08/11/17.
 *
 * @author Reda.Housni-Alaoui
 */
class DefaultExecutable implements Executable {

  private static final String SHEBANG = "#!/bin/bash";

  private final Supplier log;
  private final Path file;

  public DefaultExecutable(Supplier log, Path file) throws IOException {
    requireNonNull(log);
    requireNonNull(file);
    this.log = log;

    this.file = file;

    if (!Files.exists(file)) {
      this.log.get().debug("Creating " + file);
      Files.createFile(file);
      truncate();
    } else {
      this.log.get().debug(file + " already exists");
    }

    this.log.get().debug("Marking '" + file + "' as executable");
    Set permissions;
    try {
      permissions = Files.getPosixFilePermissions(file);
    } catch (UnsupportedOperationException ignored) {
      return;
    }

    permissions.add(PosixFilePermission.OWNER_EXECUTE);
    permissions.add(PosixFilePermission.GROUP_EXECUTE);
    permissions.add(PosixFilePermission.OTHERS_EXECUTE);

    Files.setPosixFilePermissions(file, permissions);
  }

  @Override
  public Executable truncate() throws IOException {
    log.get().debug("Truncating '" + file + "'");
    Files.write(file, Collections.singleton(SHEBANG), StandardOpenOption.TRUNCATE_EXISTING);
    return this;
  }

  @Override
  public Executable truncateWithTemplate(
      Supplier template, String sourceEncoding, Object... values) throws IOException {
    try (InputStream inputStream = template.get()) {
      String rawContent = IOUtils.toString(inputStream, sourceEncoding);
      Object[] refinedValues = Stream.of(values).map(this::unixifyPath).toArray();
      String content = String.format(rawContent, refinedValues);
      Files.write(file, content.getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
    }
    return this;
  }

  @Override
  public Executable appendCommandCall(String commandCall) throws IOException {
    String unixCommandCall = unixifyPath(commandCall, true);
    boolean callExists =
        Files.readAllLines(file).stream().anyMatch(s -> s.contains(unixCommandCall));
    if (callExists) {
      log.get().debug("Command call already exists in " + file);
    } else {
      log.get().debug("No command call found in " + file);
      log.get().debug("Appending the command call to " + file);
      Files.write(file, Collections.singletonList(unixCommandCall), StandardOpenOption.APPEND);
      log.get().debug("Appended the command call to " + file);
    }
    return this;
  }

  private String unixifyPath(Object o) {
    return unixifyPath(o, false);
  }

  private String unixifyPath(Object o, boolean force) {
    if (!force && !(o instanceof Path)) {
      return String.valueOf(o);
    }

    String result;
    if (o instanceof Path) {
      Path path = (Path) o;
      result = path.toAbsolutePath().toString();
    } else {
      result = String.valueOf(o);
    }
    return "\"" + StringUtils.replace(result, "\\", "/") + "\"";
  }

  @Override
  public String toString() {
    return file.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy