%% Create and Edit Projects Programmatically
% This example shows how to use the Project API to create a new project and automate project tasks for manipulating files. It covers how to create a project from the command line, add files and folders, set up the project path, define project shortcuts, and create a reference to the new project in another project. It also shows how to programmatically work with modified files, dependencies, shortcuts, and labels.
%
%% Set up the Example Files
% Create a working copy of the Times Table App example project files and open the project. MATLAB® copies the files to an examples folder so that you can edit them. The project puts the files under Git™ source control. Use currentProject to create a project object from the currently loaded project.

matlab.project.example.timesTable 
mainProject = currentProject;

%
%% Examine Project Files
% Examine the files in the project.

files = mainProject.Files

% Use indexing to access files in this list. For example, get file number 10. Each file has properties describing its path and attached labels.

mainProject.Files(10)

% Get Git latest revision of the tenth file.

mainProject.Files(10).Revision

% Examine the labels of the tenth file.

mainProject.Files(10).Labels

% Get a particular file by name.

myfile = findFile(mainProject,"source/timestable.mlapp")

%
%% Create New Project
% Create the Times Table Game project. This project will store the game logic behind the Times Table App. The Times Table Game project will be used by the Times Table App project through a project reference.
% Create the project and set the project name.

timesTableGameFolder = fullfile(mainProject.RootFolder,"refs","TimesTableGame");
timesTableGame = matlab.project.createProject(timesTableGameFolder);
timesTableGame.Name = "Times Table Game";

% Move the Times Table App game logic from the main project folder to the new project folder, and add it to the Times Table Game project. Then, remove the file from the Times Table App project.

movefile("..\..\source\timesTableGame.m");
addFile(timesTableGame,"timesTableGame.m");

reload(mainProject);
removeFile(mainProject,"source\timesTableGame.m");

% Add the Times Table Game project root folder to the Times Table Game project path. This makes the timesTableGame.m file available when the Times Table App project or any project that references the Times Table App project is loaded.

reload(timesTableGame);
addPath(timesTableGame,timesTableGame.RootFolder);

%
%% Add a Project Reference
% Add the new Times Table Game project to the Times Table App project as a project reference. This allows the Time Table App project to view, edit, and run files in the Times Table Game project.

reload(mainProject);
addReference(mainProject,timesTableGame);

%
%% Get Modified Files
% Get all the modified files in the Times Table App project. Compare this list with the Files > Modified view in the project. You can see the files for the new Times Table Game project, as well as the removed and modified files in the Times Table App project.

modifiedfiles = listModifiedFiles(mainProject)

% Get the second modified file in the list. Observe that the SourceControlStatus property is Added. The listModifiedFiles function returns any files that are added, modified, conflicted, deleted, and so on.

modifiedfiles(2)

% Refresh the source control status before querying individual files. You do not need to do this before calling listModifiedFiles.

refreshSourceControl(mainProject)

% Get all the project files that are Unmodified. Use the ismember function to get an array of logicals stating which files in the Times Table App project are unmodified. Use the array to get the list of unmodified files.

unmodifiedStatus = ismember([mainProject.Files.SourceControlStatus],matlab.sourcecontrol.Status.Unmodified);
mainProject.Files(unmodifiedStatus)

%
%% Get File Dependencies
% Run a dependency analysis to update the known dependencies between project files.

updateDependencies(mainProject)

% Get the list of dependencies in the Times Table App project. The Dependencies property contains the graph of dependencies between project files, stored as a MATLAB digraph object.

g = mainProject.Dependencies

% Get the files required by the timestable.mlapp file.

requiredFiles = bfsearch(g, which('source/timestable.mlapp'))

% Get the top-level files of all types in the graph. The indegree function finds all the files that are not depended on by any other file.

top = g.Nodes.Name(indegree(g)==0)

% Get the top-level files that have dependencies. The indegree function finds all the files that are not depended on by any other file, and the outdegree function finds all the files that have dependencies.

top = g.Nodes.Name(indegree(g)==0 & outdegree(g)>0)

% Find impacted (or "upstream") files by creating a transposed graph. Use the flipedge function to reverse the direction of the edges in the graph.

transposed = flipedge(g)
impacted = bfsearch(transposed,which('source/timestable.mlapp'))

% Get information on the project files, such as the number of dependencies and orphans.

averageNumDependencies = mean(outdegree(g));
numberOfOrphans = sum(indegree(g)+outdegree(g)==0);

% Change the sort order of the dependency graph to show project changes from the bottom up.

ordered = g.Nodes.Name(flip(toposort(g)));

%
%% Query Shortcuts
% You can use shortcuts to save frequent tasks and frequently accessed files, or to automate startup and shutdown tasks.
% Get the Times Table App project shortcuts.

shortcuts = mainProject.Shortcuts

% Examine a shortcut in the list.

shortcuts(2)

% Get the file path of a shortcut.

shortcuts(3).File

% Examine all the files in the shortcuts list.

{shortcuts.File}'

%
%% Label files
% Create a new category of labels of type char. In the Times Table App project, the new Engineers category appears in the Labels pane.

createCategory(mainProject,'Engineers','char')

% Define a new label in the new category.

category = findCategory(mainProject,'Engineers');
createLabel(category,'Bob');

% Get the label definition object for the new label.

ld = findLabel(category,'Bob')

% Attach a label to a project file. If you select the file in the Times Table App project, you can see this label in the Label Editor pane.

myfile = findFile(mainProject,"source/timestable.mlapp");
addLabel(myfile,'Engineers','Bob');

% Get a particular label and attach text data to it.

label = findLabel(myfile,'Engineers','Bob');
label.Data = 'Email: Bob.Smith@company.com'

% Retrieve the label data and store it in a variable.

mydata = label.Data

% Create a new label category with data type double, the type MATLAB commonly uses for numeric data.

createCategory(mainProject,'Assessors','double');
category = findCategory(mainProject,'Assessors');
createLabel(category,'Sam');

% Attach the new label to a specified file and assign data value 2 to the label.

myfile = mainProject.Files(10);
addLabel(myfile, 'Assessors', 'Sam', 2)

%
%% Close Project
% Close the project to run shutdown scripts and check for unsaved files.

close(mainProject)

%
% Copyright 2019 The MathWorks, Inc.
%