<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions;
using Microsoft.Extensions.PlatformAbstractions;

namespace OmniSharp.Tools.PublishProject
{
    public class Program
    {
        public static int Main(string[] args)
        {
            Console.WriteLine("Publish project OmniSharp");
            var root = FindRoot();
            var buildPlan = BuildPlan.Parse(root);
            
            var projectPath = Path.Combine(root, "src", buildPlan.MainProject);
            if (!Directory.Exists(projectPath))
            {
                Console.WriteLine($"Can't find project {buildPlan.MainProject}");
                return 1;
            }

            var publishOutput = Path.Combine(root, buildPlan.ArtifactsFolder, "publish");
            if (!Directory.Exists(publishOutput))
            {
                Directory.CreateDirectory(publishOutput);
            }

            var packageOutput = Path.Combine(root, buildPlan.ArtifactsFolder, "package");
            if (!Directory.Exists(packageOutput))
            {
                Directory.CreateDirectory(packageOutput);
            }

            var dotnetExecutable = new DotNetExecutor(buildPlan);
            
            Console.WriteLine($"       root: {root}");
            Console.WriteLine($"    project: {buildPlan.MainProject}");
            Console.WriteLine($"     dotnet: {dotnetExecutable}");
            Console.WriteLine($"     source: {projectPath}");
            Console.WriteLine($"publish out: {publishOutput}");
            Console.WriteLine($"package out: {packageOutput}");
            Console.WriteLine($" frameworks: {string.Join(", ", buildPlan.Frameworks)}");
            Console.WriteLine($"    runtime: {string.Join(", ", buildPlan.Rids)}");            
            
            if (!TestActions.RunTests(buildPlan))
            {
                return 1;
            }
                
            if (dotnetExecutable.Restore(Path.Combine(root, "src")) != 0)
            {
                Console.Error.WriteLine("Fail to restore projects for {rid}");
                return 1;
            }
                
            foreach (var rid in buildPlan.Rids)
            {
                foreach (var framework in buildPlan.Frameworks)
                {
                    var publish = Path.Combine(publishOutput, buildPlan.MainProject, rid, framework);
                    if(dotnetExecutable.Publish(publish, projectPath, rid, framework) != 0)
                    {
                        Console.Error.WriteLine($"Fail to publish {projectPath} on {framework} for {rid}");
                        return 1;
                    }
                    
                    Package(publish, packageOutput, buildPlan.MainProject, rid, framework);
                }
            }

            return 0;
        }

        private static void Package(string publishOutput,
                                    string packageOutput,
                                    string projectName,
                                    string rid,
                                    string framework)
        {
            var runtimeString = Regex.Replace(rid, "(\\d|\\.)*-", "-");
            var baseFilePath = Path.GetFullPath(Path.Combine(packageOutput, $"{projectName}-{runtimeString}-{framework}"));
            if (runtimeString.Contains("win-"))
            {
                var zipFilePath = Path.ChangeExtension(baseFilePath, "zip");
                ZipFile.CreateFromDirectory(publishOutput, zipFilePath);
            }
            else
            {
                var tarFilePath = Path.ChangeExtension(baseFilePath, "tar.gz");
                if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows)
                {
                    var tempFilePath = Path.ChangeExtension(baseFilePath, "tar");
                    var tarStartInfo = new ProcessStartInfo("7z", $"a {tempFilePath}")
                    {
                        UseShellExecute = false,
                        WorkingDirectory = publishOutput
                    };
                    var tarProcess = Process.Start(tarStartInfo);
                    tarProcess.WaitForExit();
                    if (tarProcess.ExitCode != 0)
                        throw new InvalidOperationException($"Tar-ing failed for {projectName} {rid}");
                    var compressStartInfo = new ProcessStartInfo("7z", $"a {tarFilePath} {tempFilePath}")
                    {
                        UseShellExecute = false,<commit_msg>Added mono naming.<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions;
using Microsoft.Extensions.PlatformAbstractions;

namespace OmniSharp.Tools.PublishProject
{
    public class Program
    {
        public static int Main(string[] args)
        {
            Console.WriteLine("Publish project OmniSharp");
            var root = FindRoot();
            var buildPlan = BuildPlan.Parse(root);
            
            var projectPath = Path.Combine(root, "src", buildPlan.MainProject);
            if (!Directory.Exists(projectPath))
            {
                Console.WriteLine($"Can't find project {buildPlan.MainProject}");
                return 1;
            }

            var publishOutput = Path.Combine(root, buildPlan.ArtifactsFolder, "publish");
            if (!Directory.Exists(publishOutput))
            {
                Directory.CreateDirectory(publishOutput);
            }

            var packageOutput = Path.Combine(root, buildPlan.ArtifactsFolder, "package");
            if (!Directory.Exists(packageOutput))
            {
                Directory.CreateDirectory(packageOutput);
            }

            var dotnetExecutable = new DotNetExecutor(buildPlan);
            
            Console.WriteLine($"       root: {root}");
            Console.WriteLine($"    project: {buildPlan.MainProject}");
            Console.WriteLine($"     dotnet: {dotnetExecutable}");
            Console.WriteLine($"     source: {projectPath}");
            Console.WriteLine($"publish out: {publishOutput}");
            Console.WriteLine($"package out: {packageOutput}");
            Console.WriteLine($" frameworks: {string.Join(", ", buildPlan.Frameworks)}");
            Console.WriteLine($"    runtime: {string.Join(", ", buildPlan.Rids)}");            
            
            if (!TestActions.RunTests(buildPlan))
            {
                return 1;
            }
                
            if (dotnetExecutable.Restore(Path.Combine(root, "src")) != 0)
            {
                Console.Error.WriteLine("Fail to restore projects for {rid}");
                return 1;
            }
                
            foreach (var rid in buildPlan.Rids)
            {
                foreach (var framework in buildPlan.Frameworks)
                {
                    var publish = Path.Combine(publishOutput, buildPlan.MainProject, rid, framework);
                    if(dotnetExecutable.Publish(publish, projectPath, rid, framework) != 0)
                    {
                        Console.Error.WriteLine($"Fail to publish {projectPath} on {framework} for {rid}");
                        return 1;
                    }
                    
                    Package(publish, packageOutput, buildPlan.MainProject, rid, framework);
                }
            }

            return 0;
        }

        private static void Package(string publishOutput,
                                    string packageOutput,
                                    string projectName,
                                    string rid,
                                    string framework)
        {
            var runtimeString = Regex.Replace(rid, "(\\d|\\.)*-", "-");
            var buildIdentifier = $"{runtimeString}-{framework}";
            if (!runtimeString.Contains("win-") && framework.Equals("dnx451"))
                buildIdentifier ="mono";
            var baseFilePath = Path.GetFullPath(Path.Combine(packageOutput, $"{projectName.ToLower()}-{buildIdentifier}"));
            if (runtimeString.Contains("win-"))
            {
                var zipFilePath = Path.ChangeExtension(baseFilePath, "zip");
                ZipFile.CreateFromDirectory(publishOutput, zipFilePath);
            }
            else
            {
                var tarFilePath = Path.ChangeExtension(baseFilePath, "tar.gz");
                if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows)
                {
                    var tempFilePath = Path.ChangeExtension(baseFilePath, "tar");
                    var tarStartInfo = new ProcessStartInfo("7z", $"a {tempFilePath}")
                    {
                        UseShellExecute = false,
                        WorkingDirectory = publishOutput
                    };
                    var tarProcess = Process.Start(tarStartInfo);
                    tarProcess.WaitForExit();
                    if (tarProcess.ExitCode != 0)
                        throw new InvalidOperationException($"Tar-ing failed for {projectName} {rid}");
                    var compressStartInfo = new ProcessStartInfo("7z", $"a {tarFilePath} {tempFilePath}")
                    {
                        UseShellExecute = false,
{'fraction_non_alphanumeric': 0.10179386477019425, 'fraction_numerical': 0.0024413544209744187, 'mean_word_length': 1.5368874528809908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1187638', 'n_tokens_mistral': 2243, 'n_tokens_neox': 2123, 'n_words': 445}