Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 2662574

Browse files
committed
Add item listener for conditional UI validations
1 parent e894502 commit 2662574

File tree

8 files changed

+251
-13
lines changed

8 files changed

+251
-13
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.mathworks.ci;
2+
3+
import org.kohsuke.stapler.DataBoundConstructor;
4+
5+
public class MatlabBuildWrapperContent {
6+
7+
private final String matlabInstName;
8+
private final String matlabRootFolder;
9+
10+
@DataBoundConstructor
11+
public MatlabBuildWrapperContent(String matlabInstName, String matlabRootFolder){
12+
this.matlabInstName = matlabInstName;
13+
this.matlabRootFolder = matlabRootFolder;
14+
}
15+
16+
public String getMatlabInstName() {
17+
return matlabInstName;
18+
}
19+
20+
public String getMatlabRootFolder() {
21+
return matlabRootFolder;
22+
}
23+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.mathworks.ci;
2+
3+
import hudson.Extension;
4+
import hudson.matrix.Axis;
5+
import hudson.matrix.AxisDescriptor;
6+
import hudson.matrix.MatrixProject;
7+
import org.kohsuke.stapler.DataBoundConstructor;
8+
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
public class MatlabInstallationAxis extends Axis {
14+
15+
@DataBoundConstructor
16+
public MatlabInstallationAxis(List<String> values) {
17+
super(Message.getValue("Axis.matlab.key"), evaluateValues(values));
18+
}
19+
20+
static private List<String> evaluateValues(List<String> values) {
21+
// Add default configuration is values are null or not selected.
22+
if (values == null || values.isEmpty()) {
23+
values = new ArrayList<>(Arrays.asList("default"));
24+
}
25+
return values;
26+
}
27+
28+
@Extension
29+
public static class DescriptorImpl extends AxisDescriptor{
30+
private final String useMATLABWarning = Message.getValue("Axis.use.matlab.warning");
31+
private final String noInstallationError = Message.getValue("Axis.no.installed.matlab.error");
32+
33+
@Override
34+
public String getDisplayName() {
35+
return Message.getValue("Axis.matlab.key");
36+
}
37+
38+
@Override
39+
public boolean isInstantiable() {
40+
return !checkMatlabInstallationEmpty();
41+
}
42+
43+
public boolean checkUseMATLABVersion(Object it) {
44+
return MatlabItemListener.getMATLABBuildWrapperCheckForPrj(((MatrixProject) it).getFullName()) && !checkMatlabInstallationEmpty();
45+
}
46+
47+
public MatlabInstallation[] getInstallations () {
48+
return MatlabInstallation.getAll();
49+
}
50+
51+
public String getUseMATLABWarning() {
52+
return useMATLABWarning;
53+
}
54+
55+
public boolean checkMatlabInstallationEmpty() {
56+
return MatlabInstallation.isEmpty();
57+
}
58+
59+
public String getNoInstallationError() {
60+
return noInstallationError;
61+
}
62+
}
63+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.mathworks.ci;
2+
3+
import hudson.Extension;
4+
import hudson.matrix.MatrixConfiguration;
5+
import hudson.matrix.MatrixProject;
6+
import hudson.model.Item;
7+
import hudson.model.TopLevelItem;
8+
import hudson.model.listeners.ItemListener;
9+
import jenkins.model.Jenkins;
10+
11+
import java.util.Collection;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
17+
@Extension
18+
public final class MatlabItemListener extends ItemListener {
19+
private static final Map<String, Boolean> prjCheckMATLABAxis = new HashMap<>();
20+
private static final Map<String, Boolean> prjCheckMATLABBuildWrapper = new HashMap<>();
21+
22+
@Override
23+
public void onLoaded(){
24+
checkElements(Jenkins.get().getItems());
25+
}
26+
27+
@Override
28+
public void onUpdated(Item item) {
29+
if(!(item instanceof MatrixProject)){
30+
return;
31+
}
32+
checkElementsSingle(item);
33+
}
34+
35+
36+
void checkElements(List<TopLevelItem> items) {
37+
for(TopLevelItem item : items){
38+
if(item instanceof MatrixProject){
39+
check((MatrixProject) item);
40+
}
41+
}
42+
}
43+
44+
void checkElementsSingle(Item item){
45+
check((MatrixProject) item);
46+
}
47+
48+
void check(MatrixProject _prj){
49+
boolean checkForAxis = false;
50+
boolean checkForBuildWrapper = false;
51+
Collection<MatrixConfiguration> conf = _prj.getActiveConfigurations();
52+
for(MatrixConfiguration _conf : conf){
53+
String a = _conf.getCombination().get(Message.getValue("Axis.matlab.key"));
54+
if (a != null) {
55+
checkForAxis = true;
56+
break;
57+
}
58+
}
59+
60+
for(Object _bWrapper : _prj.getBuildWrappersList().toArray()) {
61+
if(_bWrapper instanceof UseMatlabVersionBuildWrapper){
62+
checkForBuildWrapper = ((UseMatlabVersionBuildWrapper) _bWrapper).getMatlabInstName() != null || ((UseMatlabVersionBuildWrapper) _bWrapper).getMatlabRootFolder() != null;
63+
break;
64+
}
65+
}
66+
prjCheckMATLABAxis.put(_prj.getFullName(), checkForAxis);
67+
prjCheckMATLABBuildWrapper.put(_prj.getFullName(), checkForBuildWrapper);
68+
}
69+
70+
public static boolean getMATLABAxisCheckForPrj(String prjName) {
71+
return prjCheckMATLABAxis.get(prjName) != null && prjCheckMATLABAxis.get(prjName);
72+
}
73+
74+
public static boolean getMATLABBuildWrapperCheckForPrj(String prjName) {
75+
return prjCheckMATLABBuildWrapper.get(prjName) != null && prjCheckMATLABBuildWrapper.get(prjName);
76+
}
77+
}

src/main/java/com/mathworks/ci/UseMatlabVersionBuildWrapper.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public String getMatlabInstName() {
5454
/* For backward compatibility assign installation name to custom
5555
* if matlabRootFolder is not null.
5656
* */
57-
if(this.matlabInstName!=null && !this.matlabInstName.isEmpty() && this.matlabRootFolder!=null && !this.matlabRootFolder.isEmpty()){
57+
if(this.matlabRootFolder!=null && !this.matlabRootFolder.isEmpty()){
5858
this.matlabInstName = Message.getValue("matlab.custom.location");
5959
}
6060
return matlabInstName;
@@ -64,6 +64,10 @@ public void setMatlabRootFolder(String matlabRootFolder) {
6464
this.matlabRootFolder = matlabRootFolder;
6565
}
6666

67+
public boolean isInitialized () {
68+
return this.matlabRootFolder == null && this.matlabInstName == null;
69+
}
70+
6771
@DataBoundSetter
6872
public void setMatlabBuildWrapperContent(MatlabBuildWrapperContent matlabBuildWrapperContent){
6973
if (matlabBuildWrapperContent != null){
@@ -94,6 +98,8 @@ public static final class UseMatlabVersionDescriptor extends BuildWrapperDescrip
9498
String matlabRootFolder;
9599
private boolean isMatrix;
96100
private final String customLocation = Message.getValue("matlab.custom.location");
101+
private final String matlabAxisWarning = Message.getValue("Use.matlab.version.axis.warning");
102+
private AbstractProject<?, ?> project;
97103

98104
public String getMatlabRootFolder() {
99105
return matlabRootFolder;
@@ -105,6 +111,7 @@ public void setMatlabRootFolder(String matlabRootFolder) {
105111

106112
@Override
107113
public boolean isApplicable(AbstractProject<?, ?> item) {
114+
this.project = item;
108115
isMatrix = item instanceof MatrixProject;
109116
return true;
110117
}
@@ -122,6 +129,25 @@ public MatlabInstallation[] getInstallations() {
122129
return arr.toArray(temp);
123130
}
124131

132+
public String getCustomLocation() {
133+
return customLocation;
134+
}
135+
136+
public boolean getIsMatrix() {
137+
return isMatrix;
138+
}
139+
140+
public boolean checkAxisAdded() {
141+
if (!isMatrix) {
142+
return false;
143+
}
144+
return MatlabItemListener.getMATLABAxisCheckForPrj(project.getFullName()) && !MatlabInstallation.isEmpty();
145+
}
146+
147+
public String getMatlabAxisWarning() {
148+
return matlabAxisWarning;
149+
}
150+
125151
/*
126152
* Below methods with 'doCheck' prefix gets called by jenkins when this builder is loaded.
127153
* these methods are used to perform basic validation on UI elements associated with this
@@ -178,7 +204,7 @@ public synchronized void setUp(Context context, Run<?, ?> build, FilePath worksp
178204

179205
FilePath matlabExecutablePath = new FilePath(launcher.getChannel(),
180206
getLocalMatlab(Computer.currentComputer(), listener) + "/bin/" + getNodeSpecificExecutable(launcher));
181-
listener.getLogger().println("\n Using MATLAB : " + matlabExecutablePath.getRemote() + "\n");
207+
listener.getLogger().println("\n MATLAB executed from : " + matlabExecutablePath.getRemote() + "\n");
182208
if (!matlabExecutablePath.exists()) {
183209
throw new MatlabNotFoundError(Message.getValue("matlab.not.found.error"));
184210
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?jelly escape-by-default='true'?>
2+
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
3+
4+
<j:choose>
5+
<j:when test="${descriptor.checkUseMATLABVersion(it)}">
6+
<f:block>
7+
<div class = "warning">${descriptor.useMATLABWarning}</div>
8+
</f:block>
9+
</j:when>
10+
</j:choose>
11+
12+
<j:choose>
13+
<j:when test="${descriptor.checkMatlabInstallationEmpty()}">
14+
<f:block>
15+
<div class = "error">${descriptor.noInstallationError}</div>
16+
</f:block>
17+
</j:when>
18+
</j:choose>
19+
20+
<f:entry help="">
21+
<j:forEach var="installation" items="${descriptor.installations}">
22+
<f:checkbox name="values" json="${installation.name}" checked="${instance.values.contains(installation.name)}" title="${installation.name}" />
23+
<st:nbsp/>
24+
</j:forEach>
25+
</f:entry>
26+
</j:jelly>
Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
<?jelly escape-by-default='true'?>
2-
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
3-
<f:entry help="">
4-
<j:forEach var="installation" items="${descriptor.installations}">
5-
<f:checkbox name="values" json="${installation.name}" checked="${instance.values.contains(installation.name)}" title="${installation.name}" />
6-
<st:nbsp/>
7-
</j:forEach>
8-
</f:entry>
2+
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
3+
4+
<j:choose>
5+
<j:when test="${descriptor.checkAxisAdded()}">
6+
<f:block>
7+
<div class = "warning">${descriptor.matlabAxisWarning}</div>
8+
</f:block>
9+
</j:when>
10+
</j:choose>
11+
12+
<f:dropdownList name="matlabBuildWrapperContent" title="">
13+
<j:forEach var="installation" items="${descriptor.installations}" varStatus="loop">
14+
<!-- installation.name == instance.getMatlabInstName() || installation.name == descriptor.setSelectionTo -->
15+
<f:dropdownListBlock value="${installation.name}" selected="${(instance.getMatlabInstName() == null)? ((descriptor.isMatrix) ? installation.name == descriptor.customLocation : '') : installation.name == instance.getMatlabInstName()}" title="${installation.name}">
16+
<f:nested>
17+
<input type="hidden" name="matlabInstName" value="${installation.name}"/>
18+
</f:nested>
19+
<j:choose>
20+
<j:when test="${loop.last}">
21+
<f:entry title="MATLAB root: " field="matlabRootFolder">
22+
<f:textbox/>
23+
</f:entry>
24+
</j:when>
25+
</j:choose>
26+
</f:dropdownListBlock>
27+
</j:forEach>
28+
</f:dropdownList>
929
</j:jelly>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<p>
2-
Specify the MATLAB version for this build.
3-
</p>
1+
<div>
2+
Specify the MATLAB version for this build. Select MATLAB from the dropdown box that lists installations that are globally configured in Jenkins. You can also specify a custom value for MATLAB installation by choosing 'Custom...' and enter the full path to the MATLAB root folder .
3+
</div>

src/main/resources/config.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ matlab.command.build.step.name = runMATLABCommand
2323
matlab.tests.build.step.name = runMATLABTests
2424
matlab.command.step.display.name = Run MATLAB commands, scripts, or functions
2525
matlab.tests.step.display.name = Run MATLAB tests and generate artifacts
26-
matlab.custom.location = Custom..
26+
matlab.custom.location = Custom...
2727
Axis.matlab.key = MATLAB
28+
Axis.use.matlab.warning = 'Use MATLAB version' is selected. MATLAB axis values will be overridden with 'Use MATLAB Version' values.
29+
Axis.no.installed.matlab.error = No MATLAB installation is configured. Default configuration will be executed.
30+
Use.matlab.version.axis.warning = 'MATLAB' Axis is added. MATLAB axis values will be overridden with 'Use MATLAB Version' values.

0 commit comments

Comments
 (0)