/// <inheritdoc/>
        public AssetCompilerResult Compile(CompilerContext context, AssetItem assetItem)
        {
            if (context == null) throw new ArgumentNullException("context");
            if (assetItem == null) throw new ArgumentNullException("assetItem");

            var compilerResult = new AssetCompilerResult();

            if (assetItem.Package == null)
            {
                compilerResult.Warning("Asset [{0}] is not attached to a package", assetItem);
                return compilerResult;
            }

            var assetCompilerContext = (AssetCompilerContext)context;

            // create the a package that contains only the asset and its the dependencies
            var dependenciesCompilePackage = assetItem.Package.Session.CreateCompilePackageFromAsset(assetItem);
            var clonedAsset = dependenciesCompilePackage.FindAsset(assetItem.Id);

            CompileWithDependencies(assetCompilerContext, clonedAsset, assetItem, compilerResult);

            // Check unloadable items
            foreach (var currentAssetItem in dependenciesCompilePackage.Assets)
            {
                var unloadableItems = UnloadableObjectRemover.Run(currentAssetItem.Asset);
                foreach (var unloadableItem in unloadableItems)
                {
                    compilerResult.Log(new AssetLogMessage(dependenciesCompilePackage, currentAssetItem.ToReference(), LogMessageType.Warning, $"Unable to load the object of type {unloadableItem.UnloadableObject.TypeName} which is located at [{unloadableItem.MemberPath}] in the asset"));
                }
            }

            // Find AssetBuildStep
            var assetBuildSteps = new Dictionary<AssetId, AssetBuildStep>();
            foreach (var step in compilerResult.BuildSteps.EnumerateRecursively())
            {
                var assetStep = step as AssetBuildStep;
                if (assetStep != null)
                {
                    assetBuildSteps[assetStep.AssetItem.Id] = assetStep;
                }
            }

            // TODO: Refactor logging of CompilerApp and BuildEngine
            // Copy log top-level to proper asset build steps
            foreach (var message in compilerResult.Messages)
            {
                var assetMessage = message as AssetLogMessage;

                // Find asset (if nothing found, default to main asset)
                var assetId = assetMessage?.AssetReference.Id ?? assetItem.Id;
                AssetBuildStep assetBuildStep;
                if (assetBuildSteps.TryGetValue(assetId, out assetBuildStep))
                {
                    // Log to AssetBuildStep
                    assetBuildStep.Logger?.Log(message);
                }
            }

            return compilerResult;
        }
        /// <inheritdoc/>
        public AssetCompilerResult Compile(CompilerContext context, AssetItem assetItem)
        {
            if (context == null) throw new ArgumentNullException("context");
            if (assetItem == null) throw new ArgumentNullException("assetItem");

            assetItem = assetItem.Package.Session.DependencyManager.FindDependencySet(assetItem.Id).Item;

            var compilerResult = new AssetCompilerResult();

            if (assetItem.Package == null)
            {
                compilerResult.Warning("Asset [{0}] is not attached to a package", assetItem);
                return compilerResult;
            }

            var assetCompilerContext = (AssetCompilerContext)context;

            // create the a package that contains only the asset and its the dependencies
            var dependenciesCompilePackage = assetItem.Package.Session.CreateCompilePackageFromAsset(assetItem);
            assetCompilerContext.Package = dependenciesCompilePackage.LocalPackages.FirstOrDefault();
            var clonedAsset = dependenciesCompilePackage.FindAsset(assetItem.Id);

            CompileWithDependencies(assetCompilerContext, clonedAsset, assetItem, compilerResult);

            return compilerResult;
        }
        public AssetCompilerResult Compile(CompilerContext context, AssetItem assetItem)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            if (assetItem == null) throw new ArgumentNullException(nameof(assetItem));

            var result = new AssetCompilerResult(GetType().Name)
            {
                BuildSteps = new AssetBuildStep(assetItem)
            };

            // Only use the path to the asset without its extension
            var fullPath = assetItem.FullPath;
            if (!fullPath.IsAbsolute)
            {
                throw new InvalidOperationException("assetItem must be an absolute path");
            }

            // Try to compile only if we're sure that the sources exist.
            if (EnsureSourcesExist(result, assetItem))
            {
                Compile((AssetCompilerContext)context, assetItem, assetItem.Location.GetDirectoryAndFileName(), result);
            }

            return result;
        }
Example #4
0
 /// <summary>
 /// Compile the required build steps necessary to produce the desired outputs items.
 /// </summary>
 /// <param name="context">The context source.</param>
 /// <param name="assetItems">The list of items to compile</param>
 /// <param name="compilationResult">The current compilation result, containing the build steps and the logging</param>
 protected void Compile(CompilerContext context, IEnumerable<AssetItem> assetItems,
     AssetCompilerResult compilationResult)
 {
     foreach (var assetItem in assetItems)
     {
         var itemBuildStep = CompileItem(context, compilationResult, assetItem);
         if (itemBuildStep != null)
             compilationResult.BuildSteps.Add(itemBuildStep);
     }
 }
Example #5
0
        /// <summary>
        /// Compile the required build step necessary to produce the desired output item.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="compilationResult">The compilation result.</param>
        /// <param name="assetItem">The asset item.</param>
        protected ListBuildStep CompileItem(CompilerContext context, AssetCompilerResult compilationResult, AssetItem assetItem)
        {
            // First try to find an asset compiler for this particular asset.
            IAssetCompiler compiler;
            try
            {
                compiler = compilerRegistry.GetCompiler(assetItem.Asset.GetType());
            }
            catch (Exception ex)
            {
                compilationResult.Error("Cannot find a compiler for asset [{0}] from path [{1}]", ex, assetItem.Id,
                    assetItem.Location);
                return null;
            }

            if (compiler == null)
            {
                return null;
            }

            // Second we are compiling the asset (generating a build step)
            try
            {
                var resultPerAssetType = compiler.Compile(context, assetItem);

                // Raise the AssetCompiled event.
                var handler = AssetCompiled;
                if (handler != null)
                    handler(this, new AssetCompiledArgs(assetItem, resultPerAssetType));

                // TODO: See if this can be unified with PackageBuilder.BuildStepProcessed
                foreach (var message in resultPerAssetType.Messages)
                {
                    var assetMessage = new AssetLogMessage(null, assetItem.ToReference(), message.Type, AssetMessageCode.CompilationMessage, assetItem.Location, message.Text)
                    {
                        Exception = message is LogMessage ? ((LogMessage)message).Exception : null
                    };
                    // Forward log messages to compilationResult
                    compilationResult.Log(assetMessage);

                    // Forward log messages to build step logger
                    resultPerAssetType.BuildSteps.Logger.Log(assetMessage);
                }

                // Make the build step fail if there was an error during compiling (only when we are compiling the build steps of an asset)
                if (resultPerAssetType.BuildSteps is AssetBuildStep && resultPerAssetType.BuildSteps.Logger.HasErrors)
                    resultPerAssetType.BuildSteps.Add(new CommandBuildStep(new FailedCommand(assetItem.Location)));

                // Build the module string
                var assetAbsolutePath = assetItem.FullPath;
                assetAbsolutePath = Path.GetFullPath(assetAbsolutePath);
                var module = string.Format("{0}(1,1)", assetAbsolutePath);

                // Assign module string to all command build steps
                SetModule(resultPerAssetType.BuildSteps, module);

                // Add a wait command to the build steps if required by the item build
                if (resultPerAssetType.ShouldWaitForPreviousBuilds)
                    compilationResult.BuildSteps.Add(new WaitBuildStep());

                foreach (var buildStep in resultPerAssetType.BuildSteps)
                {
                    buildStep.Priority = latestPriority++;
                }

                // Add the item result build steps the item list result build steps 
                return resultPerAssetType.BuildSteps;
            }
            catch (Exception ex)
            {
                compilationResult.Error("Unexpected exception while compiling asset [{0}] from path [{1}]", ex, assetItem.Id,
                    assetItem.Location);
                return null;
            }
        }
Example #6
0
        /// <summary>
        /// Compile the required build step necessary to produce the desired output item.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="compilationResult">The compilation result.</param>
        /// <param name="assetItem">The asset item.</param>
        protected ListBuildStep CompileItem(CompilerContext context, AssetCompilerResult compilationResult, AssetItem assetItem)
        {
            // First try to find an asset compiler for this particular asset.
            IAssetCompiler compiler;

            try
            {
                compiler = compilerRegistry.GetCompiler(assetItem.Asset.GetType());
            }
            catch (Exception ex)
            {
                compilationResult.Error("Cannot find a compiler for asset [{0}] from path [{1}]", ex, assetItem.Id,
                                        assetItem.Location);
                return(null);
            }

            if (compiler == null)
            {
                return(null);
            }

            // Second we are compiling the asset (generating a build step)
            try
            {
                var resultPerAssetType = compiler.Compile(context, assetItem);

                // Raise the AssetCompiled event.
                AssetCompiled?.Invoke(this, new AssetCompiledArgs(assetItem, resultPerAssetType));

                // TODO: See if this can be unified with PackageBuilder.BuildStepProcessed
                foreach (var message in resultPerAssetType.Messages)
                {
                    var assetMessage = new AssetLogMessage(null, assetItem.ToReference(), message.Type, AssetMessageCode.CompilationMessage, assetItem.Location, message.Text)
                    {
                        Exception = message is LogMessage ? ((LogMessage)message).Exception : null
                    };
                    // Forward log messages to compilationResult
                    compilationResult.Log(assetMessage);

                    // Forward log messages to build step logger
                    resultPerAssetType.BuildSteps.Logger.Log(assetMessage);
                }

                // Make the build step fail if there was an error during compiling (only when we are compiling the build steps of an asset)
                if (resultPerAssetType.BuildSteps is AssetBuildStep && resultPerAssetType.BuildSteps.Logger.HasErrors)
                {
                    resultPerAssetType.BuildSteps.Add(new CommandBuildStep(new FailedCommand(assetItem.Location)));
                }

                // Build the module string
                var assetAbsolutePath = assetItem.FullPath;
                assetAbsolutePath = Path.GetFullPath(assetAbsolutePath);
                var module = string.Format("{0}(1,1)", assetAbsolutePath);

                // Assign module string to all command build steps
                SetModule(resultPerAssetType.BuildSteps, module);

                // Add a wait command to the build steps if required by the item build
                if (resultPerAssetType.ShouldWaitForPreviousBuilds)
                {
                    compilationResult.BuildSteps.Add(new WaitBuildStep());
                }

                foreach (var buildStep in resultPerAssetType.BuildSteps)
                {
                    buildStep.Priority = latestPriority++;
                }

                // Add the item result build steps the item list result build steps
                return(resultPerAssetType.BuildSteps);
            }
            catch (Exception ex)
            {
                compilationResult.Error("Unexpected exception while compiling asset [{0}] from path [{1}]", ex, assetItem.Id,
                                        assetItem.Location);
                return(null);
            }
        }
Example #7
0
        /// <summary>
        /// Compile the required build step necessary to produce the desired output item.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="compilationResult">The compilation result.</param>
        /// <param name="assetItem">The asset item.</param>
        protected BuildStep CompileItem(CompilerContext context, AssetCompilerResult compilationResult, AssetItem assetItem)
        {
            // First try to find an asset compiler for this particular asset.
            IAssetCompiler compiler;

            try
            {
                compiler = compilerRegistry.GetCompiler(assetItem.Asset.GetType());
            }
            catch (Exception ex)
            {
                compilationResult.Error("Cannot find a compiler for asset [{0}] from path [{1}]", ex, assetItem.Id,
                                        assetItem.Location);
                return(null);
            }

            if (compiler == null)
            {
                return(null);
            }

            // Second we are compiling the asset (generating a build step)
            try
            {
                var resultPerAssetType = compiler.Compile(context, assetItem);

                // Raise the AssetCompiled event.
                var handler = AssetCompiled;
                if (handler != null)
                {
                    handler(this, new AssetCompiledArgs(assetItem, resultPerAssetType));
                }

                resultPerAssetType.CopyTo(compilationResult);

                if (resultPerAssetType.BuildSteps == null)
                {
                    return(null);
                }

                // Build the module string
                var assetAbsolutePath = assetItem.FullPath;
                assetAbsolutePath = Path.GetFullPath(assetAbsolutePath);
                var module = string.Format("{0}(1,1)", assetAbsolutePath);

                // Assign module string to all command build steps
                SetModule(resultPerAssetType.BuildSteps, module);

                // Add a wait command to the build steps if required by the item build
                if (resultPerAssetType.ShouldWaitForPreviousBuilds)
                {
                    compilationResult.BuildSteps.Add(new WaitBuildStep());
                }

                foreach (var buildStep in resultPerAssetType.BuildSteps)
                {
                    buildStep.Priority = latestPriority++;
                }

                // Add the item result build steps the item list result build steps
                return(resultPerAssetType.BuildSteps);
            }
            catch (Exception ex)
            {
                compilationResult.Error("Unexpected exception while compiling asset [{0}] from path [{1}]", ex, assetItem.Id,
                                        assetItem.Location);
                return(null);
            }
        }
        /// <summary>
        /// Compile the required build step necessary to produce the desired output item.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="compilationResult">The compilation result.</param>
        /// <param name="assetItem">The asset item.</param>
        protected ListBuildStep CompileItem(CompilerContext context, AssetCompilerResult compilationResult, AssetItem assetItem)
        {
            // First try to find an asset compiler for this particular asset.
            IAssetCompiler compiler;
            try
            {
                compiler = compilerRegistry.GetCompiler(assetItem.Asset.GetType());
            }
            catch (Exception ex)
            {
                compilationResult.Error("Cannot find a compiler for asset [{0}] from path [{1}]", ex, assetItem.Id,
                    assetItem.Location);
                return null;
            }

            if (compiler == null)
            {
                return null;
            }

            // Second we are compiling the asset (generating a build step)
            try
            {
                var resultPerAssetType = compiler.Compile(context, assetItem);

                // Raise the AssetCompiled event.
                AssetCompiled?.Invoke(this, new AssetCompiledArgs(assetItem, resultPerAssetType));

                // TODO: See if this can be unified with PackageBuilder.BuildStepProcessed
                var assetFullPath = assetItem.FullPath.ToWindowsPath();
                foreach (var message in resultPerAssetType.Messages)
                {
                    var assetMessage = AssetLogMessage.From(null, assetItem.ToReference(), message, assetFullPath);
                    // Forward log messages to compilationResult
                    compilationResult.Log(assetMessage);

                    // Forward log messages to build step logger
                    resultPerAssetType.BuildSteps.Logger.Log(assetMessage);
                }

                // Make the build step fail if there was an error during compiling (only when we are compiling the build steps of an asset)
                if (resultPerAssetType.BuildSteps is AssetBuildStep && resultPerAssetType.BuildSteps.Logger.HasErrors)
                    resultPerAssetType.BuildSteps.Add(new CommandBuildStep(new FailedCommand(assetItem.Location)));

                // TODO: Big review of the log infrastructure of CompilerApp & BuildEngine!
                // Assign module string to all command build steps
                SetAssetLogger(resultPerAssetType.BuildSteps, assetItem.Package, assetItem.ToReference(), assetItem.FullPath.ToWindowsPath());

                // Add a wait command to the build steps if required by the item build
                if (resultPerAssetType.ShouldWaitForPreviousBuilds)
                    compilationResult.BuildSteps.Add(new WaitBuildStep());

                foreach (var buildStep in resultPerAssetType.BuildSteps)
                {
                    buildStep.Priority = latestPriority++;
                }

                // Add the item result build steps the item list result build steps 
                return resultPerAssetType.BuildSteps;
            }
            catch (Exception ex)
            {
                compilationResult.Error("Unexpected exception while compiling asset [{0}] from path [{1}]", ex, assetItem.Id,
                    assetItem.Location);
                return null;
            }
        }
        /// <summary>
        /// Compile the required build step necessary to produce the desired output item.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="compilationResult">The compilation result.</param>
        /// <param name="assetItem">The asset item.</param>
        protected ListBuildStep CompileItem(CompilerContext context, AssetCompilerResult compilationResult, AssetItem assetItem)
        {
            // First try to find an asset compiler for this particular asset.
            IAssetCompiler compiler;

            try
            {
                compiler = compilerRegistry.GetCompiler(assetItem.Asset.GetType());
            }
            catch (Exception ex)
            {
                compilationResult.Error($"Cannot find a compiler for asset [{assetItem.Id}] from path [{assetItem.Location}]", ex);
                return(null);
            }

            if (compiler == null)
            {
                return(null);
            }

            // Second we are compiling the asset (generating a build step)
            try
            {
                var resultPerAssetType = compiler.Compile(context, assetItem);

                // Raise the AssetCompiled event.
                AssetCompiled?.Invoke(this, new AssetCompiledArgs(assetItem, resultPerAssetType));

                // TODO: See if this can be unified with PackageBuilder.BuildStepProcessed
                var assetFullPath = assetItem.FullPath.ToWindowsPath();
                foreach (var message in resultPerAssetType.Messages)
                {
                    var assetMessage = AssetLogMessage.From(null, assetItem.ToReference(), message, assetFullPath);
                    // Forward log messages to compilationResult
                    compilationResult.Log(assetMessage);

                    // Forward log messages to build step logger
                    resultPerAssetType.BuildSteps.Logger.Log(assetMessage);
                }

                // Make the build step fail if there was an error during compiling (only when we are compiling the build steps of an asset)
                if (resultPerAssetType.BuildSteps is AssetBuildStep && resultPerAssetType.BuildSteps.Logger.HasErrors)
                {
                    resultPerAssetType.BuildSteps.Add(new CommandBuildStep(new FailedCommand(assetItem.Location)));
                }

                // TODO: Big review of the log infrastructure of CompilerApp & BuildEngine!
                // Assign module string to all command build steps
                SetAssetLogger(resultPerAssetType.BuildSteps, assetItem.Package, assetItem.ToReference(), assetItem.FullPath.ToWindowsPath());

                // Add a wait command to the build steps if required by the item build
                if (resultPerAssetType.ShouldWaitForPreviousBuilds)
                {
                    compilationResult.BuildSteps.Add(new WaitBuildStep());
                }

                foreach (var buildStep in resultPerAssetType.BuildSteps)
                {
                    buildStep.Priority = latestPriority++;
                }

                // Add the item result build steps the item list result build steps
                return(resultPerAssetType.BuildSteps);
            }
            catch (Exception ex)
            {
                compilationResult.Error($"Unexpected exception while compiling asset [{assetItem.Id}] from path [{assetItem.Location}]", ex);
                return(null);
            }
        }
Example #10
0
        /// <inheritdoc/>
        public AssetCompilerResult Compile(CompilerContext context, AssetItem assetItem)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (assetItem == null)
            {
                throw new ArgumentNullException("assetItem");
            }

            var compilerResult = new AssetCompilerResult();

            if (assetItem.Package == null)
            {
                compilerResult.Warning($"Asset [{assetItem}] is not attached to a package");
                return(compilerResult);
            }

            var assetCompilerContext = (AssetCompilerContext)context;

            // create the a package that contains only the asset and its the dependencies
            var dependenciesCompilePackage = assetItem.Package.Session.CreateCompilePackageFromAsset(assetItem);
            var clonedAsset = dependenciesCompilePackage.FindAsset(assetItem.Id);

            CompileWithDependencies(assetCompilerContext, clonedAsset, assetItem, compilerResult);

            // Check unloadable items
            foreach (var currentAssetItem in dependenciesCompilePackage.Assets)
            {
                var unloadableItems = UnloadableObjectRemover.Run(currentAssetItem.Asset);
                foreach (var unloadableItem in unloadableItems)
                {
                    compilerResult.Log(new AssetLogMessage(dependenciesCompilePackage, currentAssetItem.ToReference(), LogMessageType.Warning, $"Unable to load the object of type {unloadableItem.UnloadableObject.TypeName} which is located at [{unloadableItem.MemberPath}] in the asset"));
                }
            }

            // Find AssetBuildStep
            var assetBuildSteps = new Dictionary <AssetId, AssetBuildStep>();

            foreach (var step in compilerResult.BuildSteps.EnumerateRecursively())
            {
                var assetStep = step as AssetBuildStep;
                if (assetStep != null)
                {
                    assetBuildSteps[assetStep.AssetItem.Id] = assetStep;
                }
            }

            // TODO: Refactor logging of CompilerApp and BuildEngine
            // Copy log top-level to proper asset build steps
            foreach (var message in compilerResult.Messages)
            {
                var assetMessage = message as AssetLogMessage;

                // Find asset (if nothing found, default to main asset)
                var            assetId = assetMessage?.AssetReference.Id ?? assetItem.Id;
                AssetBuildStep assetBuildStep;
                if (assetBuildSteps.TryGetValue(assetId, out assetBuildStep))
                {
                    // Log to AssetBuildStep
                    assetBuildStep.Logger?.Log(message);
                }
            }

            return(compilerResult);
        }