Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make rascal.toml an alternative to rascal.mf #1881

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<lucence-version>7.5.0</lucence-version>
<jackson-version>2.15.3</jackson-version>
<exec.mainClass>org.rascalmpl.shell.RascalShell</exec.mainClass>
<rascal.test.memory>2</rascal.test.memory>
<maven.compiler.release>11</maven.compiler.release>
Expand Down Expand Up @@ -441,5 +442,15 @@
<artifactId>icu4j</artifactId>
<version>69.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-toml</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
<version>${jackson-version}</version>
</dependency>
</dependencies>
</project>
154 changes: 154 additions & 0 deletions src/org/rascalmpl/interpreter/utils/RascalToml.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package org.rascalmpl.interpreter.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Collections;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.uri.URIUtil;

import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.dataformat.toml.TomlMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

import io.usethesource.vallang.ISourceLocation;

public class RascalToml {
private final String version;

private final Project project;
private final @Nullable Main main;


private static class Project {
private final String name;
private final List<String> sources;
private final @Nullable List<String> libraries;

private Project(String name, @Nullable List<String> sources, @Nullable List<String> libraries) {
this.name = name;

Check warning on line 37 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L36-L37

Added lines #L36 - L37 were not covered by tests
this.sources = (sources == null || sources.isEmpty()) ? Collections.singletonList("src") : Collections.unmodifiableList(sources);
this.libraries = (libraries == null || libraries.isEmpty()) ? null : Collections.unmodifiableList(libraries);
}

Check warning on line 40 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L40

Added line #L40 was not covered by tests
}

private static class Main {
private final @Nullable String module;
private final @Nullable String function;

private Main(@Nullable String module, @Nullable String function) {
this.module = module;
this.function = function;
}

Check warning on line 50 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L47-L50

Added lines #L47 - L50 were not covered by tests
}


private RascalToml(String version, Project project, @Nullable Main main) {
this.version = version;
this.project = project;
this.main = main;
}

Check warning on line 58 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L54-L58

Added lines #L54 - L58 were not covered by tests

public String getVersion() {
return version;

Check warning on line 61 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L61

Added line #L61 was not covered by tests
}

public @Nullable String getProjectName() {
return project.name;

Check warning on line 65 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L65

Added line #L65 was not covered by tests
}

public @Nullable String getMainModule() {
return main.module;

Check warning on line 69 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L69

Added line #L69 was not covered by tests
}

public @Nullable String getMainFunction() {
return main.function;

Check warning on line 73 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L73

Added line #L73 was not covered by tests
}

public List<String> getSource() {
return project.sources;

Check warning on line 77 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L77

Added line #L77 was not covered by tests
}

public @Nullable List<String> getRequireLibraries() {
return project.libraries;

Check warning on line 81 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L81

Added line #L81 was not covered by tests
}

public static RascalToml parse(JarInputStream from) throws IOException {
return parse(tomlFile(from));

Check warning on line 85 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L85

Added line #L85 was not covered by tests
}

public static RascalToml parse(Class<?> from) throws IOException {
return parse(tomlFile(from));

Check warning on line 89 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L89

Added line #L89 was not covered by tests
}

public static RascalToml parse(File from) throws IOException {
return parse(tomlFile(from));

Check warning on line 93 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L93

Added line #L93 was not covered by tests
}

public static RascalToml parse(ISourceLocation from) throws IOException {
return parse(tomlFile(from));

Check warning on line 97 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L97

Added line #L97 was not covered by tests
}

private static ObjectReader tomlMapper = new TomlMapper()
.registerModule(new ParameterNamesModule())
.readerFor(RascalToml.class);

Check warning on line 102 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L100-L102

Added lines #L100 - L102 were not covered by tests

public static RascalToml parse(Reader from) throws IOException {
return tomlMapper.readValue(from);

Check warning on line 105 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L105

Added line #L105 was not covered by tests
}

public static RascalToml parse(InputStream from) throws IOException {
return tomlMapper.readValue(from);

Check warning on line 109 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L109

Added line #L109 was not covered by tests
}

private static final String TOML_LOCATION = "META_INF/rascal.toml";

private static InputStream tomlFile(JarInputStream stream) throws IOException {
try {
JarEntry next = null;

Check warning on line 116 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L116

Added line #L116 was not covered by tests
while ((next = stream.getNextJarEntry()) != null) {
if (next.getName().equals(TOML_LOCATION)) {
return stream; // the stream now behaves as an input stream for this file entry

Check warning on line 119 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L119

Added line #L119 was not covered by tests
}
}
throw new IOException("Could not find: " + TOML_LOCATION);
} catch (IOException e) {
throw new IOException("Error iterating jar for " + TOML_LOCATION, e);

Check warning on line 124 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L122-L124

Added lines #L122 - L124 were not covered by tests
}
}

private static InputStream tomlFile(File jarFile) throws IOException {
try (var file = new JarFile(jarFile)) {
return file.getInputStream(new ZipEntry(TOML_LOCATION));

Check warning on line 130 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L129-L130

Added lines #L129 - L130 were not covered by tests
}
catch (IOException e) {
throw new IOException("Error loading " + TOML_LOCATION +" from " + jarFile, e);

Check warning on line 133 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L132-L133

Added lines #L132 - L133 were not covered by tests
}
}

private static InputStream tomlFile(Class<?> clazz) throws IOException {
var result = clazz.getResourceAsStream("/" + TOML_LOCATION);

Check warning on line 138 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L138

Added line #L138 was not covered by tests
if (result == null) {
throw new IOException("Error loading " + TOML_LOCATION + " from " + clazz);

Check warning on line 140 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L140

Added line #L140 was not covered by tests
}
return result;

Check warning on line 142 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L142

Added line #L142 was not covered by tests
}

private static Reader tomlFile(ISourceLocation root) throws IOException {
try {
return URIResolverRegistry.getInstance()
.getCharacterReader(URIUtil.getChildLocation(RascalManifest.jarify(root), TOML_LOCATION));
} catch (IOException e) {
throw new IOException("Error loading " + TOML_LOCATION +" from " + root, e);

Check warning on line 150 in src/org/rascalmpl/interpreter/utils/RascalToml.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/utils/RascalToml.java#L147-L150

Added lines #L147 - L150 were not covered by tests
}
}

}
38 changes: 38 additions & 0 deletions test/org/rascalmpl/test/util/RascalTomlTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.rascalmpl.test.util;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.StringReader;
import java.util.Collections;

import org.junit.Test;
import org.rascalmpl.interpreter.utils.RascalToml;

public class RascalTomlTests {

private static RascalToml parse(String s) throws IOException {
System.out.println(s);
return RascalToml.parse(new StringReader(s));
}

@Test
public void parserWorks() throws IOException {
var toml = parse(
"version='1.0' \n" +

"[project]\n" +
"name = 'test-project'\n" +
"sources = [ 'src/main/rascal']\n" +
"libraries = [ '|lib://typepal|']\n" +

"[main]\n" +
"module = 'lang::testing::Mod'\n" +
"function = 'main'\n"
);
assertEquals("1.0", toml.getVersion());
assertEquals("test-project", toml.getProjectName());
assertEquals(Collections.singletonList("src/main/rascal"), toml.getSource());
assertEquals(Collections.singletonList("|lib://typepal|"), toml.getRequireLibraries());
}
}
Loading