示例#1
0
        static Int32 Main(string[] args)
        {
            PdlCodeGeneratorOptions options = new PdlCodeGeneratorOptions();

            List <String> nonOptionArgs = options.Parse(args);

            if (nonOptionArgs.Count != 2)
            {
                return(options.ErrorAndUsage("Expected 2 non-option arguments but got {0}", nonOptionArgs.Count));
            }

            String @namespace    = nonOptionArgs[0];
            String inputFileName = nonOptionArgs[1];

            PdlFile pdlFile;

            using (StreamReader reader = new StreamReader(new FileStream(inputFileName, FileMode.Open, FileAccess.Read, FileShare.Read)))
            {
                pdlFile = PdlFileParser.ParsePdlFile(reader);
            }

            PdlCodeGenerator.GenerateCode(Console.Out, pdlFile, @namespace, false);

            return(0);
        }
示例#2
0
        public Boolean Execute()
        {
            //
            // Check Options
            //
            if (buildEngine == null)
            {
                throw new ArgumentNullException("BuildEngine");
            }

            if (inputFiles == null || inputFiles.Length <= 0)
            {
                LogError("Missing InputFiles");
                return(TaskFailed);
            }
            if (String.IsNullOrEmpty(outputFile))
            {
                LogError("Missing OutputFile");
                return(TaskFailed);
            }
            if (String.IsNullOrEmpty(@namespace))
            {
                LogError("Missing Namespace");
                return(TaskFailed);
            }

            //
            // Check that input files exist
            //
            Int32 missingInputFileCount = 0;

            for (int i = 0; i < inputFiles.Length; i++)
            {
                String inputFile = inputFiles[i];
                if (!File.Exists(inputFiles[i]))
                {
                    missingInputFileCount++;
                    LogError("Missing InputFile '{0}'", inputFile);
                }
            }
            if (missingInputFileCount > 0)
            {
                LogError("{0} of the input files {1} missing", missingInputFileCount, (missingInputFileCount == 1) ? "is" : "are");
                return(TaskFailed);
            }

            //
            // Load the input files
            //
            InputFileObject[] inputFileObjects = new InputFileObject[inputFiles.Length];

            Byte[]        fileBuffer      = new Byte[1024];
            Sha1Builder   inputShaBuilder = new Sha1Builder();
            StringBuilder builder         = new StringBuilder();

            for (int i = 0; i < inputFileObjects.Length; i++)
            {
                inputFileObjects[i] = new InputFileObject(inputFiles[i], fileBuffer, builder, inputShaBuilder, Encoding.UTF8);
            }
            Sha1   inputHash       = inputShaBuilder.Finish(false);
            String inputHashString = inputHash.ToString();

            if (forceCodeGeneration)
            {
                Log(MessageImportance.High, "Skipping the InputHash check because ForceCodeGeneration is set to true");
            }
            else
            {
                //
                // Try to get the saved hash from output file
                //
                Sha1   savedInputHash;
                String savedInputHashString = TryGetSavedInputHash(outputFile, out savedInputHash);
                if (savedInputHashString != null)
                {
                    if (inputHash.Equals(savedInputHash))
                    {
                        Log(MessageImportance.Normal, "Input hash matches saved input hash, no code generation done");
                        return(TaskSucceeded);
                    }
                }
            }

            //
            // Parse Pdl Files
            //
            PdlFile pdlFile = new PdlFile();

            for (int i = 0; i < inputFileObjects.Length; i++)
            {
                InputFileObject inputFileObject = inputFileObjects[i];
                PdlFileParser.ParsePdlFile(pdlFile, new LfdReader(new StringReader(inputFileObject.contents)));
            }

            //
            // Generate the code
            //
            StringBuilder outputStringBuilder = new StringBuilder();

            using (StringWriter outputStringWriter = new StringWriter(outputStringBuilder))
            {
                // Save the hash first
                outputStringWriter.WriteLine("// {0}{1}", PdlFile.InputShaPrefix, inputHashString);
                PdlCodeGenerator.GenerateCode(outputStringWriter, pdlFile, @namespace, generateStructs);
            }

            String outputContents = outputStringBuilder.ToString();

            FileExtensions.SaveStringToFile(outputFile, FileMode.Create, outputContents);

            return(TaskSucceeded);
        }