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

guiNewPackage(varargin)
function varargout = guiNewPackage(varargin)
% GUINEWPACKAGE MATLAB code for guiNewPackage.fig
%      GUINEWPACKAGE, by itself, creates a new GUINEWPACKAGE or raises the existing
%      singleton*.
%
%      H = GUINEWPACKAGE returns the handle to a new GUINEWPACKAGE or the handle to
%      the existing singleton*.
%
%      GUINEWPACKAGE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUINEWPACKAGE.M with the given input arguments.
%
%      GUINEWPACKAGE('Property','Value',...) creates a new GUINEWPACKAGE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before guiNewPackage_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to guiNewPackage_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help guiNewPackage

% Last Modified by GUIDE v2.5 27-Apr-2013 19:24:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @guiNewPackage_OpeningFcn, ...
                   'gui_OutputFcn',  @guiNewPackage_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before guiNewPackage is made visible.
function guiNewPackage_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to guiNewPackage (see VARARGIN)

hmain = getappdata(0, 'h_pack');
if isempty(hmain)
    return;
end

defloc  = getappdata(hmain, 'defloc');  %desktop folder
guiroot = getappdata(hmain, 'guiroot'); %root of the application

%set the default values for this gui
default_projectpath = guiroot;
default_zippath     = fullfile(defloc, 'ProjectPackage.zip');
default_projectname = 'My Project';
default_ignoredir   = '^.';
default_ignorefile  = '^., ~$';

setappdata(0, 'h_newpack', gcf);
setappdata(gcf, 'default_projectpath', default_projectpath);
setappdata(gcf, 'default_zippath', default_zippath);
setappdata(gcf, 'default_projectname', default_projectname);
setappdata(gcf, 'default_ignoredir', default_ignoredir);
setappdata(gcf, 'default_ignorefile', default_ignorefile);

%set some default values
set(handles.edit_projectroot, 'String', default_projectpath);
set(handles.edit_ziploc, 'String', default_zippath);

% Choose default command line output for guiNewPackage
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes guiNewPackage wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = guiNewPackage_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in button_modifyroot.
function button_modifyroot_Callback(hObject, eventdata, handles)
hmain = getappdata(0, 'h_pack');
guiroot = getappdata(hmain, 'guiroot');

%ask the user for a project folder
rootdir = uigetdir(guiroot, 'Select the project root folder');

if rootdir == 0
    return;
else
    set(handles.edit_projectroot, 'String', rootdir);
end


% --- Executes on button press in button_modifyzip.
function button_modifyzip_Callback(hObject, eventdata, handles)
hmain = getappdata(0, 'h_pack');
defloc = getappdata(hmain, 'defloc');

%ask the user for a zip file
[zipfile, zippath] = uiputfile({'*.zip' 'Zip archive (*.zip)'}, 'Create an archive for the project', defloc);

if zippath == 0
    return;
else
    set(handles.edit_ziploc, 'String', fullfile(zippath, zipfile));
end




function edit_projectroot_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edit_projectroot_CreateFcn(hObject, eventdata, handles)
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_ziploc_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edit_ziploc_CreateFcn(hObject, eventdata, handles)
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function edit_projectname_Callback(hObject, eventdata, handles)
% hObject    handle to edit_projectname (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_projectname as text
%        str2double(get(hObject,'String')) returns contents of edit_projectname as a double


% --- Executes during object creation, after setting all properties.
function edit_projectname_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_projectname (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in button_done.
function button_done_Callback(hObject, eventdata, handles)
%get the relevant parameters
rootdir  = get(handles.edit_projectroot, 'String');
archive  = get(handles.edit_ziploc, 'String');
packname = get(handles.edit_projectname, 'String');

if isempty(packname)
    errordlg('The project name cannot be empty!');
    return;
end

%check the dependencies for non-matlab functions and files
flst = collectProjectFiles(rootdir, makeIgnoreList(handles));
[~]  = checkProjectDependencies(flst, rootdir, false);

%add the relevant data to the packageStruct
hmain = getappdata(0, 'h_pack');
packageStruct = getappdata(hmain, 'packageStruct');
idx = numel(packageStruct) + 1;

packageStruct(idx).packagename = packname;
packageStruct(idx).rootdir = rootdir;
packageStruct(idx).ignorelist = makeIgnoreList(handles);
packageStruct(idx).archive = archive;
packageStruct(idx).created = datestr(now());

%finished with the packageStruct, return it to the main app
setappdata(hmain, 'packageStruct', packageStruct);

helpdlg('The project was succesfully added.');

%reset the fields to their default values
h = getappdata(0, 'h_newpack');
default_projectpath = getappdata(h, 'default_projectpath');
default_zippath     = getappdata(h, 'default_zippath');
default_projectname = getappdata(h, 'default_projectname');
default_ignoredir   = getappdata(h, 'default_ignoredir');
default_ignorefile  = getappdata(h, 'default_ignorefile');

set(handles.edit_projectroot, 'String', default_projectpath);
set(handles.edit_ziploc, 'String', default_zippath);
set(handles.edit_projectname, 'String', default_projectname);
set(handles.edit_ignorefolder, 'String', default_ignoredir);
set(handles.edit_ignorefile, 'String', default_ignorefile);

function [ignore] = makeIgnoreList(handles)
ignore_dirs = get(handles.edit_ignorefolder, 'String');
ignore_file = get(handles.edit_ignorefile, 'String');

%remove white spaces
ignore_dirs = regexprep(ignore_dirs, '\s', '');
ignore_file = regexprep(ignore_file, '\s', '');

%escape special chars like dots
ignore_dirs = regexprep(ignore_dirs, '\.', '\\.');
ignore_file = regexprep(ignore_file, '\.', '\\.');

%split the patterns
ignoredirs = regexp(ignore_dirs, ',', 'split');
ignorefile = regexp(ignore_file, ',', 'split');

%create the ignore structure
ignore.files = ignorefile;
ignore.dirs  = ignoredirs;



% --- Executes on button press in button_cancel.
function button_cancel_Callback(hObject, eventdata, handles)
% hObject    handle to button_cancel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function edit_ignorefolder_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edit_ignorefolder_CreateFcn(hObject, eventdata, handles)
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit_ignorefile_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edit_ignorefile_CreateFcn(hObject, eventdata, handles)
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in buttonList.
function buttonList_Callback(hObject, eventdata, handles)
%get the data from the fields and do a look up
projroot = get(handles.edit_projectroot, 'String');

ignoredirs = get(handles.edit_ignorefolder, 'String');
ignorefile = get(handles.edit_ignorefile, 'String');

%process the ignore patterns
ignoredirs = regexprep(ignoredirs, '\.', '\\.');
ignoredirs = regexprep(ignoredirs, '\s', '');
ignorefile = regexprep(ignorefile, '\.', '\\.');
ignorefile = regexprep(ignorefile, '\s', '');
%create the cell arrays
ignoredirs = regexp(ignoredirs, ',', 'split');
ignorefile = regexp(ignorefile, ',', 'split');
%add it to the structure
ignore.files = ignorefile;
ignore.dirs  = ignoredirs;

filelist = collectProjectFiles(projroot, ignore);

%show only the filenames and not the whole path
filelist = strrep(filelist, projroot, '');

[selection, ok] = listdlg('ListString', filelist,...
                          'PromptString', 'List of files to be included:',...
                          'SelectionMode', 'multiple',...
                          'OKString', 'Ignore ext.',...
                          'CancelString', 'No changes',...
                          'ListSize', [300 400]);
if ok == 0
    return;
end

%add an ignore pattern based on the extension
filenames = filelist(selection);
for f = 1:numel(filenames)
    [~,~,ext] = fileparts(filenames{f});
    
    ignore_file = get(handles.edit_ignorefile, 'String');
    ignore_file = [ignore_file [', ' ext '$']];
    set(handles.edit_ignorefile, 'String', ignore_file);
end

Contact us