Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Fix symlink validation & git utilities (#843)
Browse files Browse the repository at this point in the history
# XRTK - Mixed Reality Toolkit Pull Request

## Overview
<!-- Please provide a clear and concise description of the pull request. -->

- Better symblolic link validation
- Better git utilities
  • Loading branch information
StephenHodgson committed May 19, 2021
1 parent fdc4973 commit 262fa99
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 225 deletions.
11 changes: 11 additions & 0 deletions Editor/Utilities/EditorPreferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ namespace XRTK.Editor.Utilities
/// <summary>
/// Convenience class for setting Editor Preferences with <see cref="Application.productName"/> as key prefix.
/// </summary>
[InitializeOnLoad]
public static class EditorPreferences
{
static EditorPreferences()
{
applicationProductName = Application.productName;
applicationDataPath = Application.dataPath;
}

private static string applicationDataPath;

public static string ApplicationDataPath = applicationDataPath ?? (applicationDataPath = Application.dataPath);

private static string applicationProductName = null;

public static string ApplicationProductName => applicationProductName ?? (applicationProductName = Application.productName);
Expand Down
70 changes: 53 additions & 17 deletions Editor/Utilities/GitUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ namespace XRTK.Editor.Utilities
{
public static class GitUtilities
{
private static bool? isGitInstalled = null;

public static bool IsGitInstalled
{
get
{
if (!isGitInstalled.HasValue)
{
isGitInstalled = new Process().Run("git --version", out var message) && !message.Contains("'git' is not recognized");
}

if (!isGitInstalled.Value)
{
Debug.LogWarning("Git installation not found on this machine! Please validate your git installation or environment variables.");
}

return isGitInstalled.Value;
}
}

/// <summary>
/// Returns the root directory of this repository.
/// </summary>
Expand All @@ -31,25 +51,28 @@ public static string RepositoryRootDir
{
if (!string.IsNullOrEmpty(projectRootDir)) { return projectRootDir; }

if (new Process().Run($@"cd ""{Application.dataPath}"" && git rev-parse --show-toplevel", out var rootDir))
if (!IsGitInstalled)
{
return projectRootDir = rootDir.ToBackSlashes().Replace("\n", string.Empty);
projectRootDir = Directory.GetParent(Directory.GetParent(EditorPreferences.ApplicationDataPath).FullName).FullName;
}
else
{
if (new Process().Run($@"cd ""{Application.dataPath}"" && git rev-parse --show-toplevel", out var rootDir))
{
projectRootDir = rootDir.ToBackSlashes().Replace("\n", string.Empty);
}
}

Debug.LogWarning($"git command failed! Do you have git installed?\n{rootDir}");
return projectRootDir = Directory.GetParent(Directory.GetParent(Application.dataPath).FullName).FullName;
return projectRootDir;
}
}

private static string projectRootDir;

internal static bool HasSubmodules => File.Exists($"{RepositoryRootDir}/.gitmodules");
internal static bool HasSubmodules => IsGitInstalled && File.Exists($"{RepositoryRootDir}/.gitmodules");

[MenuItem("Assets/Submodules/Force update all submodules", true, 23)]
public static bool ForceUpdateSubmodulesValidation()
{
return HasSubmodules;
}
public static bool ForceUpdateSubmodulesValidation() => HasSubmodules;

[MenuItem("Assets/Submodules/Force update all submodules", false, 23)]
public static void ForceUpdateSubmodules()
Expand All @@ -64,6 +87,11 @@ public static void ForceUpdateSubmodules()
/// <param name="ignoredPath"></param>
public static void WritePathToGitIgnore(string ignoredPath)
{
if (!IsGitInstalled)
{
return;
}

if (string.IsNullOrEmpty(ignoredPath))
{
Debug.LogError("You cannot pass null or empty parameter.");
Expand Down Expand Up @@ -130,20 +158,23 @@ internal static bool UpdateSubmodules()
{
if (HasSubmodules)
{
bool success = false;
EditorUtility.DisplayProgressBar("Updating Submodules...", "Please wait...", 0.5f);

var isGitInstalled = new Process().Run("git --version", out var message) && !message.Contains("'git' is not recognized");

if (isGitInstalled)
try
{
success = new Process().Run($@"cd ""{RepositoryRootDir}"" && git submodule update --init --recursive", out _);
}
catch (Exception e)
{
Debug.LogError(e);
}
finally
{
var success = new Process().Run($@"cd ""{RepositoryRootDir}"" && git submodule update --init --all", out _);

EditorUtility.ClearProgressBar();
return success;
}

EditorUtility.ClearProgressBar();
Debug.LogError(message);
return success;
}

return true;
Expand All @@ -156,6 +187,11 @@ internal static bool UpdateSubmodules()
/// <returns>A list of tags from the remote repository.</returns>
public static async Task<IEnumerable<string>> GetAllTagsFromRemoteAsync(string url)
{
if (!IsGitInstalled)
{
return new List<string>(0);
}

var result = await new Process().RunAsync($"git ls-remote --tags {url}");

if (result.ExitCode != 0 || !(result.Output.Length > 0))
Expand Down
30 changes: 25 additions & 5 deletions Editor/Utilities/SymbolicLinks/SymbolicLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,48 @@

using System;
using UnityEngine;
using XRTK.Extensions;

namespace XRTK.Editor.Utilities.SymbolicLinks
{
[Serializable]
public class SymbolicLink
{
public SymbolicLink() { }

public SymbolicLink(string sourceRelativePath, string targetRelativePath)
{
this.sourceRelativePath = sourceRelativePath;
this.targetRelativePath = targetRelativePath;
}

[SerializeField]
private string sourceRelativePath;

public string SourceRelativePath
{
get => sourceRelativePath;
internal set => sourceRelativePath = value;
get => sourceRelativePath.ToBackSlashes();
internal set => sourceRelativePath = value.ToBackSlashes();
}

public string SourceAbsolutePath
{
get => $"{SymbolicLinker.ProjectRoot}{sourceRelativePath}".ToBackSlashes();
internal set => sourceRelativePath = value.ToBackSlashes().Replace(SymbolicLinker.ProjectRoot, string.Empty);
}

[SerializeField]
private string targetRelativePath;

public string TargetRelativePath
{
get => targetRelativePath;
internal set => targetRelativePath = value;
get => targetRelativePath.ToBackSlashes();
internal set => targetRelativePath = value.ToBackSlashes();
}
public string TargetAbsolutePath
{
get => $"{SymbolicLinker.ProjectRoot}{targetRelativePath}".ToBackSlashes();
internal set => targetRelativePath = value.ToBackSlashes().Replace(SymbolicLinker.ProjectRoot, string.Empty);
}

[SerializeField]
Expand All @@ -43,4 +63,4 @@ public bool IsActive
}
}
}
}
}
Loading

0 comments on commit 262fa99

Please sign in to comment.