Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on attempt to commit with unsaved scenes #2071

Merged
merged 16 commits into from
Apr 20, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using JetBrains.DataFlow;
using JetBrains.Lifetimes;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Feature.Services.Cpp.Daemon;
using JetBrains.ReSharper.Plugins.Unity.ProjectModel;
using JetBrains.ReSharper.Plugins.Unity.Settings;
using JetBrains.ReSharper.Psi.Cpp.Tree;
Expand All @@ -16,7 +17,7 @@ public class UnityHlslAllErrorsPredicate : IHlslUnresolvedUnqualifiedNamesErrors
private readonly IProperty<bool> mySuppressShaderErrors;
private readonly IProperty<bool> mySuppressShaderErrorsInRenderPipeline;

public UnityHlslAllErrorsPredicate(Lifetime lifetime, ISolution solution,
public UnityHlslAllErrorsPredicate(Lifetime lifetime,
UnitySolutionTracker unitySolutionTracker,
IApplicationWideContextBoundSettingStore settingsStore)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ private void AdviseFrontendToUnityModel(Lifetime lifetime, FrontendBackendModel
? RdTask<RunMethodResult>.Cancelled()
: backendUnityModel.RunMethodInUnity.Start(l, data).ToRdTask(l);
});

frontendBackendModel.HasUnsavedScenes.Set((l, u) =>
backendUnityModelProperty.Maybe.ValueOrDefault?.HasUnsavedScenes.Start(l, u).ToRdTask(l));
}

private void AdviseUnityToFrontendModel(Lifetime lifetime, BackendUnityModel backendUnityModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public IOptionNode GetOptionTree(ITreeNode node, CodeFormattingContext context)
public string OpeningHighlightingId => null;

public string ClosingHighlightingId => null;
public bool IgnoreRegionIfClosingNodeIsNull { get; }
}

public static string GetIndentInCgProgram(ITreeNode node)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using JetBrains.ReSharper.Psi.Cpp.Daemon;
using JetBrains.ReSharper.Feature.Services.Cpp.Daemon.Highlightings;
using NUnit.Framework;

namespace JetBrains.ReSharper.Plugins.Unity.Tests.ShaderLab.Daemon.Hlsl
Expand Down
4 changes: 2 additions & 2 deletions rider/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
id 'idea'
id 'org.jetbrains.kotlin.jvm' version '1.4.20'
id 'org.jetbrains.intellij' // Version comes from buildSrc
id 'org.jetbrains.changelog' version '0.6.0'
id 'org.jetbrains.changelog' version '1.1.2'
id 'com.jetbrains.rdgen' version '0.203.161'
id 'com.ullink.nuget' version '2.22'
id 'com.ullink.nunit' version '2.1'
Expand Down Expand Up @@ -44,7 +44,7 @@ logger.lifecycle("version=$version")
logger.lifecycle("BuildConfiguration=$BuildConfiguration")

changelog {
path = "../CHANGELOG.md"
path = file("${project.projectDir}/../CHANGELOG.md")
headerParserRegex = new Regex(~"\\d+\\.\\d+(\\.\\d+)?.*")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,7 @@ object BackendUnityModel: Root() {
// Unit testing
property("unitTestLaunch", UnitTestLaunch).documentation = "Set the details of the current unit test session"
call("runUnitTestLaunch", void, bool).documentation = "Start the unit test session. Results are fired via UnitTestLaunch.TestResult"

call ("hasUnsavedScenes", void, bool)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,7 @@ object FrontendBackendModel : Ext(SolutionModel.Solution) {
property("riderFrontendTests", bool)
call("runMethodInUnity", Library.RunMethodData, Library.RunMethodResult)
property("isDeferredCachesCompletedOnce", bool)

call ("hasUnsavedScenes", void, bool)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class DumpUnityExplorerAction : DumpAction("Dump Unity Explorer") {
writer.append(" ".repeat(indent))
writer.append(m.name)
writer.append(" ")
writer.appendln(m.value.toString())
writer.appendLine(m.value.toString())
if (m.children.any()) {
if (m is AbstractTreeNode<*>)
dumpReq(m,indent + 1, writer)
else
{
writer.appendln(" unexpected node: " + m.toString())
writer.appendLine(" unexpected node: $m")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.jetbrains.rider.plugins.unity.ui.vcs

import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.vcs.CheckinProjectPanel
import com.intellij.openapi.vcs.changes.CommitContext
import com.intellij.openapi.vcs.changes.CommitExecutor
import com.intellij.openapi.vcs.changes.ui.BooleanCommitOption
import com.intellij.openapi.vcs.checkin.CheckinHandler
import com.intellij.openapi.vcs.checkin.CheckinHandlerFactory
import com.intellij.openapi.vcs.ui.RefreshableOnComponent
import com.intellij.util.PairConsumer
import com.jetbrains.rd.framework.impl.RpcTimeouts
import com.jetbrains.rider.model.unity.frontendBackend.frontendBackendModel
import com.jetbrains.rider.projectView.solution

/**
* Checks if there are unsaved scenes in Unity.
*/
class UnsavedSceneCheckinHandlerFactory : CheckinHandlerFactory() {
override fun createHandler(panel: CheckinProjectPanel, commitContext: CommitContext): CheckinHandler =
UnresolvedMergeCheckHandler(panel)
}

private val logger = Logger.getInstance(UnsavedSceneCheckinHandlerFactory::class.java)

private class UnresolvedMergeCheckHandler(
private val panel: CheckinProjectPanel
) : CheckinHandler() {

private val project = panel.project
private val settings = UnityCheckinState.getService(project)
override fun getBeforeCheckinConfigurationPanel(): RefreshableOnComponent =
BooleanCommitOption(panel, "Check unsaved Unity scenes", false,
settings::checkUnsavedScenes)

override fun beforeCheckin(
executor: CommitExecutor?,
additionalDataConsumer: PairConsumer<Any, Any>
): ReturnResult {
if (settings.checkUnsavedScenes)
{
var providerResult = false
try {
providerResult = project.solution.frontendBackendModel.hasUnsavedScenes
.sync(Unit, RpcTimeouts(200L, 200L))
} catch (t: Throwable) {
logger.warn("Unable to fetch hasUnsavedScenes", t)
}

if (providerResult) return askUser()
}

return ReturnResult.COMMIT
}

private fun askUser(): ReturnResult {
val dialogResult = Messages.showOkCancelDialog(
project,
"Unsaved changes in Unity scenes will not be included in the commit",
"Unsaved Unity Scenes",
"Commit Anyway",
"Cancel",
Messages.getWarningIcon()
)

if (dialogResult == Messages.OK) return ReturnResult.COMMIT
return ReturnResult.CANCEL
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.jetbrains.rider.plugins.unity.ui.vcs

import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.StoragePathMacros
import com.intellij.openapi.project.Project
import com.intellij.util.getAttributeBooleanValue
import com.jetbrains.rider.util.idea.getService
import org.jdom.Element

@State(name = "UnityCheckinConfiguration", storages = [(Storage(StoragePathMacros.WORKSPACE_FILE))])
class UnityCheckinState(val project: Project) : PersistentStateComponent<Element> {

companion object {
fun getService(project: Project) = project.getService<UnityCheckinState>()
const val attributeName = "checkUnsavedScenes"
}

var checkUnsavedScenes: Boolean = true

override fun getState(): Element {
val element = Element("state")
element.setAttribute(attributeName, checkUnsavedScenes.toString())
return element
}

override fun loadState(element: Element) {
val attributeValue = element.getAttributeBooleanValue(attributeName)
checkUnsavedScenes = attributeValue
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ import com.jetbrains.rider.plugins.unity.packageManager.PackageManagerListener
import com.jetbrains.rider.plugins.unity.packageManager.PackageSource
import com.jetbrains.rider.projectView.solutionDirectory
import com.jetbrains.rider.projectView.workspace.RiderEntitySource
import com.jetbrains.rider.projectView.workspace.impl.addRiderModuleEntity
import com.jetbrains.rider.projectView.workspace.getOrCreateRiderModuleEntity

class UnityWorkspaceModelUpdater(private val project: Project) {

companion object {
private const val PackagesModuleName = "UnityPackages"
}

init {
application.invokeLater {
rebuildWorkspaceModel()
Expand All @@ -43,7 +38,7 @@ class UnityWorkspaceModelUpdater(private val project: Project) {
val builder = WorkspaceEntityStorageBuilder.create()
val virtualFileUrlManager = VirtualFileUrlManager.getInstance(project)

val packagesModuleEntity = builder.addRiderModuleEntity(PackagesModuleName, RiderUnityEntitySource)
val packagesModuleEntity = builder.getOrCreateRiderModuleEntity()
van800 marked this conversation as resolved.
Show resolved Hide resolved

// TODO: WORKSPACEMODEL
// We want to include list of special files (by extensions comes from unity editor)
Expand Down
3 changes: 3 additions & 0 deletions rider/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@
<solutionManagerExtensions implementation="com.jetbrains.rider.plugins.unity.workspace.UnitySolutionManagerExtensions"/>

<diff.merge.external.AutomaticExternalMergeTool implementation="com.jetbrains.rider.plugins.unity.diff.UnityYamlAutomaticExternalMergeTool"/>

<checkinHandlerFactory implementation="com.jetbrains.rider.plugins.unity.ui.vcs.UnsavedSceneCheckinHandlerFactory"/>
<projectService serviceImplementation="com.jetbrains.rider.plugins.unity.ui.vcs.UnityCheckinState"/>
</extensions>

<project-components>
Expand Down
17 changes: 17 additions & 0 deletions unity/EditorPlugin/AfterUnity56/UnitTesting/Initialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using JetBrains.Diagnostics;
using JetBrains.Rd.Tasks;
using UnityEditor;
using UnityEngine.SceneManagement;

namespace JetBrains.Rider.Unity.Editor.AfterUnity56.UnitTesting
{
Expand Down Expand Up @@ -33,6 +34,22 @@ public static void OnModelInitializationHandler(UnityModelAndLifetime modelAndLi
var testLauncher = new UnityEditorTestLauncher(model.UnitTestLaunch.Value, connectionLifetime);
return testLauncher.TryLaunchUnitTests();
});

GetUnsavedChangesInScenes(modelAndLifetime);
}

private static void GetUnsavedChangesInScenes(UnityModelAndLifetime modelAndLifetime)
{
modelAndLifetime.Model.HasUnsavedScenes.Set(rdVoid =>
{
var count = SceneManager.sceneCount;
for (var i = 0; i < count; i++)
{
if (SceneManager.GetSceneAt(i).isDirty)
return true;
}
return false;
} );
}
}
}