classdef MakeResult < handle
%MakeResult captures results from running compile stage

% Copyright 2019-2020 The MathWorks, Inc.
    
    properties(SetAccess=private, Hidden=true)
        ChildMakeResults
        EvaluatedMakeCommand = ''
        EvaluatedRunMakefile = false
        EvaluatedWrappedMakeCommand = ''
        FinalProduct
        FoundCreatedMessage = false;
        LegacyDownload = false
        LegacyTmfMacros = []
    end
    
    properties(Access=private)
        BuildLog
    end
    
    properties(Dependent=true, SetAccess=private, Hidden=true)
        EvaluatedRunMakefileAll
    end
    
    properties(Dependent=true, SetAccess=private)
        Log
    end
        
    methods
        function val = get.Log(this)
            if ~isempty(this.ChildMakeResults)
                % Concatenate logs for all child components with log for
                % this component
                val = char(join(...
                    [string({this.ChildMakeResults.BuildLog}) this.BuildLog], ...
                    ''));
            else
                val = this.BuildLog;
            end
        end
        
        % Logical array with elements indicating if makefile was run for each
        % child component followed by current component
        function val = get.EvaluatedRunMakefileAll(this)
            val = logical.empty;            
            for i=1:numel(this.ChildMakeResults)
                val(i) = this.ChildMakeResults(i).EvaluatedRunMakefile;
            end            
            val(end+1) = this.EvaluatedRunMakefile;            
        end
    end
    
    methods(Access=public, Hidden=true)
        function setChildMakeResults(this, lChildMakeResults)
            this.ChildMakeResults = lChildMakeResults;
        end
        
        function this = MakeResult...
                (lBuildLog, lFinalProduct, makeCmd, wrappedMakeCmd, ...
                 tmfMacros, download, lFoundCreatedMessage, runMakefile)

            this.BuildLog = lBuildLog;
            this.FinalProduct = lFinalProduct;
            if nargin > 2
                this.EvaluatedMakeCommand = makeCmd;
                this.EvaluatedWrappedMakeCommand = wrappedMakeCmd;
                this.LegacyTmfMacros = tmfMacros;
                this.LegacyDownload = download;
                this.FoundCreatedMessage = lFoundCreatedMessage;
                this.EvaluatedRunMakefile = runMakefile;
            end
        end
    end
    
    methods(Access=public)        
        function disp(this)
            disp(this.Log);
        end
    end
end
