Beispiel #1
0
        /// <summary>
        ///     Uploads one file to the Now
        /// </summary>
        /// <see cref="https://zeit.co/docs/api/#endpoints/deployments/upload-deployment-files" />
        private static async Task UploadFileRequest(DeploymentFile deploymentFile, HttpMessageInvoker httpClient,
                                                    ConfigureNow configuration,
                                                    CancellationToken cancellationToken = default)
        {
            var httpMessage = new HttpRequestMessage
            {
                Method     = HttpMethod.Post,
                RequestUri = new Uri($"{configuration.BaseUrl}/files"),
                Headers    =
                {
                    { "Authorization", $"Bearer {configuration.Token}" },
                    { "x-now-digest",  deploymentFile.Checksum         }
                },
                Content = new ByteArrayContent(deploymentFile.Data)
            };

            httpMessage.Content.Headers.ContentLength = deploymentFile.FileSize;

            using (var response = await httpClient.SendAsync(httpMessage, cancellationToken))
            {
                var responseMessage = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    Debug.LogError($"[unity-now]: Something went wrong during file upload.\n{responseMessage}");
                }
            }
        }
Beispiel #2
0
        public static async void Deploy()
        {
            var configuration = AssetDatabase.LoadAssetAtPath <ConfigureNow>("Assets/unity-now/ConfigureNow.asset");

            // Check if access token is not empty
            if (string.IsNullOrWhiteSpace(configuration.Token))
            {
                Debug.LogError(
                    "[unity-now]: Zei Now access token is missing. Please set token in Assets/unity-now/ConfigureNow." +
                    "\nYou can create a new token at: <i>https://zeit.co/account/tokens</i>");
                return;
            }

            // Use Unity project name for deployment name
            var deploymentName = PlayerSettings.productName;

            var httpClient = new HttpClient();

            var deploymentFileReferences = new ConcurrentBag <DeploymentFileReference>();
            var uploadFileRequests       = new ConcurrentBag <Task>();

            var path = EditorUtility.OpenFolderPanel("Select the folder to deploy", "", "");

            Debug.Log("Syncing files...");

            Parallel.ForEach(Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories), file =>
            {
                var deploymentFile = new DeploymentFile();

                using (var stream = File.OpenRead(file))
                {
                    // File size in bytes
                    var contentLength       = stream.Length;
                    deploymentFile.FileSize = contentLength;

                    // File relative path
                    deploymentFile.RelativePath = file.Replace($"{path}/", "");

                    // File data
                    var data = new byte[contentLength];
                    stream.Read(data, 0, (int)contentLength);
                    deploymentFile.Data = data;

                    // File SHA1 used to check the integrity
                    using (var sha = SHA1.Create())
                    {
                        deploymentFile.Checksum = BitConverter.ToString(sha.ComputeHash(data)).ToLowerInvariant()
                                                  .Replace("-", "");
                    }
                }

                deploymentFileReferences.Add(new DeploymentFileReference(deploymentFile));
                uploadFileRequests.Add(UploadFileRequest(deploymentFile, httpClient, configuration));
            });

            // Wait for all files to upload
            await Task.WhenAll(uploadFileRequests);

            Debug.Log($"[unity-now]: All files synced! Creating deployment for <i>{deploymentName}</i>...");

            // Create Deployment
            await CreateDeploymentRequest(deploymentFileReferences, deploymentName, httpClient, configuration);
        }
Beispiel #3
0
 public DeploymentFileReference(DeploymentFile deploymentFile)
 {
     _deploymentFile = deploymentFile;
 }