|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using Bee.Core; |
| 4 | +using Bee.Stevedore; |
| 5 | +using NiceIO; |
| 6 | +using Unity.BuildTools; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Text; |
| 9 | + |
| 10 | +namespace BuildProgram |
| 11 | +{ |
| 12 | + public class BuildProgram |
| 13 | + { |
| 14 | + internal static void Main() |
| 15 | + { |
| 16 | + if (IsRunningOnBuildMachine()) |
| 17 | + Console.WriteLine("\n>>> Running on build machine"); |
| 18 | + |
| 19 | + var monoRoot = GetMonoRootDir(); |
| 20 | + Console.WriteLine(">>> Mono root directory: " + monoRoot); |
| 21 | + |
| 22 | + var buildScriptsRoot = monoRoot.Combine("external").Combine("buildscripts"); |
| 23 | + Console.WriteLine(">>> Build scripts directory: " + buildScriptsRoot); |
| 24 | + |
| 25 | + var monoBuildDeps = monoRoot.Parent.Parent.Combine("mono-build-deps").Combine("build"); |
| 26 | + Console.WriteLine(">>> Mono build dependecies directory: " + monoBuildDeps); |
| 27 | + |
| 28 | + var buildDependenciesConfigFile = buildScriptsRoot.Combine("buildDependencies.txt"); |
| 29 | + Console.WriteLine(">>> Mono build dependecies stevedore version config file: " + buildDependenciesConfigFile); |
| 30 | + |
| 31 | + var stevedoreArtifactsDir = buildScriptsRoot.Combine("artifacts").Combine("Stevedore"); |
| 32 | + Console.WriteLine(">>> Stevedore artifacts directory: " + stevedoreArtifactsDir + "\n"); |
| 33 | + |
| 34 | + if (buildDependenciesConfigFile.Exists()) |
| 35 | + { |
| 36 | + if (!monoBuildDeps.DirectoryExists()) |
| 37 | + { |
| 38 | + Console.WriteLine(">>> " + monoBuildDeps + " does not exist. Creating it ..."); |
| 39 | + monoBuildDeps.CreateDirectory(); |
| 40 | + } |
| 41 | + |
| 42 | + var artifactNameIdFilesDictionary = ParseBuildDependenciesConfigFile(buildDependenciesConfigFile.ToString()); |
| 43 | + |
| 44 | + foreach (var item in artifactNameIdFilesDictionary) |
| 45 | + { |
| 46 | + var artifactName = item.Key.Key; |
| 47 | + var artifactId = item.Key.Value; |
| 48 | + var artifactFiles = item.Value; |
| 49 | + DownloadAndCopyArtifact(artifactId, artifactName, artifactFiles, monoBuildDeps, stevedoreArtifactsDir); |
| 50 | + } |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + throw new Exception($"{buildDependenciesConfigFile} does not exist"); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static void DownloadAndCopyArtifact(string artifactId, string artifactName, IEnumerable<NPath> artifacts, NPath monoBuildDeps, NPath stevedoreArtifactsDir) |
| 59 | + { |
| 60 | + var artifact = StevedoreArtifact.Testing(artifactId); |
| 61 | + Backend.Current.Register(artifact); |
| 62 | + |
| 63 | + var inputs = new List<NPath>(); |
| 64 | + var targetFiles = new List<NPath>(); |
| 65 | + foreach (var item in artifacts) |
| 66 | + { |
| 67 | + inputs.Add(stevedoreArtifactsDir.Combine(artifactName).Combine(item)); |
| 68 | + targetFiles.Add(monoBuildDeps.Combine(artifactName).Combine(item)); |
| 69 | + } |
| 70 | + |
| 71 | + var targetDir = monoBuildDeps; |
| 72 | + if (HostPlatform.IsWindows) |
| 73 | + { |
| 74 | + targetDir = monoBuildDeps.Combine(artifactName); |
| 75 | + } |
| 76 | + |
| 77 | + Backend.Current.AddAction( |
| 78 | + actionName: "CopyArtifact", |
| 79 | + targetFiles: targetFiles.ToArray(), |
| 80 | + inputs: inputs.ToArray(), |
| 81 | + executableStringFor: ExecutableStringForDirectoryCopy(stevedoreArtifactsDir.Combine(artifactName), targetDir), |
| 82 | + commandLineArguments: new string[] { }, |
| 83 | + allowUnwrittenOutputFiles: true |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + private static void ExecuteBuildScript(NPath[] inputFiles, NPath buildScript, NPath buildRoot) |
| 88 | + { |
| 89 | + Backend.Current.AddAction( |
| 90 | + actionName: "ExecuteBuildScript", |
| 91 | + targetFiles: new[] { buildRoot}, |
| 92 | + inputs: inputFiles, |
| 93 | + executableStringFor: $"perl {buildScript}", |
| 94 | + commandLineArguments: new string[] { }, |
| 95 | + allowUnwrittenOutputFiles: true |
| 96 | + ); |
| 97 | + } |
| 98 | + private static NPath GetMonoRootDir() |
| 99 | + { |
| 100 | + var exePath = new NPath(System.Reflection.Assembly.GetEntryAssembly().Location); |
| 101 | + var monoRoot = exePath; |
| 102 | + |
| 103 | + //Assume "external" directory exists under monoRoot. |
| 104 | + while (monoRoot.ToString().Contains("external")) |
| 105 | + monoRoot = monoRoot.Parent; |
| 106 | + |
| 107 | + return monoRoot; |
| 108 | + } |
| 109 | + |
| 110 | + private static string ExecutableStringForDirectoryCopy(NPath from, NPath target) |
| 111 | + { |
| 112 | + return HostPlatform.IsWindows |
| 113 | + ? $"xcopy {from.InQuotes(SlashMode.Native)} {target.InQuotes(SlashMode.Native)} /s /e /d /Y" |
| 114 | + : $"cp -r -v {from.InQuotes(SlashMode.Native)} {target.InQuotes(SlashMode.Native)}"; |
| 115 | + } |
| 116 | + |
| 117 | + private static bool IsRunningOnBuildMachine() |
| 118 | + { |
| 119 | + var buildMachine = Environment.GetEnvironmentVariable("UNITY_THISISABUILDMACHINE"); |
| 120 | + return buildMachine != null && buildMachine == "1"; |
| 121 | + } |
| 122 | + |
| 123 | + //Sample config file format: |
| 124 | + /* |
| 125 | + # Dependencoes to pull down from Stevedore. Please follow the following format: |
| 126 | + # name : <stevedore artifact name> |
| 127 | + # id : <stevedore artifact id> |
| 128 | + # files : <folder and/or comma-separated list of files downloaded and unpacked> |
| 129 | +
|
| 130 | + name: 7z |
| 131 | + id: 7z/9df1e3b3b120_12ed325f6a47f0e5cebc247dbe9282a5da280d392cce4e6c9ed227d57ff1e2ff.7z |
| 132 | + files : 7z |
| 133 | +
|
| 134 | + name: libgdiplus |
| 135 | + id : libgdiplus/9df1e3b3b120_4cf7c08770db93922f54f38d2461b9122cddc898db58585864446e70c5ad3057.7z |
| 136 | + files : libgdiplus,lib2 |
| 137 | + */ |
| 138 | + private static Dictionary<KeyValuePair<string, string>, List<NPath>> ParseBuildDependenciesConfigFile(string buildDependenciesConfigFile) |
| 139 | + { |
| 140 | + var artifactNameIdFilesDictionary = new Dictionary<KeyValuePair<string, string>, List<NPath>>(); |
| 141 | + |
| 142 | + var fileStream = new FileStream(buildDependenciesConfigFile, FileMode.Open, FileAccess.Read); |
| 143 | + using (var streamReader = new StreamReader(fileStream, Encoding.UTF8)) |
| 144 | + { |
| 145 | + string line; |
| 146 | + while ((line = streamReader.ReadLine()) != null) |
| 147 | + { |
| 148 | + //Check if line contains a comment |
| 149 | + if (!string.IsNullOrEmpty(line) && !line.Contains("#")) |
| 150 | + { |
| 151 | + if (line.Contains("name :") || line.Contains("name:")) |
| 152 | + { |
| 153 | + var name = ""; |
| 154 | + var id = ""; |
| 155 | + var files = ""; |
| 156 | + |
| 157 | + //read name |
| 158 | + name = line.Split(':')[1].Trim(); |
| 159 | + |
| 160 | + //read id |
| 161 | + if ((line = streamReader.ReadLine()) != null) |
| 162 | + id = line.Split(':')[1].Trim(); |
| 163 | + else |
| 164 | + throw new Exception($">>> Invalid {buildDependenciesConfigFile}"); |
| 165 | + |
| 166 | + //read comma separated folder/files list |
| 167 | + if ((line = streamReader.ReadLine()) != null) |
| 168 | + files = line.Split(':')[1].Trim(); |
| 169 | + else |
| 170 | + throw new Exception($">>> Invalid {buildDependenciesConfigFile}"); |
| 171 | + |
| 172 | + var filesList = new List<NPath>(); |
| 173 | + if (!string.IsNullOrEmpty(files)) |
| 174 | + { |
| 175 | + if (files.Contains(",")) |
| 176 | + files.Split(',').ForEach(f => { filesList.Add(new NPath(f.Trim())); }); |
| 177 | + else |
| 178 | + filesList.Add(new NPath(files.Trim())); |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + throw new Exception($">>> Invalid {buildDependenciesConfigFile}"); |
| 183 | + } |
| 184 | + artifactNameIdFilesDictionary.Add(new KeyValuePair<string, string>(name, id), filesList); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + return artifactNameIdFilesDictionary; |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments