Skip to content

Commit

Permalink
Merge pull request #21 from CandyCoded/feature/file-watcher
Browse files Browse the repository at this point in the history
[feat] File watcher
  • Loading branch information
neogeek authored Jan 3, 2020
2 parents 1700001 + 8229c18 commit 08660b1
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information.

#if UNITY_EDITOR
using System.IO;
using UnityEditor;
using UnityEngine;

namespace CandyCoded.GitStatus
{

[InitializeOnLoad]
public static class FileWatcher
{

public delegate void EventHandler();

public static event EventHandler UpdateEvent;

private static readonly FileSystemWatcher _fileSystemWatcher;

private static bool _changeRegistered;

static FileWatcher()
{

if (_fileSystemWatcher != null)
{

return;

}

_fileSystemWatcher = new FileSystemWatcher(Application.dataPath);

_fileSystemWatcher.Created += OnChanged;
_fileSystemWatcher.Changed += OnChanged;
_fileSystemWatcher.Deleted += OnChanged;

_fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
_fileSystemWatcher.Filter = "*.*";
_fileSystemWatcher.IncludeSubdirectories = true;
_fileSystemWatcher.EnableRaisingEvents = true;

}

private static void OnChanged(object sender, FileSystemEventArgs e)
{

if (e.FullPath.EndsWith(".meta"))
{

return;

}

if (!_changeRegistered)
{

EditorApplication.update += OnAllChanged;

_changeRegistered = true;

}

}

private static void OnAllChanged()
{

UpdateEvent?.Invoke();

EditorApplication.update -= OnAllChanged;

_changeRegistered = false;

}

}

}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information.

#if UNITY_EDITOR
using System;
using UnityEditor;

namespace CandyCoded.GitStatus
{

[InitializeOnLoad]
public static class GitStatus
{

public static string branch;

public static string[] branches;

public static string[] changedFiles;

public static string[] untrackedFiles;

public static DateTime lastUpdated;

static GitStatus()
{

FileWatcher.UpdateEvent -= Update;
FileWatcher.UpdateEvent += Update;

Update();

}

public static void Update()
{

branch = Git.Branch();

branches = Git.Branches();

changedFiles = Git.ChangedFiles();

untrackedFiles = Git.UntrackedFiles();

lastUpdated = DateTime.Now;

}

}

}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

#if UNITY_EDITOR
using System;
using System.Collections;
using Unity.EditorCoroutines.Editor;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace CandyCoded.GitStatus
Expand All @@ -14,22 +11,6 @@ namespace CandyCoded.GitStatus
public class GitStatusPanel : EditorWindow
{

private readonly EditorWaitForSeconds _delayBetweenUpdates = new EditorWaitForSeconds(60);

private string _branch;

private string[] _branches;

private string[] _changedFiles;

private string[] _untrackedFiles;

private DateTime _lastUpdated;

private EditorCoroutine _coroutine;

private bool _isEditorFocused;

[MenuItem("Git/Git Status")]
public static void ShowWindow()
{
Expand All @@ -38,117 +19,61 @@ public static void ShowWindow()

}

private void Update()
{

if (InternalEditorUtility.isApplicationActive && !_isEditorFocused)
{

UpdateData();

}

_isEditorFocused = InternalEditorUtility.isApplicationActive;

}

private void OnGUI()
{

GUILayout.Space(5);

var selectedBranch = Array.IndexOf(_branches, _branch);
var selectedBranch = Array.IndexOf(GitStatus.branches, GitStatus.branch);

selectedBranch = EditorGUILayout.Popup("Branch:", selectedBranch, _branches);
selectedBranch = EditorGUILayout.Popup("Branch:", selectedBranch, GitStatus.branches);

if (!_branches[selectedBranch].Equals(_branch))
if (!GitStatus.branches[selectedBranch].Equals(GitStatus.branch))
{

if (_changedFiles?.Length > 0)
if (GitStatus.changedFiles?.Length > 0)
{

EditorUtility.DisplayDialog(
"Unable to checkout branch",
$"Unable to checkout {_branches[selectedBranch]} as with {_changedFiles?.Length} changes. " +
$"Unable to checkout {GitStatus.branches[selectedBranch]} as with {GitStatus.changedFiles?.Length} changes. " +
"Commit, discard or stash before checking out a different branch.",
"Ok");

}
else
{

Git.CheckoutBranch(_branches[selectedBranch]);

_branch = _branches[selectedBranch];
Git.CheckoutBranch(GitStatus.branches[selectedBranch]);

}

}

GUILayout.Label($"Number of Changes: {_changedFiles?.Length}");
GUILayout.Label($"Untracked Files: {_untrackedFiles?.Length}");
GUILayout.Label($"Last Updated: {_lastUpdated}");
GUILayout.Label($"Number of Changes: {GitStatus.changedFiles?.Length}");
GUILayout.Label($"Untracked Files: {GitStatus.untrackedFiles?.Length}");
GUILayout.Label($"Last Updated: {GitStatus.lastUpdated}");

if (GUILayout.Button("Refresh"))
{

UpdateData();
GitStatus.Update();

}

}

private void UpdateData()
{

_branch = Git.Branch();

_branches = Git.Branches();

_changedFiles = Git.ChangedFiles();

_untrackedFiles = Git.UntrackedFiles();

_lastUpdated = DateTime.Now;

Repaint();

}

private IEnumerator UpdateCoroutine()
private void Update()
{

while (true)
if (EditorApplication.isPlaying || EditorApplication.isPaused)
{

UpdateData();

yield return _delayBetweenUpdates;
return;

}

}

private void OnEnable()
{

_coroutine = EditorCoroutineUtility.StartCoroutine(UpdateCoroutine(), this);

}

private void OnDisable()
{

EditorCoroutineUtility.StopCoroutine(_coroutine);

_coroutine = null;

}

private void OnFocus()
{

UpdateData();
Repaint();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,12 @@ public class ProjectPanel : AssetPostprocessor

private const float ICON_PADDING = 2;

private static string[] _changedFiles;

private static string[] _untrackedFiles;

static ProjectPanel()
{

EditorApplication.projectWindowItemOnGUI -= ProjectWindowItemOnGui;
EditorApplication.projectWindowItemOnGUI += ProjectWindowItemOnGui;

_changedFiles = Git.ChangedFiles();

_untrackedFiles = Git.UntrackedFiles();

}

private static void ProjectWindowItemOnGui(string guid, Rect selectionRect)
Expand Down Expand Up @@ -59,13 +51,13 @@ private static void ProjectWindowItemOnGui(string guid, Rect selectionRect)
iconSize - ICON_PADDING,
iconSize - ICON_PADDING);

if (_changedFiles.Contains(path))
if (GitStatus.changedFiles.Contains(path))
{

GUI.DrawTexture(rect, GitIcons.Changed, ScaleMode.ScaleToFit);

}
else if (_untrackedFiles.Contains(path))
else if (GitStatus.untrackedFiles.Contains(path))
{

GUI.DrawTexture(rect, GitIcons.Untracked, ScaleMode.ScaleToFit);
Expand All @@ -74,19 +66,6 @@ private static void ProjectWindowItemOnGui(string guid, Rect selectionRect)

}

private static void OnPostprocessAllAssets(
string[] importedAssets,
string[] deletedAssets,
string[] movedAssets,
string[] movedFromAssetPaths)
{

_changedFiles = Git.ChangedFiles();

_untrackedFiles = Git.UntrackedFiles();

}

}

}
Expand Down
3 changes: 0 additions & 3 deletions Assets/Plugins/CandyCoded.GitStatus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
"unityRelease": "13f1",
"description": "Unity editor panel that displays the current git status.",
"license": "MIT",
"dependencies": {
"com.unity.editorcoroutines": "0.0.2-preview.1"
},
"keywords": [
"unity",
"editor",
Expand Down
1 change: 0 additions & 1 deletion Packages/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"dependencies": {
"com.unity.editorcoroutines": "0.0.2-preview.1",
"com.unity.ide.rider": "1.1.1",
"com.unity.test-framework": "1.1.3"
}
Expand Down

0 comments on commit 08660b1

Please sign in to comment.