Main Content

프로그래밍 방식으로 프로젝트를 만들고 편집하기

이 예제에서는 명령줄을 사용하여 파일을 조작하기 위해 프로젝트 작업을 자동화하는 방법을 보여줍니다. 여기에서는 기존 프로젝트 파일에서 참조 프로젝트를 생성하고, 프로젝트 경로를 설정하고, 프로젝트 바로 가기를 정의하는 방법을 다룹니다. 수정된 파일, 종속 관계, 레이블을 사용하는 방법도 보여줍니다.

프로젝트 객체 생성하기

Times Table App 예제 프로젝트는 Git™ 소스 컨트롤 상태에 있습니다. project 객체를 생성하려면 currentProject 또는 openProject를 사용합니다.

mainProject = openProject("TimesTableApp");

프로젝트 파일 검토하기

프로젝트 내의 파일을 검토합니다.

files = mainProject.Files
files=1×13 object
  1x13 ProjectFile array with properties:

    Path
    Revision
    SourceControlStatus
    Labels

인덱싱을 사용하여 이 목록에 있는 파일에 액세스합니다. 예를 들어, 파일 번호 10을 가져와 보겠습니다. 각 파일에는 경로 및 연결된 레이블을 설명하는 속성이 있습니다.

mainProject.Files(10)
ans = 
  ProjectFile with properties:

                   Path: "/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/utilities"
               Revision: "b3ff013c1170d896663ae4ab6c07cff006850aa1"
    SourceControlStatus: NotUnderSourceControl
                 Labels: [1x0 matlab.project.Label]

10번째 파일의 Git 최신 수정본을 가져옵니다.

mainProject.Files(10).Revision
ans = 
"b3ff013c1170d896663ae4ab6c07cff006850aa1"

10번째 파일의 레이블을 살펴봅니다.

mainProject.Files(10).Labels
ans = 

  1x0 Label array with properties:

    File
    DataType
    Data
    Name
    CategoryName

이름을 사용하여 특정 파일을 가져옵니다.

myfile = findFile(mainProject,"source/timestable.mlapp")
myfile = 
  ProjectFile with properties:

                   Path: "/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/source/timestable.mlapp"
               Revision: "b3ff013c1170d896663ae4ab6c07cff006850aa1"
    SourceControlStatus: Unmodified
                 Labels: [1x1 matlab.project.Label]

새 프로젝트 만들기

Times Table Game 프로젝트를 만듭니다. 이 프로젝트는 Times Table App의 바탕이 되는 게임 로직을 저장합니다. Times Table Game 프로젝트는 프로젝트 참조를 통해 Times Table App 프로젝트에 사용됩니다.

프로젝트를 만들고 프로젝트 이름을 설정합니다.

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

기본 프로젝트 폴더에 있는 Times Table App 게임 로직을 새 프로젝트 폴더로 옮긴 다음 Times Table Game 프로젝트에 추가합니다. 그런 다음 Times Table App 프로젝트에서 파일을 제거합니다.

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

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

Times Table Game 프로젝트 루트 폴더를 Times Table Game 프로젝트 경로에 추가합니다. 이렇게 하면 Times Table App 프로젝트 또는 Times Table App 프로젝트를 참조하는 다른 프로젝트를 불러온 후에 해당 프로젝트에서 timesTableGame.m 파일을 사용할 수 있습니다.

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

프로젝트 참조 추가하기

새 Times Table Game 프로젝트를 Times Table App 프로젝트에 프로젝트 참조로 추가합니다. 이렇게 하면 Time Table App 프로젝트에서 Times Table Game 프로젝트의 파일을 보고 편집하고 실행할 수 있습니다.

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

수정된 파일 가져오기

Times Table App 프로젝트에서 수정된 모든 파일을 가져옵니다. 이 목록을 프로젝트의 파일 > 수정됨 보기와 비교합니다. Times Table App 프로젝트에서 제거되거나 수정된 파일은 물론, 새 Times Table Game 프로젝트의 파일도 볼 수 있습니다.

modifiedfiles = listModifiedFiles(mainProject)
modifiedfiles=1×37 object
  1x37 ProjectFile array with properties:

    Path
    Revision
    SourceControlStatus
    Labels

목록에 있는 두 번째 수정 파일을 가져옵니다. SourceControlStatus 속성이 Added임을 확인합니다. listModifiedFiles 함수는 추가, 수정, 충돌, 삭제 등이 발생한 모든 파일을 반환합니다.

modifiedfiles(2)
ans = 
  ProjectFile with properties:

                   Path: "/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/refs/TimesTableGame/resources/project/EEtUlUb-dLAdf0KpMVivaUlztwA/Z_K-znBXsw6jSE5Zw2JxQ1aJnJsd.xml"
               Revision: ""
    SourceControlStatus: Added

개별 파일을 쿼리하기 전에 소스 컨트롤 상태를 새로 고칩니다. listModifiedFiles를 호출하기 전까지는 이 작업을 할 필요가 없습니다.

refreshSourceControl(mainProject)

Unmodified 상태에 있는 모든 프로젝트 파일을 가져옵니다. ismember 함수를 사용하여 Times Table App 프로젝트에서 수정되지 않은 파일을 나타내는 논리형 배열을 가져옵니다. 이 배열을 사용하여 수정되지 않은 파일 목록을 가져옵니다.

unmodifiedStatus = ismember([mainProject.Files.SourceControlStatus],matlab.sourcecontrol.Status.Unmodified);
mainProject.Files(unmodifiedStatus)
ans=1×8 object
  1x8 ProjectFile array with properties:

    Path
    Revision
    SourceControlStatus
    Labels

파일 종속성 가져오기

종속성 분석을 실행하여 프로젝트 파일 간의 알려진 종속성을 업데이트합니다.

updateDependencies(mainProject)

Times Table App 프로젝트의 종속성 목록을 가져옵니다. Dependencies 속성에는 프로젝트 파일 간의 종속성을 나타내는 그래프가 포함되어 있으며, 이 그래프는 MATLAB digraph 객체로 저장되어 있습니다.

g = mainProject.Dependencies
g = 
  digraph with properties:

    Edges: [6x1 table]
    Nodes: [9x1 table]

timestable.mlapp 파일에 필요한 파일을 가져옵니다.

requiredFiles = bfsearch(g, which('source/timestable.mlapp'))
requiredFiles = 2x1 cell
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/source/timestable.mlapp'             }
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/refs/TimesTableGame/timesTableGame.m'}

그래프에 있는 모든 형식의 최상위 파일을 가져옵니다. indegree 함수는 어떤 파일의 의존 대상도 되지 않는 파일을 모두 찾습니다.

top = g.Nodes.Name(indegree(g)==0)
top = 7x1 cell
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/requirements/TimesTableRequirements.mlx'}
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/tests/tAnswerIsCorrect.m'               }
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/tests/tCurrentQuestion.m'               }
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/tests/tNewTimesTable.m'                 }
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/utilities/editTimesTable.m'             }
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/utilities/openRequirementsDocument.m'   }
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/utilities/runTheseTests.m'              }

종속성을 갖는 최상위 파일을 가져옵니다. indegree 함수는 어떤 파일의 의존 대상도 되지 않는 파일을 모두 찾고, outdegree 함수는 종속성을 갖는 파일을 모두 찾습니다.

top = g.Nodes.Name(indegree(g)==0 & outdegree(g)>0)
top = 5x1 cell
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/requirements/TimesTableRequirements.mlx'}
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/tests/tAnswerIsCorrect.m'               }
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/tests/tCurrentQuestion.m'               }
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/tests/tNewTimesTable.m'                 }
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/utilities/editTimesTable.m'             }

전치 그래프를 만들어 영향을 받는(즉, "업스트림") 파일을 찾습니다. flipedge 함수를 사용하여, 그래프에 있는 간선의 방향을 반전시킵니다.

transposed = flipedge(g)
transposed = 
  digraph with properties:

    Edges: [6x1 table]
    Nodes: [9x1 table]

impacted = bfsearch(transposed,which('source/timestable.mlapp'))
impacted = 3x1 cell
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/source/timestable.mlapp'                }
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/requirements/TimesTableRequirements.mlx'}
    {'/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/utilities/editTimesTable.m'             }

종속 관계 개수와 고아 파일 개수 등 프로젝트 파일 정보를 가져옵니다.

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

종속성 그래프의 정렬 순서를 변경하면 프로젝트 변경 내용이 아래에서 위로 표시됩니다.

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

바로 가기 쿼리하기

바로 가기를 사용하여, 자주 수행하는 작업과 자주 액세스하는 파일을 저장하거나 시작 작업과 종료 작업을 자동화할 수 있습니다.

Times Table App 프로젝트 바로 가기를 가져옵니다.

shortcuts = mainProject.Shortcuts
shortcuts=1×4 object
  1x4 Shortcut array with properties:

    Name
    Group
    File

목록에 있는 바로 가기를 검토합니다.

shortcuts(2)
ans = 
  Shortcut with properties:

     Name: "Edit Times Table App"
    Group: "Launch Points"
     File: "/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/utilities/editTimesTable.m"

바로 가기의 파일 경로를 가져옵니다.

shortcuts(3).File
ans = 
"/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/utilities/openRequirementsDocument.m"

바로 가기 목록에 있는 모든 파일을 검토합니다.

{shortcuts.File}'
ans=4×1 cell array
    {["/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/source/timestable.mlapp"             ]}
    {["/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/utilities/editTimesTable.m"          ]}
    {["/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/utilities/openRequirementsDocument.m"]}
    {["/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/utilities/runTheseTests.m"           ]}

파일에 레이블 지정하기

char형의 새로운 레이블 범주를 만듭니다. Times Table App 프로젝트의 레이블 창에 새로운 Engineers 범주가 표시됩니다.

createCategory(mainProject,'Engineers','char')
ans = 
  Category with properties:

        SingleValued: 0
            DataType: "char"
                Name: "Engineers"
    LabelDefinitions: [1x0 matlab.project.LabelDefinition]

새 범주에 새 레이블을 정의합니다.

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

새 레이블의 레이블 정의 객체를 가져옵니다.

ld = findLabel(category,'Bob')
ld = 
  LabelDefinition with properties:

            Name: "Bob"
    CategoryName: "Engineers"

레이블을 프로젝트 파일에 연결합니다. Times Table App 프로젝트에서 이 프로젝트 파일을 선택할 경우 Label Editor 창에서 이 레이블을 볼 수 있습니다.

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

특정 레이블을 가져와 이 레이블에 텍스트 데이터를 연결합니다.

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

            File: "/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/source/timestable.mlapp"
        DataType: "char"
            Data: 'Email: Bob.Smith@company.com'
            Name: "Bob"
    CategoryName: "Engineers"

레이블 데이터를 가져와 변수에 저장합니다.

mydata = label.Data
mydata = 
'Email: Bob.Smith@company.com'

MATLAB의 숫자형 데이터에 흔히 사용되는 데이터형인 double형의 새로운 레이블 범주를 만듭니다.

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

새 레이블을 지정된 파일에 연결하고 레이블에 데이터 값 2를 할당합니다.

myfile = mainProject.Files(10);
addLabel(myfile, 'Assessors', 'Sam', 2)
ans = 
  Label with properties:

            File: "/tmp/Bdoc24a_2511836_3459710/tp283b0455/matlab-ex45698718/TimesTableApp/utilities/editTimesTable.m"
        DataType: "double"
            Data: 2
            Name: "Sam"
    CategoryName: "Assessors"

프로젝트 닫기

프로젝트를 닫아 종료 스크립트를 실행하고, 저장되지 않은 파일이 있는지 확인합니다.

close(mainProject)

참고 항목

관련 항목