Exemplo n.º 1
0
        static Int32 Main(string[] args)
        {
            Options options = new Options();

            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];

            BinaryFormatFile file;

            using (StreamReader reader = new StreamReader(new FileStream(inputFileName, FileMode.Open, FileAccess.Read, FileShare.Read)))
            {
                file = BinaryFormatParser.ParseFile(reader);
            }

            CodeGenerator.GenerateCode(Console.Out, file, @namespace, false);

            return(0);
        }
Exemplo n.º 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 BinaryFormat Files
            //
            BinaryFormatFile file = new BinaryFormatFile();

            for (int i = 0; i < inputFileObjects.Length; i++)
            {
                InputFileObject inputFileObject = inputFileObjects[i];
                BinaryFormatParser.ParseFile(file, new LfdTextReader(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}", BinaryFormatFile.InputShaPrefix, inputHashString);
                CodeGenerator.GenerateCode(outputStringWriter, file, @namespace, generateStructs);
            }

            String outputContents = outputStringBuilder.ToString();

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

            return(TaskSucceeded);
        }