diff --git a/LICENSE b/LICENSE index 4404db1..ae0ff2b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2018 Spencer Park +Copyright (c) ${year} ${author} Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. diff --git a/build.gradle b/build.gradle index 2c05250..4ce2c48 100644 --- a/build.gradle +++ b/build.gradle @@ -1,153 +1,96 @@ -plugins { - id 'java' - id 'maven-publish' - id('com.github.hierynomus.license') version '0.14.0' - id('io.github.spencerpark.jupyter-kernel-installer') version '2.1.0' - id('com.github.jk1.dependency-license-report') -} - +import com.github.jk1.license.filter.LicenseBundleNormalizer +import com.github.jk1.license.render.InventoryHtmlReportRenderer +import com.github.jk1.license.render.JsonReportRenderer import org.apache.tools.ant.filters.ReplaceTokens -import com.github.jk1.license.render.* -import com.github.jk1.license.filter.* -import io.github.spencerpark.gradle.* -group = 'io.github.spencerpark' -version = '1.3.0' - -wrapper { - gradleVersion = '4.8.1' - distributionType = Wrapper.DistributionType.ALL -} - -// Add the license header to source files -license { - header = file('LICENSE') - exclude '**/*.json' - mapping { - // Use a regular multiline comment rather than a javadoc comment - java = 'SLASHSTAR_STYLE' - } -} -build.dependsOn 'licenseFormat' -// Configures the license report generated for the dependencies. -licenseReport { - excludeGroups = [] - renderers = [ - // Generate a pretty HTML report that groups dependencies by their license. - new NewInventoryHtmlReportRenderer('dependencies.html'), - // TODO make sure ci verifies that all licenses are know to be allowed to redistribute before publishing - new JsonReportRenderer('dependencies.json') - ] - - // Group same licenses despite names being slightly different (ex. Apache 2.0 vs Apache version 2) - filters = [new LicenseBundleNormalizer()] - - configurations = ['compile'] -} - -compileJava { - sourceCompatibility = 1.9 - targetCompatibility = 1.9 +plugins { + id 'java' + // id 'maven-publish' // use johnrengelman.shadow instead + id 'com.github.hierynomus.license' version '0.16.1' + id "com.github.jk1.dependency-license-report" version "2.1" + id 'com.github.johnrengelman.shadow' version '7.1.2' +// id 'io.github.spencerpark.jupyter-kernel-installer' version '2.1.0' } -configurations { - shade - // transitive true to make sure that the dependencies of shade dependencies also get shaded - // into the jar - shade.transitive = true - compile.extendsFrom(shade) -} +group = 'io.github.spencerpark' +version = '1.3.1' repositories { + mavenLocal() mavenCentral() maven { url = 'https://oss.sonatype.org/content/repositories/snapshots/' } - mavenLocal() } dependencies { - shade group: 'io.github.spencerpark', name: 'jupyter-jvm-basekernel', version: '2.3.0' - - shade group: 'org.apache.ivy', name: 'ivy', version: '2.5.0-rc1' - //shade group: 'org.apache.maven', name: 'maven-settings-builder', version: '3.6.0' - shade group: 'org.apache.maven', name: 'maven-model-builder', version: '3.6.0' + implementation group: 'io.github.spencerpark', name: 'jupyter-jvm-basekernel', version: '2.3.0' + implementation group: 'org.apache.ivy', name: 'ivy', version: '2.5.0' + implementation group: 'org.apache.maven', name: 'maven-model-builder', version: '3.8.5' - testCompile group: 'junit', name: 'junit', version: '4.12' + testImplementation group: 'junit', name: 'junit', version: '4.13.2' } -jar { - //Include all shaded dependencies in the jar - from configurations.shade - .collect { it.isDirectory() ? it : zipTree(it) } - - manifest { - attributes('Main-class': 'io.github.spencerpark.ijava.IJava') - } +// Add the license header to source files +license { + header = file('LICENSE') + include "**/*.java" + exclude "**/Test*.java" + mapping java: 'SLASHSTAR_STYLE' + ext.year = Calendar.getInstance().get(Calendar.YEAR) } +licenseMain.dependsOn 'licenseFormat' +// replace @symbol@ in properties processResources { def tokens = [ 'version': project.version, 'project': project.name ] inputs.properties(tokens) - filter ReplaceTokens, tokens: tokens + filter tokens: tokens, ReplaceTokens } -publishing { - publications { - mavenJava(MavenPublication) { - from components.java - } +jar { + manifest { + attributes 'Main-class': 'io.github.spencerpark.ijava.IJava' } } -jupyter { - kernelName = 'java' - kernelDisplayName = 'Java' - kernelLanguage = 'java' - kernelInterruptMode = 'message' +// shadow Jar config +shadowJar { + // inherit from the manifest of the standard jar task + // manifest { + // attributes 'Main-class': 'io.github.spencerpark.ijava.IJava' + // } - kernelParameters { - list('classpath', 'IJAVA_CLASSPATH') { - separator = PATH_SEPARATOR - description = '''A file path separator delimited list of classpath entries that should be available to the user code. **Important:** no matter what OS, this should use forward slash "/" as the file separator. Also each path may actually be a simple glob.''' - } - - list('comp-opts', 'IJAVA_COMPILER_OPTS') { - separator = ' ' - description = '''A space delimited list of command line options that would be passed to the `javac` command when compiling a project. For example `-parameters` to enable retaining parameter names for reflection.''' - } - - list('startup-scripts-path', 'IJAVA_STARTUP_SCRIPTS_PATH') { - separator = PATH_SEPARATOR - description = '''A file path seperator delimited list of `.jshell` scripts to run on startup. This includes ijava-jshell-init.jshell and ijava-display-init.jshell. **Important:** no matter what OS, this should use forward slash "/" as the file separator. Also each path may actually be a simple glob.''' - } - - string('startup-script', 'IJAVA_STARTUP_SCRIPT') { - description = '''A block of java code to run when the kernel starts up. This may be something like `import my.utils;` to setup some default imports or even `void sleep(long time) { try {Thread.sleep(time); } catch (InterruptedException e) { throw new RuntimeException(e); }}` to declare a default utility method to use in the notebook.''' - } - - string('timeout', 'IJAVA_TIMEOUT') { - aliases NO_TIMEOUT: '-1' - description = '''A duration specifying a timeout (in milliseconds by default) for a _single top level statement_. If less than `1` then there is no timeout. If desired a time may be specified with a `TimeUnit` may be given following the duration number (ex `"30 SECONDS"`).''' - } + // copy build.gradle to shadowed jar + from("./") { + include 'build.gradle' } } -installKernel { - kernelInstallPath = commandLineSpecifiedPath(userInstallPath) -} - -zipKernel { - installers { - with 'python' - } +// publish +//publishing { +// publications { +// shadow(MavenPublication) { publication -> +// project.shadow.component(publication) +// } +// } +// repositories { +// maven { +// url "http://repo.myorg.com" +// } +// } +//} + +// create license report +licenseReport { + renderers = [ + new InventoryHtmlReportRenderer('license-report.html'), + new JsonReportRenderer('license-report.json') + ] - from(generateLicenseReport.outputFolder) { - into 'dependency-licenses' - } + filters = [new LicenseBundleNormalizer()] } -zipKernel.dependsOn 'generateLicenseReport' \ No newline at end of file diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle deleted file mode 100644 index bb112f9..0000000 --- a/buildSrc/build.gradle +++ /dev/null @@ -1,13 +0,0 @@ -plugins { - id 'groovy' -} - -repositories { - maven { - url = 'https://plugins.gradle.org/m2/' - } -} - -dependencies { - compile group: 'com.github.jk1.dependency-license-report', name: 'com.github.jk1.dependency-license-report.gradle.plugin', version: '1.1' -} \ No newline at end of file diff --git a/buildSrc/src/main/groovy/io/github/spencerpark/gradle/DeclaredModuleInfo.groovy b/buildSrc/src/main/groovy/io/github/spencerpark/gradle/DeclaredModuleInfo.groovy deleted file mode 100644 index 4216c1a..0000000 --- a/buildSrc/src/main/groovy/io/github/spencerpark/gradle/DeclaredModuleInfo.groovy +++ /dev/null @@ -1,7 +0,0 @@ -package io.github.spencerpark.gradle - -class DeclaredModuleInfo { - String projectUrl - String license - String licenseUrl -} diff --git a/buildSrc/src/main/groovy/io/github/spencerpark/gradle/InventoryReportRenderer.groovy b/buildSrc/src/main/groovy/io/github/spencerpark/gradle/InventoryReportRenderer.groovy deleted file mode 100644 index a15d2bb..0000000 --- a/buildSrc/src/main/groovy/io/github/spencerpark/gradle/InventoryReportRenderer.groovy +++ /dev/null @@ -1,91 +0,0 @@ -package io.github.spencerpark.gradle - -import com.github.jk1.license.* -import groovy.json.JsonParserType -import groovy.json.JsonSlurper - -abstract class InventoryReportRenderer { - /** - * Collect declared from a JSON object with maven coordinates as keys and {@link DeclaredModuleInfo} - * as values - * @param declarations the stream to parse the json object from - * @return the collected declarations - */ - static Map parseDeclarations(InputStream declarations) { - def rawDeclarations = new JsonSlurper() - .setType(JsonParserType.LAX) - .parse(declarations) - - assert rawDeclarations instanceof Map: "Declaration spec must be a json object" - - return rawDeclarations.collectEntries([:]) { coords, spec -> - assert spec instanceof Map: "Declaration spec for $coords is not an object" - - DeclaredModuleInfo info = new DeclaredModuleInfo() - spec.forEach { String key, val -> - assert val instanceof String: "Declaration spec for $coords::$key must be a string" - assert info.hasProperty(key), "Declaration spec for $coords has unknown key $key" - - info[key] = val - } - assert info.projectUrl: "Declaration missing required key: projectUrl" - assert info.license: "Declaration missing required key: license" - assert info.licenseUrl: "Declaration missing required key: licenseUrl" - - return [(coords): info] - } - } - - static Map> collectModulesByLicenseName(ProjectData data, Map declared) { - Map> modulesByLicense = [:] - - def addModule = { String licenseName, ModuleData module -> - String coords = module.with { "$group:$name:$version" } - - if (licenseName == "Unknown" && declared.containsKey(coords)) - licenseName = declared[coords].license - - modulesByLicense.compute(licenseName) { k, modules -> - return (modules ?: []) << module - } - } - - data.allDependencies.each { module -> - if (module.poms.isEmpty()) { - addModule(module.licenseFiles.isEmpty() ? "Unknown" : "Embedded", module) - return - } - - PomData pom = module.poms.first() - if (pom.licenses.isEmpty()) { - addModule(module.licenseFiles.isEmpty() ? "Unknown" : "Embedded", module) - } else { - pom.licenses.each { License license -> - addModule(license.name, module) - } - } - } - - return modulesByLicense - } - - // imported modules are things declared as dependencies but not actually included via gradle means. For - // example a javascript dependency. - static Map>> collectModulesByLicenseFromImported(ProjectData data) { - Map>> externalByModulesByLicense = [:] - - data.importedModules.each { ImportedModuleBundle module -> - Map> modulesByLicense = [:] - - module.modules.each { ImportedModuleData moduleData -> - modulesByLicense.compute(moduleData.license) { k, modules -> - return (modules ?: []) << moduleData - } - } - - externalByModulesByLicense[module.name] = modulesByLicense - } - - return externalByModulesByLicense - } -} diff --git a/buildSrc/src/main/groovy/io/github/spencerpark/gradle/NewInventoryHtmlReportRenderer.groovy b/buildSrc/src/main/groovy/io/github/spencerpark/gradle/NewInventoryHtmlReportRenderer.groovy deleted file mode 100644 index 5182943..0000000 --- a/buildSrc/src/main/groovy/io/github/spencerpark/gradle/NewInventoryHtmlReportRenderer.groovy +++ /dev/null @@ -1,267 +0,0 @@ -package io.github.spencerpark.gradle - -import com.github.jk1.license.* -import com.github.jk1.license.render.ReportRenderer -import groovy.text.SimpleTemplateEngine -import groovy.text.StreamingTemplateEngine - -class NewInventoryHtmlReportRenderer implements ReportRenderer { - private final String name - private final String fileName - private final Map declared - private final Map colors = [ - accent : '#F37726', - primary : 'white', - accentBg : '#616262', - primaryBg: '#989798', - darkText : '#4E4E4E', - lightText: '#e8e5e5', - ] - - private Writer output - private int counter - - NewInventoryHtmlReportRenderer(String fileName = 'index.html', String name = null, File declarationsFileName = null, Map colors = [:]) { - this.name = name - this.fileName = fileName - - if (declarationsFileName) - declared = InventoryReportRenderer.parseDeclarations(declarationsFileName.newInputStream()) - else - declared = [:] - - this.colors.putAll(colors) - } - - @Override - void render(ProjectData data) { - this.counter = 0 - - def project = data.project - def name = project.name - LicenseReportExtension config = project.licenseReport - def outFile = new File(config.outputDir, fileName) - - def stylesheet = NewInventoryHtmlReportRenderer.class.getResourceAsStream("/license-report.template.css").withReader { - new SimpleTemplateEngine() - .createTemplate(it) - .make(this.colors) - .writeTo(new StringWriter()) - .toString() - } - def template = NewInventoryHtmlReportRenderer.class.getResourceAsStream("/license-report.template.html").withReader { - new StreamingTemplateEngine().createTemplate(it) - } - - def binding = [ - stylesheet : stylesheet, - name : name, - project : project, - inventory : InventoryReportRenderer.collectModulesByLicenseName(data, declared), - externalInventories : InventoryReportRenderer.collectModulesByLicenseFromImported(data), - serializeHref : { String... values -> - values.findAll { it != null }.collect { it.replaceAll(/\s/, '_') }.join('_') - }, - printDependency : this.&printDependency, - printImportedDependency: this.&printImportedDependency, - ] - - outFile.withWriter { - template.make(binding).writeTo(it) - } - } - - void tag(Map attrs = [:], String name, def children) { - output << "<$name ${attrs.collect { k, v -> "$k=\"$v\"" }.join(" ")}>\n" - if (children.respondsTo("call")) - children.call() - else - text(children) - output << "\n" - } - - void div(Map attrs = [:], def children) { - tag(attrs, "div", children) - } - - void p(Map attrs = [:], def children) { - tag(attrs, "p", children) - } - - void a(Map attrs = [:], def children) { - tag(attrs, "a", children) - } - - void strong(Map attrs = [:], def children) { - tag(attrs, "strong", children) - } - - void ul(Map attrs = [:], def children) { - tag(attrs, "ul", children) - } - - void li(Map attrs = [:], def children) { - tag(attrs, "li", children) - } - - void text(def contents) { - if (contents != null) - output << String.valueOf(contents) - } - - void renderDependencyProperty(String label, def children) { - div(class: "dependency-prop") { - tag("label") { text(label) } - div(class: "dependency-value", children) - } - } - - void renderDependencyTitle(ModuleData data) { - p(class: "title") { - strong(class: "index", "${++counter}.") - - if (data.group) { - strong("Group: ") - text(data.group) - } - - if (data.name) { - strong("Name: ") - text(data.name) - } - - if (data.version) { - strong("Version: ") - text(data.version) - } - } - } - - void renderDependencyTitle(ImportedModuleData data) { - p(class: "title") { - strong(class: "index", ++counter) - - if (data.name) { - strong("Name: ") - text(data.name) - } - - if (data.version) { - strong("Version: ") - text(data.version) - } - } - } - - void renderDependencyProjectUrl(ModuleData data) { - String coords = data.with { "$group:$name:$version" } - - ManifestData manifest = data.manifests.isEmpty() ? null : data.manifests.first() - PomData pomData = data.poms.isEmpty() ? null : data.poms.first() - - if (manifest?.url && pomData?.projectUrl && manifest.url == pomData.projectUrl) { - renderDependencyProperty("Project URL") { - a(href: manifest.url, { text(manifest.url) }) - } - } else if (manifest?.url || pomData?.projectUrl) { - if (manifest?.url) { - renderDependencyProperty("Manifest Project URL") { - a(href: manifest.url, { text(manifest.url) }) - } - } - - if (pomData?.projectUrl) { - renderDependencyProperty("POM Project URL") { - a(href: pomData.projectUrl, { text(pomData.projectUrl) }) - } - } - } else if (declared.containsKey(coords)) { - renderDependencyProperty("Project URL") { - a(href: declared[coords].projectUrl, { text(declared[coords].projectUrl) }) - } - } - } - - void renderReferencedLicenses(ModuleData data) { - String coords = data.with { "$group:$name:$version" } - - ManifestData manifest = data.manifests.isEmpty() ? null : data.manifests.first() - PomData pomData = data.poms.isEmpty() ? null : data.poms.first() - - if (manifest?.license || pomData?.licenses) { - if (manifest?.license) { - if (manifest.license.startsWith("http")) { - renderDependencyProperty("Manifest license URL") { - a(href: manifest.license, { text(manifest.license) }) - } - } else if (manifest.hasPackagedLicense) { - renderDependencyProperty("Packaged License File") { - a(href: manifest.license, { text(manifest.url) }) - } - } else { - renderDependencyProperty("Manifest License") { - text("${manifest.license} (Not Packaged)") - } - } - } - - if (pomData?.licenses) { - pomData.licenses.each { License license -> - if (license.url) { - renderDependencyProperty("POM License") { - text("${license.name} - ") - if (license.url.startsWith("http")) - a(href: license.url, { text(license.url) }) - else - text(license.url) - } - } else { - renderDependencyProperty("POM License", { text(license.name) }) - } - } - } - } else if (declared.containsKey(coords)) { - renderDependencyProperty("License URL") { - a(href: declared[coords].licenseUrl, { text(declared[coords].license) }) - } - } - } - - void renderIncludedLicenses(ModuleData data) { - if (!data.licenseFiles.isEmpty() && !data.licenseFiles.first().fileDetails.isEmpty()) { - renderDependencyProperty("Embedded license files") { - ul { - data.licenseFiles.first().fileDetails.each { - def file = it.file - li { - a(href: file, { text(file) }) - } - } - } - } - } - } - - private void printDependency(Writer out, ModuleData data) { - this.output = out - div(class: "dependency") { - renderDependencyTitle(data) - renderDependencyProjectUrl(data) - renderReferencedLicenses(data) - renderIncludedLicenses(data) - } - } - - private printImportedDependency(Writer out, ImportedModuleData data) { - this.output = out - div(class: "dependency") { - renderDependencyTitle(data) - renderDependencyProperty("Project URL") { - a(href: data.projectUrl, { text(data.projectUrl) }) - } - renderDependencyProperty("License URL") { - a(href: data.licenseUrl, { text(data.license) }) - } - } - } -} diff --git a/buildSrc/src/main/resources/license-report.template.css b/buildSrc/src/main/resources/license-report.template.css deleted file mode 100644 index d6bbcdb..0000000 --- a/buildSrc/src/main/resources/license-report.template.css +++ /dev/null @@ -1,151 +0,0 @@ -@media print { - .inventory { - display: none; - } - - .content { - position: static !important; - } -} - -html, body, section { - height: 100%; -} - -body { - font-family: sans-serif; - line-height: 125%; - margin: 0; - background: $primaryBg; -} - -.header { - /* #22aa44 */ - background: $accent; - color: $darkText; - padding: 2em 1em 1em 1em; -} - -.header h1 { - font-size: 16pt; - margin: 0.5em 0; -} - -.header h2 { - font-size: 10pt; - margin: 0; -} - -.container { -} - -.inventory { - background: $accentBg; - color: $lightText; - padding: 0; - position: fixed; - left: 0; - top: 0; - height: 100%; - width: 25%; - overflow: auto; -} - -.inventory ul { - margin: 0; - padding: 0; -} - -.inventory li { - list-style: none; - padding: 0; - margin: 0; -} - -.inventory li a { - width: 100%; - box-sizing: border-box; - color: $lightText; - text-decoration: none; - display: flex; - flex-direction: row; - padding: 0.938em 0.750em; -} - -.inventory li a:hover { - background: rgba(50, 50, 50, 0.5); - color: white; -} - -.inventory .section-heading { - background: rgba(50, 50, 50, 0.25); - padding-left: 0.5em; - margin: 0; - padding-top: 1em; - padding-bottom: 1em; -} - -.license .license-name { - flex-grow: 1; -} - -.license .badge { - background: $accent; - padding: 0.625em 0.938em; - border-radius: 1.250em; - color: $darkText; - display: inline-table; -} - -.content { - padding: 0 1rem; - position: absolute; - top: 0; - bottom: 0; - left: 25%; - width: 75%; - box-sizing: border-box; -} - -.content h1 { - color: $darkText; - background-color: $accent; - padding: 0.67em; - margin: 0 -1rem; -} - -.dependency { - background: white; - padding: 1em; - margin-bottom: 1em; -} - -.dependency:hover { - box-shadow: dimgrey 0.2em 0.2em 0.2em 0em; -} - -.dependency .index { - font-size: larger; - font-weight: lighter; -} - -.dependency-prop { - padding: 0.3em; -} - -.dependency-prop:hover { - background: rgba(50, 50, 50, 0.25); -} - -.dependency-prop label { - font-weight: bold; -} - -.dependency-value { - padding-left: 1em; -} - -.dependency-value ul { - margin-top: 0; - margin-bottom: 0; -} diff --git a/buildSrc/src/main/resources/license-report.template.html b/buildSrc/src/main/resources/license-report.template.html deleted file mode 100644 index 1a714d2..0000000 --- a/buildSrc/src/main/resources/license-report.template.html +++ /dev/null @@ -1,71 +0,0 @@ - - - Dependency License Report for $name - - - -
- - -
-
-

${project.name} ${!"unspecified".equals(project.version) ? project.version : ""}

-

Dependency License Report

-

- ${new Date().format("yyyy-MM-dd HH:mm:ss z")} -

-
- -

${name}

- - - <% externalInventories.each { name, modules -> %> -

${name}

- - <% } %> -
- - -
-

${name}

- <% inventory.keySet().sort().each { license -> %> - -

${license}

- <% inventory[license].sort({ a, b -> a.group <=> b.group }).each { data -> - printDependency.call(out, data) - } %> - <% } %> - - <% externalInventories.keySet().sort().each { String name -> %> -

${name}

- <% externalInventories[name].each { String license, dependencies -> %> - - <% dependencies.each { importedData -> - printImportedDependency.call(out, importedData) - } %> - <% } %> - <% } %> -
-
- - \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0c28a15..44ff29f 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,4 +1,4 @@ -distributionUrl=https\://services.gradle.org/distributions/gradle-4.8.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStorePath=wrapper/dists diff --git a/src/main/java/io/github/spencerpark/ijava/IJava.java b/src/main/java/io/github/spencerpark/ijava/IJava.java index 05dcc91..ef47218 100644 --- a/src/main/java/io/github/spencerpark/ijava/IJava.java +++ b/src/main/java/io/github/spencerpark/ijava/IJava.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Spencer Park + * Copyright (c) 2022 ${author} * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/io/github/spencerpark/ijava/JavaKernel.java b/src/main/java/io/github/spencerpark/ijava/JavaKernel.java index b61d3af..1c4638c 100644 --- a/src/main/java/io/github/spencerpark/ijava/JavaKernel.java +++ b/src/main/java/io/github/spencerpark/ijava/JavaKernel.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Spencer Park + * Copyright (c) 2022 ${author} * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -30,8 +30,8 @@ import io.github.spencerpark.jupyter.kernel.LanguageInfo; import io.github.spencerpark.jupyter.kernel.ReplacementOptions; import io.github.spencerpark.jupyter.kernel.display.DisplayData; -import io.github.spencerpark.jupyter.kernel.magic.registry.Magics; import io.github.spencerpark.jupyter.kernel.magic.common.Load; +import io.github.spencerpark.jupyter.kernel.magic.registry.Magics; import io.github.spencerpark.jupyter.kernel.util.CharPredicate; import io.github.spencerpark.jupyter.kernel.util.StringStyler; import io.github.spencerpark.jupyter.kernel.util.TextColor; diff --git a/src/main/java/io/github/spencerpark/ijava/execution/CodeEvaluator.java b/src/main/java/io/github/spencerpark/ijava/execution/CodeEvaluator.java index 4583b31..490a272 100644 --- a/src/main/java/io/github/spencerpark/ijava/execution/CodeEvaluator.java +++ b/src/main/java/io/github/spencerpark/ijava/execution/CodeEvaluator.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Spencer Park + * Copyright (c) 2022 ${author} * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/io/github/spencerpark/ijava/execution/CodeEvaluatorBuilder.java b/src/main/java/io/github/spencerpark/ijava/execution/CodeEvaluatorBuilder.java index 0ff23e7..71f7358 100644 --- a/src/main/java/io/github/spencerpark/ijava/execution/CodeEvaluatorBuilder.java +++ b/src/main/java/io/github/spencerpark/ijava/execution/CodeEvaluatorBuilder.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Spencer Park + * Copyright (c) 2022 ${author} * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,6 +23,7 @@ */ package io.github.spencerpark.ijava.execution; +import io.github.spencerpark.ijava.utils.FileUtils; import io.github.spencerpark.jupyter.kernel.util.GlobFinder; import jdk.jshell.JShell; @@ -43,13 +44,13 @@ public class CodeEvaluatorBuilder { private static final OutputStream STDERR = new LazyOutputStreamDelegate(() -> System.err); private static final InputStream STDIN = new LazyInputStreamDelegate(() -> System.in); - private String timeout; private final List classpath; private final List compilerOpts; + private final List startupScripts; + private String timeout; private PrintStream out; private PrintStream err; private InputStream in; - private List startupScripts; public CodeEvaluatorBuilder() { this.classpath = new LinkedList<>(); @@ -147,6 +148,15 @@ public CodeEvaluatorBuilder startupScript(InputStream scriptStream) { } public CodeEvaluatorBuilder startupScriptFiles(String paths) { + // todo debug + try { + String glob1 = "glob:*"; + String path1 = "."; + System.out.println("------------- startup: " + FileUtils.listMatchedFilePath(glob1, path1)); + } catch (IOException e) { + e.printStackTrace(); + } + if (paths == null) return this; if (BLANK.matcher(paths).matches()) return this; @@ -173,7 +183,7 @@ public CodeEvaluatorBuilder startupScriptFile(Path path) { return this; try { - String script = new String(Files.readAllBytes(path), "UTF-8"); + String script = Files.readString(path); this.startupScripts.add(script); } catch (IOException e) { throw new RuntimeException(String.format("IOException while loading startup script for '%s': %s", path, e.getMessage()), e); diff --git a/src/main/java/io/github/spencerpark/ijava/execution/CompilationException.java b/src/main/java/io/github/spencerpark/ijava/execution/CompilationException.java index 33777f4..c1d353f 100644 --- a/src/main/java/io/github/spencerpark/ijava/execution/CompilationException.java +++ b/src/main/java/io/github/spencerpark/ijava/execution/CompilationException.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Spencer Park + * Copyright (c) 2022 ${author} * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/io/github/spencerpark/ijava/execution/EvaluationInterruptedException.java b/src/main/java/io/github/spencerpark/ijava/execution/EvaluationInterruptedException.java index a1382fa..78ffa11 100644 --- a/src/main/java/io/github/spencerpark/ijava/execution/EvaluationInterruptedException.java +++ b/src/main/java/io/github/spencerpark/ijava/execution/EvaluationInterruptedException.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022 ${author} + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package io.github.spencerpark.ijava.execution; public class EvaluationInterruptedException extends Exception { diff --git a/src/main/java/io/github/spencerpark/ijava/execution/EvaluationTimeoutException.java b/src/main/java/io/github/spencerpark/ijava/execution/EvaluationTimeoutException.java index 9765e31..1ff05e0 100644 --- a/src/main/java/io/github/spencerpark/ijava/execution/EvaluationTimeoutException.java +++ b/src/main/java/io/github/spencerpark/ijava/execution/EvaluationTimeoutException.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Spencer Park + * Copyright (c) 2022 ${author} * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/io/github/spencerpark/ijava/execution/IJavaExecutionControl.java b/src/main/java/io/github/spencerpark/ijava/execution/IJavaExecutionControl.java index 5f536f8..e82e2f0 100644 --- a/src/main/java/io/github/spencerpark/ijava/execution/IJavaExecutionControl.java +++ b/src/main/java/io/github/spencerpark/ijava/execution/IJavaExecutionControl.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Spencer Park + * Copyright (c) 2022 ${author} * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/io/github/spencerpark/ijava/execution/IJavaExecutionControlProvider.java b/src/main/java/io/github/spencerpark/ijava/execution/IJavaExecutionControlProvider.java index ac12319..7e9aa74 100644 --- a/src/main/java/io/github/spencerpark/ijava/execution/IJavaExecutionControlProvider.java +++ b/src/main/java/io/github/spencerpark/ijava/execution/IJavaExecutionControlProvider.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Spencer Park + * Copyright (c) 2022 ${author} * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/io/github/spencerpark/ijava/execution/IncompleteSourceException.java b/src/main/java/io/github/spencerpark/ijava/execution/IncompleteSourceException.java index 263b7b0..79861da 100644 --- a/src/main/java/io/github/spencerpark/ijava/execution/IncompleteSourceException.java +++ b/src/main/java/io/github/spencerpark/ijava/execution/IncompleteSourceException.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Spencer Park + * Copyright (c) 2022 ${author} * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/io/github/spencerpark/ijava/execution/LazyInputStreamDelegate.java b/src/main/java/io/github/spencerpark/ijava/execution/LazyInputStreamDelegate.java index 8f270e2..bbb3767 100644 --- a/src/main/java/io/github/spencerpark/ijava/execution/LazyInputStreamDelegate.java +++ b/src/main/java/io/github/spencerpark/ijava/execution/LazyInputStreamDelegate.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Spencer Park + * Copyright (c) 2022 ${author} * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/io/github/spencerpark/ijava/execution/LazyOutputStreamDelegate.java b/src/main/java/io/github/spencerpark/ijava/execution/LazyOutputStreamDelegate.java index 535540c..ca488e0 100644 --- a/src/main/java/io/github/spencerpark/ijava/execution/LazyOutputStreamDelegate.java +++ b/src/main/java/io/github/spencerpark/ijava/execution/LazyOutputStreamDelegate.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Spencer Park + * Copyright (c) 2022 ${author} * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/io/github/spencerpark/ijava/execution/MagicsSourceTransformer.java b/src/main/java/io/github/spencerpark/ijava/execution/MagicsSourceTransformer.java index 5694641..7f1d644 100644 --- a/src/main/java/io/github/spencerpark/ijava/execution/MagicsSourceTransformer.java +++ b/src/main/java/io/github/spencerpark/ijava/execution/MagicsSourceTransformer.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Spencer Park + * Copyright (c) 2022 ${author} * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/io/github/spencerpark/ijava/magics/ClasspathMagics.java b/src/main/java/io/github/spencerpark/ijava/magics/ClasspathMagics.java index 3f6ddb8..796c10e 100644 --- a/src/main/java/io/github/spencerpark/ijava/magics/ClasspathMagics.java +++ b/src/main/java/io/github/spencerpark/ijava/magics/ClasspathMagics.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022 ${author} + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package io.github.spencerpark.ijava.magics; import io.github.spencerpark.jupyter.kernel.magic.registry.LineMagic; @@ -6,7 +29,6 @@ import java.io.IOException; import java.util.List; import java.util.function.Consumer; -import java.util.stream.Collectors; import java.util.stream.StreamSupport; public class ClasspathMagics { @@ -28,7 +50,7 @@ public List jars(List args) { } }) .map(p -> p.toAbsolutePath().toString()) - .collect(Collectors.toList()); + .toList(); jars.forEach(this.addToClasspath); @@ -47,7 +69,7 @@ public List classpath(List args) { } }) .map(p -> p.toAbsolutePath().toString()) - .collect(Collectors.toList()); + .toList(); paths.forEach(this.addToClasspath); diff --git a/src/main/java/io/github/spencerpark/ijava/magics/MavenResolver.java b/src/main/java/io/github/spencerpark/ijava/magics/MavenResolver.java index b0014bc..fa32d67 100644 --- a/src/main/java/io/github/spencerpark/ijava/magics/MavenResolver.java +++ b/src/main/java/io/github/spencerpark/ijava/magics/MavenResolver.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Spencer Park + * Copyright (c) 2022 ${author} * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/io/github/spencerpark/ijava/magics/dependencies/CommonRepositories.java b/src/main/java/io/github/spencerpark/ijava/magics/dependencies/CommonRepositories.java index 52b3138..3973676 100644 --- a/src/main/java/io/github/spencerpark/ijava/magics/dependencies/CommonRepositories.java +++ b/src/main/java/io/github/spencerpark/ijava/magics/dependencies/CommonRepositories.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022 ${author} + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package io.github.spencerpark.ijava.magics.dependencies; import org.apache.ivy.plugins.resolver.DependencyResolver; diff --git a/src/main/java/io/github/spencerpark/ijava/magics/dependencies/Maven.java b/src/main/java/io/github/spencerpark/ijava/magics/dependencies/Maven.java index 2e3fa23..5f321b8 100644 --- a/src/main/java/io/github/spencerpark/ijava/magics/dependencies/Maven.java +++ b/src/main/java/io/github/spencerpark/ijava/magics/dependencies/Maven.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022 ${author} + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package io.github.spencerpark.ijava.magics.dependencies; import org.apache.maven.building.StringSource; @@ -93,8 +116,7 @@ private Path getUserHomePath() { private Path getGlobalHomePath() { String envM2Home = this.getEnv("M2_HOME"); - return envM2Home != null - ? Paths.get(envM2Home).toAbsolutePath() : null; + return envM2Home != null ? Paths.get(envM2Home).toAbsolutePath() : null; } private Path getUserSettingsPath() { diff --git a/src/main/java/io/github/spencerpark/ijava/magics/dependencies/MavenToIvy.java b/src/main/java/io/github/spencerpark/ijava/magics/dependencies/MavenToIvy.java index f8238a7..5db6631 100644 --- a/src/main/java/io/github/spencerpark/ijava/magics/dependencies/MavenToIvy.java +++ b/src/main/java/io/github/spencerpark/ijava/magics/dependencies/MavenToIvy.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022 ${author} + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package io.github.spencerpark.ijava.magics.dependencies; import org.apache.ivy.plugins.resolver.ChainResolver; diff --git a/src/main/java/io/github/spencerpark/ijava/runtime/Display.java b/src/main/java/io/github/spencerpark/ijava/runtime/Display.java index 84f572e..885674a 100644 --- a/src/main/java/io/github/spencerpark/ijava/runtime/Display.java +++ b/src/main/java/io/github/spencerpark/ijava/runtime/Display.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022 ${author} + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package io.github.spencerpark.ijava.runtime; import io.github.spencerpark.ijava.JavaKernel; diff --git a/src/main/java/io/github/spencerpark/ijava/runtime/Kernel.java b/src/main/java/io/github/spencerpark/ijava/runtime/Kernel.java index 595d585..ab36ee1 100644 --- a/src/main/java/io/github/spencerpark/ijava/runtime/Kernel.java +++ b/src/main/java/io/github/spencerpark/ijava/runtime/Kernel.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022 ${author} + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package io.github.spencerpark.ijava.runtime; import io.github.spencerpark.ijava.IJava; diff --git a/src/main/java/io/github/spencerpark/ijava/runtime/Magics.java b/src/main/java/io/github/spencerpark/ijava/runtime/Magics.java index 384d7ae..c2f9dea 100644 --- a/src/main/java/io/github/spencerpark/ijava/runtime/Magics.java +++ b/src/main/java/io/github/spencerpark/ijava/runtime/Magics.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022 ${author} + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package io.github.spencerpark.ijava.runtime; import io.github.spencerpark.ijava.IJava; diff --git a/src/main/java/io/github/spencerpark/ijava/utils/FileUtils.java b/src/main/java/io/github/spencerpark/ijava/utils/FileUtils.java new file mode 100644 index 0000000..32abfda --- /dev/null +++ b/src/main/java/io/github/spencerpark/ijava/utils/FileUtils.java @@ -0,0 +1,63 @@ +package io.github.spencerpark.ijava.utils; + +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.events.StartElement; +import javax.xml.stream.events.XMLEvent; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.*; + +public class FileUtils { + private FileUtils() { + // hide + } + + public static Map readXmlElementText(String filePath, Collection elementNames) throws FileNotFoundException, XMLStreamException { + Map result = new HashMap<>(); + XMLInputFactory xmlInputFactory = XMLInputFactory.newDefaultFactory(); + XMLEventReader reader = xmlInputFactory.createXMLEventReader(new FileInputStream(filePath)); + + while (reader.hasNext()) { + XMLEvent nextEvent = reader.nextEvent(); + + if (nextEvent.isStartElement()) { + StartElement startElement = nextEvent.asStartElement(); + String elementName = startElement.getName().getLocalPart(); + if (elementNames.contains(elementName)) { + // if startElement, then next to textData + nextEvent = reader.nextEvent(); + + result.put(elementName, nextEvent.asCharacters().getData()); + } + } + } + + return result; + } + + public static Collection listMatchedFilePath(String glob, String location) throws IOException { + // String startFolder = new File(FileUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParentFile().getPath(); + final List matchedPath = new ArrayList<>(); + final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(glob); + Files.walkFileTree(Path.of(location), new HashSet<>(2), 5, new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) { + if (pathMatcher.matches(path) && Files.isReadable(path)) { + matchedPath.add(path); + } + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFileFailed(Path file, IOException exc) { + return FileVisitResult.CONTINUE; + } + }); + return matchedPath; + } +} diff --git a/src/main/resources/ijava-jshell-init.jshell b/src/main/resources/ijava-jshell-init.jshell index 9e944c1..8ee7787 100644 --- a/src/main/resources/ijava-jshell-init.jshell +++ b/src/main/resources/ijava-jshell-init.jshell @@ -12,4 +12,4 @@ import static io.github.spencerpark.ijava.runtime.Magics.*; public void printf(String format, Object... args) { System.out.printf(format, args); -} \ No newline at end of file +} diff --git a/src/test/java/io/github/spencerpark/ijava/TestUtils.java b/src/test/java/io/github/spencerpark/ijava/TestUtils.java new file mode 100644 index 0000000..b4cb081 --- /dev/null +++ b/src/test/java/io/github/spencerpark/ijava/TestUtils.java @@ -0,0 +1,57 @@ +package io.github.spencerpark.ijava; + +import io.github.spencerpark.ijava.utils.FileUtils; +import org.junit.Assert; +import org.junit.Test; + +import javax.xml.stream.XMLStreamException; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.*; + +public class TestUtils { + public static List match(String glob, String location) throws IOException { + final List matchedPath = new ArrayList<>(); + final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(glob); + Files.walkFileTree(Path.of(location), new HashSet<>(2), 5, new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) { + if (pathMatcher.matches(path) && Files.isReadable(path)) { + matchedPath.add(path); + } + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFileFailed(Path file, IOException exc) { + return FileVisitResult.CONTINUE; + } + }); + return matchedPath; + } + + @Test + public void testFileGlob() throws IOException { + String glob = "glob:**/*.zip"; + String path = "E:/flink-platform-dev"; + //path = "."; + //glob = "glob:*"; + System.out.println(File.pathSeparator); + //List match = match(glob, path); + //Assert.assertNotNull(match); + } + + @Test + public void testReadXml() throws XMLStreamException, FileNotFoundException { + String filePath = "D:\\Maven\\apache-maven-3.6.3\\conf\\settings.xml"; + Set elementNames = Collections.singleton("localRepository"); + Map elementTextData = FileUtils.readXmlElementText(filePath, elementNames); + Assert.assertNotNull(elementTextData); + for (String elementName : elementNames) { + Assert.assertEquals(elementTextData.get(elementName), "D:\\Maven\\repository"); + } + } +}