classdef (Hidden = true) IDEProjectExecutor < coder.make.Executor & coder.make.internal.IDEProjectMixIn
    % This is default Executor for Project that uses the Project Tool defined
    % in the toolchain

%   Copyright 2012-2019 The MathWorks, Inc.

    properties
    end
    
    methods
        % -----------------------------------------------------------------
        % Constructor
        % -----------------------------------------------------------------
        function h = IDEProjectExecutor(builder, buildInfo, artifact)
            % Invoke base class constructor
            h@coder.make.Executor(builder, buildInfo, artifact);
            % Initialize properties
            h@coder.make.internal.IDEProjectMixIn(builder, buildInfo);
            h.Command.wrapper = ''; % to be filled up later by the Builder
            h.Command.make  = 'make';
            if isBuildObjOnly(h.Parent) 
                h.Command.build = 'obj';
            else
                h.Command.build = 'all';
            end
            h.Command.clean = 'clean';
            h.Command.info  = 'info';

        end
                
                      
        % -----------------------------------------------------------------
        % Invoke the build tool
        % -----------------------------------------------------------------
        function [status, problem] = build(h, buildInfo)
            assert(h.Parent.isBuildToolContextInitialized());
            try
                ret = h.callProjectTool('buildProject', buildInfo, h.ToolchainInfo, h.Parent.BuildOutputType, h.Parent.Components);
                status = true;
                problem = [ret sprintf('\n### Successfully generated all binary outputs.')]; 
            catch ex
                status = false;
                problem = ex.message;
            end
            addToBuildLog(h.ExecutorBuildLogger, problem);
        end
        
        % -----------------------------------------------------------------
        % Invoke the clean tool
        % -----------------------------------------------------------------
        function [status, problem] = clean(h, buildInfo)
            if ~isempty(h.Parent) && h.Parent.isBuildToolContextInitialized()
                try
                    ret = h.callProjectTool('cleanProject', buildInfo);
                    status = true;
                    problem = [ret sprintf('\n### Successfully cleaned all binary outputs')]; 
                catch ex
                    status = false;
                    problem = ex.message;
                end
            end
        end
        
        % -----------------------------------------------------------------
        % Invoke the download tool
        % -----------------------------------------------------------------
        function [status, problem] = download(h, buildInfo) 
            assert(h.Parent.isBuildToolContextInitialized());
            try
                ret = h.callProjectTool('downloadProject', buildInfo, h.ToolchainInfo, h.Parent.BuildOutputType, h.Parent.Components);
                status = true;
                problem = ret; 
            catch ex
                status = false;
                problem = ex.message;
            end
        end
        
        % -----------------------------------------------------------------
        % Invoke the execute tool
        % -----------------------------------------------------------------
        function [status, problem] = execute(h, buildInfo)
            assert(h.Parent.isBuildToolContextInitialized());
            try
                ret = h.callProjectTool('runProject', buildInfo, h.ToolchainInfo, h.Parent.BuildOutputType, h.Parent.Components);
                status = true;
                problem = ret; 
            catch ex
                status = false;
                problem = ex.message;
            end
        end
        
        % -----------------------------------------------------------------
        % cleanup
        % -----------------------------------------------------------------
        function cleanup(h, buildInfo)
            h.terminate(buildInfo);
        end
        
        % -----------------------------------------------------------------
        % Provide proper diagnostic info to the user
        % -----------------------------------------------------------------
        function msg = diagnose(h, step, problem) %#ok<INUSL>
            msg = problem;
        end
        
    end % public methods
    
    methods (Access = private)
    end % private methods
    
end
