VC2010のカスタムウィザードを作る まとめ
Visual Studio 2010にて、UIなしのVisual C++ カスタムウィザードを作成する方法のまとめ。
手順
- プロジェクト作成
- コピー元ファイルの配置
- Templates.infでテンプレートとして配置するファイルを指定
- default.jsでプロジェクト設定
- カスタムウィザードの配置
プロジェクトの作成
- [ファイル]-[新規作成]-[プロジェクト]を選択
- 新しいプロジェクト画面で、[インストールされたテンプレート]-[Visual C++]-[カスタムウィザード]を選択
- 設定ウィザードの2枚目で[ユーザーインターフェイス]のチェックをはずす
コピー元ファイルの配置
- プロジェクトフォルダに作成された「Template/1041」以下に必要なファイルを配置
Templates.infでテンプレートとして配置するファイルを指定
- コピーするファイルを列挙して、Templates,infからの相対パスで記述
main.cpp libs\cppunit\cppunit_dll.dll libs\cppunit\cppunit_dll.lib libs\cppunit\AdditionalMessage.h ...(略)...
default.jsでプロジェクト設定
- フィルターの作成
- ファイルのプロジェクト追加時の設定
- プロジェクトの設定
フィルターの作成
- AddFilters関数を編集
- フィルターにAddFilterすることで、階層構造を生成
function AddFilters(proj) { try { // プロジェクトにフォルダーを追加する var sources = proj.Object.AddFilter('sources'); sources.AddFilter('main'); sources.AddFilter('tests'); } catch(e) { throw e; } }
ファイルのプロジェクト追加時の設定
- AddFilesToCustomProj関数を編集
- libやdllなどはコピーのみになるように指定
- プロジェクトの設定で設定するヘッダーなどは、プロジェクトに追加しないように指定
function AddFilesToCustomProj(proj, strProjectName, strProjectPath, InfFile) { try { var projItems = proj.ProjectItems var strTemplatePath = wizard.FindSymbol('TEMPLATES_PATH'); var strTpl = ''; var strName = ''; var strTextStream = InfFile.OpenAsTextStream(1, -2); while (!strTextStream.AtEndOfStream) { strTpl = strTextStream.ReadLine(); if (strTpl != '') { strName = strTpl; var strTarget = GetTargetName(strName, strProjectName); var strTemplate = strTemplatePath + '\\' + strTpl; var strFile = strProjectPath + '\\' + strTarget; var bCopyOnly = false; var strExt = strName.substr(strName.lastIndexOf(".")); if (strExt == ".bmp" || strExt == ".ico" || strExt == ".gif" || strExt == ".rtf" || strExt == ".css" || strExt == ".dll" || strExt == ".lib") bCopyOnly = true; wizard.RenderTemplate(strTemplate, strFile, bCopyOnly); if (strFile.search("main.cpp") != -1) { var filter = proj.Object.Filters.Item("main"); filter.AddFile(strFile); } else if (strFile.search("SampleClassTest") != -1) { var filter = proj.Object.Filters.Item("tests"); filter.AddFile(strFile); } else if (strExt == ".dll" || strExt == ".lib" || strExt == ".h") { // 無視する } else { proj.Object.AddFile(strFile); } } } strTextStream.Close(); } catch(e) { throw e; } }
プロジェクトの設定
- AddConfig関数を編集
- 追加のインクルードファイルや追加の依存ファイルを指定
- デバッグ情報の生成を有効化
- Release構成に最適化を指定
- 出力フォルダ、ビルド後イベントを指定
function AddConfig(proj, strProjectName) { try { // Debug var config = proj.Object.Configurations('Debug'); //config.OutputDirectory = "$(SolutionDir)_$(Configuration)\\"; //config.IntermediateDirectory = "_$(Configuration)\\"; { // コンパイラ var CLTool = config.Tools('VCCLCompilerTool'); CLTool.AdditionalIncludeDirectories += 'libs'; CLTool.WarningLevel = warningLevelOption.warningLevel_3; CLTool.PreprocessorDefinitions += "WIN32;_DEBUG;_CONSOLE;"; // リンカ var LinkTool = config.Tools('VCLinkerTool'); LinkTool.AdditionalDependencies += "libs\\cppunit\\cppunitd_dll.lib"; LinkTool.GenerateDebugInformation = true; LinkTool.LinkIncremental = linkIncrementalType.linkIncrementalYes; // ビルド後イベント var postBuild = config.Tools('VCPostBuildEventTool'); postBuild.CommandLine = 'copy "$(ProjectDir)libs\\cppunit\\cppunitd_dll.dll" "$(TargetDir)"'; } var config = proj.Object.Configurations('Release'); //config.OutputDirectory = "$(SolutionDir)_$(Configuration)\\"; //config.IntermediateDirectory = "_$(Configuration)\\"; config.WholeProgramOptimization = WholeProgramOptimizationLinkTimeCodeGen; { // コンパイラ var CLTool = config.Tools('VCCLCompilerTool'); CLTool.AdditionalIncludeDirectories += 'libs'; CLTool.WarningLevel = warningLevelOption.warningLevel_3; CLTool.EnableIntrinsicFunctions = true; CLTool.WholeProgramOptimization = true; CLTool.PreprocessorDefinitions += "WIN32;NDEBUG;_CONSOLE;"; // リンカ var LinkTool = config.Tools('VCLinkerTool'); LinkTool.AdditionalDependencies += "libs\\cppunit\\cppunit_dll.lib"; LinkTool.GenerateDebugInformation = true; LinkTool.LinkIncremental = linkIncrementalType.linkIncrementalNo; LinkTool.OptimizeReferences = optRefType.optReferences; LinkTool.EnableCOMDATFolding = optFoldingType.optFolding; // ビルド後イベント var postBuild = config.Tools('VCPostBuildEventTool'); postBuild.CommandLine = 'copy "$(ProjectDir)libs\\cppunit\\cppunit_dll.dll" "$(TargetDir)"'; } } catch(e) { throw e; } }
カスタムウィザードの配置
- 「マイドキュメント/Visual Studio 2010/Wizards」以下に「.ico」「.vsdir」「.vsz」を配置
- 上記フォルダに「Scripts」と「Templates」を格納したフォルダを配置
- 「.vsz」の「ABSOLUTE_PATH」に「Scripts」と「Templates」を格納したフォルダへの絶対パスを指定
- 「.vsdir」にデフォルトプロジェクト名とテンプレートの説明を記述