Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Commit

Permalink
externalize mapping of archive information
Browse files Browse the repository at this point in the history
  • Loading branch information
klieber committed Feb 17, 2017
1 parent 2d8685a commit 15cf96c
Show file tree
Hide file tree
Showing 25 changed files with 622 additions and 503 deletions.
12 changes: 12 additions & 0 deletions phantomjs-maven-core/pom.xml
Expand Up @@ -45,6 +45,18 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>

</project>

This file was deleted.

This file was deleted.

Expand Up @@ -20,68 +20,10 @@
*/
package com.github.klieber.phantomjs.archive;

public abstract class PhantomJSArchive {

private final String basename;
private final String version;

public PhantomJSArchive(String version) {
this.basename = "phantomjs";
this.version = version;
}

public abstract String getExtension();
protected abstract String getPlatform();
protected abstract String getExecutable();

protected String getArch() {
return null;
}

protected String getPatch() { return null; }

public final String getArchiveName() {
return this.getArchiveNameSB().toString();
}

public final String getPathToExecutable() {
return this.getNameWithoutExtension()
.append("/")
.append(this.getExecutable())
.toString();
}

public final String getExtractToPath() {
return this.getNameWithoutExtension().append("/").append(this.getExecutable()).toString();
}

private StringBuilder getArchiveNameSB() {
return this.getNameWithoutExtension()
.append(".")
.append(this.getExtension());
}

private StringBuilder getNameWithoutExtension() {
return new StringBuilder()
.append(this.basename)
.append("-")
.append(this.version)
.append("-")
.append(this.getClassifier());
}

public final String getVersion() {
return this.version;
}

public final String getClassifier() {
StringBuilder sb = new StringBuilder().append(this.getPlatform());
if (this.getArch() != null) {
sb.append("-").append(this.getArch());
}
if (this.getPatch() != null) {
sb.append("-").append(this.getPatch());
}
return sb.toString();
}
public interface PhantomJSArchive {
String getExtension();
String getArchiveName();
String getPathToExecutable();
String getVersion();
String getClassifier();
}
Expand Up @@ -21,39 +21,62 @@
package com.github.klieber.phantomjs.archive;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.github.klieber.phantomjs.archive.mapping.ArchiveSpec;
import com.github.klieber.phantomjs.archive.mapping.ArchiveMapping;
import com.github.klieber.phantomjs.archive.mapping.ArchiveMappings;
import com.github.klieber.phantomjs.os.OperatingSystem;
import com.github.klieber.phantomjs.util.VersionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URL;

public class PhantomJSArchiveBuilder {

private final String platform;
private final String arch;
private final String version;

public PhantomJSArchiveBuilder(String platform, String arch, String version) {
this.platform = platform;
this.arch = arch;
this.version = version;
}

public PhantomJSArchiveBuilder(String version) {
this(
System.getProperty("os.name").toLowerCase(),
System.getProperty("os.arch").toLowerCase(),
version
);
}

public PhantomJSArchive build() {
PhantomJSArchive archive = null;
if (platform.contains("win")) {
archive = new WindowsPhantomJSArchive(version);
} else if (platform.contains("mac")) {
archive = new MacOSXPhantomJSArchive(version);
} else if (platform.contains("nux")) {
String modifier = arch.contains("64") ? "x86_64" : "i686";
archive = new LinuxPhantomJSArchive(version, modifier);
}
if (archive == null) {
throw new IllegalArgumentException("unknown platform: " + platform);
}
return archive;
}

private static final Logger LOGGER = LoggerFactory.getLogger(PhantomJSArchiveBuilder.class);

private final OperatingSystem operatingSystem;
private final String version;

public PhantomJSArchiveBuilder(OperatingSystem operatingSystem,
String version) {
this.operatingSystem = operatingSystem;
this.version = version;
}

public PhantomJSArchive build() {

PhantomJSArchive archive = null;

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
URL resource = PhantomJSArchiveBuilder.class.getResource("/archive-mapping.yaml");
try {
ArchiveMappings archiveMappings = mapper.readValue(resource, ArchiveMappings.class);
for (ArchiveMapping archiveMapping : archiveMappings.getMappings()) {
if (archiveMapping.getSpec().matches(this.version, this.operatingSystem)) {
archive = new PhantomJSArchiveImpl(archiveMapping.getFormat(), this.version);
break;
}
}
} catch (IOException e) {
LOGGER.error("Unable to read archive-mapping.yaml", e);
}
if (archive == null) {
throw new UnsupportedPlatformException(this.operatingSystem);
}
return archive;
}

private boolean matches(ArchiveSpec condition) {
return
VersionUtil.isWithin(versionNumberOnly(), condition.getVersionSpec()) &&
condition.getOperatingSystemSpec().matches(operatingSystem);
}

private String versionNumberOnly() {
return this.version.replaceAll("[^0-9.]", "");
}
}
@@ -0,0 +1,47 @@
package com.github.klieber.phantomjs.archive;

import com.github.klieber.phantomjs.archive.mapping.ArchiveFormat;

public class PhantomJSArchiveImpl implements PhantomJSArchive {

private final ArchiveFormat archiveFormat;
private final String version;

public PhantomJSArchiveImpl(ArchiveFormat archiveFormat,
String version) {
this.archiveFormat = archiveFormat;
this.version = version;
}

@Override
public String getExtension() {
return this.archiveFormat.getExtension();
}

@Override
public String getArchiveName() {
return applyTemplate(this.archiveFormat.getFileTemplate());
}

@Override
public String getPathToExecutable() {
return applyTemplate(this.archiveFormat.getExecutableTemplate());
}

@Override
public String getVersion() {
return version;
}

@Override
public String getClassifier() {
return this.archiveFormat.getClassifier();
}

private String applyTemplate(String template) {
return template
.replaceAll("\\{version}", this.version)
.replaceAll("\\{classifier}", this.archiveFormat.getClassifier())
.replaceAll("\\{extension}", this.archiveFormat.getExtension());
}
}
@@ -0,0 +1,12 @@
package com.github.klieber.phantomjs.archive;

import com.github.klieber.phantomjs.os.OperatingSystem;

public class UnsupportedPlatformException extends RuntimeException {

private static final String MESSAGE = "Unsupported platform: %s";

public UnsupportedPlatformException(OperatingSystem operatingSystem) {
super(String.format(MESSAGE, operatingSystem));
}
}

0 comments on commit 15cf96c

Please sign in to comment.