Skip to content

Commit

Permalink
Move shared code into utils class
Browse files Browse the repository at this point in the history
  • Loading branch information
valery1707 committed Dec 11, 2018
1 parent fd6f886 commit 31c7da7
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 158 deletions.
2 changes: 1 addition & 1 deletion src/main/java/name/valery1707/kaitai/KaitaiGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.Set;

import static java.util.Collections.unmodifiableSet;
import static name.valery1707.kaitai.IoUtils.*;
import static name.valery1707.kaitai.KaitaiUtils.*;

@SuppressWarnings("WeakerAccess")
public class KaitaiGenerator {
Expand Down
61 changes: 9 additions & 52 deletions src/main/java/name/valery1707/kaitai/KaitaiMojo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package name.valery1707.kaitai;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -14,13 +12,13 @@
import org.slf4j.impl.StaticLoggerBinder;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.*;
import java.util.Arrays;
import java.util.List;

import static java.lang.String.format;
import static name.valery1707.kaitai.IoUtils.*;
import static name.valery1707.kaitai.KaitaiUtils.*;

/**
* @see <a href="http://maven.apache.org/developers/mojo-api-specification.html">Mojo API Specification</a>
Expand All @@ -30,8 +28,6 @@
, defaultPhase = LifecyclePhase.GENERATE_SOURCES
)
public class KaitaiMojo extends AbstractMojo {
private static final String URL_FORMAT = "https://dl.bintray.com/kaitai-io/universal/%s/kaitai-struct-compiler-%s.zip";
private static final String KAITAI_START_SCRIPT = "kaitai-struct-compiler.bat";

/**
* Version of <a href="http://kaitai.io/#download">KaiTai</a> library.
Expand Down Expand Up @@ -166,7 +162,7 @@ private void executeInt() throws KaitaiException {

//Download Kaitai distribution into cache and unzip it
URL url = prepareUrl(this.url, version);
Path cacheDir = prepareCache(this.cacheDir, session, logger);
Path cacheDir = prepareCache(detectCacheDir(), logger);
Path kaitai = downloadKaitai(url, cacheDir, logger);

//Generate Java sources
Expand All @@ -189,51 +185,12 @@ private void executeInt() throws KaitaiException {
project.addCompileSourceRoot(generatedRoot.normalize().toFile().getAbsolutePath());
}

static URL prepareUrl(URL url, String version) throws KaitaiException {
if (url == null) {
try {
url = new URL(format(URL_FORMAT, version, version));
} catch (MalformedURLException e) {
throw new KaitaiException("Invalid version: " + version, e);
}
}
return url;
}

static Path prepareCache(File target, MavenSession session, Logger log) throws KaitaiException {
Path cache;
if (target == null) {
Path repository = new File(session.getLocalRepository().getBasedir()).toPath();
cache = repository.resolve(".cache").resolve("kaitai").normalize();
private Path detectCacheDir() {
if (cacheDir != null) {
return cacheDir.toPath();
} else {
cache = target.toPath();
}
log.debug(format(
"KaiTai distribution: Prepare cache directory: %s"
, cache.normalize().toFile().getAbsolutePath()
));
return mkdirs(cache);
}

private static final Map<Boolean, String> SCRIPT_SUFFIX_REMOVER = Collections.unmodifiableMap(new HashMap<Boolean, String>() {{
put(true, "");
put(false, ".bat");
}});

static Path downloadKaitai(URL url, Path cacheDir, Logger log) throws KaitaiException {
Path distZip = cacheDir.resolve(FilenameUtils.getName(url.getFile()));
download(url, distZip, log);
Path dist = unpack(distZip, log);
List<Path> bats = scanFiles(dist, new String[]{KAITAI_START_SCRIPT}, new String[0]);
if (bats.size() != 1) {
throw new KaitaiException(format(
"Fail to find start script '%s' in Kaitai distribution: %s"
, KAITAI_START_SCRIPT
, dist.normalize().toFile().getAbsolutePath()
));
Path repository = new File(session.getLocalRepository().getBasedir()).toPath();
return repository.resolve(".cache").resolve("kaitai").normalize();
}
Path bat = bats.get(0);
String suffixToRemove = SCRIPT_SUFFIX_REMOVER.get(SystemUtils.IS_OS_WINDOWS);
return bat.resolveSibling(bat.getFileName().toString().replace(suffixToRemove, ""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;

import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import static java.lang.String.format;
import static org.apache.commons.lang3.StringUtils.removeStart;

@SuppressWarnings("WeakerAccess")
public final class IoUtils {
private IoUtils() {
public final class KaitaiUtils {
private KaitaiUtils() {
}

public static void checkFileIsReadable(Path target) throws KaitaiException {
Expand Down Expand Up @@ -219,6 +218,50 @@ public static Path unpack(Path zip, Logger log) throws KaitaiException {
return dir;
}

private static final String URL_FORMAT = "https://dl.bintray.com/kaitai-io/universal/%s/kaitai-struct-compiler-%s.zip";

public static URL prepareUrl(URL url, String version) throws KaitaiException {
if (url == null) {
try {
url = new URL(format(URL_FORMAT, version, version));
} catch (MalformedURLException e) {
throw new KaitaiException("Invalid version: " + version, e);
}
}
return url;
}

public static Path prepareCache(Path cache, Logger log) throws KaitaiException {
log.debug(format(
"KaiTai distribution: Prepare cache directory: %s"
, cache.normalize().toFile().getAbsolutePath()
));
return mkdirs(cache);
}

private static final String KAITAI_START_SCRIPT = "kaitai-struct-compiler.bat";
private static final Map<Boolean, String> SCRIPT_SUFFIX_REMOVER = Collections.unmodifiableMap(new HashMap<Boolean, String>() {{
put(true, "");
put(false, ".bat");
}});

public static Path downloadKaitai(URL url, Path cacheDir, Logger log) throws KaitaiException {
Path distZip = cacheDir.resolve(FilenameUtils.getName(url.getFile()));
download(url, distZip, log);
Path dist = unpack(distZip, log);
List<Path> bats = scanFiles(dist, new String[]{KAITAI_START_SCRIPT}, new String[0]);
if (bats.size() != 1) {
throw new KaitaiException(format(
"Fail to find start script '%s' in Kaitai distribution: %s"
, KAITAI_START_SCRIPT
, dist.normalize().toFile().getAbsolutePath()
));
}
Path bat = bats.get(0);
String suffixToRemove = SCRIPT_SUFFIX_REMOVER.get(SystemUtils.IS_OS_WINDOWS);
return bat.resolveSibling(bat.getFileName().toString().replace(suffixToRemove, ""));
}

private static class FilterFileVisitor extends SimpleFileVisitor<Path> {
private final FileFilter filter;
private final List<Path> target;
Expand Down
96 changes: 0 additions & 96 deletions src/test/java/name/valery1707/kaitai/KaitaiMojoTest.java

This file was deleted.

Loading

0 comments on commit 31c7da7

Please sign in to comment.