LoginSignup
3
3

More than 5 years have passed since last update.

SharpDevelop のプロジェクトテンプレートをカスタマイズする

Last updated at Posted at 2012-09-08

以前、以下の記事を書きましたが、
今回はプロジェクトテンプレートをカスタマイズしてみます。

今回の目的

新規作成メニューに「コンソールアプリケーション(Log4Net)」を追加する。(最初から以下のロジックを足したもの)

  • Log4Netのひな形
  • NDesk.Options によるコマンドライン処理のひな形
  • 補足されない例外の処理

手順

プロジェクトテンプレート等が保存されている場所は以下の場所と仮定します。
環境が異なる場合は読み替えてください。
(C:\Program Files\SharpDevelop\4.2\data\templates)

  1. 「 コンソールアプリケーション」のテンプレートの実体はConsoleProject.xptなので、このファイルをコピーしてリネームします。 -> ConsoleProjectRohinomiya.xpt
  2. その中身を見ながら、なんとなくこんな感じだろう、と書き換えてみます。
  3. SharpDevelop を再起動すれば、新規作成のメニューに出てくるようになります。

すべてのファイルは、以下の場所においておりますので、SharpDevelop の対応するディレクトリに配置してください。

テンプレートファイルの中身

ConsoleProjectRohinomiya.xpt
<?xml version="1.0"?>
<Template originator = "Rohinomiya"
          created      = "2012/09/07"
          lastModified = "2012/09/07">

    <!-- Template Header -->
    <TemplateConfiguration>
        <Name>${res:Templates.Project.ConsoleProject.Name}(with log4net)</Name>
        <Category>C#</Category>
        <Subcategory>${res:Templates.File.Categories.WindowsApplications}</Subcategory>
        <Icon>C#.Project.DOSProject</Icon>
        <Description>${res:Templates.Project.ConsoleProject.Description} (with log4net)</Description>
        <SupportedTargetFrameworks>v2.0;v3.5Client</SupportedTargetFrameworks>
    </TemplateConfiguration>

    <!-- Actions -->
    <Actions>
        <Open filename = "Program.cs"/>
    </Actions>

    <!-- Template Content -->
    <Project language = "C#">
        <ProjectItems>
            <Reference Include="System" />
            <Reference Include="System.Core" />
            <Reference Include="System.Windows.Forms" />
            <Reference Include="log4net">
                <HintPath>C:\Program Files\SharpDevelop\4.2\data\templates\project\CSharp\log4net.dll</HintPath>
                <SpecificVersion>False</SpecificVersion>
                <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
                <Private>True</Private>
            </Reference>
            <Reference Include="NDesk.Options">
                <HintPath>C:\Program Files\SharpDevelop\4.2\data\templates\project\CSharp\NDesk.Options.dll</HintPath>
                <SpecificVersion>False</SpecificVersion>
                <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
                <Private>True</Private>
            </Reference>
        </ProjectItems>

        <PropertyGroup>
            <OutputType>Exe</OutputType>
            <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
            <TargetFrameworkProfile></TargetFrameworkProfile>
            <AppDesignerFolder>Properties</AppDesignerFolder>
        </PropertyGroup>
        <Files>
            <File name="Program.cs"><![CDATA[${StandardHeader.C#}
using log4net;
using NDesk.Options;
using Rohinomiya;   // my namespace
using System;
using System.Collections.Generic;

namespace ${StandardNamespace}
{
    class Program
    {
        public static void Main(string[] args)
        {
            HandlerForUnhandledException.GetInstance().HandleUnhandledException();

            // TODO: edit output directory in Log4Net.Config.xml
            //ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            ILog logger = NullLogger.GetInstance();

            // TODO: refactor Configuration class
            bool verbose = false;
            bool recursive = false;
            string output_dir = "";

            List<string> extraCommandLineArgs = new List<string>();

            // define command line options
            var p = new OptionSet()
                .Add ("v|version",    dummy => { ShowHelp(); Environment.Exit(0); } )
                .Add ("?|h|help",     dummy => { ShowHelp(); Environment.Exit(0); } )
                .Add ("verbose",          v => { if(v!=null) verbose = true; }      )
                .Add ("o=|output_dir=", dir => { if(dir!=null) output_dir = dir; }  )
                .Add ("r|recursive",      r => { if(r!=null) recursive = true; }    )
                ;

            try {
                // extraCommandLineArgs = commandLineArgs except options
                extraCommandLineArgs = p.Parse(Environment.GetCommandLineArgs());
            }
            catch(Exception ex){
                Console.WriteLine(ex.Message);
                Environment.Exit(0);
            }

            // todo: parse result 
            Console.WriteLine(String.Format("verbose={0}",verbose.ToString()));
            Console.WriteLine(String.Format("recursive={0}",recursive.ToString()));
            Console.WriteLine(String.Format("output_dir={0}",output_dir));

            int index = 0;
            foreach (var arg in extraCommandLineArgs) {
                Console.WriteLine(String.Format("arg[{0}]={1}",index,arg));
                index++;
            }

            // TODO: Implement Functionality Here

            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }

        /// <summary>
        /// Show Help
        /// </summary>
        public static void ShowHelp()
        {
            Console.WriteLine("This is help message.");
        }
    }
}]]></File>
            <File name="Properties\AssemblyInfo.cs" src="DefaultAssemblyInfoLog4Net.cs"/>
            <File name="ClassNullLogger.cs" src="ClassNullLogger.cs"/>
            <File name="Log4net.Config.xml" src="Log4net.Config.xml"/>
            <File name="ClassHandlerForUnhandledException.cs" src="ClassHandlerForUnhandledException.cs"/>
        </Files>        
    </Project>
</Template>

ポイント

ビルド時にDLLをターゲットディレクトリにコピーする。

<Reference Include="log4net">
    <HintPath>C:\Program Files\SharpDevelop\4.2\data\templates\project\CSharp\log4net.dll</HintPath>
    <SpecificVersion>False</SpecificVersion>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    <Private>True</Private>
</Reference>

.NET Framework 4.0 を使用する (.NET Framework 4.0 Client Profile に非ず)

<PropertyGroup>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <TargetFrameworkProfile></TargetFrameworkProfile>
</PropertyGroup>

参考

3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3