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

refactor: introduced jbang-core library #1128

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ repositories {
}

dependencies {
implementation project(":jbang-core")

implementation 'com.offbytwo:docopt:0.6.0.20150202'

implementation 'org.apache.commons:commons-text:1.9'
Expand Down
22 changes: 22 additions & 0 deletions jbang-core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.classpath
.project
.vscode
.settings
target
.idea
*.iml
/build
.gradle
.factorypath
bin
homebrew-tap
RESULTS
*.db
jbang-action
out
node_modules
package-lock.json
*.jfr
itests/hello.java
*.class
CHANGELOG.md
37 changes: 37 additions & 0 deletions jbang-core/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
plugins {
id 'java'
id 'de.fuerstenau.buildconfig'
}

repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}

dependencies {
implementation 'info.picocli:picocli:4.6.1'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'org.jsoup:jsoup:1.13.1'
implementation 'io.quarkus.qute:qute-core:1.12.2.Final'
implementation 'org.jboss:jandex:2.2.3.Final'
implementation 'org.apache.commons:commons-compress:1.20'

implementation 'org.codehaus.plexus:plexus-java:1.0.6'
implementation 'kr.motd.maven:os-maven-plugin:1.7.0'

implementation 'com.github.jbangdev.jbang-resolver:shrinkwrap-resolver-api:3.1.5-allowpom'
implementation 'com.github.jbangdev.jbang-resolver:shrinkwrap-resolver-impl-maven:3.1.5-allowpom'

testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.1'
testImplementation 'org.hamcrest:hamcrest-library:2.2'
testImplementation 'com.github.stefanbirkner:system-rules:1.17.2'
}

test {
useJUnitPlatform()
}

group 'dev.jbang'
sourceCompatibility = '8'
targetCompatibility = '8'
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import java.nio.file.Paths;
import java.util.Optional;

import dev.jbang.Main;

import io.quarkus.qute.Engine;
import io.quarkus.qute.ReflectionValueResolver;
import io.quarkus.qute.Template;
Expand All @@ -36,7 +34,7 @@ public class TemplateEngine {
private URL locatePath(String path) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
cl = Main.class.getClassLoader();
cl = TemplateEngine.class.getClassLoader();
}
return cl.getResource(path);
}
Expand Down
52 changes: 52 additions & 0 deletions jbang-core/src/test/java/dev/jbang/BaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.jbang;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Rule;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;

import dev.jbang.util.Util;

public abstract class BaseTest {

@BeforeEach
void initEnv(@TempDir Path tempPath) throws IOException {
jbangTempDir = Files.createDirectory(tempPath.resolve("jbang"));
cwdDir = Files.createDirectory(tempPath.resolve("cwd"));
Util.setCwd(cwdDir);
environmentVariables.set(Settings.JBANG_DIR, jbangTempDir.toString());
environmentVariables.set(Settings.JBANG_CACHE_DIR, jbangTempDir.resolve("cache").toString());
environmentVariables.set(Settings.ENV_NO_VERSION_CHECK, "true");
if (Util.isWindows()) {
environmentVariables.set(Util.JBANG_RUNTIME_SHELL, "cmd");
}
}

public static final String EXAMPLES_FOLDER = "itests";
public static Path examplesTestFolder;

@BeforeAll
static void init() throws URISyntaxException {
URL examplesUrl = BaseTest.class.getClassLoader().getResource(EXAMPLES_FOLDER);
if (examplesUrl == null) {
examplesTestFolder = Paths.get(EXAMPLES_FOLDER).toAbsolutePath();
} else {
examplesTestFolder = Paths.get(new File(examplesUrl.toURI()).getAbsolutePath());
}
}

@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

public Path jbangTempDir;
public Path cwdDir;
}
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ plugins {
id "com.gradle.enterprise"
}


// Configuration of com.gradle.enterprise (build scan) plugin
gradleEnterprise {
buildScan {
Expand All @@ -20,3 +19,5 @@ gradleEnterprise {
//publishAlways()
}
}

include 'jbang-core'
Original file line number Diff line number Diff line change
@@ -1,64 +1,19 @@
package dev.jbang;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Function;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.junit.Rule;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;

import dev.jbang.cli.BaseCommand;
import dev.jbang.cli.JBang;
import dev.jbang.cli.TestRun;
import dev.jbang.util.Util;

import picocli.CommandLine;

public abstract class BaseTest {

@BeforeEach
void initEnv(@TempDir Path tempPath) throws IOException {
jbangTempDir = Files.createDirectory(tempPath.resolve("jbang"));
cwdDir = Files.createDirectory(tempPath.resolve("cwd"));
Util.setCwd(cwdDir);
environmentVariables.set(Settings.JBANG_DIR, jbangTempDir.toString());
environmentVariables.set(Settings.JBANG_CACHE_DIR, jbangTempDir.resolve("cache").toString());
environmentVariables.set(Settings.ENV_NO_VERSION_CHECK, "true");
if (Util.isWindows()) {
environmentVariables.set(Util.JBANG_RUNTIME_SHELL, "cmd");
}
}

public static final String EXAMPLES_FOLDER = "itests";
public static Path examplesTestFolder;

@BeforeAll
static void init() throws URISyntaxException, IOException {
URL examplesUrl = TestRun.class.getClassLoader().getResource(EXAMPLES_FOLDER);
if (examplesUrl == null) {
examplesTestFolder = Paths.get(EXAMPLES_FOLDER).toAbsolutePath();
} else {
examplesTestFolder = Paths.get(new File(examplesUrl.toURI()).getAbsolutePath());
}
}

@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

public Path jbangTempDir;
public Path cwdDir;
public abstract class BaseTestExt extends BaseTest {

protected <T> ExecutionResult checkedRun(Function<T, Integer> commandRunner, String... args) throws IOException {
JBang jbang = new JBang();
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/dev/jbang/cli/TestAddDeps.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import dev.jbang.BaseTest;
import dev.jbang.BaseTestExt;
import dev.jbang.util.Util;

class TestAddDeps extends BaseTest {
class TestAddDeps extends BaseTestExt {

String example = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"\n" +
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/dev/jbang/cli/TestAlias.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import dev.jbang.BaseTest;
import dev.jbang.BaseTestExt;
import dev.jbang.catalog.Alias;
import dev.jbang.catalog.Catalog;
import dev.jbang.catalog.CatalogUtil;
import dev.jbang.util.Util;

import picocli.CommandLine;

public class TestAlias extends BaseTest {
public class TestAlias extends BaseTestExt {

static final String aliases = "{\n" +
" \"aliases\": {\n" +
Expand Down