< Summary

Class:BuildCommand
Assembly:MainEditor
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/Editor/BuildCommand.cs
Covered lines:0
Uncovered lines:59
Coverable lines:59
Total lines:162
Line coverage:0% (0 of 59)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetArgument(...)0%12300%
GetEnabledScenes()0%20400%
GetBuildTarget()0%2100%
GetBuildPath()0%6200%
GetBuildName()0%6200%
GetFixedBuildPath(...)0%12300%
GetBuildOptions()0%6200%
ToEnum[TEnum](...)0%6200%
getEnv(...)0%20400%
PerformBuild()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/Editor/BuildCommand.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using UnityEditor;
 4using UnityEditor.Build.Reporting;
 5
 6static class BuildCommand
 7{
 8    static string GetArgument(string name)
 9    {
 010        string[] args = Environment.GetCommandLineArgs();
 11
 012        for (int i = 0; i < args.Length; i++)
 13        {
 014            if (args[i].Contains(name))
 15            {
 016                return args[i + 1];
 17            }
 18        }
 19
 020        return null;
 21    }
 22
 23    static string[] GetEnabledScenes()
 24    {
 25        // Get enabled scenes.
 026        List<string> enabledScenesList = new List<string>();
 027        for (int i = 0; i < EditorBuildSettings.scenes.Length; i++)
 28        {
 029            if (EditorBuildSettings.scenes[i].enabled)
 30            {
 031                enabledScenesList.Add(EditorBuildSettings.scenes[i].path);
 32            }
 33        }
 34
 35        // Transform enabled scenes list into an array to be returned.
 036        string[] enabledScenesArray = new string[enabledScenesList.Count];
 037        for (int i = 0; i < enabledScenesArray.Length; i++)
 38        {
 039            enabledScenesArray[i] = enabledScenesList[i];
 40        }
 41
 042        return enabledScenesArray;
 43    }
 44
 45    static BuildTarget GetBuildTarget()
 46    {
 047        string buildTargetName = GetArgument("customBuildTarget");
 048        Console.WriteLine(":: Received customBuildTarget " + buildTargetName);
 49
 050        return ToEnum<BuildTarget>(buildTargetName, BuildTarget.NoTarget);
 51    }
 52
 53    static string GetBuildPath()
 54    {
 055        string buildPath = GetArgument("customBuildPath");
 056        Console.WriteLine(":: Received customBuildPath " + buildPath);
 57
 058        if (buildPath == "")
 59        {
 060            throw new Exception("customBuildPath argument is missing");
 61        }
 62
 063        return buildPath;
 64    }
 65
 66    static string GetBuildName()
 67    {
 068        string buildName = GetArgument("customBuildName");
 069        Console.WriteLine(":: Received customBuildName " + buildName);
 70
 071        if (buildName == "")
 72        {
 073            throw new Exception("customBuildName argument is missing");
 74        }
 75
 076        return buildName;
 77    }
 78
 79    static string GetFixedBuildPath(BuildTarget buildTarget, string buildPath, string buildName)
 80    {
 081        if (buildTarget.ToString().ToLower().Contains("windows"))
 82        {
 083            buildName = buildName + ".exe";
 084        }
 085        else if (buildTarget.ToString().ToLower().Contains("webgl"))
 86        {
 87            // webgl produces a folder with index.html inside, there is no executable name for this buildTarget
 088            buildName = "";
 89        }
 90
 091        return buildPath + buildName;
 92    }
 93
 94    static BuildOptions GetBuildOptions()
 95    {
 096        string buildOptions = GetArgument("customBuildOptions");
 97
 098        return buildOptions == "AcceptExternalModificationsToPlayer"
 99            ? BuildOptions.AcceptExternalModificationsToPlayer
 100            : BuildOptions.None;
 101    }
 102
 103    // https://stackoverflow.com/questions/1082532/how-to-tryparse-for-enum-value
 104    static TEnum ToEnum<TEnum>(this string strEnumValue, TEnum defaultValue)
 105    {
 0106        if (!Enum.IsDefined(typeof(TEnum), strEnumValue))
 107        {
 0108            return defaultValue;
 109        }
 110
 0111        return (TEnum) Enum.Parse(typeof(TEnum), strEnumValue);
 112    }
 113
 114    static string getEnv(string key, bool secret = false, bool verbose = true)
 115    {
 0116        var env_var = Environment.GetEnvironmentVariable(key);
 117
 0118        if (verbose)
 119        {
 0120            if (env_var != null)
 121            {
 0122                if (secret)
 123                {
 0124                    Console.WriteLine(":: env['" + key + "'] set");
 0125                }
 126                else
 127                {
 0128                    Console.WriteLine(":: env['" + key + "'] set to '" + env_var + "'");
 129                }
 0130            }
 131            else
 132            {
 0133                Console.WriteLine(":: env['" + key + "'] is null");
 134            }
 135        }
 136
 0137        return env_var;
 138    }
 139
 140    static void PerformBuild()
 141    {
 0142        Console.WriteLine(":: Performing build");
 143
 0144        var buildTarget = GetBuildTarget();
 0145        var buildPath = GetBuildPath();
 0146        var buildName = GetBuildName();
 0147        var fixedBuildPath = GetFixedBuildPath(buildTarget, buildPath, buildName);
 148
 0149        if (buildTarget.ToString().ToLower().Contains("webgl"))
 150        {
 0151            PlayerSettings.WebGL.emscriptenArgs = " --profiling-funcs ";
 152        }
 153
 0154        var buildSummary = BuildPipeline.BuildPlayer(GetEnabledScenes(), fixedBuildPath, buildTarget, GetBuildOptions())
 0155        Console.WriteLine(":: Done with build process");
 156
 0157        if (buildSummary.summary.result != BuildResult.Succeeded)
 158        {
 0159            throw new Exception("The build was not successful");
 160        }
 0161    }
 162}