Ejemplo n.º 1
0
        private void WriteParameters(CompilerClass compilerClass, Dictionary <string, AttributedProperty> settings)
        {
            foreach (var pair in settings)
            {
                string description;

                if (pair.Value.Attribute.Optional)
                {
                    string def = compilerClass.Type.GetProperty(pair.Key).GetValue(compilerClass.Instance, null).ToString();

                    description = "{0} (optional, default \"{1}\");".CultureFormat(
                        pair.Value.Attribute.Description, def);

                    Console.Write(
                        "    {0,-15}{1,-15}",
                        pair.Key,
                        pair.Value.Property.PropertyType.Name);
                }
                else
                {
                    description = "{0} (required)".CultureFormat(pair.Value.Attribute.Description);

                    Console.Write(
                        "    {0,-15}{1,-15}",
                        pair.Key,
                        pair.Value.Property.PropertyType.Name);
                }

                int      indent = 4 + 15 + 15;
                string[] lines  = description.WordWrap(79 - indent);
                int      i      = 0;

                Console.WriteLine(lines[i++]);

                for (; i < lines.Length; i++)
                {
                    Console.WriteLine(new String(' ', indent) + lines[i]);
                }
            }
        }
Ejemplo n.º 2
0
        private void LoadCompilerClasses()
        {
            CompilerClasses = new List<CompilerClass>();
            NewestAssemblyWriteTime = DateTime.MinValue;

            ParsedPathList assemblyPaths = new ParsedPathList();

            foreach (var rawAssembly in ContentFile.CompilerAssemblies)
            {
                ParsedPath pathSpec = null;

                try
                {
                    pathSpec = new ParsedPath(Properties.ExpandVariables(rawAssembly.Value), PathType.File);
                }
                catch (KeyNotFoundException e)
                {
                    throw new ContentFileException(rawAssembly, e);
                }

                assemblyPaths.Add(pathSpec);
            }

            for (int i = 0; i < assemblyPaths.Count; i++)
            {
                var assemblyPath = assemblyPaths[i];
                Assembly assembly = null;

                try
                {
                    // We use Assembly.Load so that the test assembly and subsequently loaded
                    // assemblies end up in the correct load context.  If the assembly cannot be
                    // found it will raise a AssemblyResolve event where we will search for the
                    // assembly.
                    assembly = Assembly.LoadFrom(assemblyPath);
                }
                catch (Exception e)
                {
                    throw new ContentFileException(this.ContentFile.CompilerAssemblies[i], e);
                }

                Type[] types;

                // We won't get dependency errors until we actually try to reflect on all the types in the assembly
                try
                {
                    types = assembly.GetTypes();
                }
                catch (ReflectionTypeLoadException e)
                {
                    string message = "Unable to reflect on assembly '{0}'".CultureFormat(assemblyPath);

                    // There is one entry in the exceptions array for each null in the types array,
                    // and they correspond positionally.
                    foreach (Exception ex in e.LoaderExceptions)
                        message += Environment.NewLine + "   " + ex.Message;

                    // Not being able to reflect on classes in the compiler assembly is a critical error
                    throw new ContentFileException(this.ContentFile.CompilerAssemblies[i], message, e);
                }

                int compilerCount = 0;

                // Go through all the types in the test assembly and find all the
                // compiler classes, those that inherit from IContentCompiler.
                foreach (var type in types)
                {
                    Type interfaceType = type.GetInterface(typeof(IContentCompiler).ToString());

                    if (interfaceType == null)
                        continue;

                    CompilerClass compilerClass = new CompilerClass(this.ContentFile.CompilerAssemblies[i], assembly, type, interfaceType);

                    CompilerClasses.Add(compilerClass);
                    compilerCount++;
                }

                DateTime dateTime = File.GetLastWriteTime(assembly.Location);

                if (dateTime > NewestAssemblyWriteTime)
                    NewestAssemblyWriteTime = dateTime;

                WriteMessage("Loaded {0} compilers from assembly '{1}'".CultureFormat(compilerCount, assembly.Location));
            }
        }
Ejemplo n.º 3
0
        private void LoadCompilerClasses()
        {
            CompilerClasses         = new List <CompilerClass>();
            NewestAssemblyWriteTime = DateTime.MinValue;

            ParsedPathList assemblyPaths = new ParsedPathList();

            foreach (var rawAssembly in ContentFile.CompilerAssemblies)
            {
                ParsedPath pathSpec = null;

                try
                {
                    pathSpec = new ParsedPath(Properties.ExpandVariables(rawAssembly.Value), PathType.File);
                }
                catch (KeyNotFoundException e)
                {
                    throw new ContentFileException(rawAssembly, e);
                }

                assemblyPaths.Add(pathSpec);
            }

            for (int i = 0; i < assemblyPaths.Count; i++)
            {
                var      assemblyPath = assemblyPaths[i];
                Assembly assembly     = null;

                try
                {
                    // We use Assembly.Load so that the test assembly and subsequently loaded
                    // assemblies end up in the correct load context.  If the assembly cannot be
                    // found it will raise a AssemblyResolve event where we will search for the
                    // assembly.
                    assembly = Assembly.LoadFrom(assemblyPath);
                }
                catch (Exception e)
                {
                    throw new ContentFileException(this.ContentFile.CompilerAssemblies[i], e);
                }

                Type[] types;

                // We won't get dependency errors until we actually try to reflect on all the types in the assembly
                try
                {
                    types = assembly.GetTypes();
                }
                catch (ReflectionTypeLoadException e)
                {
                    string message = "Unable to reflect on assembly '{0}'".CultureFormat(assemblyPath);

                    // There is one entry in the exceptions array for each null in the types array,
                    // and they correspond positionally.
                    foreach (Exception ex in e.LoaderExceptions)
                    {
                        message += Environment.NewLine + "   " + ex.Message;
                    }

                    // Not being able to reflect on classes in the compiler assembly is a critical error
                    throw new ContentFileException(this.ContentFile.CompilerAssemblies[i], message, e);
                }

                int compilerCount = 0;

                // Go through all the types in the test assembly and find all the
                // compiler classes, those that inherit from IContentCompiler.
                foreach (var type in types)
                {
                    Type interfaceType = type.GetInterface(typeof(IContentCompiler).ToString());

                    if (interfaceType == null)
                    {
                        continue;
                    }

                    CompilerClass compilerClass = new CompilerClass(this.ContentFile.CompilerAssemblies[i], assembly, type, interfaceType);

                    CompilerClasses.Add(compilerClass);
                    compilerCount++;
                }

                DateTime dateTime = File.GetLastWriteTime(assembly.Location);

                if (dateTime > NewestAssemblyWriteTime)
                {
                    NewestAssemblyWriteTime = dateTime;
                }

                WriteMessage("Loaded {0} compilers from assembly '{1}'".CultureFormat(compilerCount, assembly.Location));
            }
        }
Ejemplo n.º 4
0
        private void Build(List <BuildTarget> buildTargets)
        {
            string           oldGlobalHash;
            HashSet <string> oldTargetHashes;

            ReadOldContentFileHashes(out oldGlobalHash, out oldTargetHashes);

            if (ShowProperties)
            {
                WriteProperties(buildContext.Properties);
            }

            foreach (var buildTarget in buildTargets)
            {
                foreach (var inputPath in buildTarget.InputPaths)
                {
                    if (!File.Exists(inputPath))
                    {
                        throw new ContentFileException("Required input file '{0}' does not exist".CultureFormat(inputPath));
                    }
                }

                if (!IsCompileRequired(buildTarget, oldGlobalHash, oldTargetHashes))
                {
                    continue;
                }

                CompilerClass compilerClass = buildTarget.CompilerClass;
                string        msg           = String.Format("Building target '{0}' with '{1}' compiler", buildTarget.Name, compilerClass.Name);

                foreach (var input in buildTarget.InputPaths)
                {
                    msg += Environment.NewLine + "  " + input;
                }
                msg += Environment.NewLine + "  ->";
                foreach (var output in buildTarget.OutputPaths)
                {
                    msg += Environment.NewLine + "  " + output;
                }
                WriteMessage(msg);

                if (ShowProperties)
                {
                    WriteProperties(buildTarget.Properties);
                }

                if (TestOnly)
                {
                    continue;
                }

                // Set the Context and Target properties on the Compiler class instance
                compilerClass.ContextProperty.SetValue(compilerClass.Instance, buildContext, null);
                compilerClass.TargetProperty.SetValue(compilerClass.Instance, buildTarget, null);

                // Set all target properties
                ApplyParameters(
                    buildTarget.RawTarget.Name,
                    compilerClass.Name,
                    compilerClass.Instance,
                    compilerClass.TargetParameters,
                    buildTarget.RawTarget.Parameters);

                try
                {
                    compilerClass.CompileMethod.Invoke(compilerClass.Instance, null);
                }
                catch (TargetInvocationException e)
                {
                    throw new ContentFileException(
                              buildTarget.RawTarget.Name, "Unable to compile target '{0}'".CultureFormat(buildTarget.Name), e.InnerException);
                }

                // Ensure that the output files were generated
                foreach (var outputFile in buildTarget.OutputPaths)
                {
                    if (!File.Exists(outputFile))
                    {
                        throw new ContentFileException(
                                  buildTarget.RawTarget.Name, "Output file '{0}' was not generated".CultureFormat(outputFile));
                    }
                }
            }

            WriteNewContentFileHashes(buildTargets);
        }