added a JemClassLoader which can "add" jem on the fly

This commit is contained in:
Christian Meier 2015-08-15 20:23:03 +02:00
parent 9b0254a51b
commit f1641a3a18
8 changed files with 54 additions and 6 deletions

View File

@ -7,7 +7,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.File;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

View File

@ -0,0 +1,50 @@
package com.github.jrubygradle.jem;
import com.github.jrubygradle.jem.internal.GemInstaller;
import com.github.jrubygradle.jem.GemInstaller.DuplicateBehavior;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.util.Collections;
/**
* Created by cmeier on 8/15/15.
*/
public class JemsClassLoader extends URLClassLoader {
private final File jemsDir;
public static JemsClassLoader create() throws IOException {
return new JemsClassLoader(Files.createTempDirectory("jems").toFile());
}
public JemsClassLoader(File jemsDir) throws IOException {
super(new URL[] { jemsDir.toURI().toURL() });
this.jemsDir = jemsDir;
}
public Gem addJem(String jem) throws IOException {
return addJem(new File(jem));
}
public Gem addJem(File jem) throws IOException {
GemInstaller installer = new GemInstaller(jemsDir.getAbsolutePath(), Collections.<File>emptyList());
installer.mkdirs();
Gem gemspec = installer.installGem(jemsDir, jem, DuplicateBehavior.OVERWRITE);
String dir = "gems/" + installer.gemFullName(gemspec);
createDirInfoFile(dir);
for(String path: gemspec.requirePaths){
createDirInfoFile(dir + "/" + path);
}
return gemspec;
}
private void createDirInfoFile(String dir) throws IOException {
try (FileWriter out = new FileWriter(new File(jemsDir, dir + "/.jrubydir"))){
out.append(".");
}
}
}

View File

@ -50,14 +50,14 @@ public class GemInstaller {
}
}
public boolean installGem(File installDir, File gem, DuplicateBehavior onDuplicate) {
public Gem installGem(File installDir, File gem, DuplicateBehavior onDuplicate) {
/* TODO: isValidGem? */
try {
cacheGemInInstallDir(installDir, gem);
}
catch (IOException ex) {
logger.error("Failed to cache our gem in %s", installDir, ex);
return false;
return null;
}
Gem gemMetadata;
@ -73,7 +73,7 @@ public class GemInstaller {
}
catch (IOException ex) {
logger.error("Failed to process the metadata", ex);
return false;
return null;
}
logger.info(String.format("We've processed metadata for %s at version %s",
gemMetadata.name, gemMetadata.version.version));
@ -96,7 +96,7 @@ public class GemInstaller {
gemMetadata.name, installDir), ex);
}
return true;
return gemMetadata;
}
/**

View File

@ -6,7 +6,6 @@ import spock.lang.*
import java.nio.file.Files
import java.nio.file.Path
import org.apache.commons.io.FileUtils
class GemInstallerSpec extends Specification {
static final String FIXTURES_ROOT = new File(['src', 'test', 'resources'].join(File.separator)).absolutePath