プログラムを書いているとよくちょっとしたサンプルを作ることが多いです。
そういうときにいちいちVisualStudioを起動するのも面倒って場合が私の場合多いです。
そんな場合は、自分用のちょんプロ環境をもっていると便利です。
私の場合は、エディタがvimなので以下のようにしています。
1.サンプル用ディレクトリ作成
mkdir GsfSamples
2.サンプルとして実行可能なものを表すインターフェースを定義.
[IExecutor.cs] using System; namespace Gsf.Samples.Tmp{ public interface IExecutor{ void Execute(); } }
3.ダミーの実装クラスを作成.
[DummyClass.cs] using System; namespace Gsf.Samples.Tmp{ public class DummyClass : IExecutor{ public void Execute(){ Console.WriteLine("This is Dummy Class......"); } } }
4.エントリポイントクラスを作成
[EntryPoint.cs] // vim:set ts=4 sw=4 et ws is nowrap ft=cs: using System; using System.Runtime.Remoting; namespace Gsf.Samples.Tmp{ public class EntryPoint{ [STAThread] static void Main(string[] args){ if(args.Length == 0){ throw new ArgumentException("引数が指定されていません。"); } (Activator.CreateInstance(null, args[0]).Unwrap() as IExecutor).Execute(); } } }
5.プロジェクトファイル(msbuild)を作成.
ポイントは、自分でCompileターゲットなどを作成せずにMicrosoftが用意してくれている共通ターゲットを利用すること。
<?xml version="1.0" encoding="utf-8"?> <!-- vim:set ts=4 sw=4 et ws is nowrap ft=xml: --> <!-- サンプルプロジェクト用ビルドファイル 共通ターゲットファイルは、以下の場所にある。 ${.net frameworkインストールフォルダ}の下 vimから利用する場合は、以下のようにして実行する。 :!msbuild /t:Build,Run /p:TargetClass=クラス名 作成されたEXEファイルは以下のようにして実行する事が出来る。 $ GsfSamples.exe Gsf.Samples.Tmp.対象クラス名 --> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <!-- 共通プロパティの設定. --> <PropertyGroup> <!-- アセンブリ名 --> <AssemblyName>GsfSamples</AssemblyName> <!-- 出力形式 --> <OutputType>winexe</OutputType> <!-- ルート名前空間 --> <RootNamespace>Gsf.Samples.Tmp</RootNamespace> <!-- 要求される.NET Frameworkのバージョン --> <MinFrameworkVersionRequired>3.0</MinFrameworkVersionRequired> <!-- デバッグタイプ --> <DebugType>full</DebugType> <!-- シンボルの定義 --> <DefineConstants>DEBUG;TRACE;GSF</DefineConstants> </PropertyGroup> <!-- 実行するエントリポイントクラスの指定 TargetClassプロパティは、コマンドラインより渡される。 指定されていない場合は、既定のダミークラスを使用する。 --> <PropertyGroup> <TargetClass>DummyClass</TargetClass> </PropertyGroup> <!-- Runターゲット内にて使用されているプロパティの設定. --> <PropertyGroup> <!-- 実行時の引数 --> <StartArguments>$(RootNamespace).$(TargetClass)</StartArguments> <!-- アプリケーション内での作業ディレクトリ --> <StartWorkingDirectory></StartWorkingDirectory> </PropertyGroup> <!-- コンパイル系のターゲットにて利用されている項目の設定. --> <ItemGroup> <!-- 参照設定. --> <Reference Include="System"/> <Reference Include="System.Data"/> <Reference Include="System.Drawing"/> <Reference Include="System.Drawing"/> <Reference Include="System.Transactions"/> <Reference Include="System.Windows.Forms"/> <Reference Include="System.XML"/> <Reference Include="PresentationCore"/> <Reference Include="PresentationFramework"/> <Reference Include="WindowsBase"/> </ItemGroup> <ItemGroup> <!-- コンパイル対象. --> <Compile Include="*.cs"/> </ItemGroup> <!-- 共通ターゲットファイルの読み込み. --> <!-- C#用の共通ターゲット定義ファイル.(内部でCommon定義ファイルが読み込まれている.) --> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <!-- WinFX用の共通ターゲット定義ファイル. --> <Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" /> </Project>
6.サンプルクラスを作成してみる。
[DateTimeParseExactSample.cs] // vim:set ts=4 sw=4 et ws is nowrap ft=cs: using System; namespace Gsf.Samples.Tmp{ public class DateTimeParseExactSample : IExecutor{ public void Execute(){ // // 指定した書式の日付をパースする。 // // 最後の引数はIFormatProviderとなっており、nullを渡すと // 現在のカルチャの書式情報が使用される。 // string targetDateString = "20071001101112"; string targetDateFormat = "yyyyMMddHHmmss"; DateTime d = DateTime.ParseExact(targetDateString, targetDateFormat, null); Console.WriteLine("{0} {1}", d.ToShortDateString(), d.ToLongTimeString()); } } }
7.VIMからそのままビルドして実行する。(その際に上記のサンプルクラスがあるディレクトリにcdしておくこと。)
コマンドモードから実行。
:!msbuild /t:Build,Run /p:TargetClass=%<
一旦、この構成を作成しておいたら、後はコマンドモードでCTRL-Pとかで履歴を上がれば何度も実行できますね。
%<としているので、現在対象となっているファイル名のクラスが実行されます。
ちなみに、VB2005の場合は、以下のようなプロジェクトファイルを作成すればオッケイです。
<?xml version="1.0" encoding="utf-8"?> <!-- vim:set ts=4 sw=4 et ws is nowrap ft=xml --> <!-- サンプルプロジェクト用ビルドファイル 共通ターゲットファイルは、以下の場所にある。 ${.net frameworkインストールフォルダ}の下 vimから利用する場合は、以下のようにして実行する。 :!msbuild /t:Build,Run /p:TargetClass=クラス名 作成されたEXEファイルは以下のようにして実行する事が出来る。 $ GsfVBSamples.exe Gsf.Samples.Tmp.対象クラス名 --> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <!-- 共通プロパティの設定. --> <PropertyGroup> <!-- アセンブリ名 --> <AssemblyName>GsfVBSamples</AssemblyName> <!-- 出力形式 --> <OutputType>winexe</OutputType> <!-- ルート名前空間 --> <RootNamespace>Gsf.Samples.VisualBasic.Tmp</RootNamespace> <!-- 要求される.NET Frameworkのバージョン --> <MinFrameworkVersionRequired>3.0</MinFrameworkVersionRequired> <!-- デバッグタイプ --> <DebugType>full</DebugType> <!-- シンボルの定義(VBの場合はカンマをセパレーターとする) --> <DefineConstants>DEBUG,TRACE</DefineConstants> </PropertyGroup> <!-- 実行するエントリポイントクラスの指定 TargetClassプロパティは、コマンドラインより渡される。 指定されていない場合は、既定のダミークラスを使用する。 --> <PropertyGroup> <TargetClass>DummyClass</TargetClass> </PropertyGroup> <!-- Runターゲット内にて使用されているプロパティの設定. --> <PropertyGroup> <!-- 実行時の引数 --> <StartArguments>$(RootNamespace).$(TargetClass)</StartArguments> <!-- アプリケーション内での作業ディレクトリ --> <StartWorkingDirectory></StartWorkingDirectory> </PropertyGroup> <!-- コンパイル系のターゲットにて利用されている項目の設定. --> <ItemGroup> <!-- 参照設定. --> <Reference Include="System"/> <Reference Include="System.Data"/> <Reference Include="System.Drawing"/> <Reference Include="System.Drawing"/> <Reference Include="System.Transactions"/> <Reference Include="System.Windows.Forms"/> <Reference Include="System.XML"/> <Reference Include="Microsoft.VisualBasic"/> <Reference Include="PresentationCore"/> <Reference Include="PresentationFramework"/> <Reference Include="WindowsBase"/> </ItemGroup> <ItemGroup> <!-- コンパイル対象. --> <Compile Include="*.vb"/> </ItemGroup> <!-- 共通ターゲットファイルの読み込み. --> <!-- VisualBasic用の共通ターゲット定義ファイル.(内部でCommon定義ファイルが読み込まれている.) --> <Import Project="$(MSBuildBinPath)\Microsoft.VisualBasic.targets" /> <!-- WinFX用の共通ターゲット定義ファイル. --> <Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" /> </Project>