Ejemplo n.º 1
0
        /// <summary>
        /// Check all given files if they are readable and writable.
        /// </summary>
        private Check CheckFilesAccessible(string[] files)
        {
            var result = Check.Create();

            foreach (var file in files)
            {
                try
                {
                    using (var fileStream = new FileStream(file, FileMode.Open))
                    {
                        if (false == fileStream.CanRead && fileStream.CanWrite)
                        {
                            TerminalWriter.WriteLocation();

                            SetResultNotAccessible(result, file);
                        }
                    }
                }
                catch (DirectoryNotFoundException)
                {
                    TerminalWriter.WriteLocation();

                    result = SetResultNotAccessible(result, file);
                }
                catch (Exception)
                {
                    TerminalWriter.WriteLocation();

                    result = SetResultNotAccessible(result, file);
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        private bool IsWritable(string file)
        {
            try
            {
                FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                fs.Close();

                return(true);
            }
            catch (Exception)
            {
                TerminalWriter.WriteLocation();

                return(false);
            }
        }
Ejemplo n.º 3
0
        private Check CheckFilesExist(string[] files)
        {
            var result = Check.Create();

            foreach (var file in files)
            {
                if (false == File.Exists(file))
                {
                    TerminalWriter.WriteLocation();

                    result.CheckResult = CheckResult.Error;
                    result.Messages.Add(new TerminalMessage()
                    {
                        Color = Color.Red, Message = $"{file} is not a file."
                    });
                }
            }

            return(result);
        }
Ejemplo n.º 4
0
        public static void WriteText(this TerminalWriter writer, ReadOnlySpan <char> value)
        {
            _ = writer ?? throw new ArgumentNullException(nameof(writer));

            var len   = writer.Encoding.GetByteCount(value);
            var array = ArrayPool <byte> .Shared.Rent(len);

            try
            {
                var span = array.AsSpan(0, len);

                _ = writer.Encoding.GetBytes(value, span);

                writer.WriteBinary(span);
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(array);
            }
        }
Ejemplo n.º 5
0
 private void OutputStartupMessages()
 {
     TerminalWriter.WriteTerminalMessages(TerminalMessages.InitialMessagesWOLicense);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Runs JsMrg.
        /// </summary>
        public ProgramRunnerExit Run()
        {
            var helpCheck = false;
            List <TerminalMessage> helpMessages = new List <TerminalMessage>();

            if (0 == Args.Length)
            {
                helpCheck = true;
                HelpCheck(out helpMessages, true);
            }

            if (helpCheck || CheckResult.Apply == HelpCheck(out helpMessages))
            {
                TerminalWriter.WriteTerminalMessages(helpMessages);

                return(ProgramRunnerExit.Help);
            }

            OutputStartupMessages();

            // If help is not requested, we are expecting exactly two parameters.
            if (2 != Args.Length)
            {
                var tm = TerminalMessage.Create(TerminalMessages.UnexpectedNumberOfParams, Color.Red);
                TerminalWriter.WriteTerminalMessage(tm);

                return(ProgramRunnerExit.Error);
            }

            InputFile  = Args[0];
            InputFile  = IoHelper.OptionalAdjustmentToAbsolutPath(InputFile);
            OutputFile = Args[1];

            if (CheckResult.Ok != IoCheck(out var terminalMessages))
            {
                TerminalWriter.WriteTerminalMessages(terminalMessages);

                // Bail out, we are not ready to run.
                return(ProgramRunnerExit.IoCheckOut);
            }

            var jsMrgRunner = new JsMrgRunner();
            var runResult   = jsMrgRunner.Run(InputFile, OutputFile, out var combinedRunMessages);
            var runExit     = ProgramRunnerExit.Done;

            if (false == runResult)
            {
                var tm = TerminalMessage.Create(TerminalMessages.JsMrgRunEndedWErrors, Color.Red);
                TerminalWriter.WriteTerminalMessage(tm);

                runExit = ProgramRunnerExit.Error;
            }

            TerminalWriter.WriteTerminalMessages(combinedRunMessages);
            if (runExit == ProgramRunnerExit.Done)
            {
                JsMrgOutput = jsMrgRunner.ResultingFileContent;
            }

            return(runExit);
        }
Ejemplo n.º 7
0
 public JsmrgException(TerminalMessage terminalMessage, string excMessage) : base(excMessage)
 {
     TerminalWriter.WriteTerminalMessage(terminalMessage);
 }
Ejemplo n.º 8
0
 public static void WriteLine(this TerminalWriter writer, string format, params object?[] args)
 {
     writer.WriteLine(string.Format(CultureInfo.CurrentCulture, format, args));
 }
Ejemplo n.º 9
0
 public static void WriteLine <T>(this TerminalWriter writer, T value)
 {
     writer.WriteLine(value?.ToString());
 }
Ejemplo n.º 10
0
 public static void WriteLine(this TerminalWriter writer, string?value)
 {
     writer.Write(value + Environment.NewLine);
 }
Ejemplo n.º 11
0
 public static void WriteLine(this TerminalWriter writer)
 {
     writer.Write(Environment.NewLine);
 }
Ejemplo n.º 12
0
 public static void Write(this TerminalWriter writer, string?value)
 {
     writer.WriteText(value.AsSpan());
 }
Ejemplo n.º 13
0
        public static void WriteBinary(this TerminalWriter writer, ReadOnlySpan <byte> value)
        {
            _ = writer ?? throw new ArgumentNullException(nameof(writer));

            writer.Write(value);
        }