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

namespace Plainion.CI.Services
{
    sealed class FakeScriptExecutor 
    {
        public FakeScriptExecutor( BuildDefinition buildDefinition, IProgress<string> progress )
        {
            Contract.RequiresNotNull( buildDefinition, "buildDefinition" );
            Contract.RequiresNotNull( progress, "progress" );

            BuildDefinition = buildDefinition;
            Progress = progress;
        }

        private IProgress<string> Progress { get; set; }

        private BuildDefinition BuildDefinition { get; set; }

        private string Interpreter
        {
            get
            {
                var home = Path.GetDirectoryName( GetType().Assembly.Location );
                return Path.Combine( home, "FAKE", "fake.exe" );
            }
        }

        private IEnumerable<string> ValidScriptExtensions
        {
            get { yield return ".fsx"; }
        }

        public bool CanExecute( string script )
        {
            var fileExtension = Path.GetExtension( script );
            return ValidScriptExtensions.Any( x => x.Equals( fileExtension, StringComparison.OrdinalIgnoreCase ) );
        }

        public bool Execute( string script, params string[] args )
        {
            Contract.Requires( !string.IsNullOrWhiteSpace( script ), "No script given" );

            Contract.Requires( CanExecute( script ), "{0} is not a valid script. Valid file extensions are: {1}", script, string.Join( ",", ValidScriptExtensions ) );

            if( !Path.IsPathRooted( script ) )
            {
                script = Path.Combine( BuildDefinition.RepositoryRoot, script );
            }

            var process = new UiShellCommand( Interpreter, Progress );

            var toolsHome = Path.GetDirectoryName( GetType().Assembly.Location );
            var PATH = Environment.GetEnvironmentVariable( "PATH" );

            // extend PATH so that FAKE targets can find the tools
            process.Environment[ "PATH" ] = Path.Combine( toolsHome, "FAKE" )
                + Path.PathSeparator + Path.Combine( toolsHome, "NuGet" )
                + Path.PathSeparator + PATH;

            process.Environment[ "ToolsHome" ] = toolsHome;
            process.Environment[ "BuildDefinitionFile" ] = BuildDefinitionSerializer.GetLocation( BuildDefinition );

            process.Environment[ "ProjectRoot" ] = BuildDefinition.RepositoryRoot;
            process.Environment[ "outputPath " ] = BuildDefinition.GetOutputPath();

            var compiledArguments = CompileScriptArgumentsInternal( script, args ).ToArray();

            process.Execute( compiledArguments );

            return process.ExitCode == 0;
        }

        private IEnumerable<string> CompileScriptArgumentsInternal( string script, string[] args )
        {
            yield return "--fsiargs \"--define:FAKE\"";

            yield return script;

            foreach( var arg in args )
            {
                yield return arg;
            }
        }
    }
}<commit_msg>code cleanup<commit_after>﻿using System;
using System.IO;
using System.Linq;

namespace Plainion.CI.Services
{
    sealed class FakeScriptExecutor
    {
        private IProgress<string> myProgress;
        private BuildDefinition myBuildDefinition;

        public FakeScriptExecutor( BuildDefinition buildDefinition, IProgress<string> progress )
        {
            Contract.RequiresNotNull( buildDefinition, "buildDefinition" );
            Contract.RequiresNotNull( progress, "progress" );

            myBuildDefinition = buildDefinition;
            myProgress = progress;
        }

        public bool Execute( string script, params string[] args )
        {
            Contract.Requires( !string.IsNullOrWhiteSpace( script ), "No script given" );

            if( !Path.IsPathRooted( script ) )
            {
                script = Path.Combine( myBuildDefinition.RepositoryRoot, script );
            }

            var home = Path.GetDirectoryName( GetType().Assembly.Location );
            var interpreter = Path.Combine( home, "FAKE", "fake.exe" );

            var process = new UiShellCommand( interpreter, myProgress );

            var toolsHome = Path.GetDirectoryName( GetType().Assembly.Location );
            var PATH = Environment.GetEnvironmentVariable( "PATH" );

            // extend PATH so that FAKE targets can find the tools
            process.Environment[ "PATH" ] = Path.Combine( toolsHome, "FAKE" )
                + Path.PathSeparator + Path.Combine( toolsHome, "NuGet" )
                + Path.PathSeparator + PATH;

            process.Environment[ "ToolsHome" ] = toolsHome;
            process.Environment[ "BuildDefinitionFile" ] = BuildDefinitionSerializer.GetLocation( myBuildDefinition );

            process.Environment[ "ProjectRoot" ] = myBuildDefinition.RepositoryRoot;
            process.Environment[ "outputPath " ] = myBuildDefinition.GetOutputPath();

            var compiledArguments = new[] { "--fsiargs \"--define:FAKE\"", script }
                .Concat( args )
                .ToArray();

            process.Execute( compiledArguments );

            return process.ExitCode == 0;
        }
    }
}
{'fraction_non_alphanumeric': 0.09542781255928666, 'fraction_numerical': 0.0007588692847656991, 'mean_word_length': 2.168269230769231, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '7050918', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1257, 'n_words': 320}