Skip to content

Commit

Permalink
New !URI syntax for remote data request ($LATEST$) on template variables
Browse files Browse the repository at this point in the history
  • Loading branch information
duckAsteroid committed Nov 29, 2019
1 parent 9ff52e6 commit 0e09faf
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 16 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = 'com.asteroid.duck'
version = '0.0.2'
version = '0.0.3'

apply plugin: 'java'
apply plugin: 'application'
Expand Down Expand Up @@ -86,7 +86,7 @@ task createTemplate(type: Zip) {
task extractDistribution(type: Copy) {
description 'Extracts the distribution ZIP into a temp dir; ready for InnoSetup to consume'
group 'distribution'
from(zipTree("${buildDir}/distributions/velociwraptor-0.0.1.zip"))
from(zipTree("${buildDir}/distributions/velociwraptor-${project.version}.zip"))
into "${buildDir}/distributions/"
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/inno-setup/velociwraptor.iss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Setup]
AppId=Velocipwraptor
AppName=velociwraptor
AppVersion=0.0.1
AppVersion=0.0.3
DefaultDirName={pf}\velociwraptor
DefaultGroupName=Velociwraptor
SourceDir=..\..\..\
Expand All @@ -11,12 +11,12 @@ WizardImageFile=icons\wizard.bmp
Compression=lzma2
SolidCompression=yes
OutputDir=build\distributions
OutputBaseFilename=velociwraptor-setup
OutputBaseFilename=velociwraptor-0.0.3-setup
ChangesEnvironment=yes

[Files]
Source: "build\distributions\velociwraptor-0.0.1\bin\*.*"; DestDir: "{app}\bin"
Source: "build\distributions\velociwraptor-0.0.1\lib\*.*"; DestDir: "{app}\lib"
Source: "build\distributions\velociwraptor-0.0.3\bin\*.*"; DestDir: "{app}\bin"
Source: "build\distributions\velociwraptor-0.0.3\lib\*.*"; DestDir: "{app}\lib"

[Registry]
Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; ValueType: expandsz; ValueName: "Path"; ValueData: "{olddata};{app}\bin"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package com.asteroid.duck.velociwraptor.model;

import com.asteroid.duck.velociwraptor.util.FakeJsonString;
import org.slf4j.Logger;

import javax.json.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.function.Function.identity;
import static org.slf4j.LoggerFactory.getLogger;

public class TemplateData<K, V> implements Map<String, Object> {

/**
* Logger (SLF4J)
*/
private static final Logger LOG = getLogger(TemplateData.class);
/**
* A delegate who gets a chance to provide a value if this cannot
*/
Expand Down Expand Up @@ -41,17 +55,50 @@ public static TemplateData<String, Object> wrap(Map<String, Object> data) {

@Override
public Object get(Object key) {
Object delegateValue = null;
Object value = null;

if(delegate != null) {
delegateValue = delegate.get(key);
value = delegate.get(key);
}

if (value == null) {
value = dataObject.get(key);
}

if (delegateValue == null) {
return dataObject.get(key);
if (value != null && value instanceof JsonString) {
String jsValue = ((JsonString) value).getString();
if (jsValue.startsWith("!")) {
jsValue = jsValue.substring(1);
try {
LOG.trace("! escaped, treating as URI");
URI uri = new URI(jsValue);
LOG.trace("Requesting data from "+jsValue);
InputStream input = uri.toURL().openStream();
byte[] bytes = input.readAllBytes();
jsValue = new String(bytes, StandardCharsets.UTF_8);
LOG.trace("Received "+jsValue);
// is the string actually JSON?
JsonReader reader = Json.createReader(new StringReader(jsValue));
value = reader.readObject();
String fragment = uri.getFragment();
if (fragment != null)
{
JsonValue jsonValue = ((JsonObject) value).get(fragment);
if (jsonValue != null) {
value = jsonValue;
}
}
}
catch(URISyntaxException | IOException e)
{
// maybe it's not a URL, or we can't reach it
LOG.debug("Unable to interpret, request or parse - returning:"+jsValue, e);
value = new FakeJsonString(jsValue);
}
}
}

return delegateValue;
return value;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import javax.json.JsonArray;
import javax.json.JsonString;
import javax.json.JsonValue;
import javax.xml.ws.WebServiceException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

Expand Down Expand Up @@ -119,7 +119,7 @@ public String askFor(String key, String current) {
}

@Override
public void close() throws WebServiceException {
public void close() throws IOException {
if (!noColors) {
AnsiConsole.systemUninstall();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.asteroid.duck.velociwraptor.model.JsonConverter;
import com.asteroid.duck.velociwraptor.model.ValueAdapter;
import com.asteroid.duck.velociwraptor.util.FakeJsonString;

import javax.json.*;
import java.io.Closeable;
Expand Down Expand Up @@ -49,7 +50,7 @@ public Object get(String promptKey, JsonValue jsonValue) {
}

private JsonValue asJsonString(String value) {
return Json.createArrayBuilder().add(value).build().get(0);
return new FakeJsonString(value);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.asteroid.duck.velociwraptor.util;

import javax.json.JsonString;
import java.util.Objects;

public class FakeJsonString implements JsonString {
private final String string;

public FakeJsonString(String string) {
this.string = string;
}

@Override
public String getString() {
return string;
}

@Override
public CharSequence getChars() {
return string;
}

@Override
public ValueType getValueType() {
return ValueType.STRING;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || !(o instanceof JsonString)) return false;
JsonString that = (JsonString) o;
return Objects.equals(string, that.getString());
}

@Override
public int hashCode() {
return Objects.hash(string);
}

@Override
public String toString() {
return string;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.asteroid.duck.velociwraptor.model;

import com.asteroid.duck.velociwraptor.user.UserInteractive;
import com.asteroid.duck.velociwraptor.util.FakeJsonString;
import org.junit.Test;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;

import java.io.StringReader;

import static org.junit.Assert.*;

public class JsonTemplateDataTest {

private static final String TEST_FILE = "test.json";

public static final JsonObject parseString(String json)
{
JsonReader reader = Json.createReader(new StringReader(json));
return reader.readObject();
}

public static final JsonObject parseResource(String resource)
{
JsonReader reader = Json.createReader(JsonTemplateDataTest.class.getResourceAsStream(resource));
return reader.readObject();
}

@Test
public void get() {
JsonObject test = parseResource(TEST_FILE);
JsonTemplateData subject = new JsonTemplateData(test, UserInteractive.nullInteractive());
assertEquals(new FakeJsonString("Chris Senior"), subject.get("Author"));
assertEquals(new FakeJsonString("6.0.1"), subject.get("GradleVersion"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"Description": "An example project",
"Org": "com.asteroid.duck",
"Version": "1.0.0",
"GradleVersion": "!https://services.gradle.org/versions/current#version",
"Author": "Chris Senior",
"Year": 2018,
"Fraction": 3.14,
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<root level="all">
<appender-ref ref="STDOUT" />
</root>
<logger name="org.eclipse" level="WARN"/>
Expand Down

0 comments on commit 0e09faf

Please sign in to comment.