-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathHome.java
More file actions
147 lines (124 loc) · 4.06 KB
/
Home.java
File metadata and controls
147 lines (124 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package io.jenkins.plugins.designlibrary;
import hudson.Extension;
import hudson.PluginWrapper;
import hudson.model.RootAction;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.StaplerResponse2;
/**
* Entry point to all the UI samples.
*
* @author Kohsuke Kawaguchi
*/
@Extension
public class Home implements RootAction {
public String getIconFileName() {
return "symbol-design-library plugin-design-library";
}
public String getDisplayName() {
return "Design Library";
}
public String getUrlName() {
return "design-library";
}
@Override
public boolean isPrimaryAction() {
return true;
}
public List<UISample> getAll() {
return UISample.getAll();
}
public static Map<Category, List<UISample>> getGrouped() {
return UISample.getGrouped();
}
/**
* Dynamically retrieves the appropriate UI sample based on the current URL
*/
public UISample getDynamic(String name) {
for (UISample ui : getAll()) {
String urlName = ui.getUrlName();
if (urlName != null && urlName.equals(name)) {
return ui;
}
}
return null;
}
/**
* Serves LLM-friendly content as plain text markdown.
*
* <ul>
* <li>{@code llms.txt} - index of all components</li>
* <li>{@code llms-all.txt} - all component documentation in a single file</li>
* </ul>
*/
public void doDynamic(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException {
String restOfPath = req.getRestOfPath();
if (restOfPath.startsWith("/")) {
restOfPath = restOfPath.substring(1);
}
String content = resolveLlmContent(restOfPath, req);
if (content != null) {
rsp.setContentType("text/plain;charset=UTF-8");
try (PrintWriter w = rsp.getWriter()) {
w.write(content);
}
return;
}
rsp.sendError(404);
}
private String resolveLlmContent(String name, StaplerRequest2 req) {
if ("llms.txt".equals(name)) {
return LlmContent.generateIndex();
}
if ("llms-all.txt".equals(name)) {
return LlmContent.generateAll(req.getServletContext());
}
return null;
}
public String getPluginVersion() {
Jenkins jenkins = Jenkins.get();
PluginWrapper plugin = jenkins.getPluginManager().getPlugin("design-library");
if (plugin != null) {
return plugin.getVersion();
}
return "Version not available";
}
/**
* Generates a dynamic gradient for the Home cards
*/
public String buildGradientVar() {
return GradientFactory.buildGradientVar();
}
private static final class GradientFactory {
private static final int LAYERS = 10;
private static final String[] COLOURS = {
"var(--light-orange)",
"var(--light-cyan)",
"var(--light-pink)",
"var(--light-red)",
"var(--light-yellow)",
"var(--light-purple)",
"var(--light-teal)",
"var(--light-indigo)",
"var(--light-brown)"
};
private GradientFactory() {}
public static String buildGradientVar() {
StringBuilder css = new StringBuilder("--aurora").append(": \n ");
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < LAYERS; i++) {
int x = rnd.nextInt(101);
int y = rnd.nextInt(101);
String colour = COLOURS[rnd.nextInt(COLOURS.length)];
css.append(String.format("radial-gradient(at %d%% %d%%, %s 0, transparent 50%%)", x, y, colour));
css.append(i < LAYERS - 1 ? ",\n " : ";");
}
return css.toString();
}
}
}