-
Notifications
You must be signed in to change notification settings - Fork 164
/
BuildPackage.cs
80 lines (73 loc) · 2.7 KB
/
BuildPackage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
static class BuildPackage {
static string kVersionNormalLocation = "Assets/Editor/DummyVERSION.txt";
static string kVersionBuildLocation = "Assets/TiltBrush/VERSION.txt";
[System.Serializable()]
public class BuildFailedException : System.Exception {
public BuildFailedException(string fmt, params object[] args)
: base(string.Format(fmt, args)) { }
}
/// Temporarily create a VERSION.txt build stamp so we can embed it
/// in the unitypackage. Cleans up afterwards.
/// Ensures that the VERSION.txt has a consistent GUID.
class TempBuildStamp : System.IDisposable {
byte[] m_previousContents;
public TempBuildStamp(string contents) {
string err = AssetDatabase.MoveAsset(
kVersionNormalLocation, kVersionBuildLocation);
if (err != "") {
throw new BuildFailedException(
"Couldn't move {0} to {1}: {2}",
kVersionNormalLocation, kVersionBuildLocation, err);
}
m_previousContents = File.ReadAllBytes(kVersionBuildLocation);
File.WriteAllText(kVersionBuildLocation, contents);
}
public void Dispose() {
string err = AssetDatabase.MoveAsset(kVersionBuildLocation, kVersionNormalLocation);
if (err == "" && m_previousContents != null) {
File.WriteAllBytes(kVersionNormalLocation, m_previousContents);
}
}
}
static string GetGitVersion() {
// Start the child process.
var p = new System.Diagnostics.Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "git.exe";
p.StartInfo.Arguments = "describe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
var version = p.StandardOutput.ReadToEnd().Trim();
p.WaitForExit();
if (p.ExitCode != 0) {
throw new BuildFailedException("git describe exited with code {0}", p.ExitCode);
}
return version;
}
[MenuItem("Tilt Brush/Build Package")]
static void DoBuild() {
string version = GetGitVersion();
string name = string.Format("../../tilt-brush-toolkit-UnitySDK-{0}.unitypackage", version);
using (var tmp = new TempBuildStamp(version)) {
AssetDatabase.ExportPackage(
new string[] {
"Assets/ThirdParty",
"Assets/TiltBrush",
},
name,
ExportPackageOptions.Recurse);
Debug.LogFormat("Done building {0}", name);
}
}
}