示例#1
0
        /// <summary>
        /// Parse commandline arguments
        /// </summary>
        /// <param name="args">list of commandline inputs</param>
        /// <param name="job">P's compilation job</param>
        /// <returns></returns>
        public static CommandLineParseResult ParseArguments(IEnumerable <string> args, out CompilationJob job)
        {
            job = null;

            var commandlineParser = new ParseCommandlineOptions(CommandlineOutput);
            // enforce the argument priority
            var commandlineArgs = args.ToList();

            if (commandlineArgs.Any(a => a.ToLowerInvariant().Contains("-h")))
            {
                return(HelpRequested);
            }
            // proj takes priority over everything else and no other arguments should be allowed
            if (commandlineArgs.Any(a => a.ToLowerInvariant().Contains("-proj:")))
            {
                if (commandlineArgs.Count() > 1)
                {
                    CommandlineOutput.WriteMessage("-proj option cannot be combined with other commandline options", SeverityKind.Error);
                    return(Failure);
                }
                else
                {
                    var option      = commandlineArgs.First();
                    var projectPath = option.Substring(option.IndexOf(":", StringComparison.Ordinal) + 1);
                    // Parse the project file and generate the compilation job
                    return(commandlineParser.ParseProjectFile(projectPath, out job) ? Success : Failure);
                }
            }
            else
            {
                // parse command line options and generate the compilation job
                return(commandlineParser.ParseCommandLineOptions(commandlineArgs, out job) ? Success : Failure);
            }
        }
示例#2
0
        public static CommandLineParseResult ParseArguments(IEnumerable <string> args, out CompilationJob job)
        {
            job = null;

            CompilerOutput outputLanguage  = CompilerOutput.C;
            DirectoryInfo  outputDirectory = null;

            List <string>   commandLineFileNames = new List <string>();
            List <FileInfo> inputFiles           = new List <FileInfo>();
            string          targetName           = null;
            bool            generateSourceMaps   = false;

            // enforce the argument prority
            // proj takes priority over everything else and no other arguments should be passed
            if (args.Where(a => a.ToLowerInvariant().Contains("-proj:")).Any() && args.Count() > 1)
            {
                CommandlineOutput.WriteMessage("-proj cannot be combined with other commandline options", SeverityKind.Error);
                return(Failure);
            }

            foreach (string x in args)
            {
                string arg      = x;
                string colonArg = null;
                if (arg[0] == '-')
                {
                    int colonIndex = arg.IndexOf(':');
                    if (colonIndex >= 0)
                    {
                        arg      = x.Substring(0, colonIndex);
                        colonArg = x.Substring(colonIndex + 1);
                    }

                    switch (arg.Substring(1).ToLowerInvariant())
                    {
                    case "t":
                    case "target":
                        if (colonArg == null)
                        {
                            CommandlineOutput.WriteMessage("Missing target name", SeverityKind.Error);
                        }
                        else if (targetName == null)
                        {
                            targetName = colonArg;
                        }
                        else
                        {
                            CommandlineOutput.WriteMessage("Only one target must be specified", SeverityKind.Error);
                        }

                        break;

                    case "g":
                    case "generate":
                        switch (colonArg?.ToLowerInvariant())
                        {
                        case null:
                            CommandlineOutput.WriteMessage(
                                "Missing generation argument, expecting generate:[C,Coyote]", SeverityKind.Error);
                            return(Failure);

                        case "c":
                            outputLanguage = CompilerOutput.C;
                            break;

                        case "coyote":
                            outputLanguage = CompilerOutput.Coyote;
                            break;

                        default:
                            CommandlineOutput.WriteMessage(
                                $"Unrecognized generate option '{colonArg}', expecting C or Coyote",
                                SeverityKind.Error);
                            return(Failure);
                        }

                        break;

                    case "o":
                    case "outputdir":
                        if (colonArg == null)
                        {
                            CommandlineOutput.WriteMessage("Must supply path for output directory",
                                                           SeverityKind.Error);
                            return(Failure);
                        }

                        outputDirectory = Directory.CreateDirectory(colonArg);
                        break;

                    case "proj":
                        if (colonArg == null)
                        {
                            CommandlineOutput.WriteMessage("Must supply project file for compilation",
                                                           SeverityKind.Error);
                            return(Failure);
                        }
                        else
                        {
                            // Parse the project file and generate the compilation job, ignore all other arguments passed
                            if (ParseProjectFile(colonArg, out job))
                            {
                                return(Success);
                            }
                            else
                            {
                                return(Failure);
                            }
                        }

                    case "s":
                    case "sourcemaps":
                        switch (colonArg?.ToLowerInvariant())
                        {
                        case null:
                        case "true":
                            generateSourceMaps = true;
                            break;

                        case "false":
                            generateSourceMaps = false;
                            break;

                        default:
                            CommandlineOutput.WriteMessage(
                                "sourcemaps argument must be either 'true' or 'false'", SeverityKind.Error);
                            return(Failure);
                        }

                        break;

                    case "h":
                    case "help":
                    case "-help":
                        return(HelpRequested);

                    default:
                        commandLineFileNames.Add(arg);
                        CommandlineOutput.WriteMessage($"Unknown Command {arg.Substring(1)}", SeverityKind.Error);
                        return(Failure);
                    }
                }
                else
                {
                    commandLineFileNames.Add(arg);
                }
            }

            // We are here so no project file supplied lets create a compilation job with other arguments

            // Each command line file name must be a legal P file name
            foreach (string inputFileName in commandLineFileNames)
            {
                if (IsLegalPFile(inputFileName, out FileInfo fullPathName))
                {
                    inputFiles.Add(fullPathName);
                }
                else
                {
                    CommandlineOutput.WriteMessage(
                        $"Illegal P file name {inputFileName} or file {fullPathName.FullName} not found", SeverityKind.Error);
                }
            }

            if (inputFiles.Count == 0)
            {
                CommandlineOutput.WriteMessage("At least one .p file must be provided", SeverityKind.Error);
                return(Failure);
            }

            string projectName = targetName ?? Path.GetFileNameWithoutExtension(inputFiles[0].FullName);

            if (!IsLegalUnitName(projectName))
            {
                CommandlineOutput.WriteMessage($"{projectName} is not a legal project name", SeverityKind.Error);
                return(Failure);
            }

            if (outputDirectory == null)
            {
                outputDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
            }

            job = new CompilationJob(output: new DefaultCompilerOutput(outputDirectory), outputLanguage: outputLanguage, inputFiles: inputFiles, projectName: projectName, generateSourceMaps: generateSourceMaps);
            return(Success);
        }
示例#3
0
        public static CommandLineParseResult ParseArguments(IEnumerable <string> args, out CompilationJob job)
        {
            job = null;

            var           outputLanguage  = CompilerOutput.C;
            DirectoryInfo outputDirectory = null;

            var    commandLineFileNames = new List <string>();
            var    inputFiles           = new List <FileInfo>();
            string targetName           = null;
            var    generateSourceMaps   = false;

            foreach (var x in args)
            {
                var    arg      = x;
                string colonArg = null;
                if (arg[0] == '-')
                {
                    var colonIndex = arg.IndexOf(':');
                    if (colonIndex >= 0)
                    {
                        arg      = x.Substring(0, colonIndex);
                        colonArg = x.Substring(colonIndex + 1);
                    }

                    switch (arg.Substring(1).ToLowerInvariant())
                    {
                    case "t":
                    case "target":
                        if (colonArg == null)
                        {
                            CommandlineOutput.WriteMessage("Missing target name", SeverityKind.Error);
                        }
                        else if (targetName == null)
                        {
                            targetName = colonArg;
                        }
                        else
                        {
                            CommandlineOutput.WriteMessage("Only one target must be specified", SeverityKind.Error);
                        }

                        break;

                    case "g":
                    case "generate":
                        switch (colonArg?.ToLowerInvariant())
                        {
                        case null:
                            CommandlineOutput.WriteMessage(
                                "Missing generation argument, expecting generate:[C,P#]", SeverityKind.Error);
                            return(Failure);

                        case "c":
                            outputLanguage = CompilerOutput.C;
                            break;

                        case "p#":
                            outputLanguage = CompilerOutput.PSharp;
                            break;

                        default:
                            CommandlineOutput.WriteMessage(
                                $"Unrecognized generate option '{colonArg}', expecting C or P#",
                                SeverityKind.Error);
                            return(Failure);
                        }

                        break;

                    case "o":
                    case "outputdir":
                        if (colonArg == null)
                        {
                            CommandlineOutput.WriteMessage("Must supply path for output directory",
                                                           SeverityKind.Error);
                            return(Failure);
                        }

                        outputDirectory = Directory.CreateDirectory(colonArg);
                        break;

                    case "s":
                    case "sourcemaps":
                        switch (colonArg?.ToLowerInvariant())
                        {
                        case null:
                        case "true":
                            generateSourceMaps = true;
                            break;

                        case "false":
                            generateSourceMaps = false;
                            break;

                        default:
                            CommandlineOutput.WriteMessage(
                                "sourcemaps argument must be either 'true' or 'false'", SeverityKind.Error);
                            return(Failure);
                        }

                        break;

                    case "h":
                    case "help":
                    case "-help":
                        return(HelpRequested);

                    default:
                        commandLineFileNames.Add(arg);
                        CommandlineOutput.WriteMessage($"Unknown Command {arg.Substring(1)}", SeverityKind.Error);
                        return(Failure);
                    }
                }
                else
                {
                    commandLineFileNames.Add(arg);
                }
            }

            // Each command line file name must be a legal P file name
            foreach (var inputFileName in commandLineFileNames)
            {
                if (IsLegalPFile(inputFileName, out var fullPathName))
                {
                    inputFiles.Add(fullPathName);
                }
                else
                {
                    CommandlineOutput.WriteMessage(
                        $"Illegal P file name {fullPathName} or file {fullPathName} not found", SeverityKind.Error);
                }
            }

            if (inputFiles.Count == 0)
            {
                CommandlineOutput.WriteMessage("At least one .p file must be provided", SeverityKind.Error);
                return(Failure);
            }

            var projectName = targetName ?? Path.GetFileNameWithoutExtension(inputFiles[0].FullName);

            if (!IsLegalUnitName(projectName))
            {
                CommandlineOutput.WriteMessage($"{projectName} is not a legal project name", SeverityKind.Error);
                return(Failure);
            }


            if (outputDirectory == null)
            {
                outputDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
            }

            job = new CompilationJob(new DefaultCompilerOutput(outputDirectory), outputLanguage, inputFiles,
                                     projectName, generateSourceMaps);
            return(Success);
        }