Skip to content

Commit 98e9c8b

Browse files
javier-godoypaodb
authored andcommitted
feat: allow linking to demo snippets in branch
Close #34
1 parent d81d7e0 commit 98e9c8b

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*-
2+
* #%L
3+
* Commons Demo
4+
* %%
5+
* Copyright (C) 2020 - 2022 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.addons;
21+
22+
import com.flowingcode.vaadin.addons.demo.DemoSource;
23+
import com.flowingcode.vaadin.addons.demo.TabbedDemo;
24+
import java.lang.annotation.ElementType;
25+
import java.lang.annotation.Inherited;
26+
import java.lang.annotation.Retention;
27+
import java.lang.annotation.RetentionPolicy;
28+
import java.lang.annotation.Target;
29+
30+
/**
31+
* This annotation is used for configuring the default branch for {@link TabbedDemo} code snippets.
32+
*
33+
* The annotation can be placed in either the demo class or its containing package. Class-level
34+
* annotations take precedence over package level annotations.
35+
*
36+
* @author Javier Godoy / Flowing Code
37+
* @see DemoSource
38+
*/
39+
@Retention(RetentionPolicy.RUNTIME)
40+
@Target({ElementType.TYPE, ElementType.PACKAGE})
41+
@Inherited
42+
public @interface GithubBranch {
43+
String value();
44+
}

src/main/java/com/flowingcode/vaadin/addons/demo/DemoSource.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,21 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.demo;
2121

22+
import com.flowingcode.vaadin.addons.GithubBranch;
23+
import com.flowingcode.vaadin.addons.GithubLink;
2224
import java.lang.annotation.ElementType;
2325
import java.lang.annotation.Retention;
2426
import java.lang.annotation.RetentionPolicy;
2527
import java.lang.annotation.Target;
2628

2729
/**
2830
* This annotation is used for configuring the source code URL in a {@link TabbedDemo}. If no {@code
29-
* value} is specified, and the demo view is annotated with {@link @GithubLink}, the source URL
30-
* defaults to the location of the annotated class under {@code src/java/test} in the master branch
31-
* of the repository.
31+
* value} is specified, and the demo view is annotated with {@link GithubLink}, then the source URL
32+
* defaults to the location of the annotated class under {@code src/java/test} and the branch is
33+
* determined from the value of {@link GithubBranch} in the demo view class (if the annotation is
34+
* present) or the containing package of the demo view class. If the source URL is defaulted and no
35+
* {@code GithubBranch} annotation is present either in the demo view class or its containing
36+
* package, then the branch defaults to {@code master}.
3237
*
3338
* @author Javier Godoy / Flowing Code
3439
*/

src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.demo;
2121

22+
import com.flowingcode.vaadin.addons.GithubBranch;
2223
import com.flowingcode.vaadin.addons.GithubLink;
2324
import com.vaadin.flow.component.AttachEvent;
2425
import com.vaadin.flow.component.Component;
@@ -185,9 +186,11 @@ public void showRouterLayoutContent(HasElement content) {
185186
if (demoSource != null) {
186187
sourceCodeUrl = demoSource.value();
187188
if (sourceCodeUrl.equals(DemoSource.GITHUB_SOURCE)) {
189+
String branch = lookupGithubBranch(this.getClass());
190+
String demoFile = demo.getClass().getName().replace('.', '/');
188191
sourceCodeUrl = Optional.ofNullable(this.getClass().getAnnotation(GithubLink.class))
189-
.map(githubLink -> githubLink.value() + "/blob/master/src/test/java/"
190-
+ demo.getClass().getName().replace('.', '/') + ".java")
192+
.map(githubLink -> String.format("%s/blob/%s/src/test/java/%s.java", githubLink.value(),
193+
branch, demoFile))
191194
.orElse(null);
192195
}
193196
content = new SplitLayoutDemo(demo, sourceCodeUrl);
@@ -209,6 +212,17 @@ public void showRouterLayoutContent(HasElement content) {
209212
getElement().insertChild(1, content.getElement());
210213
}
211214

215+
private String lookupGithubBranch(Class<? extends TabbedDemo> clazz) {
216+
GithubBranch branch = clazz.getAnnotation(GithubBranch.class);
217+
if (branch == null) {
218+
Package pkg = clazz.getPackage();
219+
if (pkg!=null) {
220+
branch = pkg.getAnnotation(GithubBranch.class);
221+
}
222+
}
223+
return Optional.ofNullable(branch).map(GithubBranch::value).orElse("master");
224+
}
225+
212226
@Override
213227
public void removeRouterLayoutContent(HasElement oldContent) {
214228
getElement().removeChild(1);

0 commit comments

Comments
 (0)