Skip to content

Instantly share code, notes, and snippets.

@yKimisaki
Created July 16, 2019 09:59
Show Gist options
  • Save yKimisaki/9ecc52c87cd7e926423a506bab519de1 to your computer and use it in GitHub Desktop.
Save yKimisaki/9ecc52c87cd7e926423a506bab519de1 to your computer and use it in GitHub Desktop.
/*
MIT License
Copyright (c) 2019 Yoshitaka Kimisaki
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.
*/
static class BuilderConstants
{
public static string ProjectRoot = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName);
public static string GeneratorsRoot = Path.Combine(ProjectRoot, "Generators");
public static string InputCsproj = Path.Combine(ProjectRoot, "MyProject.Client", "Assembly-CSharp.csproj");
public static string OutputRoot = Path.Combine(ProjectRoot, "MyProject.Client", "Assets", "Scripts", "Generated");
}
class Builder
{
[MenuItem("Build/MasterMemory/Tables")]
public static void BuildTables()
{
var exe = Path.Combine(BuilderConstants.ExternalsRoot, "MasterMemory.Generator", "win-x64", "MasterMemory.Generator.exe");
var outputPath = Path.Combine(BuilderConstants.OutputRoot, "Masters");
ProcessExecuter.Execute(exe, $"-i {Directory.GetCurrentDirectory()} -o {outputPath} -n MyProject");
}
[MenuItem("Build/CodeGenerator/MessagePackResolvers")]
public static void GenerateMessagePackResolvers()
{
var exe = Path.Combine(BuilderConstants.ExternalsRoot, "MessagePackUniversalCodeGenerator", "win-x64", "mpc.exe");
var outputPath = Path.Combine(BuilderConstants.OutputRoot, "MessagePackResolvers.Generated.cs");
ProcessExecuter.Execute(exe, $"-i {BuilderConstants.InputCsproj} -o {outputPath} -n MessagePack");
}
[MenuItem("Build/AssetBundle/Windows")]
public static void BuildWindows()
{
Build(RuntimePlatform.WindowsPlayer, BuildTarget.StandaloneWindows);
}
[MenuItem("Build/AssetBundle/Android")]
public static void BuildAndroid()
{
Build(RuntimePlatform.Android, BuildTarget.Android);
}
public static void Build(RuntimePlatform platform, BuildTarget buildTarget)
{
// AssetBuldleをとりあえずStreamingAssetsに入れておく
var exportPath = Path.Combine(Application.streamingAssetsPath, AssetLoader.GetPlatformAssetBundlesPath(platform));
Directory.CreateDirectory(exportPath);
BuildPipeline.BuildAssetBundles(
exportPath,
BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression,
buildTarget);
AssetDatabase.Refresh();
}
}
// 外部プロセスをいい感じに実行するやつ
static class ProcessExecuter
{
private static Process process;
private static string caughtError;
private static string processName;
private static string completedProcessName;
public static void Execute(string exePath, string args)
{
if (process != null)
{
return;
}
try
{
processName = Path.GetFileNameWithoutExtension(exePath);
process = new Process();
process.StartInfo.FileName = exePath;
process.StartInfo.Arguments = args;
process.EnableRaisingEvents = true;
process.ErrorDataReceived += OnReceivedProcessError;
process.Exited += OnExitedProcess;
process.Start();
}
catch
{
throw;
}
}
private static void OnReceivedProcessError(object sender, DataReceivedEventArgs e)
{
caughtError = e.Data;
}
private static void OnExitedProcess(object sender, System.EventArgs e)
{
if (sender != null && e != null)
{
completedProcessName = processName;
}
process?.Dispose();
process = null;
}
[InitializeOnLoadMethod]
private static void WatchProcess()
{
EditorApplication.update += () =>
{
if (!string.IsNullOrWhiteSpace(completedProcessName))
{
Debug.Log($"{completedProcessName} is completed.");
completedProcessName = null;
}
if (!string.IsNullOrWhiteSpace(caughtError))
{
Debug.LogError(caughtError);
caughtError = null;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment