Skip to content

Instantly share code, notes, and snippets.

@wotakuro
Last active May 29, 2020 15:42
Show Gist options
  • Save wotakuro/07de61e76172cc2b47a6350b92836477 to your computer and use it in GitHub Desktop.
Save wotakuro/07de61e76172cc2b47a6350b92836477 to your computer and use it in GitHub Desktop.
PackageManagerを自動で入れる君
/**
MIT License
Copyright (c) 2019 Yusuke Kurokawa
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.PackageManager;
using System.Diagnostics;
using System.IO;
/**
* 独自に作ったPackageのインストールが面倒になってきたのでつくりました
* gitコマンドへパスを通している必要があります。
*/
public class PackageManagerAutoInstaller
{
const string MyPackagesDir = "MyPackages";
// installするパッケージ情報
private class MyPackageInformation
{
public bool isCloneToLocal;
public string packageName;
public string packageGitUrl;
public string gitBranch;
public string gitSubDir;
public MyPackageInformation(string name, string giturl)
{
this.packageName = name;
this.packageGitUrl = giturl;
this.isCloneToLocal = false;
}
public MyPackageInformation(string name, string giturl,string branch,string subDir)
{
this.packageName = name;
this.packageGitUrl = giturl;
this.gitBranch = branch;
this.gitSubDir = subDir;
this.isCloneToLocal = true;
}
}
private List<MyPackageInformation> myPackages = new List<MyPackageInformation>();
private IEnumerator executionRoutine;
[MenuItem("Tools/MyPackages/Install")]
[InitializeOnLoadMethod]
public static void Install()
{
PackageManagerAutoInstaller installer = new PackageManagerAutoInstaller();
installer.executionRoutine = installer.InstallPackages();
EditorApplication.update += installer.Update;
}
[MenuItem("Tools/MyPackages/UpdateLocalRepo")]
public static void UpdatePackages()
{
PackageManagerAutoInstaller updater = new PackageManagerAutoInstaller();
updater.executionRoutine = updater.UpdateLocalDiskPackages();
EditorApplication.update += updater.Update;
EditorUtility.DisplayDialog("Complete", "Update localPackages at " + MyPackagesDir,"ok");
}
private PackageManagerAutoInstaller()
{
// Pacakge Listをここに追加してください
myPackages.Add(new MyPackageInformation("com.utj.framedebuggersave",
"https://github.com/wotakuro/FrameDebuggerSave.git"));
myPackages.Add(new MyPackageInformation("com.utj.uniconfigutil",
"https://github.com/wotakuro/UnityCofigUtil.git"));
myPackages.Add(new MyPackageInformation("com.utj.largefileuploader",
"https://github.com/wotakuro/UnityLargeFileUploader.git",null, "client/FileUploader"));
myPackages.Add(new MyPackageInformation("com.utj.memorysnapshot2csv",
"https://github.com/wotakuro/UnityMemorySnapshotToCsv.git") );
myPackages.Add(new MyPackageInformation("com.utj.assetsreporter",
"https://github.com/wotakuro/AssetsReporter.git") );
}
private void Update()
{
if( this.executionRoutine != null)
{
bool result = this.executionRoutine.MoveNext();
if(!result)
{
this.executionRoutine = null;
}
}
else
{
EditorApplication.update -= this.Update;
}
}
private IEnumerator InstallPackages()
{
// create package list
yield return null;
var listRequest = Client.List(true);
while(!listRequest.IsCompleted)
{
yield return null;
}
var installedPackages = new Dictionary<string, UnityEditor.PackageManager.PackageInfo>();
if (listRequest.Result != null)
{
foreach (var package in listRequest.Result)
{
installedPackages.Add(package.name, package);
}
}
// Package Install
foreach( var myPackage in this.myPackages)
{
if(!installedPackages.ContainsKey(myPackage.packageName))
{
IEnumerator req = null;
if (myPackage.isCloneToLocal)
{
req = InstallMyPackageToLocalDisk(myPackage);
}
else
{
req = InstallMyPackage(myPackage);
}
while(req.MoveNext()) { yield return null; }
}
}
}
// install
private IEnumerator InstallMyPackage(MyPackageInformation myPackage)
{
UnityEngine.Debug.Log("Install Mypackage " + myPackage.packageName);
var addReq = Client.Add(myPackage.packageGitUrl);
while (!addReq.IsCompleted)
{
yield return null;
}
}
// update local packages
private IEnumerator UpdateLocalDiskPackages()
{
string workingPath = Path.Combine(Directory.GetCurrentDirectory(), MyPackagesDir);
if (!Directory.Exists(workingPath))
{
yield break;
}
var dirs = Directory.GetDirectories(workingPath);
if( dirs == null) {
yield break;
}
foreach( var dir in dirs)
{
var exec = GitPull(dir);
while (exec.MoveNext())
{
yield return null;
}
}
}
// todo no test
private IEnumerator GitPull( string path)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "git";
startInfo.Arguments = "pull";
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.WorkingDirectory = path;
Process proc = new Process();
proc.StartInfo = startInfo;
proc.EnableRaisingEvents = true;
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
while (!proc.HasExited)
{
yield return null;
}
}
// Install Packages
private IEnumerator InstallMyPackageToLocalDisk(MyPackageInformation myPackage)
{
string workingPath = Path.Combine(Directory.GetCurrentDirectory(), MyPackagesDir);
if(!Directory.Exists(workingPath))
{
Directory.CreateDirectory(workingPath);
}
int lastSlash = myPackage.packageGitUrl.LastIndexOf('/');
string gitRepoDir = myPackage.packageGitUrl.Substring(lastSlash + 1).Replace(".git", "");
if (!Directory.Exists(Path.Combine(workingPath, gitRepoDir)))
{
ProcessStartInfo startInfo = new ProcessStartInfo();
string args = "clone " + myPackage.packageGitUrl;
if (!string.IsNullOrEmpty(myPackage.gitBranch))
{
args += " -b " + myPackage.gitBranch;
}
startInfo.FileName = "git";
startInfo.Arguments = args;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.WorkingDirectory = workingPath;
Process proc = new Process();
proc.StartInfo = startInfo;
proc.EnableRaisingEvents = true;
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
while (!proc.HasExited)
{
yield return null;
}
}
string localDir = "file:../" + MyPackagesDir + "/"+ gitRepoDir;
if( !string.IsNullOrEmpty(myPackage.gitSubDir) )
{
localDir += "/" + myPackage.gitSubDir;
}
var addReq = Client.Add(localDir);
while (!addReq.IsCompleted)
{
yield return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment