Skip to content

Commit

Permalink
GH-545 Replace HJSON with CDN (Resolve #545)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Aug 14, 2020
1 parent 055c52e commit d2c6725
Show file tree
Hide file tree
Showing 21 changed files with 46 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Panda sources
*.panda text
*.cdn text
*.hjson text

# Java sources
Expand Down
4 changes: 2 additions & 2 deletions examples/package_manager/panda-module-dependency/app.panda
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module panda-module-dependency

// require panda-dependency defined in the panda.hjson
// require panda-dependency defined in the panda.cdn
require panda-dependency

main {
// print panda-dependency message
log PandaDependency.getMessage()

// print subdependency (defined in panda-dependency/panda.hjson) message
// print subdependency (defined in panda-dependency/panda.cdn) message
log LDDependency.getMessage()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ scripts: {
}

dependencies: [
github:dzikoysk/[email protected].4
github:dzikoysk/[email protected].5
]

File renamed without changes.
6 changes: 0 additions & 6 deletions panda-framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@
<artifactId>panda-utilities</artifactId>
</dependency>

<!-- General -->
<dependency>
<groupId>org.hjson</groupId>
<artifactId>hjson</artifactId>
</dependency>

<!-- Benchmarks -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public <T> ContentJoiner join(Collection<T> elements, Function<T, ?> mapper) {
return this;
}

public ContentJoiner join(Object... elements) {
@SafeVarargs
public final <T> ContentJoiner join(T... elements) {
return join(Arrays.asList(elements));
}

Expand Down
4 changes: 4 additions & 0 deletions panda/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
</dependency>

<!-- General -->
<dependency>
<groupId>net.dzikoysk</groupId>
<artifactId>cdn</artifactId>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ protected String insertPrefix(String content, String prefix) {
lines[i] = CharacterUtils.BACKSPACE + prefix + lines[i];
}

//noinspection ConfusingArgumentToVarargsMethod
return ContentJoiner.on("\n")
.join(lines)
.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ public int hashCode() {
return Objects.hash(getOwner(), getName());
}

protected String getIdentifier() {
return getOwner() + "/" + getName();
}

protected String getVersion() {
return version;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.panda_lang.panda.manager;

import org.panda_lang.utilities.commons.FileUtils;
import org.panda_lang.utilities.commons.IOUtils;
import org.panda_lang.utilities.commons.ZipUtils;

Expand All @@ -37,8 +38,10 @@ final class GitHubInstall implements CustomInstall {

@Override
public List<Dependency> install(BiConsumer<InstallStatus, Dependency> resultConsumer, File ownerDirectory, File packageInfoFile) throws IOException {
String address = "https://github.com/" + dependency.getOwner() + "/" + dependency.getName() + "/archive/" + dependency.getVersion() + ".zip";
File packageDirectory = new File(ownerDirectory, dependency.getName());
FileUtils.delete(packageDirectory);

String address = "https://github.com/" + dependency.getOwner() + "/" + dependency.getName() + "/archive/" + dependency.getVersion() + ".zip";
BufferedInputStream in = null;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public List<Dependency> install(BiConsumer<InstallStatus, Dependency> resultCons
File dependencyFile = new File(dependencyDirectory, dependency.getName() + "-" + dependency.getVersion() + ".jar");
Files.copy(in, dependencyFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

// File dependencyDocument = new File(dependencyDirectory, "panda.hjson");
// File dependencyDocument = new File(dependencyDirectory, "panda.cdn");
// FileUtils.overrideFile(dependencyDocument, "name: " + dependency.getName() + "\nversion: " + dependency.getVersion());

manager.getFrameworkController().getClassLoader().addURL(dependencyFile.toURI().toURL());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,44 @@

package org.panda_lang.panda.manager;

import org.hjson.JsonObject;
import org.hjson.JsonValue;
import net.dzikoysk.cdn.CDN;
import net.dzikoysk.cdn.model.Configuration;
import org.jetbrains.annotations.Nullable;
import org.panda_lang.utilities.commons.function.Option;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

final class PackageDocument {

private final File document;
private final JsonObject content;
private final Configuration content;

PackageDocument(File document, JsonObject content) {
PackageDocument(File document, String source) {
this.document = document;
this.content = content;
this.content = CDN.defaultInstance().parse(source);
}

protected Dependency toDependency() {
return new Dependency("", getOwner(), getName(), getVersion());
}

private List<? extends String> getList(String name) {
return Option.of(content.get(name))
.map(object -> object.asArray().values().stream()
.map(JsonValue::asString)
.collect(Collectors.toList()))
.orElseGet(ArrayList::new);
}

private List<Dependency> getDependencies(String name) {
DependencyFactory factory = new DependencyFactory();
List<String> dependencies = content.getList(name);

if (dependencies == null) {
return Collections.emptyList();
}

return getList(name).stream()
return dependencies.stream()
.map(factory::createDependency)
.collect(Collectors.toList());
}

protected List<? extends String> getRepositories() {
return getList("repositories");
return content.getList("repositories");
}

protected List<Dependency> getTestsDependencies() {
Expand All @@ -69,20 +65,19 @@ protected List<Dependency> getDependencies() {
}

protected @Nullable String getMainScript() {
JsonValue scripts = content.get("scripts");
return scripts == null ? null : scripts.asObject().getString("main", null);
return content.getString("scripts.main");
}

protected String getOwner() {
return content.getString("owner", null);
return content.getString("owner");
}

protected String getVersion() {
return content.getString("version", null);
return content.getString("version");
}

protected String getName() {
return content.getString("name", null);
return content.getString("name");
}

protected File getPandaModules() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.panda_lang.panda.manager;

import org.hjson.JsonValue;
import org.panda_lang.utilities.commons.FileUtils;

import java.io.File;
Expand All @@ -31,7 +30,7 @@ final class PackageDocumentFile {
}

PackageDocument getContent() throws IOException {
return new PackageDocument(document, JsonValue.readHjson(FileUtils.getContentOfFile(document)).asObject());
return new PackageDocument(document, FileUtils.getContentOfFile(document));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class PackageManagerConstants {

public static final String MODULES = "panda_modules";

public static final String PACKAGE_INFO = "panda.hjson";
public static final String PACKAGE_INFO = "panda.cdn";

private PackageManagerConstants() { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void run() throws Exception {
return;
}

if (script.getName().endsWith("panda.hjson")) {
if (script.getName().endsWith("panda.cdn")) {
PackageManager packageManager = new PackageManager(panda, script.getParentFile());
packageManager.install(script);
packageManager.run(panda, script);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.panda_lang.panda.manager;

import org.hjson.JsonValue;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -35,16 +34,16 @@ static void prepare() {
"scripts: {\n" +
" main: app.panda\n" +
"}\n" +
"dependencies: [\n" +
"dependencies: {\n" +
" github:owner-one/[email protected]\n" +
" github:owner-three/[email protected]\n" +
" maven:org.panda-lang/[email protected]\n" +
"]\n" +
"tests-dependencies: [\n" +
"}\n" +
"tests-dependencies: {\n" +
" github:owner-two/[email protected]\n" +
"]\n";
"}\n";

document = new PackageDocument(new File("."), JsonValue.readHjson(value).asObject());
document = new PackageDocument(new File("."), value);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PandaModuleDependencyTest {
@Test
void test() throws Exception {
PandaUtils.printJVMUptime(MANAGER);
File document = new File(DIRECTORY, "panda.hjson");
File document = new File(DIRECTORY, "panda.cdn");

MANAGER.install(document);
MANAGER.run(document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PandaWithJavaLibraryDependencyTest {
@Test
void test() throws Exception {
PandaUtils.printJVMUptime(MANAGER);
File document = new File(DIRECTORY, "panda.hjson");
File document = new File(DIRECTORY, "panda.cdn");

MANAGER.install(document);
MANAGER.run(document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class PandaShellTest {

@Test
void test() throws Exception {
SHELL.invoke("--level=info", "../examples/package_manager/panda-module-dependency/panda.hjson");
SHELL.invoke("--level=info", "../examples/package_manager/panda-module-dependency/panda.cdn");
}

}
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@

<!-- General -->
<dependency>
<groupId>org.hjson</groupId>
<artifactId>hjson</artifactId>
<version>3.0.0</version>
<groupId>net.dzikoysk</groupId>
<artifactId>cdn</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
Expand Down

0 comments on commit d2c6725

Please sign in to comment.