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

Feat/add project to hub #20

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ public class Program
private static UnityCli _untiyCli;
private static QuickStartProject _project;
private static UserSettings _userSettings;
private static UnityHub _unityHub;

static async Task Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;

_git = new Git();
_untiyCli = new UnityCli();
_unityHub = new UnityHub();
_project = new QuickStartProject();
_userSettings = new UserSettings();
await _userSettings.LoadSettings();
Expand Down Expand Up @@ -59,6 +61,7 @@ await Parser.Default.ParseArguments<Options>(args).WithParsedAsync(async options
if(createdLocalRepo) await _git.CreateRemoteRepo(_project, _userSettings);

var createdUnityProject = await _untiyCli.CreateUnityProject(_project, unityVersion, installPath);
if (createdUnityProject) await _unityHub.AddProjectToHub(_project, _project.ProjectName, _project.ProjectPath, unityVersion);
if(createdUnityProject) await _untiyCli.OpenUnityProject(_project, unityVersion, installPath);
if(createdUnityProject) Output.WriteSuccessWithTick("Complete");

Expand Down
97 changes: 97 additions & 0 deletions App/Unity/UnityHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using UnityQuickStart.App.IO;
using UnityQuickStart.App.Project;

namespace UnityQuickStart.App.Unity;

public class UnityHub
{
public async Task AddProjectToHub(QuickStartProject project, string projectName, string projectPath, string version)
{
var addToHub = UserInput.GetYesNo("Would you like to add this project to Unity Hub:");
if(!addToHub) Output.WriteSuccessWithTick("Skipped adding project to Hub");

var unityHubPath = string.Empty;
var isHubOpen = IsUnityHubOpen(out unityHubPath);
var closedHub = isHubOpen && await CloseUnityHub();

//get projects
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var projectJsonPath = Path.Combine(appData, "UnityHub", "projects-v1.json");
var projectJsonTxt = await File.ReadAllTextAsync(projectJsonPath);
var projectsJson = JObject.Parse(projectJsonTxt);

var directoryInfo = new DirectoryInfo(projectPath);
var newProject = new JObject
{
["title"] = projectName,
["lastModified"] = 1700000000000,
["isCustomEditor"] = false,
["path"] = projectPath,
["containingFolderPath"] = directoryInfo.Parent?.FullName,
["version"] = version
};
projectsJson["data"]![$@"{projectPath}"] = newProject;

//write projects
await File.WriteAllTextAsync(projectJsonPath, projectsJson.ToString());

if (closedHub)
{
await OpenHub(unityHubPath);
}
}

private async Task OpenHub(string fileName)
{
Process.Start(new ProcessStartInfo()
{
FileName = fileName,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
});
}

private bool IsUnityHubOpen(out string unityHubPath)
{
unityHubPath = string.Empty;
var processes = Process.GetProcessesByName("Unity Hub");

if (processes.Length > 0)
{
unityHubPath = processes[0].MainModule.FileName;
return true;
}

return false;
}

private async Task<bool> CloseUnityHub()
{
const string processMsg = "Closing Unity Hub";
const string fileName = "taskkill";
const string args = "/F /IM \"Unity Hub.exe\"";
var success = false;

await ProcessExecutor.ExecuteProcess(fileName,args, processMsg,
(output) =>
{
success = true;
Output.WriteSuccessWithTick($"Ok close Hub");

//wait to ensure hub to close
Task.Delay(2000);
},
(error) =>
{
success = false;
Output.WriteError($"Failed to close Hub: {error}");
});

return success;
}
}