<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Crayon
{
    internal class Program
    {
#if RELEASE
        private static readonly string USAGE = string.Join("\n", new string[] {
            "Usage:",
            "  crayon BUILD-FILE -target BUILD-TARGET-NAME [OPTIONS...]",
            "",
            "Flags:",
            "",
            "  -target            (REQUIRED) When a build file is specified, selects the",
            "                     target within that build file to build.",
            "",
            "  -readablebytecode  (OPTIONAL) Output a file of the final byte code in a",
            "                     semi-readable fashion for debugging purposes.",
        });
#endif

        static void Main(string[] args)
        {
#if DEBUG
            // First chance exceptions should crash in debug builds.
            Program.Compile(args);
#else

            if (args.Length == 0)
            {
                System.Console.WriteLine(USAGE);
            }
            else
            {
                try
                {
                    Program.Compile(args);
                }
                catch (InvalidOperationException e)
                {
                    System.Console.Error.WriteLine(e.Message);
                }
                catch (ParserException e)
                {
                    System.Console.Error.WriteLine(e.Message);
                }
            }
#endif
        }

        private static void Compile(string[] args)
        {
            BuildContext buildContext = Program.GetBuildContext(args);
            AbstractPlatform platform = GetPlatformInstance(buildContext);
            platform.Compile(buildContext, buildContext.SourceFolder, buildContext.OutputFolder);
        }

        private static AbstractPlatform GetPlatformInstance(BuildContext buildContext)
        {
            switch (buildContext.Platform.ToLowerInvariant())
            {
                case "game-csharp-android": return new Crayon.Translator.CSharp.CSharpXamarinAndroidPlatform();
                case "game-csharp-ios": return new Crayon.Translator.CSharp.CSharpXamarinIosPlatform();
                case "game-csharp-opentk": return new Crayon.Translator.CSharp.CSharpOpenTkPlatform();
                case "game-java-android": return new Crayon.Translator.Java.JavaAndroidPlatform();
                case "game-java-awt": return new Crayon.Translator.Java.JavaAwtPlatform();
                case "game-javascript": return new Crayon.Translator.JavaScript.JavaScriptPlatform(buildContext.Minified, buildContext.JsFilePrefix);
                case "game-python-pygame": return new Crayon.Translator.Python.PythonPlatform();
                case "server-php": return new Crayon.Translator.Php.PhpPlatform();
                case "ui-csharp-winforms": return new Crayon.Translator.CSharp.CSharpWinFormsPlatform();
                case "ui-javascript": throw new NotImplementedException();
                default:
                    throw new InvalidOperationException("Unrecognized platform. See usage.");
            }
        }

        private static BuildContext GetBuildContext(string[] args)
        {
#if DEBUG
            if (args.Length == 0)
            {
                string command;

                command = @"C:\Crayon\UnitTests\UnitTests.build -target windows";

                args = command.Split(' ');
            }
#endif

            Dictionary<string, string> argLookup = Program.ParseArgs(args);

            string buildFile = argLookup.ContainsKey("buildfile") ? argLookup["buildfile"] : null;
            string target = argLookup.ContainsKey("target") ? argLookup["target"] : null;
            string workingDirectory = ".";

            BuildContext buildContext = null;
            if (buildFile != null || target != null)
            {
                if (buildFile == null || target == null)
                {
                    throw new InvalidOperationException("Build file and target must be specified together.");
                }

                argLookup.Remove("buildfile");
                argLookup.Remove("target");
                workingDirectory = System.IO.Path.GetDirectoryName(buildFile);

                if (!System.IO.File.Exists(buildFile))<commit_msg>Move debug argument overriding out of the code and into a text file in CRAYON_HOME.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Crayon
{
    internal class Program
    {
#if RELEASE
        private static readonly string USAGE = string.Join("\n", new string[] {
            "Usage:",
            "  crayon BUILD-FILE -target BUILD-TARGET-NAME [OPTIONS...]",
            "",
            "Flags:",
            "",
            "  -target            (REQUIRED) When a build file is specified, selects the",
            "                     target within that build file to build.",
            "",
            "  -readablebytecode  (OPTIONAL) Output a file of the final byte code in a",
            "                     semi-readable fashion for debugging purposes.",
        });
#endif

        static void Main(string[] args)
        {
#if DEBUG
            // First chance exceptions should crash in debug builds.
            Program.Compile(args);
#else

            if (args.Length == 0)
            {
                System.Console.WriteLine(USAGE);
            }
            else
            {
                try
                {
                    Program.Compile(args);
                }
                catch (InvalidOperationException e)
                {
                    System.Console.Error.WriteLine(e.Message);
                }
                catch (ParserException e)
                {
                    System.Console.Error.WriteLine(e.Message);
                }
            }
#endif
        }

        private static void Compile(string[] args)
        {
            BuildContext buildContext = Program.GetBuildContext(args);
            AbstractPlatform platform = GetPlatformInstance(buildContext);
            platform.Compile(buildContext, buildContext.SourceFolder, buildContext.OutputFolder);
        }

        private static AbstractPlatform GetPlatformInstance(BuildContext buildContext)
        {
            switch (buildContext.Platform.ToLowerInvariant())
            {
                case "game-csharp-android": return new Crayon.Translator.CSharp.CSharpXamarinAndroidPlatform();
                case "game-csharp-ios": return new Crayon.Translator.CSharp.CSharpXamarinIosPlatform();
                case "game-csharp-opentk": return new Crayon.Translator.CSharp.CSharpOpenTkPlatform();
                case "game-java-android": return new Crayon.Translator.Java.JavaAndroidPlatform();
                case "game-java-awt": return new Crayon.Translator.Java.JavaAwtPlatform();
                case "game-javascript": return new Crayon.Translator.JavaScript.JavaScriptPlatform(buildContext.Minified, buildContext.JsFilePrefix);
                case "game-python-pygame": return new Crayon.Translator.Python.PythonPlatform();
                case "server-php": return new Crayon.Translator.Php.PhpPlatform();
                case "ui-csharp-winforms": return new Crayon.Translator.CSharp.CSharpWinFormsPlatform();
                case "ui-javascript": throw new NotImplementedException();
                default:
                    throw new InvalidOperationException("Unrecognized platform. See usage.");
            }
        }

        private static BuildContext GetBuildContext(string[] args)
        {
#if DEBUG
            if (args.Length == 0)
            {
                string crayonHome = System.Environment.GetEnvironmentVariable("CRAYON_HOME");
                if (crayonHome != null)
                {
                    string debugArgsFile = System.IO.Path.Combine(crayonHome, "DEBUG_ARGS.txt");
                    if (System.IO.File.Exists(debugArgsFile ))
                    {
                        string[] debugArgs = System.IO.File.ReadAllText(debugArgsFile).Trim().Split('\n');
                        string lastArgSet = debugArgs[debugArgs.Length - 1].Trim();
                        if (lastArgSet.Length > 0)
                        {
                            args = lastArgSet.Split(' ');
                        }
                    }
                }
            }
#endif

            Dictionary<string, string> argLookup = Program.ParseArgs(args);

            string buildFile = argLookup.ContainsKey("buildfile") ? argLookup["buildfile"] : null;
            string target = argLookup.ContainsKey("target") ? argLookup["target"] : null;
            string workingDirectory = ".";

            BuildContext buildContext = null;
            if (buildFile != null || target != null)
            {
                if (buildFile == null || target == null)
                {
                    throw new InvalidOperationException("Build file and target must be specified together.");
                }

                argLookup.Remove("buildfile");
                argLookup.Remove("target");
                workingDirectory = System.IO.Path.GetDirectoryName(buildFile);

                if (!System.IO.File.Exists(buildFile))
{'fraction_non_alphanumeric': 0.09485825158995365, 'fraction_numerical': 0.0006467608062951385, 'mean_word_length': 1.6653260557311118, 'pattern_counts': {'":': 20, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1025734', 'n_tokens_mistral': 2335, 'n_tokens_neox': 2190, 'n_words': 505}