-
Notifications
You must be signed in to change notification settings - Fork 318
/
Copy pathExternalSamplesUtility.cs
136 lines (122 loc) · 5.03 KB
/
ExternalSamplesUtility.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Linq;
using System.Net;
using System.Diagnostics;
using UnityEngine.InputSystem;
public class ExternalSamplesUtility
{
static string token => System.Environment.GetEnvironmentVariable("GITHUB_AUTH");
struct ReleaseResponse
{
public string upload_url;
}
static string CallGithubAPI(string commandUrl, string contentType, string requestData = null)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(commandUrl);
httpWebRequest.ContentType = contentType;
httpWebRequest.Method = requestData != null ? "POST" : "GET";
httpWebRequest.Accept = "application/vnd.github.v3+json";
httpWebRequest.Headers["Authorization"] = $"token {token}";
httpWebRequest.UserAgent = "Unity";
httpWebRequest.Timeout = 1000000;
if (requestData != null)
{
var stream = httpWebRequest.GetRequestStream();
if (contentType == "application/binary")
{
var data = File.ReadAllBytes(requestData);
stream.Write(data, 0, data.Length);
stream.Close();
}
else
{
using (var streamWriter = new StreamWriter(stream))
{
streamWriter.Write(requestData);
}
}
}
try
{
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
UnityEngine.Debug.Log("Server said " + result);
return result;
}
}
catch (WebException e)
{
UnityEngine.Debug.Log($"Failed request {commandUrl} with data: '{requestData}'. {e}");
using (var streamReader = new StreamReader(e.Response.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
UnityEngine.Debug.Log("Server said " + result);
}
throw;
}
}
static void RunGitCommannd(string arguments)
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "git";
p.StartInfo.Arguments = arguments;
p.Start();
p.WaitForExit();
}
static string[] GetAssetsToPublish()
{
return
Directory.GetFileSystemEntries("Assets").Where(x => !x.Contains("ExternalSamplesUtility"))
.Concat(Directory.GetFileSystemEntries("ProjectSettingsBackup").Select(x => x.Replace("ProjectSettingsBackup", "ProjectSettings")))
.ToArray();
}
public static void DryRun()
{
var packagePath = $"{PlayerSettings.productName}-{InputSystem.version}.unitypackage";
AssetDatabase.ExportPackage(GetAssetsToPublish(), packagePath, ExportPackageOptions.Recurse);
UnityEngine.Debug.Log($"Created package at {packagePath}");
UnityEngine.Debug.Log("Done!");
EditorApplication.Exit(0);
}
public static void Publish()
{
var packagePath = $"{PlayerSettings.productName}-{InputSystem.version}.unitypackage";
AssetDatabase.ExportPackage(GetAssetsToPublish(), packagePath, ExportPackageOptions.Recurse);
UnityEngine.Debug.Log($"Created package at {packagePath}");
RunGitCommannd($"tag {InputSystem.version}");
RunGitCommannd($"push --tags https://stefanunity:{token}@github.com/Unity-Technologies/InputSystem.git");
string uploadUrl;
try
{
var checkResponse = CallGithubAPI($"https://api.github.com/repos/Unity-Technologies/InputSystem/releases/tags/{InputSystem.version}", "application/json");
uploadUrl = JsonUtility.FromJson<ReleaseResponse>(checkResponse).upload_url;
uploadUrl = uploadUrl.Replace("{?name,label}", $"?name={packagePath}");
}
catch (WebException e)
{
if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.NotFound)
{
var createResponse = CallGithubAPI(
"https://api.github.com/repos/Unity-Technologies/InputSystem/releases",
"application/json",
$"{{\"tag_name\": \"{InputSystem.version}\",\n\"target_commitish\": \"develop\",\n\"name\": \"{InputSystem.version}\",\n\"body\": \"\",\n\"draft\": false,\n\"prerelease\": true\n}}"
);
uploadUrl = JsonUtility.FromJson<ReleaseResponse>(createResponse).upload_url;
uploadUrl = uploadUrl.Replace("{?name,label}", $"?name={packagePath}");
}
else
throw;
}
var uploadResponse = CallGithubAPI(uploadUrl, "application/binary", packagePath);
UnityEngine.Debug.Log("Done!");
EditorApplication.Exit(0);
}
}