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

chore: Mix templates and aliases ...but missing a way to filter/more visual differences. #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
101 changes: 47 additions & 54 deletions .github/appstore.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
//DEPS org.commonmark:commonmark-ext-gfm-strikethrough:0.17.1
//DEPS org.commonmark:commonmark-ext-task-list-items:0.17.1

import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.Callable;

import com.google.gson.*;
import com.google.gson.annotations.SerializedName;

Expand All @@ -19,21 +24,9 @@
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.kohsuke.github.*;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import picocli.CommandLine;
import picocli.CommandLine.*;

/**
* To run this script, set two environment variables GH_USER and GH_TOKEN. These
Expand All @@ -46,7 +39,8 @@ class appstore implements Callable<Integer> {

static {
excludedCatalogs.add("jbangdev/jbang/itests/jbang-catalog.json");
// excludedCatalogs.add("jbangdev/jbang/src/main/resources/jbang-catalog.json"); // todo: treat special to just have it be jbang -t xxx ?
// excludedCatalogs.add("jbangdev/jbang/src/main/resources/jbang-catalog.json");
// // todo: treat special to just have it be jbang -t xxx ?
}

@Option(names = { "-d",
Expand Down Expand Up @@ -92,13 +86,15 @@ public Integer call() throws Exception {
}
}

List<CatalogItem> sortedAliases = aliasItems.stream()
.sorted(Comparator.comparing(catalogerItem -> -catalogerItem.stars)).collect(Collectors.toList());
List<CatalogItem> sortedItems = new ArrayList<>();

List<CatalogItem> sortedTemplates = templateItems.stream()
.sorted(Comparator.comparing(catalogerItem -> -catalogerItem.stars)).collect(Collectors.toList());

var cataloger = new Cataloger(sortedAliases,sortedTemplates);
sortedItems.addAll(aliasItems);
sortedItems.addAll(templateItems);
sortedItems.sort(Comparator.comparing((CatalogItem item) -> item.link )
//.thenComparing(catalogerItem -> -catalogerItem.stars)
);

var cataloger = new Cataloger(sortedItems);
String catalogerContent = gson.toJson(cataloger);
destinationDir.toFile().mkdirs();
var path = destinationDir.resolve("jbang-appstore.json");
Expand All @@ -107,26 +103,6 @@ public Integer call() throws Exception {
return 0;
}

private CatalogItem templateToItem(Map.Entry<String, Template> entry, GHContent ghContent) {
var item = new CatalogItem();
item.alias = entry.getKey();
item.scriptRef = null;
item.description = entry.getValue().description;

if (item.description != null) {
item.description = md2html(item.description);
}

setupGeneralInfo(ghContent, item);

StringBuffer cmd = aliasToCommand(ghContent, item.alias, item.repoName, item.repoOwner);

item.command = cmd.toString();
item.fullcommand = "jbang init -t " + item.command + " app.java";

return item;
}

private void setupGeneralInfo(GHContent ghContent, CatalogItem item) {

item.repoOwner = ghContent.getOwner().getOwnerName();
Expand All @@ -152,8 +128,31 @@ private void setupGeneralInfo(GHContent ghContent, CatalogItem item) {
}
}

private CatalogItem templateToItem(Map.Entry<String, Template> entry, GHContent ghContent) {
var item = new CatalogItem();
item.type = "template";
item.alias = entry.getKey();
item.scriptRef = null;
item.description = entry.getValue().description;

if (item.description != null) {
item.description = md2html(item.description);
}

setupGeneralInfo(ghContent, item);

StringBuffer cmd = aliasToCommand(ghContent, item.alias, item.repoName, item.repoOwner);

item.command = cmd.toString();
item.fullcommand = "jbang init -t " + item.command + " app.java";

return item;
}


private CatalogItem toCatalogerItem(Map.Entry<String, Alias> entry, GHContent ghContent) {
var item = new CatalogItem();
item.type = "alias";
item.alias = entry.getKey();
item.scriptRef = entry.getValue().scriptRef;
item.description = entry.getValue().description;
Expand All @@ -162,15 +161,13 @@ private CatalogItem toCatalogerItem(Map.Entry<String, Alias> entry, GHContent gh
item.description = md2html(item.description);
}


setupGeneralInfo(ghContent, item);

StringBuffer cmd = aliasToCommand(ghContent, item.alias, item.repoName, item.repoOwner);

item.command = cmd.toString();
item.fullcommand = "jbang init -t " + item.command + " app.java";


return item;
}

Expand All @@ -195,7 +192,7 @@ private String md2html(String markdown) {
var document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).sanitizeUrls(true).escapeHtml(true).build();
var html = renderer.render(document);
//System.out.println(item.description + "=>" + html);
// System.out.println(item.description + "=>" + html);
return html;
}

Expand All @@ -206,7 +203,7 @@ private Catalog toJsonElement(Gson gson, GHContent catalogContent) throws IOExce
try (InputStream stream = catalogContent.read(); InputStreamReader streamR = new InputStreamReader(stream)) {
try {
json = gson.fromJson(streamR, Catalog.class);

} catch (JsonParseException e) {
e.printStackTrace();
json = null;
Expand All @@ -222,8 +219,8 @@ class Catalog {

@Override
public String toString() {
return aliases.toString() +" - " + templates.toString();

return aliases.toString() + " - " + templates.toString();

}
}
Expand All @@ -241,20 +238,16 @@ class Template {
class Cataloger {
public final int aliasCount;
public final List<CatalogItem> aliases;
private List<CatalogItem> templates;
private int templateCount;

public Cataloger(List<CatalogItem> items, List<CatalogItem> sortedTemplates) {

public Cataloger(List<CatalogItem> items) {
this.aliases = items;
aliasCount = items.size();

this.templates = sortedTemplates;
this.templateCount = sortedTemplates.size();
}
}

class CatalogItem {
public String url;
public String type;
public int stars;
public String icon_url;
public String repoOwner;
Expand Down
Loading