|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | +using Immutable.Audience.Samples.SampleApp; |
| 6 | +using UnityEditor; |
| 7 | +using UnityEditor.SceneManagement; |
| 8 | +using UnityEditor.Build.Reporting; |
| 9 | +using UnityEngine; |
| 10 | +using UnityEngine.SceneManagement; |
| 11 | + |
| 12 | +namespace Immutable.Audience.Samples.SampleApp.Editor |
| 13 | +{ |
| 14 | + // Invoked by CI via: |
| 15 | + // Unity -batchmode -buildTarget StandaloneLinux64 \ |
| 16 | + // -executeMethod Immutable.Audience.Samples.SampleApp.Editor.LinuxSmokeBuilder.Build \ |
| 17 | + // -quit |
| 18 | + // |
| 19 | + // Optional CLI arg: |
| 20 | + // --buildPath <path> Output path for the player (default: Builds/LinuxSmoke/LinuxSmokePlayer.x86_64) |
| 21 | + // |
| 22 | + // Produces a single-scene Linux player whose only behaviour is the |
| 23 | + // LinuxSmokeRunner MonoBehaviour. The scene is generated in memory at |
| 24 | + // build time to avoid shipping a hand-written .unity asset. |
| 25 | + internal static class LinuxSmokeBuilder |
| 26 | + { |
| 27 | + private const string DefaultBuildPath = "Builds/LinuxSmoke/LinuxSmokePlayer.x86_64"; |
| 28 | + private const string TempScenePath = "Assets/_LinuxSmokeBuild.unity"; |
| 29 | + |
| 30 | + public static void Build() |
| 31 | + { |
| 32 | + string buildPath = GetArgValue("--buildPath") ?? DefaultBuildPath; |
| 33 | + string scenePath = TempScenePath; |
| 34 | + |
| 35 | + try |
| 36 | + { |
| 37 | + CreateSmokeScene(scenePath); |
| 38 | + |
| 39 | + Directory.CreateDirectory(Path.GetDirectoryName(buildPath)!); |
| 40 | + |
| 41 | + var options = new BuildPlayerOptions |
| 42 | + { |
| 43 | + scenes = new[] { scenePath }, |
| 44 | + locationPathName = buildPath, |
| 45 | + target = BuildTarget.StandaloneLinux64, |
| 46 | + targetGroup = BuildTargetGroup.Standalone, |
| 47 | + options = BuildOptions.None, |
| 48 | + }; |
| 49 | + |
| 50 | + Debug.Log($"[LinuxSmokeBuilder] Building -> {buildPath}"); |
| 51 | + var report = BuildPipeline.BuildPlayer(options); |
| 52 | + var summary = report.summary; |
| 53 | + |
| 54 | + if (summary.result == BuildResult.Succeeded) |
| 55 | + { |
| 56 | + Debug.Log($"[LinuxSmokeBuilder] Build succeeded ({summary.totalSize / 1024 / 1024} MB)."); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + Debug.LogError($"[LinuxSmokeBuilder] Build failed: {summary.totalErrors} error(s)."); |
| 61 | + EditorApplication.Exit(1); |
| 62 | + } |
| 63 | + } |
| 64 | + finally |
| 65 | + { |
| 66 | + if (AssetDatabase.LoadAssetAtPath<SceneAsset>(scenePath) != null) |
| 67 | + { |
| 68 | + AssetDatabase.DeleteAsset(scenePath); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static void CreateSmokeScene(string scenePath) |
| 74 | + { |
| 75 | + var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); |
| 76 | + |
| 77 | + var go = new GameObject("SmokeRunner"); |
| 78 | + go.AddComponent<LinuxSmokeRunner>(); |
| 79 | + |
| 80 | + EditorSceneManager.SaveScene(scene, scenePath); |
| 81 | + } |
| 82 | + |
| 83 | + private static string? GetArgValue(string flag) |
| 84 | + { |
| 85 | + var args = Environment.GetCommandLineArgs(); |
| 86 | + for (int i = 0; i < args.Length - 1; i++) |
| 87 | + { |
| 88 | + if (args[i] == flag) |
| 89 | + return args[i + 1]; |
| 90 | + } |
| 91 | + return null; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments