コード例 #1
0
        Version IncrementBuildNumber(Version version, BuildTarget target)
        {
            version.build++;

            string numberString;
            var    optionOverrideBuild = GetChild <OptionOverrideBuild>();

            if (!string.IsNullOrEmpty(optionOverrideBuild.Value))
            {
                // Build number has been overridden, increment override
                optionOverrideBuild.Value = version.build.ToString();
            }
            else if (shareBuildNumber)
            {
                // Increment all build numbers together
                numberString = version.build.ToString();
                foreach (BuildTargetGroup targetGroup in System.Enum.GetValues(typeof(BuildTargetGroup)))
                {
                    var value = (string)GetBuildNumber.Invoke(null, new object[] { targetGroup });
                    if (!string.IsNullOrEmpty(value) && value != "0")
                    {
                        SetBuildNumber.Invoke(null, new object[] { targetGroup, numberString });
                    }
                }

                PlayerSettings.Android.bundleVersionCode = version.build;
            }
            else
            {
                // Only increment the number for the current build target group
                var targetGroup = BuildPipeline.GetBuildTargetGroup(target);
                if (targetGroup == BuildTargetGroup.Android)
                {
                    PlayerSettings.Android.bundleVersionCode = version.build;
                }
                else
                {
                    SetBuildNumber.Invoke(null, new object[] { targetGroup, version.build.ToString() });
                }
            }

            return(version);
        }
コード例 #2
0
        Version DetermineProjectVersion(BuildTarget target)
        {
            // Get version string from either the override or PlayerSettings.bundleVersion
            string versionString;

            var overrideVersion = GetChild <OptionOverrideVersion>().Value;

            if (!string.IsNullOrEmpty(overrideVersion))
            {
                versionString = overrideVersion;
            }
            else
            {
                versionString = PlayerSettings.bundleVersion;
                if (string.IsNullOrEmpty(versionString))
                {
                    Debug.LogError("OptionVersion: Please configure the version in the player settings.");
                    return(default(Version));
                }
            }

            // -- Parse version
            string error;
            var    version = Version.Parse(versionString, out error);

            if (error != null)
            {
                Debug.LogError(error);
                return(default(Version));
            }

            if (!version.IsDefined)
            {
                Debug.LogWarning("OptionVersion: No valid version defined (" + version.MajorMinorPatch + ")");
            }

            // -- Parse build
            string numberString;
            var    overrideBuild = GetChild <OptionOverrideBuild>().Value;

            if (!string.IsNullOrEmpty(overrideBuild))
            {
                // Build number has been overridden
                if (!int.TryParse(overrideBuild, out version.build) || version.build < 0)
                {
                    Debug.LogError("OptionVersion: Override build not a positive number: " + overrideBuild);
                }
            }
            else if (shareBuildNumber)
            {
                // Look for highest build number across build target groups, increment it
                // and then apply it back to all groups
                if (GetBuildNumber == null || SetBuildNumber == null)
                {
                    Debug.LogError("OptionVersion: Could not find internal GetBuildNumber method on PlayerSettings.");
                    return(default(Version));
                }

                var buildNumbers = new List <int>();
                foreach (BuildTargetGroup targetGroup in System.Enum.GetValues(typeof(BuildTargetGroup)))
                {
                    numberString = (string)GetBuildNumber.Invoke(null, new object[] { targetGroup });
                    int number;
                    if (!int.TryParse(numberString, out number) || number < 0)
                    {
                        Debug.LogError("OptionVersion: " + targetGroup + " build number should be a positive integer (" + numberString + ")");
                    }
                    else
                    {
                        buildNumbers.Add(number);
                    }
                }

                var android = PlayerSettings.Android.bundleVersionCode;
                if (android < 0)
                {
                    Debug.LogError("OptionVersion: Android bundle version code should be a positive integer.");
                }
                else
                {
                    buildNumbers.Add(android);
                }

                // Use the highest defined build number
                version.build = buildNumbers.Max();
            }
            else
            {
                // Only increment the number for the current build target group
                var targetGroup = BuildPipeline.GetBuildTargetGroup(target);
                if (targetGroup == BuildTargetGroup.Android)
                {
                    version.build = PlayerSettings.Android.bundleVersionCode;
                }
                else
                {
                    if (GetBuildNumber == null || SetBuildNumber == null)
                    {
                        Debug.LogError("OptionVersion: Could not find internal GetBuildNumber method on PlayerSettings.");
                        return(default(Version));
                    }

                    numberString = (string)GetBuildNumber.Invoke(null, new object[] { targetGroup });
                    var number = 0;
                    if (!int.TryParse(numberString, out number) || number < 0)
                    {
                        Debug.LogError("OptionVersion: " + targetGroup + " build number should be a positive integer (" + numberString + ")");
                    }

                    version.build = number;
                }
            }

            // -- Version Control
            if (GetChild <OptionVersionControl>().Value)
            {
                // -- Git
                if (System.IO.Directory.Exists(".git"))
                {
                    OptionHelper.RunScript("git", "rev-parse --verify HEAD", out version.commit);
                    if (version.commit != null)
                    {
                        version.commit = version.commit.Trim();
                        if (version.commit.Length == 0)
                        {
                            version.commit = null;
                        }
                    }

                    OptionHelper.RunScript("git", "rev-parse --abbrev-ref --verify HEAD", out version.branch);
                    if (version.branch != null)
                    {
                        version.branch = version.branch.Trim();
                        if (version.branch.Length == 0)
                        {
                            version.branch = null;
                        }
                    }
                }
            }

            return(version);
        }