Code covered by the BSD License  

Highlights from
Project Packaging interface

Project Packaging interface

by

 

15 Apr 2013 (Updated )

An interface and command line tool for creating an archive containing project files and dependencies

createProjectPackage()
function createProjectPackage()
    %CREATEPROJECTPACKAGE create a zip file containing all project files
    %
    %Usage: createProjectPackage()
    %
    %This is the command line only entry point for the application. For a
    %more user-friendly interface, use guiProjectPackager.
    
    %ask the user for the project folder
    projfolder = uigetdir();
    
    if projfolder == 0
        return;
    end
    
    %get the list of files to include in the project file
    filelist = collectProjectFiles(projfolder);
    if isempty(filelist)
        return;
    end
    
    %create a temp folder for the project
    tempfolder = fullfile(tempdir, 'ProjectPackager');
    if exist(tempfolder, 'dir') ~= 7
        %folder does not exist: create it
        try
            mkdir(tempfolder);
        catch me
            error(getReport(me, 'basic'));
        end
    end
    %replicate the folder structure in the temp folder
    try
        destlist = cellfun(@(c) regexprep(c, projfolder, tempfolder), filelist, 'UniformOutput', false);
        for f = 1:numel(destlist)
            srce = filelist{f};
            dest = destlist{f};
            %check if dest folders exist
            [targetdir, ~, ~] = fileparts(dest);
            if exist(targetdir, 'dir') ~= 7
                mkdir(targetdir);
            end
            copyfile(srce, dest);
        end
    catch me
        error(getReport(me, 'basic'));
    end
    
    % get the dependencies
    filedeps = checkProjectDependencies(filelist, projfolder);
    if ~isempty(filedeps)
        %copy the external dependencies to the tempdir
        destination = fullfile(tempfolder, 'dependencies');
        if exist(destination, 'dir') == 7
            %there was already a folder, empty it
            try
                delete(fullfile(destination, '*'));
            catch me
                error(getReport(me, 'basic'));
            end
            %now copy the files
            try
                for fd = 1:numel(filedeps)
                    fdep = filedeps{fd};
                    copyfile(fdep, destination);
                end
            catch me
                error(getReport(me, 'basic'));
            end
        else
            try
                %no such folder, create and fill it
                mkdir(destination);
                for fd = 1:numel(filedeps)
                    fdep = filedeps{fd};
                    copyfile(fdep, destination);
                end
            catch me
                error(getReport(me, 'basic'));
            end
        end
    end
    
    %ask the user for the zip file location
    [zipfile, zippath] = uiputfile({'*.zip' 'Zip archive (*.zip)'}, 'Save the package');
    
    if zippath == 0
        %remove the destination folder
        try
            rmdir(tempfolder, 's');
        catch me
            error(getReport(me, 'basic'));
        end
        return;
    end
    
    %create the zipfile
    try
        zip(fullfile(zippath, zipfile), fullfile(tempfolder, '*'));
        rmdir(tempfolder, 's');
    catch me
        error(getReport(me, 'basic'));
    end
end

Contact us