Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 1개 있습니다.)

하나의 T4 템플릿으로 여러 개의 소스코드 파일을 자동으로 생성하는 방법

T4를 이용한 자동 생성 코드는 예전에 한번 소개해 드렸습니다.

T4 를 이용한 INotifyPropertyChanged 코드 자동 생성
; https://www.sysnet.pe.kr/2/0/1619

그런데, 때로는 출력 파일을 다중으로 할 필요가 있습니다. 다행히, 이에 대한 방법은 아래의 글에 전부 공개되어 있습니다.

How to generate multiple outputs from single T4 template
; http://www.olegsych.com/2008/03/how-to-generate-multiple-outputs-from-single-t4-template/

좀 복잡하니, ^^ 간단하게 정리해 볼까요?

우선 "How to generate multiple outputs from single T4 template" 글에 공개된 예제 프로젝트를 다운로드 받습니다.

Source code 
; http://www.olegsych.com/wp-content/uploads/2008/03/multioutputpot.zip

이 압축 파일의 내용 중에 MultiOutput.tt 파일만 필요하니 그것만 복사해서 여러분들의 솔루션에 추가해 줍니다. MultiOutput.tt 파일의 경우 스스로에 대한 소스 코드 생성이 목적은 아니므로 "Custom Tool"의 내용을 지워줍니다.

t4_multiple_output_1.png

자, 이제 소스 코드를 생성할 용도의 tt 파일을 원하는 만큼 추가해줍니다. 여기서는 csgen.tt, jsgen.tt 2개의 파일을 추가합니다. 2개 모두 마찬가지로 "Custom Tool"의 내용을 지워줍니다.

예를 들어, 아래는 jsgen.tt 파일의 내용이고,

<#@ template language="C#" #>
<#@ output extension=".js" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>

var <#= Parameter #> = {
}

<#+
    string Parameter
    {
        get 
        { 
            string result = (string)CallContext.GetData("Parameter");
            if (result == null)
                result = string.Empty;
            return result; 
        }
    }
#>

다음은 csgen.tt 파일입니다.

<#@ template language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>

public class <#= Parameter #>
{
}

<#+
    string Parameter
    {
        get 
        { 
            string result = (string)CallContext.GetData("Parameter");
            if (result == null)
                result = string.Empty;
            return result; 
        }
    }
#>

마지막으로 2개의 tt 파일과 MultiOutput.tt에서 제공되는 메서드를 이용해 다중 파일을 생성하는 tt 파일을 만들어 줍니다.

<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#@ include file="MultiOutput.tt" #>
<#
    CallContext.SetData("Parameter", "MyType"); // csgen.tt, jsgen.tt 내부에서 활용할 데이터를 전달

    ProcessTemplate("csgen.tt", "DataObject.cs");
    ProcessTemplate("jsgen.tt", "DataObject.js");

    DeleteOldOutputs();
#>

이번 tt 파일은 소스 코드를 생성할 것이므로 Custom Tool의 내용을 지워서는 안됩니다. 이렇게 만들고 tt 파일을 저장하면 하위에 다음과 같이 3개의 파일이 생성됩니다

t4_multiple_output_2.png

이 중에서 DataObject.cs, DataObject.js 파일은 각각 csgen.tt, jsgen.tt에 의해 생성된 것이고 DataObjectGen.js 파일은 본래의 DataObjectGen.tt에 의해 성성된 것입니다. (현재는 DataObjectGen.tt에 별다른 출력 내용이 없으므로 아무것도 포함하고 있지 않습니다.)




그리하여, "T4 를 이용한 INotifyPropertyChanged 코드 자동 생성" 글과 이번 글의 내용을 합치면 어떻게 될까요?

단일 XML 하나에 정의된 Data Object 스키마를 통해 다중 언어로의 구현 타입으로 자동으로 출력하는 것이 가능합니다. ^^

(첨부된 파일은 위의 실습을 포함한 프로젝트입니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 5/24/2014]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2014-05-26 11시01분
[spowner] 와우! 브라보!
[guest]

... 106  107  108  109  110  111  112  113  114  115  116  [117]  118  119  120  ...
NoWriterDateCnt.TitleFile(s)
10999정성태7/16/201622007오류 유형: 341. .NET Framework 4.5.2가 설치 안 되는 경우
10998정성태7/16/201621762.NET Framework: 598. C# - Excel 시트에 윈도우 폼 기능을 추가하는 방법 [1]파일 다운로드1
10997정성태7/16/201621069오류 유형: 340. HTTP Error 500.23 - Internal Server Error파일 다운로드1
10996정성태7/14/201626659Windows: 118. 유선 접속 상태에서 재부팅하면 무선 연결이 자동 연결 안되는 문제 [4]파일 다운로드1
10995정성태6/27/201620793VS.NET IDE: 109. Visual Studio 유료 버전 사용자의 주기적인 온라인 인증을 없애는 방법
10994정성태6/23/201620204개발 환경 구성: 285. 알고스팟(https://algospot.com)을 위한 Visual C++ 답안 작성 요령파일 다운로드1
10993정성태6/23/201621025.NET Framework: 597. 닷넷 메타데이터에 struct/class(값/참조 형식)의 구분이 있을까요?
10992정성태6/13/201618257오류 유형: 339. vbs 스크립트 실행 시 항상 실행 여부를 묻는 질문 창이 뜬다면?
10991정성태6/13/201622511오류 유형: 338. octave-gui 실행 시 "octave-gui.exe has stopped working" 오류
10990정성태6/13/201624055오류 유형: 337. missing type specifier - [type] assumed. Note: C++ does not support default-[type]
10989정성태6/7/201620505.NET Framework: 596. C# - WCF wsDualHttpBinding의 ClientBaseAddress 속성 - 두 번째 이야기
10988정성태6/3/201621477기타: 57. Outlook blocked access to the following potentially unsafe attachments
10987정성태6/2/201622527.NET Framework: 595. XLL 파일에 포함된 .NET 어셈블리를 추출하는 방법
10986정성태6/1/201622964.NET Framework: 594. C# - WCF wsDualHttpBinding의 ClientBaseAddress 속성
10985정성태6/1/201621511오류 유형: 336. An error occurred while ejecting 'DVD RW drive ...'
10984정성태5/31/201627160.NET Framework: 593. C# - wsDualHttpBinding WCF 예제 프로그램파일 다운로드1
10983정성태5/30/201621330VC++: 97. C++ 템플릿 remove_pointer, enable_if, is_pointer 사용 예제파일 다운로드1
10982정성태5/26/201619652오류 유형: 335. SQL Server Management Studio - The database ... is not accessible.
10981정성태5/24/201624695.NET Framework: 592. C# - Lights Out 퍼즐 풀기 [2]파일 다운로드1
10980정성태5/24/201621968VS.NET IDE: 108. Visual Studio 2013/2015를 위한 "Macros for Visual Studio"
10979정성태5/23/201625232.NET Framework: 591. C# - 조합(Combination) 예제 코드 - 두 번째 이야기파일 다운로드1
10978정성태5/23/201623861.NET Framework: 590. C# - 모든 경우의 수를 조합하는 코드 (2)파일 다운로드1
10977정성태5/23/201628318.NET Framework: 589. C# - 모든 경우의 수를 조합하는 코드 (1)파일 다운로드1
10976정성태5/20/201622688Math: 18. C# - 오일러 공식을 이용한 복소수 값의 라디안 회전파일 다운로드1
10975정성태5/20/201623090Math: 17. C# - 복소수 타입의 승수를 지원하는 Power 메서드파일 다운로드1
10974정성태5/20/201623649.NET Framework: 588. C# - OxyPlot 라이브러리로 복소수 표현파일 다운로드1
... 106  107  108  109  110  111  112  113  114  115  116  [117]  118  119  120  ...