Skip to content

Commit 8afc8e2

Browse files
authored
Dark, but not Dark (System), theme support (#654)
* Dark theme support * fixup * Apply method restrictions * Make theme-manager optional
1 parent bf9de4f commit 8afc8e2

File tree

8 files changed

+63
-5
lines changed

8 files changed

+63
-5
lines changed

plugin/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,17 @@
180180
</exclusion>
181181
</exclusions>
182182
</dependency>
183+
<dependency>
184+
<groupId>io.jenkins.plugins</groupId>
185+
<artifactId>theme-manager</artifactId>
186+
<optional>true</optional>
187+
<exclusions>
188+
<exclusion>
189+
<groupId>io.jenkins.blueocean</groupId>
190+
<artifactId>blueocean-web</artifactId>
191+
</exclusion>
192+
</exclusions>
193+
</dependency>
183194
<dependency>
184195
<groupId>org.jenkins-ci.plugins</groupId>
185196
<artifactId>scm-api</artifactId>

plugin/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsFlowDefinition.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@
3737
import net.sf.json.JSONObject;
3838
import org.apache.commons.lang.StringUtils;
3939
import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn;
40+
import org.jenkinsci.plugins.workflow.cps.replay.ReplayAction;
41+
import org.jenkinsci.plugins.workflow.cps.view.ThemeUtil;
4042
import org.jenkinsci.plugins.workflow.flow.DurabilityHintProvider;
4143
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
4244
import org.jenkinsci.plugins.workflow.flow.FlowDefinitionDescriptor;
4345
import org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint;
4446
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
4547
import org.jenkinsci.plugins.workflow.flow.GlobalDefaultFlowDurabilityLevel;
48+
import org.kohsuke.accmod.Restricted;
49+
import org.kohsuke.accmod.restrictions.DoNotUse;
4650
import org.kohsuke.stapler.AncestorInPath;
4751
import org.kohsuke.stapler.DataBoundConstructor;
4852

@@ -180,4 +184,10 @@ public JSON doCheckScriptCompile(@AncestorInPath Item job, @QueryParameter Strin
180184
}
181185

182186
}
187+
188+
/** @see ReplayAction#getTheme */
189+
@Restricted(DoNotUse.class)
190+
/* accessible to Jelly */ public String getTheme() {
191+
return ThemeUtil.getTheme();
192+
}
183193
}

plugin/src/main/java/org/jenkinsci/plugins/workflow/cps/replay/ReplayAction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import org.acegisecurity.AccessDeniedException;
7171
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
7272
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution;
73+
import org.jenkinsci.plugins.workflow.cps.view.ThemeUtil;
7374
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
7475
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
7576
import org.kohsuke.accmod.Restricted;
@@ -199,6 +200,12 @@ private ReplayAction(Run run) {
199200
return run;
200201
}
201202

203+
/** @see CpsFlowDefinition#getTheme */
204+
@Restricted(DoNotUse.class)
205+
/* accessible to Jelly */ public String getTheme() {
206+
return ThemeUtil.getTheme();
207+
}
208+
202209
@Restricted(DoNotUse.class)
203210
@RequirePOST
204211
public void doRun(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.jenkinsci.plugins.workflow.cps.view;
2+
3+
import io.jenkins.plugins.thememanager.Theme;
4+
import io.jenkins.plugins.thememanager.ThemeManagerPageDecorator;
5+
import org.kohsuke.accmod.Restricted;
6+
import org.kohsuke.accmod.restrictions.NoExternalUse;
7+
8+
@Restricted(NoExternalUse.class)
9+
public final class ThemeUtil {
10+
11+
private ThemeUtil() {
12+
// Suppress default constructor for noninstantiability
13+
throw new AssertionError();
14+
}
15+
16+
public static String getTheme() {
17+
try {
18+
Theme theme = ThemeManagerPageDecorator.get().findTheme();
19+
return theme.getProperty("ace-editor", "theme").orElse("tomorrow");
20+
} catch (LinkageError e) {
21+
// Optional plugin not installed
22+
return "tomorrow";
23+
}
24+
}
25+
}

plugin/src/main/js/workflow-editor.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import ace from "ace-builds/src-noconflict/ace";
1010
import "ace-builds/src-noconflict/ext-language_tools";
1111
import "ace-builds/src-noconflict/mode-groovy";
1212
import "ace-builds/src-noconflict/theme-tomorrow";
13+
import "ace-builds/src-noconflict/theme-tomorrow_night";
1314

1415
// Import custom snippets
1516
import "./snippets/workflow";
@@ -45,7 +46,8 @@ var editorIdCounter = 0;
4546
snippetManager.register(snippets, 'groovy');
4647

4748
editor.session.setMode("ace/mode/groovy");
48-
editor.setTheme("ace/theme/tomorrow");
49+
var theme = aceContainer.attr("theme") === "tomorrow_night" ? "tomorrow_night" : "tomorrow";
50+
editor.setTheme("ace/theme/" + theme);
4951
editor.setAutoScrollEditorIntoView(true);
5052
editor.setOption("minLines", 20);
5153
// enable autocompletion and snippets

plugin/src/main/resources/org/jenkinsci/plugins/workflow/cps/CpsFlowDefinition/config.jelly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler" xmlns:wfe="/org/jenkinsci/plugins/workflow/editor">
2727
<input type="hidden" name="oldScript" value="${instance.script}"/>
2828
<f:entry title="${%Script}" field="script">
29-
<wfe:workflow-editor />
29+
<wfe:workflow-editor theme="${instance.theme}" />
3030
</f:entry>
3131
<f:entry field="sandbox">
3232
<f:checkbox title="${%Use Groovy Sandbox}" default="true"/>

plugin/src/main/resources/org/jenkinsci/plugins/workflow/cps/replay/ReplayAction/index.jelly

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
</p>
1414
<f:form action="run" method="POST" name="config">
1515
<f:entry field="mainScript" title="${%Main Script}">
16-
<wfe:workflow-editor script="${it.originalScript}" checkUrl="${rootURL}/${it.owner.url}${it.urlName}/checkScript" checkDependsOn=""/>
16+
<wfe:workflow-editor script="${it.originalScript}" theme="${it.theme}" checkUrl="${rootURL}/${it.owner.url}${it.urlName}/checkScript" checkDependsOn=""/>
1717
</f:entry>
1818
<j:forEach var="loadedScript" items="${it.originalLoadedScripts.entrySet()}">
1919
<f:entry field="${loadedScript.key.replace('.', '_')}" title="${loadedScript.key}">
20-
<wfe:workflow-editor script="${loadedScript.value}" checkUrl="${rootURL}/${it.owner.url}${it.urlName}/checkScript" checkDependsOn=""/>
20+
<wfe:workflow-editor script="${loadedScript.value}" theme="${it.theme}" checkUrl="${rootURL}/${it.owner.url}${it.urlName}/checkScript" checkDependsOn=""/>
2121
</f:entry>
2222
</j:forEach>
2323
<f:block>

plugin/src/main/resources/org/jenkinsci/plugins/workflow/editor/workflow-editor.jelly

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<st:attribute name="script">
66
The script.
77
</st:attribute>
8+
<st:attribute name="theme">
9+
The ACE editor theme.
10+
</st:attribute>
811
</st:documentation>
912
<j:choose>
1013
<j:when test="${h.isUnitTest}">
@@ -14,7 +17,7 @@
1417
<j:otherwise>
1518
<div class="workflow-editor-wrapper" style="display: none; position: relative">
1619
<f:textarea value="${script}" checkMethod="post" checkUrl="${attrs.checkUrl}" checkDependsOn="${attrs.checkDependsOn}"/>
17-
<div class="pane-frame editor" style="height: 250px;" samplesUrl="${rootURL}/workflow-cps-samples/"/>
20+
<div class="pane-frame editor" style="height: 250px;" samplesUrl="${rootURL}/workflow-cps-samples/" theme="${theme ?: 'tomorrow'}"/>
1821
</div>
1922
<st:adjunct includes="org.jenkinsci.plugins.workflow.cps.workflow-editor"/>
2023
</j:otherwise>

0 commit comments

Comments
 (0)