Skip to content

Commit

Permalink
Merge pull request #204 from nils-a/feature/GH-198
Browse files Browse the repository at this point in the history
(#198) enabled using a local installed Cake .NET Tool
  • Loading branch information
nils-a authored Dec 1, 2021
2 parents 6dc090b + 57dc043 commit 60be5a2
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 29 deletions.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 13 additions & 3 deletions docs/input/docs/integrations/editors/rider/run-configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ In order to use the run configurations you must have Cake installed on your mach
</p>
<pre><code class="language-cmd hljs">dotnet tool install --global Cake.Tool</code></pre>
<p>
This is the default setup in Cake for Rider.
This is the default setup in Cake for Rider. Alternatively it is also possible to use a project local installation of the Cake .NET Tool.
</p>
</div>
<div id="frosting" class="tab-pane fade">
Expand Down Expand Up @@ -112,6 +112,14 @@ in the [Cake Tasks window](#running-builds-directly).
### Runner settings

Allows to define the runner to use.

The first option, *Use 'dotnet cake' instead of executable*, will use `dotnet cake` as the runner.
The `dotnet` executable must be in available in the path and the Cake .NET Tool must either be globally installed to the machine
or locally installed to the project. If this option is set, all other options on the settings page are ignored.

Alternatively, when *Use 'dotnet cake' instead of executable* is not set,
a path to the Cake executable (either `dotnet-cake.exe`, or `cake.exe`) has to be supplied:

Different runners for different operating systems can be set by defining a regular expression which is matched against the system property `os.name`.

The Cake runner settings are able to process environment variables: Use a specific variable like `${VARIABLE}` and the environment variable will be expanded when the runner is called.
Expand Down Expand Up @@ -146,8 +154,10 @@ An editor for run configurations is available:

* *Ensure .NET Tool (Global)*:
Ensures the Cake .NET tool is installed globally before running Cake.

![Run Actions](/assets/img/cake-rider/docs/beforeRunAction-ensureTool.png){.img-responsive}
* *Restore .NET Tools*:
Restores the .NET tools, effectively calling `dotnet tool restore` before running the build.

![Run Actions](/assets/img/cake-rider/docs/beforeRunActions.png){.img-responsive}

## Running builds directly

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,68 @@
<text value=""/>
</properties>
</component>
<grid id="d68b9" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="126e" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints border-constraint="North"/>
<constraints border-constraint="First"/>
<properties/>
<border type="none"/>
<children>
<component id="86e26" class="javax.swing.JLabel">
<component id="d3b77" class="javax.swing.JCheckBox" binding="useNetTool">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Default"/>
<text value="Use 'dotnet cake' instead of executable"/>
<toolTipText value="When selected, &quot;dotnet cake&quot; is used as the runner. The Cake tool must be installed as a global or local tool. All other settings on this page are ignored in that case."/>
</properties>
</component>
<component id="6b50" class="javax.swing.JTextField" binding="cakeRunnerField">
<grid id="d68b9" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<toolTipText value="This is the tool to used to run cake."/>
</properties>
</component>
<properties/>
<border type="none"/>
<children>
<component id="86e26" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Default"/>
</properties>
</component>
<component id="6b50" class="javax.swing.JTextField" binding="cakeRunnerField">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<toolTipText value="This is the tool to used to run cake."/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="6b33f" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="6b33f" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints border-constraint="Center"/>
<properties/>
<border type="none"/>
<children>
<component id="5215b" class="javax.swing.JLabel" binding="tableLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Overrides"/>
</properties>
</component>
<grid id="28141" layout-manager="GridBagLayout">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="1" fill="0" indent="0" use-parent-layout="false"/>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="1" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down Expand Up @@ -92,7 +111,7 @@
</grid>
<component id="8ddbf" class="javax.swing.JTable" binding="overrides">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import net.cakebuild.shared.ui.RegexCellEditor;

import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.awt.Component;
import java.util.Hashtable;
import java.util.Map;

Expand All @@ -17,10 +23,11 @@ public class CakeRunnerSettingsEditor {
private JButton removeButton;
private JLabel errorText;
private JLabel tableLabel;
private JCheckBox useNetTool;
private final DefaultTableModel model;

public CakeRunnerSettingsEditor() {
model = (DefaultTableModel) overrides.getModel();
model = new MyTableModel(useNetTool);
model.addColumn("OS Regex");
model.addColumn("Override");
overrides.setModel(model);
Expand All @@ -32,7 +39,7 @@ public CakeRunnerSettingsEditor() {
overrides.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
overrides.getSelectionModel().addListSelectionListener(e -> {
int row = overrides.getSelectedRow();
removeButton.setEnabled(row >= 0);
removeButton.setEnabled(!useNetTool.isSelected() && row >= 0);
});
RegexCellEditor regexCellEditor = new RegexCellEditor();
regexCellEditor.setOnValidationError(s -> { setValidationError(s); return null; });
Expand All @@ -57,8 +64,11 @@ public CakeRunnerSettingsEditor() {
model.removeRow(row);
});
tableLabel.setToolTipText("Current os.name: "+System.getProperty("os.name"));
useNetTool.addActionListener(e -> updateEnabledState());
}

public JCheckBox getUseNetTool() { return useNetTool; }

public JPanel getContent() { return content; }

public JTextField getCakeRunnerField() { return cakeRunnerField; }
Expand All @@ -83,6 +93,13 @@ public void setCakeRunnerOverrides(Map<String, String> overrides) {
overrides.forEach((regex, override) -> model.addRow(new Object[]{regex, override}));
}

public void updateEnabledState() {
boolean enableExeSetting = !useNetTool.isSelected();
cakeRunnerField.setEnabled(enableExeSetting);
addButton.setEnabled(enableExeSetting);
removeButton.setEnabled(enableExeSetting);
}

private void setValidationError(String error) {
if(error == null) {
errorText.setText("");
Expand All @@ -91,4 +108,19 @@ private void setValidationError(String error) {

errorText.setText(error);
}

static class MyTableModel extends DefaultTableModel {

private final JCheckBox checkBox;

public MyTableModel(JCheckBox checkBox){

this.checkBox = checkBox;
}

@Override
public boolean isCellEditable(int row, int column) {
return !checkBox.isSelected();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package net.cakebuild.beforeRunner

import com.intellij.execution.BeforeRunTask
import com.intellij.execution.BeforeRunTaskProvider
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.configurations.RunConfiguration
import com.intellij.execution.runners.ExecutionEnvironment
import com.intellij.execution.util.ExecUtil
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.util.Key
import icons.CakeIcons
import net.cakebuild.run.CakeConfiguration
import net.cakebuild.shared.CakeBalloonNotifications

@Suppress("DialogTitleCapitalization")
class RestoreNetToolsBeforeRunTaskProvider :
BeforeRunTaskProvider<RestoreNetToolsBeforeRunTaskProvider.RestoreNetToolsBeforeRunTask>() {
companion object {
val providerId = Key.create<RestoreNetToolsBeforeRunTask>("Cake.RestoreNetToolsBeforeRunTask")
}

private val log = Logger.getInstance(RestoreNetToolsBeforeRunTaskProvider::class.java)

override fun getId() = providerId

override fun getName() = "Restore .NET Tools"

override fun getIcon() = CakeIcons.CakeAction

override fun createTask(runConfiguration: RunConfiguration): RestoreNetToolsBeforeRunTask? {
if (runConfiguration is CakeConfiguration) {
return RestoreNetToolsBeforeRunTask()
}

return null
}

override fun executeTask(
context: DataContext,
configuration: RunConfiguration,
env: ExecutionEnvironment,
task: RestoreNetToolsBeforeRunTask
): Boolean {
val commandLine = GeneralCommandLine()
.withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE)
.withExePath("dotnet")
.withParameters("tool", "restore")
.withWorkDirectory((configuration as CakeConfiguration).getWorkingDirectory())
val output = ExecUtil.execAndGetOutput(commandLine)
if (output.exitCode != 0) {
val err = "'${commandLine.commandLineString}' exited with unexpected Exitcode. Exitcode: ${output.exitCode}"
log.warn(err)
log.warn(output.stdout)
log.warn(output.stderr)
CakeBalloonNotifications.notifyError(env.project, err)
}

return output.exitCode == 0
}

class RestoreNetToolsBeforeRunTask :
BeforeRunTask<RestoreNetToolsBeforeRunTask>(providerId)
}
11 changes: 10 additions & 1 deletion src/rider/src/main/kotlin/net/cakebuild/run/CakeConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class CakeConfiguration(project: Project, factory: CakeConfigurationFactory) :
options.verbosity = verbosity
}

fun getWorkingDirectory(): String {
val fileSystems = FileSystems.getDefault()
val scriptPath = fileSystems
.getPath(project.basePath!!)
.resolve(fileSystems.getPath(options.scriptPath!!))
return scriptPath.parent.toString()
}

override fun getState(executor: Executor, environment: ExecutionEnvironment): RunProfileState {
return object : CommandLineState(environment) {
override fun startProcess(): ProcessHandler {
Expand All @@ -40,6 +48,7 @@ class CakeConfiguration(project: Project, factory: CakeConfigurationFactory) :
.getPath(project.basePath!!)
.resolve(fileSystems.getPath(options.scriptPath!!))
val arguments = mutableListOf<String>()
arguments.addAll(runner.drop(1))
arguments.add(scriptPath.toString())
arguments.add("--target=${options.taskName}")
arguments.add("--verbosity=${options.verbosity}")
Expand All @@ -49,7 +58,7 @@ class CakeConfiguration(project: Project, factory: CakeConfigurationFactory) :
val commandLine = GeneralCommandLine()
.withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE)
.withWorkDirectory(scriptPath.parent.toString())
.withExePath(runner)
.withExePath(runner[0])
.withParameters(arguments)
log.trace("calling cake: ${commandLine.commandLineString}")
val processHandler = ProcessHandlerFactory.getInstance().createColoredProcessHandler(commandLine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class CakeRunnerSettingsConfigurable(private val project: Project) : Configurabl
val settings = CakeSettings.getInstance(project)
settings.cakeRunner = editor.cakeRunnerField.text
settings.cakeRunnerOverrides = editor.cakeRunnerOverrides
settings.cakeUseNetTool = editor.useNetTool.isSelected
}

override fun createComponent(): JComponent? {
Expand All @@ -35,6 +36,10 @@ class CakeRunnerSettingsConfigurable(private val project: Project) : Configurabl
if (isModified(settings.cakeRunnerOverrides, editor.cakeRunnerOverrides)) {
return true
}

if (isModified(editor.useNetTool, settings.cakeUseNetTool)) {
return true
}
return false
}

Expand All @@ -43,6 +48,8 @@ class CakeRunnerSettingsConfigurable(private val project: Project) : Configurabl
val settings = CakeSettings.getInstance(project)
editor.cakeRunnerField.text = settings.cakeRunner
editor.cakeRunnerOverrides = settings.cakeRunnerOverrides
editor.useNetTool.isSelected = settings.cakeUseNetTool
editor.updateEnabledState()
}

override fun getDisplayName(): String {
Expand Down
12 changes: 8 additions & 4 deletions src/rider/src/main/kotlin/net/cakebuild/settings/CakeSettings.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.cakebuild.settings

import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.diagnostic.Logger
Expand All @@ -16,14 +15,15 @@ import net.cakebuild.shared.Constants
class CakeSettings : PersistentStateComponent<CakeSettings> {
companion object {
fun getInstance(project: Project): CakeSettings {
return ServiceManager.getService(project, CakeSettings::class.java)
return project.getService(CakeSettings::class.java)
}
}

private val log = Logger.getInstance(CakeSettings::class.java)

var cakeTaskParsingRegex = "Task\\s*?\\(\\s*?\"(.*?)\"\\s*?\\)"
var cakeVerbosity = "normal"
var cakeUseNetTool = false
var cakeRunner = "~/.dotnet/tools/dotnet-cake"
var cakeRunnerOverrides = mutableMapOf(Pair("^.*windows.*$", "\${USERPROFILE}\\.dotnet\\tools\\dotnet-cake.exe"))
var cakeScriptSearchPaths: Collection<String> = mutableListOf(".")
Expand All @@ -44,7 +44,11 @@ class CakeSettings : PersistentStateComponent<CakeSettings> {
var downloadContentUrlBootstrapperNetToolSh =
"https://cakebuild.net/download/bootstrapper/dotnet-tool/bash"

fun getCurrentCakeRunner(): String {
fun getCurrentCakeRunner(): Array<String> {
if (cakeUseNetTool) {
return arrayOf("dotnet", "cake")
}

val os = System.getProperty("os.name")
var runner = cakeRunner
cakeRunnerOverrides.forEach forEach@{
Expand Down Expand Up @@ -73,7 +77,7 @@ class CakeSettings : PersistentStateComponent<CakeSettings> {
}

log.trace("resolved Cake runner to $runner")
return runner
return arrayOf(runner)
}

override fun getState(): CakeSettings {
Expand Down
1 change: 1 addition & 0 deletions src/rider/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<projectService serviceImplementation="net.cakebuild.settings.CakeSettings" />
<errorHandler implementation="net.cakebuild.shared.GitHubErrorReporter" />
<stepsBeforeRunProvider implementation="net.cakebuild.beforeRunner.EnsureNetToolBeforeRunTaskProvider"/>
<stepsBeforeRunProvider implementation="net.cakebuild.beforeRunner.RestoreNetToolsBeforeRunTaskProvider"/>
<createFromTemplateHandler implementation="net.cakebuild.actions.CakeCreateFromTemplateHandler"/>
<defaultLiveTemplates file="/liveTemplates/Cake.xml"/>
<liveTemplateContext implementation="net.cakebuild.liveTemplates.CakeContext"/>
Expand Down
Loading

0 comments on commit 60be5a2

Please sign in to comment.