<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

namespace SuperSystems.UnityBuild
{

public static class BuildProject
{
    #region Public Methods

    public static void BuildAll()
    {
        string[] buildConfigs = BuildSettings.projectConfigurations.BuildAllKeychains();
        PerformBuild(buildConfigs);
    }

    public static void BuildSingle(string keyChain, BuildOptions options = BuildOptions.None)
    {
        string[] buildConfigs = new string[] { keyChain };
        PerformBuild(buildConfigs, options);
    }

    public static string GenerateDefaultDefines(BuildReleaseType releaseType, BuildPlatform buildPlatform, BuildArchitecture arch, BuildDistribution dist)
    {
        List<string> defines = new List<string>();

        if (releaseType != null)
            defines.Add("BUILD_TYPE_" + SanitizeCodeString(releaseType.typeName.ToUpper().Replace(" ", "")));

        if (buildPlatform != null)
            defines.Add("BUILD_PLATFORM_" + SanitizeCodeString(buildPlatform.platformName.ToUpper().Replace(" ", "")));

        if (arch != null)
            defines.Add("BUILD_ARCH_" + SanitizeCodeString(arch.name.ToUpper().Replace(" ", "")));

        if (dist != null)
            defines.Add("BUILD_DIST_" + SanitizeCodeString(dist.distributionName.ToUpper().Replace(" ", "")));

        if (releaseType != null && !string.IsNullOrEmpty(releaseType.customDefines))
        {
            string[] customDefines = releaseType.customDefines.Split(';', ',');
            for (int i = 0; i < customDefines.Length; i++)
            {
                defines.Add(SanitizeCodeString(customDefines[i]).ToUpper());
            }
        }

        return string.Join(";", defines.ToArray());
    }

    public static string GenerateVersionString(ProductParameters productParameters, DateTime buildTime)
    {
        string prototypeString = productParameters.version;
        StringBuilder sb = new StringBuilder(prototypeString);

        // Regex = (?:\$DAYSSINCE\(")([^"]*)(?:"\))
        Match match = Regex.Match(prototypeString, "(?:\\$DAYSSINCE\\(\")([^\"]*)(?:\"\\))");
        while (match.Success)
        {
            DateTime parsedTime;
            int daysSince = 0;
            if (DateTime.TryParse(match.Groups[1].Value, out parsedTime))
            {
                daysSince = buildTime.Subtract(parsedTime).Days;
            }
            
            sb.Replace(match.Captures[0].Value, daysSince.ToString());
            match = match.NextMatch();
        }

        ReplaceFromFile(sb, "$NOUN", "nouns.txt");
        ReplaceFromFile(sb, "$ADJECTIVE", "adjectives.txt");

        sb.Replace("$SECONDS", (buildTime.TimeOfDay.TotalSeconds / 15f).ToString("F0"));

        sb.Replace("$YEAR", buildTime.ToString("yyyy"));
        sb.Replace("$MONTH", buildTime.ToString("MM"));
        sb.Replace("$DAY", buildTime.ToString("dd"));
        sb.Replace("$TIME", buildTime.ToString("hhmmss"));

        sb.Replace("$BUILD", (++productParameters.buildCounter).ToString());

        string retVal = sb.ToString();
        productParameters.lastGeneratedVersion = retVal;

        return retVal;
    }

    public static string GenerateBuildPath(string prototype, BuildReleaseType releaseType, BuildPlatform buildPlatform, BuildArchitecture arch, BuildDistribution dist, DateTime buildTime)
    {
        string resolvedProto = ResolvePath(prototype, releaseType, buildPlatform, arch, dist, buildTime);
        string buildPath = Path.Combine(BuildSettings.basicSettings.baseBuildFolder, resolvedProto);
        buildPath = Path.GetFullPath(buildPath);

        return buildPath;
    }

    public static string ResolvePath(string prototype, BuildReleaseType releaseType, BuildPlatform buildPlatform, BuildArchitecture arch, BuildDistribution dist, DateTime buildTime)
    {
        StringBuilder sb = new StringBuilder(prototype);

        sb.Replace("$YEAR", buildTime.ToString("yyyy"));
        sb.Replace("$MONTH", buildTime.ToString("MM"));
        sb.Replace("$DAY", buildTime.ToString("dd"));
        sb.Replace("$TIME", buildTime.ToString("hhmmss"));

        sb.Replace("$RELEASE_TYPE", SanitizeFolderName(releaseType.typeName));
        sb.Replace("$PLATFORM", SanitizeFolderName(buildPlatform.platformName));
        sb.Replace("$ARCHITECTURE", SanitizeFolderName(arch.name));
        sb.Replace("$DISTRIBUTION", SanitizeFolderName(dist != null ? dist.distributionName : BuildConstantsGenerator.NONE));
        sb.Replace("$VERSION", SanitizeFolderName(BuildSettings.productParameters.lastGeneratedVersion));
        sb.Replace("$BUILD", BuildSettings.productParameters.buildCounter.ToString());
        sb.Replace("$PRODUCT_NAME", SanitizeFolderName(releaseType.productName));

        return sb.ToString();
    }<commit_msg>Trim trailing slashes from buildPath.<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

namespace SuperSystems.UnityBuild
{

public static class BuildProject
{
    #region Public Methods

    public static void BuildAll()
    {
        string[] buildConfigs = BuildSettings.projectConfigurations.BuildAllKeychains();
        PerformBuild(buildConfigs);
    }

    public static void BuildSingle(string keyChain, BuildOptions options = BuildOptions.None)
    {
        string[] buildConfigs = new string[] { keyChain };
        PerformBuild(buildConfigs, options);
    }

    public static string GenerateDefaultDefines(BuildReleaseType releaseType, BuildPlatform buildPlatform, BuildArchitecture arch, BuildDistribution dist)
    {
        List<string> defines = new List<string>();

        if (releaseType != null)
            defines.Add("BUILD_TYPE_" + SanitizeCodeString(releaseType.typeName.ToUpper().Replace(" ", "")));

        if (buildPlatform != null)
            defines.Add("BUILD_PLATFORM_" + SanitizeCodeString(buildPlatform.platformName.ToUpper().Replace(" ", "")));

        if (arch != null)
            defines.Add("BUILD_ARCH_" + SanitizeCodeString(arch.name.ToUpper().Replace(" ", "")));

        if (dist != null)
            defines.Add("BUILD_DIST_" + SanitizeCodeString(dist.distributionName.ToUpper().Replace(" ", "")));

        if (releaseType != null && !string.IsNullOrEmpty(releaseType.customDefines))
        {
            string[] customDefines = releaseType.customDefines.Split(';', ',');
            for (int i = 0; i < customDefines.Length; i++)
            {
                defines.Add(SanitizeCodeString(customDefines[i]).ToUpper());
            }
        }

        return string.Join(";", defines.ToArray());
    }

    public static string GenerateVersionString(ProductParameters productParameters, DateTime buildTime)
    {
        string prototypeString = productParameters.version;
        StringBuilder sb = new StringBuilder(prototypeString);

        // Regex = (?:\$DAYSSINCE\(")([^"]*)(?:"\))
        Match match = Regex.Match(prototypeString, "(?:\\$DAYSSINCE\\(\")([^\"]*)(?:\"\\))");
        while (match.Success)
        {
            DateTime parsedTime;
            int daysSince = 0;
            if (DateTime.TryParse(match.Groups[1].Value, out parsedTime))
            {
                daysSince = buildTime.Subtract(parsedTime).Days;
            }
            
            sb.Replace(match.Captures[0].Value, daysSince.ToString());
            match = match.NextMatch();
        }

        ReplaceFromFile(sb, "$NOUN", "nouns.txt");
        ReplaceFromFile(sb, "$ADJECTIVE", "adjectives.txt");

        sb.Replace("$SECONDS", (buildTime.TimeOfDay.TotalSeconds / 15f).ToString("F0"));

        sb.Replace("$YEAR", buildTime.ToString("yyyy"));
        sb.Replace("$MONTH", buildTime.ToString("MM"));
        sb.Replace("$DAY", buildTime.ToString("dd"));
        sb.Replace("$TIME", buildTime.ToString("hhmmss"));

        sb.Replace("$BUILD", (++productParameters.buildCounter).ToString());

        string retVal = sb.ToString();
        productParameters.lastGeneratedVersion = retVal;

        return retVal;
    }

    public static string GenerateBuildPath(string prototype, BuildReleaseType releaseType, BuildPlatform buildPlatform, BuildArchitecture arch, BuildDistribution dist, DateTime buildTime)
    {
        string resolvedProto = ResolvePath(prototype, releaseType, buildPlatform, arch, dist, buildTime);
        string buildPath = Path.Combine(BuildSettings.basicSettings.baseBuildFolder, resolvedProto);
        buildPath = Path.GetFullPath(buildPath).TrimEnd('\\').TrimEnd('/');


        return buildPath;
    }

    public static string ResolvePath(string prototype, BuildReleaseType releaseType, BuildPlatform buildPlatform, BuildArchitecture arch, BuildDistribution dist, DateTime buildTime)
    {
        StringBuilder sb = new StringBuilder(prototype);

        sb.Replace("$YEAR", buildTime.ToString("yyyy"));
        sb.Replace("$MONTH", buildTime.ToString("MM"));
        sb.Replace("$DAY", buildTime.ToString("dd"));
        sb.Replace("$TIME", buildTime.ToString("hhmmss"));

        sb.Replace("$RELEASE_TYPE", SanitizeFolderName(releaseType.typeName));
        sb.Replace("$PLATFORM", SanitizeFolderName(buildPlatform.platformName));
        sb.Replace("$ARCHITECTURE", SanitizeFolderName(arch.name));
        sb.Replace("$DISTRIBUTION", SanitizeFolderName(dist != null ? dist.distributionName : BuildConstantsGenerator.NONE));
        sb.Replace("$VERSION", SanitizeFolderName(BuildSettings.productParameters.lastGeneratedVersion));
        sb.Replace("$BUILD", BuildSettings.productParameters.buildCounter.ToString());
        sb.Replace("$PRODUCT_NAME", SanitizeFolderName(releaseType.productName));

        return sb.ToString();
    }
{'fraction_non_alphanumeric': 0.1335083821450212, 'fraction_numerical': 0.0014138557867097556, 'mean_word_length': 3.351054481546573, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7030264', 'n_tokens_mistral': 2692, 'n_tokens_neox': 2632, 'n_words': 486}