Skip to content

Commit

Permalink
add opt. snapshot to TemplateInfo
Browse files Browse the repository at this point in the history
experimental as described here:
#6286 (comment)

example:
bndtools/workspace-templates/gradle#567648ff425693b27b191bd38ace7c9c10539c2d;name=b;description=B;snapshot=bndtools/workspace-templates/gradle#master

This defines a default TemplateID to a specific commit SHA and a snapshot to the master (HEAD branch)

Signed-off-by: Christoph Rueger <[email protected]>
  • Loading branch information
chrisrueger committed Sep 27, 2024
1 parent 9b0c0d1 commit b4dc9e3
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class FragmentTemplateEngine {
private static final String DESCRIPTION = "description";
private static final String WORKSPACE_TEMPLATES = "-workspace-templates";
private static final String NAME = "name";
private static final String SNAPSHOT = "snapshot";

final static Logger log = LoggerFactory.getLogger(FragmentTemplateEngine.class);
final List<TemplateInfo> templates = new ArrayList<>();
Expand All @@ -75,7 +76,8 @@ public enum UpdateStatus {
* Info about a template, comes from the index files.
*/

public record TemplateInfo(TemplateID id, String name, String description, String[] require, String... tag)
public record TemplateInfo(TemplateID id, String name, String description, TemplateID snapshotId, String[] require,
String... tag)
implements Comparable<TemplateInfo> {

@Override
Expand All @@ -84,6 +86,66 @@ public int compareTo(TemplateInfo o) {
}
}

/**
* Represents a TemplateInfo with an additional flag indicating if the
* default version or snapshot version should be downloaded. We cannot use
* java record because we need useSnapshot to be mutable because we render
* it as a checkbox in the table
*/
public static class SelectedTemplateInfo implements Comparable<SelectedTemplateInfo> {

final TemplateInfo templateInfo;
private boolean useSnapshot;

public SelectedTemplateInfo(TemplateInfo templateInfo, boolean useSnapshot) {
this.templateInfo = templateInfo;
this.useSnapshot = useSnapshot;
}

public TemplateID id() {
return useSnapshot ? templateInfo.snapshotId() : templateInfo.id();
}

public TemplateInfo templateInfo() {
return templateInfo;
}

public boolean useSnapshot() {
return useSnapshot;
}

public void setUseSnapshot(boolean useSnapshot) {
this.useSnapshot = useSnapshot;
}

@Override
public int hashCode() {
return Objects.hash(templateInfo, useSnapshot);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SelectedTemplateInfo other = (SelectedTemplateInfo) obj;
return Objects.equals(templateInfo, other.templateInfo) && useSnapshot == other.useSnapshot;
}

@Override
public String toString() {
return "SelectedTemplateInfo [templateInfo=" + templateInfo + ", useSnapshot=" + useSnapshot + "]";
}

@Override
public int compareTo(SelectedTemplateInfo o) {
return templateInfo.compareTo(o.templateInfo);
}
}

public enum Action {
skip,
append,
Expand Down Expand Up @@ -176,12 +238,13 @@ public List<TemplateInfo> read(Parameters ps) {
Attrs attrs = e.getValue();

TemplateID templateId = TemplateID.from(id);
TemplateID snapshotId = TemplateID.from(attrs.getOrDefault(SNAPSHOT, id.toString()));
String name = attrs.getOrDefault(NAME, id.toString());
String description = attrs.getOrDefault(DESCRIPTION, "");
String require[] = toArray(attrs.get(REQUIRE));
String tags[] = toArray(attrs.get(TAG));

templates.add(new TemplateInfo(templateId, name, description, require, tags));
templates.add(new TemplateInfo(templateId, name, description, snapshotId, require, tags));
}
return templates;
}
Expand Down Expand Up @@ -211,12 +274,12 @@ public List<TemplateInfo> getAvailableTemplates() {
*/
public class TemplateUpdater implements AutoCloseable {
private static final String TOOL_BND = "tool.bnd";
final List<TemplateInfo> templates;
final List<SelectedTemplateInfo> templates;
final File folder;
final MultiMap<File, Update> updates = new MultiMap<>();
final List<AutoCloseable> closeables = new ArrayList<>();

TemplateUpdater(File folder, List<TemplateInfo> templates) {
TemplateUpdater(File folder, List<SelectedTemplateInfo> templates) {
this.folder = folder;
this.templates = templates;
templates.forEach(templ -> {
Expand Down Expand Up @@ -277,12 +340,16 @@ public Map<File, List<Update>> updaters() {
return updates;
}

List<Update> make(TemplateInfo template) {
Jar jar = getFiles(template.id()
List<Update> make(SelectedTemplateInfo selectedTemplate) {

TemplateID id = selectedTemplate.id();
TemplateInfo template = selectedTemplate.templateInfo();

Jar jar = getFiles(id
.uri());
closeables.add(jar);

String prefix = fixup(template.id()
String prefix = fixup(id
.path());

List<Update> updates = new ArrayList<>();
Expand Down Expand Up @@ -396,7 +463,7 @@ public void close() throws Exception {
/**
* Create a TemplateUpdater
*/
public TemplateUpdater updater(File folder, List<TemplateInfo> templates) {
public TemplateUpdater updater(File folder, List<SelectedTemplateInfo> templates) {
return new TemplateUpdater(folder, templates);
}

Expand Down
20 changes: 10 additions & 10 deletions biz.aQute.bndlib/src/aQute/bnd/wstemplates/TemplateID.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public record TemplateID(String organisation, String repository, String path, St
g("path", set(lit("/"), SEGMENT_P)), opt(lit("/"))),
opt(lit("#"), g("branch", REF_P)) //
);
final static URI ROOT = URI.create("https://github.com/bndtools/bndtools.workspace.min#master");
final static URI ROOT = URI.create("https://github.com/bndtools/bndtools.workspace.min#HEAD");

@Override
public int compareTo(TemplateID o) {
Expand Down Expand Up @@ -62,20 +62,16 @@ public URI uri() {
public String repoUrl() {
String uri = this.other;
if (uri == null) {
if (!path.startsWith("tree")) {
uri = "https://github.com/" + organisation + "/" + repository + "/tree/master/" + path;
} else {
uri = "https://github.com/" + organisation + "/" + repository + "/" + path;
}
uri = "https://github.com/" + organisation + "/" + repository + "/tree/" + ref + "/" + path;
}
return uri;
}

/**
* Parse the id into a Template ID. The default is
* `bndtools/bndtools.workspace.min#master`. The missing fields are taken
* from this default. If the id does not match the pattern, it is assumed to
* be a URI.
* `bndtools/bndtools.workspace.min#HEAD`. The missing fields are taken from
* this default. If the id does not match the pattern, it is assumed to be a
* URI.
*
* @param id id or uri
* @return a TemplateId
Expand All @@ -89,7 +85,11 @@ public static TemplateID from(String id) {
String path = vs.getOrDefault("path", "");
if (!path.isEmpty())
path = path.substring(1);
String branch = vs.getOrDefault("branch", "master");

// HEAD seems to be the universal name for the primary branch
// (regardless if named 'master', 'main' or else)
// see https://github.com/orgs/community/discussions/23213
String branch = vs.getOrDefault("branch", "HEAD");
return new TemplateID(org, repo, path, branch, null);
})
.orElse(new TemplateID(null, null, "", null, id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import aQute.bnd.http.HttpClient;
import aQute.bnd.osgi.Builder;
import aQute.bnd.result.Result;
import aQute.bnd.wstemplates.FragmentTemplateEngine.SelectedTemplateInfo;
import aQute.bnd.wstemplates.FragmentTemplateEngine.TemplateInfo;
import aQute.bnd.wstemplates.FragmentTemplateEngine.TemplateUpdater;
import aQute.bnd.wstemplates.FragmentTemplateEngine.Update;
Expand All @@ -31,13 +32,13 @@ void testId() {
assertThat(defaultId.organisation()).isEqualTo("bndtools");
assertThat(defaultId.repository()).isEqualTo("workspace");
assertThat(defaultId.path()).isEqualTo("");
assertThat(defaultId.ref()).isEqualTo("master");
assertThat(defaultId.ref()).isEqualTo("HEAD");

TemplateID withOrg = TemplateID.from("acme");
assertThat(withOrg.organisation()).isEqualTo("acme");
assertThat(withOrg.repository()).isEqualTo("workspace");
assertThat(withOrg.path()).isEqualTo("");
assertThat(withOrg.ref()).isEqualTo("master");
assertThat(withOrg.ref()).isEqualTo("HEAD");

TemplateID withOrgRef = TemplateID.from("acme#main");
assertThat(withOrgRef.organisation()).isEqualTo("acme");
Expand All @@ -49,7 +50,7 @@ void testId() {
assertThat(withOrgRepo.organisation()).isEqualTo("acme");
assertThat(withOrgRepo.repository()).isEqualTo("template");
assertThat(withOrgRepo.path()).isEqualTo("");
assertThat(withOrgRepo.ref()).isEqualTo("master");
assertThat(withOrgRepo.ref()).isEqualTo("HEAD");

TemplateID withOrgRepoRef = TemplateID.from("acme/template#foo/bar");
assertThat(withOrgRepoRef.organisation()).isEqualTo("acme");
Expand All @@ -61,7 +62,7 @@ void testId() {
assertThat(withOrgRepoPath.organisation()).isEqualTo("acme");
assertThat(withOrgRepoPath.repository()).isEqualTo("template");
assertThat(withOrgRepoPath.path()).isEqualTo("foo/bar");
assertThat(withOrgRepoPath.ref()).isEqualTo("master");
assertThat(withOrgRepoPath.ref()).isEqualTo("HEAD");

TemplateID withOrgRepoPathRef = TemplateID.from("acme/template/foo/bar/y#feature/bar/x");
assertThat(withOrgRepoPathRef.organisation()).isEqualTo("acme");
Expand All @@ -84,15 +85,19 @@ void testId() {
// translates to the followingg templateID:
// org/repo/tree/commitSHA/subfolder/workspace-template/
TemplateID commitSHAUrl = TemplateID
.from("org/repo/tree/commitSHA/subfolder/workspace-template");
.from("org/repo/subfolder/workspace-template#commitSHA");
assertThat(commitSHAUrl.organisation()).isEqualTo("org");
assertThat(commitSHAUrl.repository()).isEqualTo("repo");
assertThat(commitSHAUrl.path()).isEqualTo("tree/commitSHA/subfolder/workspace-template");
assertThat(commitSHAUrl.path()).isEqualTo("subfolder/workspace-template");
assertThat(commitSHAUrl.repoUrl())
.isEqualTo("https://github.com/org/repo/tree/commitSHA/subfolder/workspace-template");

TemplateID externalRepo = TemplateID.from("org/repo/subfolder/workspace-template");
assertThat(externalRepo.repoUrl())
TemplateID defaultHEADUrl = TemplateID.from("org/repo/subfolder/workspace-template");
assertThat(defaultHEADUrl.repoUrl())
.isEqualTo("https://github.com/org/repo/tree/HEAD/subfolder/workspace-template");

TemplateID defaultMasterUrl = TemplateID.from("org/repo/subfolder/workspace-template#master");
assertThat(defaultMasterUrl.repoUrl())
.isEqualTo("https://github.com/org/repo/tree/master/subfolder/workspace-template");

}
Expand All @@ -107,7 +112,7 @@ void testRemote() throws Exception {

// use an archived repository
Result<List<TemplateInfo>> result = tfs.read("-workspace-templates " + a.toURI() + ";name=a;description=A,"
+ "bndtools/workspace-templates/gradle#567648ff425693b27b191bd38ace7c9c10539c2d;name=b;description=B");
+ "bndtools/workspace-templates/gradle#567648ff425693b27b191bd38ace7c9c10539c2d;name=b;description=B;snapshot=bndtools/workspace-templates/gradle");

assertThat(result.isOk()).describedAs(result.toString())
.isTrue();
Expand All @@ -117,7 +122,9 @@ void testRemote() throws Exception {
assertThat(infos.remove(0)
.name()).isEqualTo("a");

TemplateUpdater updater = tfs.updater(wsDir, infos);
TemplateUpdater updater = tfs.updater(wsDir, infos.stream()
.map(ti -> new SelectedTemplateInfo(ti, false))
.toList());
updater.commit();

assertThat(IO.getFile(wsDir, "cnf/build.bnd")).isFile();
Expand Down Expand Up @@ -152,7 +159,9 @@ void test() throws Exception {
List<TemplateInfo> infos = result.unwrap();
assertThat(infos).hasSize(2);

TemplateUpdater updater = tfs.updater(wsDir, infos);
TemplateUpdater updater = tfs.updater(wsDir, infos.stream()
.map(ti -> new SelectedTemplateInfo(ti, false))
.toList());
updater.commit();

assertThat(IO.getFile(wsDir, "cnf/build.bnd")).isFile();
Expand Down
8 changes: 4 additions & 4 deletions bndtools.core/src/bndtools/wizards/newworkspace/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import aQute.bnd.build.Workspace;
import aQute.bnd.wstemplates.FragmentTemplateEngine;
import aQute.bnd.wstemplates.FragmentTemplateEngine.TemplateInfo;
import aQute.bnd.wstemplates.FragmentTemplateEngine.SelectedTemplateInfo;
import aQute.bnd.wstemplates.FragmentTemplateEngine.TemplateUpdater;
import aQute.lib.io.IO;

Expand All @@ -46,8 +46,8 @@ enum NewWorkspaceType {
boolean clean = false;
boolean updateWorkspace = false;
boolean switchWorkspace = true;
List<TemplateInfo> templates = new ArrayList<>();
List<TemplateInfo> selectedTemplates = new ArrayList<>();
List<SelectedTemplateInfo> templates = new ArrayList<>();
List<SelectedTemplateInfo> selectedTemplates = new ArrayList<>();
Progress validatedUrl = Progress.init;
String urlValidationError;
String error;
Expand Down Expand Up @@ -162,7 +162,7 @@ public void clean(boolean selection) {
clean = selection;
}

void selectedTemplates(List<TemplateInfo> list) {
void selectedTemplates(List<SelectedTemplateInfo> list) {
selectedTemplates = list;
}

Expand Down
Loading

0 comments on commit b4dc9e3

Please sign in to comment.