Example #1
0
        protected sealed override bool ProcessItem(TkItem item)
        {
            var hasErrors = false;

            var inputFilePath  = item.InputFilePath;
            var outputFilePath = item.OutputFilePath;

            try
            {
                var dependencyFilePath = Path.Combine(Path.Combine(ProjectDirectory.ItemSpec, IntermediateDirectory.ItemSpec),
                                                      FileDependencyList.GetDependencyFileNameFromSourcePath(item.LinkName));

                CreateDirectoryIfNotExists(dependencyFilePath);

                Log.LogMessage(MessageImportance.Low, "Check Toolkit file to compile {0} with dependency file {1}", inputFilePath, dependencyFilePath);
                if (FileDependencyList.CheckForChanges(dependencyFilePath) || !File.Exists(outputFilePath))
                {
                    Log.LogMessage(MessageImportance.Low, "Starting compilation of {0}", inputFilePath);

                    var logger = ProcessFileAndGetLogResults(inputFilePath, outputFilePath, dependencyFilePath, item);

                    LogLogger(logger);

                    if (logger.HasErrors)
                    {
                        hasErrors = true;
                    }
                    else
                    {
                        Log.LogMessage(MessageImportance.High, "Compilation successful of {0} to {1}", inputFilePath, outputFilePath);
                        if (item.OutputCs)
                        {
                            Log.LogWarning("Compilation to CS not yet supported");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.LogError("Cannot process file '{0}' : {1}", inputFilePath, ex.Message);
                hasErrors = true;
            }

            return(!hasErrors);
        }
Example #2
0
        public static ContentCompilerResult CompileAndSave(string fileName, string outputFile, ModelCompilerOptions compilerOptions)
        {
            Contract.Requires <ArgumentNullException>(fileName != null, "fileName");
            Contract.Requires <ArgumentNullException>(outputFile != null, "outputFile");
            Contract.Requires <ArgumentNullException>(compilerOptions != null, "compilerOptions");

            bool contentToUpdate = true;

            if (compilerOptions.DependencyFile != null)
            {
                if (!FileDependencyList.CheckForChanges(compilerOptions.DependencyFile))
                {
                    contentToUpdate = false;
                }
            }

            var result = new ContentCompilerResult();

            if (contentToUpdate)
            {
                try
                {
                    result = CompileFromFile(fileName, compilerOptions);

                    if (result.HasErrors)
                    {
                        return(result);
                    }

                    var modelData = result.ModelData;

                    // Make sure that directory name doesn't collide with filename
                    var directoryName = Path.GetDirectoryName(outputFile + ".tmp");
                    if (!string.IsNullOrEmpty(directoryName) && !Directory.Exists(directoryName))
                    {
                        Directory.CreateDirectory(directoryName);
                    }

                    // Save the model
                    modelData.Save(outputFile);

                    if (compilerOptions.DependencyFile != null)
                    {
                        // Save the dependency
                        var dependencyList = new FileDependencyList();
                        dependencyList.AddDefaultDependencies();
                        dependencyList.AddDependencyPath(fileName);
                        dependencyList.Save(compilerOptions.DependencyFile);
                    }

                    result.IsContentGenerated = true;
                }
                catch (Exception ex)
                {
                    result.HasErrors = true;
                    LogEvent.Tool.Error("Unexpected exception while converting {0} : {1}", fileName, ex.ToString());
                }
            }


            return(result);
        }