diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/GitHub.Unity.csproj b/src/UnityExtension/Assets/Editor/GitHub.Unity/GitHub.Unity.csproj index 1ddcf006f..d2ecc2a53 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/GitHub.Unity.csproj +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/GitHub.Unity.csproj @@ -91,8 +91,6 @@ - - diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/Misc/Utility.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/Misc/Utility.cs index 66cb612b9..e645e5654 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/Misc/Utility.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/Misc/Utility.cs @@ -1,9 +1,7 @@ using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; -using System.Text.RegularExpressions; using UnityEditor; using UnityEngine; @@ -11,65 +9,6 @@ namespace GitHub.Unity { class Utility : ScriptableObject { - private static readonly ILogging logger = Logging.GetLogger(); - - public const string StatusRenameDivider = "->"; - public static readonly Regex ListBranchesRegex = - new Regex(@"^(?\*)?\s+(?[\w\d\/\-_]+)\s*(?:[a-z|0-9]{7} \[(?[\w\d\/\-\_]+)\])?"); - public static readonly Regex ListRemotesRegex = - new Regex(@"(?[\w\d\-_]+)\s+(?https?:\/\/(?(?[\w\d]+)(?::(?[\w\d]+))?)@(?[\w\d\.\/\%]+))\s+\((?fetch|push)\)"); - public static readonly Regex LogCommitRegex = new Regex(@"commit\s(\S+)"); - public static readonly Regex LogMergeRegex = new Regex(@"Merge:\s+(\S+)\s+(\S+)"); - public static readonly Regex LogAuthorRegex = new Regex(@"Author:\s+(.+)\s<(.+)>"); - public static readonly Regex LogTimeRegex = new Regex(@"Date:\s+(.+)"); - public static readonly Regex LogDescriptionRegex = new Regex(@"^\s+(.+)"); - public static readonly Regex StatusStartRegex = new Regex(@"(?[AMRDC]|\?\?)(?:\d*)\s+(?[\w\d\/\.\-_ \@]+)"); - public static readonly Regex StatusEndRegex = new Regex(@"->\s(?[\w\d\/\.\-_ ]+)"); - public static readonly Regex StatusBranchLineValidRegex = new Regex(@"\#\#\s+(?:[\w\d\/\-_\.]+)"); - public static readonly Regex StatusAheadBehindRegex = - new Regex( - @"\[ahead (?\d+), behind (?\d+)\]|\[ahead (?\d+)\]|\[behind (?\d+>)\]"); - - private static bool ready; - private static Action onReady; - - public static void RegisterReadyCallback(Action callback) - { - if (!ready) - { - onReady += callback; - } - else - { - try - { - callback(); - } - catch (Exception ex) - { - Debug.LogException(ex); - } - - } - } - - public static void UnregisterReadyCallback(Action callback) - { - onReady -= callback; - } - - public static void Initialize() - { - // Evaluate project settings - Issues = new List(); - } - - public static void Run() - { - ready = true; - onReady.SafeInvoke(); - } - public static Texture2D GetIcon(string filename, string filename2x = "") { if (EditorGUIUtility.pixelsPerPoint > 1f && !string.IsNullOrEmpty(filename2x)) @@ -81,73 +20,9 @@ public static Texture2D GetIcon(string filename, string filename2x = "") if (stream != null) return stream.ToTexture2D(); - var iconPath = ExtensionInstallPath.ToNPath().Combine("IconsAndLogos", filename).ToString(SlashMode.Forward); + var iconPath = EntryPoint.Environment.ExtensionInstallPath.Combine("IconsAndLogos", filename).ToString(SlashMode.Forward); return AssetDatabase.LoadAssetAtPath(iconPath); } - - public static Texture2D CreateTextureFromColor(Color color) - { - Texture2D backgroundTexture = new Texture2D(1, 1); - Color c = color; - backgroundTexture.SetPixel(1, 1, c); - backgroundTexture.Apply(); - - return backgroundTexture; - } - - public static string GitInstallPath - { - get { return EntryPoint.Environment.GitExecutablePath; } - } - - public static string GitRoot - { - get { return EntryPoint.Environment.RepositoryPath; } - } - - public static string UnityAssetsPath - { - get { return EntryPoint.Environment.UnityAssetsPath; } - } - - public static string UnityProjectPath - { - get { return EntryPoint.Environment.UnityProjectPath; } - } - - public static string ExtensionInstallPath - { - get { return EntryPoint.Environment.ExtensionInstallPath; } - } - - public static List Issues { get; protected set; } - - public static bool GitFound - { - get { return !string.IsNullOrEmpty(GitInstallPath); } - } - - public static bool ActiveRepository - { - get { return !string.IsNullOrEmpty(GitRoot); } - } - - public static bool IsDevelopmentBuild - { - get { return File.Exists(Path.Combine(UnityProjectPath.Replace('/', Path.DirectorySeparatorChar), ".devroot")); } - } - - public static Texture2D GetTextureFromColor(Color color) - { - Color[] pix = new Color[1]; - pix[0] = color; - - Texture2D result = new Texture2D(1, 1); - result.SetPixels(pix); - result.Apply(); - - return result; - } } static class StreamExtensions diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/Tasks/EvaluateProjectConfigurationTask.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/Tasks/EvaluateProjectConfigurationTask.cs deleted file mode 100644 index b45473937..000000000 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/Tasks/EvaluateProjectConfigurationTask.cs +++ /dev/null @@ -1,349 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text.RegularExpressions; -using System.Threading; -using UnityEditor; -using UnityEditorInternal; -using UnityEngine; -using Object = UnityEngine.Object; - -namespace GitHub.Unity -{ - [Flags] - enum ProjectSettingsEvaluation - { - None = 0, - EditorSettingsMissing = 1 << 0, - BadVCSSettings = 1 << 1, - BinarySerialization = 1 << 2, - MixedSerialization = 1 << 3 - } - - enum GitIgnoreRuleEffect - { - Require = 0, - Disallow = 1 - } - - abstract class ProjectConfigurationIssue - {} - - class ProjectSettingsIssue : ProjectConfigurationIssue - { - public ProjectSettingsIssue(ProjectSettingsEvaluation evaluation) - { - Evaluation = evaluation; - } - - public bool WasCaught(ProjectSettingsEvaluation evaluation) - { - return (Evaluation & evaluation) != 0; - } - - public ProjectSettingsEvaluation Evaluation { get; protected set; } - } - - class GitIgnoreException : ProjectConfigurationIssue - { - public GitIgnoreException(Exception exception) - { - Exception = exception; - } - - public Exception Exception { get; protected set; } - } - - class GitIgnoreIssue : ProjectConfigurationIssue - { - public GitIgnoreIssue(string file, string line, string description) - { - File = file; - Line = line; - Description = description; - } - - public string File { get; protected set; } - public string Line { get; protected set; } - public string Description { get; protected set; } - } - - struct GitIgnoreRule - { - private const string CountKey = "GitIgnoreRuleCount"; - private const string EffectKey = "GitIgnoreRule{0}Effect"; - private const string FileKey = "GitIgnoreRule{0}File"; - private const string LineKey = "GitIgnoreRule{0}Line"; - private const string TriggerTextKey = "GitIgnoreRule{0}TriggerText"; - - public static bool TryLoad(int index, out GitIgnoreRule result) - { - result = new GitIgnoreRule(); - - int effect; - if (!int.TryParse(EntryPoint.ApplicationManager.LocalSettings.Get(String.Format(EffectKey, index), "-1"), out effect) || effect < 0) - { - return false; - } - - result.Effect = (GitIgnoreRuleEffect)effect; - - result.FileString = EntryPoint.ApplicationManager.LocalSettings.Get(String.Format(FileKey, index)); - - try - { - result.File = new Regex(result.FileString); - } - catch (ArgumentException) - { - result.File = null; - } - - result.LineString = EntryPoint.ApplicationManager.LocalSettings.Get(String.Format(LineKey, index)); - - try - { - result.Line = new Regex(result.LineString); - } - catch (ArgumentException) - { - result.Line = null; - } - - result.TriggerText = EntryPoint.ApplicationManager.LocalSettings.Get(String.Format(TriggerTextKey, index)); - - return true; - } - - public static void Save(int index, GitIgnoreRuleEffect effect, string file, string line, string triggerText) - { - EntryPoint.ApplicationManager.LocalSettings.Set(String.Format(EffectKey, index), ((int)effect).ToString()); - EntryPoint.ApplicationManager.LocalSettings.Set(String.Format(FileKey, index), file); - EntryPoint.ApplicationManager.LocalSettings.Set(String.Format(LineKey, index), line); - EntryPoint.ApplicationManager.LocalSettings.Set(String.Format(TriggerTextKey, index), triggerText); - } - - public static void New() - { - Save(Count, GitIgnoreRuleEffect.Require, "", "", ""); - EntryPoint.ApplicationManager.LocalSettings.Set(CountKey, (Count + 1)); - } - - public static void Delete(int index) - { - EntryPoint.ApplicationManager.LocalSettings.Unset(String.Format(EffectKey, index)); - EntryPoint.ApplicationManager.LocalSettings.Unset(String.Format(FileKey, index)); - EntryPoint.ApplicationManager.LocalSettings.Unset(String.Format(LineKey, index)); - EntryPoint.ApplicationManager.LocalSettings.Unset(String.Format(TriggerTextKey, index)); - - var count = Count; - for (; index < count; ++index) - { - EntryPoint.ApplicationManager.LocalSettings.Rename(String.Format(EffectKey, index), String.Format(EffectKey, index - 1)); - EntryPoint.ApplicationManager.LocalSettings.Rename(String.Format(FileKey, index), String.Format(FileKey, index - 1)); - EntryPoint.ApplicationManager.LocalSettings.Rename(String.Format(LineKey, index), String.Format(LineKey, index - 1)); - EntryPoint.ApplicationManager.LocalSettings.Rename(String.Format(TriggerTextKey, index), String.Format(TriggerTextKey, index - 1)); - } - - EntryPoint.ApplicationManager.LocalSettings.Set(CountKey, (count - 1)); - } - - public override string ToString() - { - return String.Format("{0} \"{1}\" in \"{2}\": {3}", Effect, Line, File, TriggerText); - } - - public static int Count - { - get { return Mathf.Max(0, EntryPoint.ApplicationManager.LocalSettings.Get(CountKey, 0)); } - } - - public GitIgnoreRuleEffect Effect { get; private set; } - public string FileString { get; private set; } - public string LineString { get; private set; } - public Regex File { get; private set; } - public Regex Line { get; private set; } - public string TriggerText { get; private set; } - } - - class EvaluateProjectConfigurationTask : DataTaskBase> - { - private const string GitIgnoreFilePattern = ".gitignore"; - private const string VCSPropertyName = "m_ExternalVersionControlSupport"; - private const string SerializationPropertyName = "m_SerializationMode"; - private const string VisibleMetaFilesValue = "Visible Meta Files"; - private const string HiddenMetaFilesValue = "Hidden Meta Files"; - - private const int ThreadSyncDelay = 100; - - public static string EditorSettingsPath = "ProjectSettings/EditorSettings.asset"; - - public static void RegisterCallback(Action> callback) - { - } - - public static void UnregisterCallback(Action> callback) - { - } - - public static Object LoadEditorSettings() - { - return InternalEditorUtility.LoadSerializedFileAndForget(EditorSettingsPath).FirstOrDefault(); - } - - public EvaluateProjectConfigurationTask(CancellationToken token) - : base(token) - { - Name = "Project evaluation"; - } - - protected override List RunWithReturn(bool success) - { - RaiseOnStart(); - - foreach (var issue in EvaluateLocalConfiguration()) - RaiseOnData(issue); - - // Git config - foreach (var issue in EvaluateGitIgnore()) - RaiseOnData(issue); - - RaiseOnEnd(); - - return Result; - } - - private IEnumerable EvaluateLocalConfiguration() - { - var result = ProjectSettingsEvaluation.None; - - var settingsAsset = LoadEditorSettings(); - if (settingsAsset == null) - { - result |= ProjectSettingsEvaluation.EditorSettingsMissing; - yield break; - } - - var settingsObject = new SerializedObject(settingsAsset); - - var vcsSetting = settingsObject.FindProperty(VCSPropertyName).stringValue; - if (!vcsSetting.Equals(VisibleMetaFilesValue) && !vcsSetting.Equals(HiddenMetaFilesValue)) - { - result |= ProjectSettingsEvaluation.BadVCSSettings; - } - - var serializationSetting = (SerializationSetting)settingsObject.FindProperty(SerializationPropertyName).intValue; - if (serializationSetting == SerializationSetting.ForceBinary) - { - result |= ProjectSettingsEvaluation.BinarySerialization; - } - else if (serializationSetting == SerializationSetting.Mixed) - { - result |= ProjectSettingsEvaluation.MixedSerialization; - } - - if (result != ProjectSettingsEvaluation.None) - { - yield return new ProjectSettingsIssue(result); - } - } - - private IEnumerable EvaluateGitIgnore() - { - // Read rules - var rules = new List(GitIgnoreRule.Count); - for (var index = 0; index < rules.Capacity; ++index) - { - GitIgnoreRule rule; - if (GitIgnoreRule.TryLoad(index, out rule)) - { - rules.Add(rule); - } - } - - if (!rules.Any()) - { - yield break; - } - - // Read gitignore files - GitIgnoreFile[] files; - files = - Directory.GetFiles(Utility.GitRoot, GitIgnoreFilePattern, SearchOption.AllDirectories) - .Select(p => new GitIgnoreFile(p)) - .ToArray(); - - if (files.Length < 1) - { - yield break; - } - - // Evaluate each rule - for (var ruleIndex = 0; ruleIndex < rules.Count; ++ruleIndex) - { - var rule = rules[ruleIndex]; - for (var fileIndex = 0; fileIndex < files.Length; ++fileIndex) - { - var file = files[fileIndex]; - // Check against all files with matching path - if (rule.File == null || !rule.File.IsMatch(file.Path)) - { - continue; - } - - // Validate all lines in that file - for (var lineIndex = 0; lineIndex < file.Contents.Length; ++lineIndex) - { - var line = file.Contents[lineIndex]; - var match = rule.Line != null && rule.Line.IsMatch(line); - - if (rule.Effect == GitIgnoreRuleEffect.Disallow && match) - // This line is not allowed - { - yield return new GitIgnoreIssue(file.Path, line, rule.TriggerText); - } - else if (rule.Effect == GitIgnoreRuleEffect.Require) - // If the line is required, see if we're there - { - if (match) - // We found it! No sense in searching further in this file. - { - break; - } - else if (lineIndex == file.Contents.Length - 1) - // We reached the last line without finding it - { - yield return new GitIgnoreIssue(file.Path, string.Empty, rule.TriggerText); - } - } - } - } - } - } - - private enum SerializationSetting - { - Mixed = 0, - ForceBinary = 1, - ForceText = 2 - } - - private struct GitIgnoreFile - { - public string Path { get; private set; } - public string[] Contents { get; private set; } - - public GitIgnoreFile(string path) - { - Path = path.Substring(Utility.GitRoot.Length + 1); - Contents = File.ReadAllLines(path).Select(l => l.Trim()).Where(l => !string.IsNullOrEmpty(l)).ToArray(); - } - - public override string ToString() - { - return String.Format("{0}:{1}{2}", Path, Environment.NewLine, string.Join(Environment.NewLine, Contents)); - } - } - } -} diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/Tasks/TaskException.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/Tasks/TaskException.cs deleted file mode 100644 index f856fa39d..000000000 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/Tasks/TaskException.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Runtime.Serialization; - -namespace GitHub.Unity -{ - [Serializable] - public class TaskException : Exception - { - public TaskException() - { - } - - public TaskException(string message) : base(message) - { - } - - public TaskException(string message, Exception innerException) : base(message, innerException) - { - } - - protected TaskException(SerializationInfo info, StreamingContext context) : base(info, context) - { - } - } -} \ No newline at end of file