Writing the JScript
When the user clicks the Finish button, the wizard loads the default.js file into the Script Files folder in Solution Explorer. The file contains by default the following functions:
- OnFinish: Called by the wizard when the Finish button is clicked; it adds files and filters, render templates and creates the configurations
- CreateCustomProject: Creates the project at the specified location when the Finish button is pressed
- AddFilters: Adds filters to the project
- AddConfig: Adds configurations to the project
- PchSettings: Sets the precompiled header settings
- DelFile: Deletes the specified files
- CreateCustomInfFile: Creates the project's Templates.inf file
- GetTargetName: Gets the name of a specified file
- AddFilesToCustomProj: Adds specified files to the project
In addition to these functions, you can add your own JScript function to Default.js. To access properties and methods in the wizard object model or the environment model from the JScript file, prepend the object model item with wizard and respective date.
OnFinish
This function is called when the Finish button in the dialog is pressed. This function calls most of the other functions mentioned above. In addition to the default provided code, you need to do three things:
- Generate a symbol with the value of the current date to write in the files header
- Add a symbol for each filter to add to the project
- Call AddCommonConfig function to add the default Debug and Release configurations to the project
The code of the function is shown below:
function OnFinish(selProj, selObj)
{
try
{
var strProjectPath = wizard.FindSymbol('PROJECT_PATH');
var strProjectName = wizard.FindSymbol('PROJECT_NAME');
// get the current date and add a symbol CURRENT_DATE
var currentTime = new Date();
var month = currentTime.getMonth() + 1
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var today = year + '.' + month + '.' + day;
wizard.AddSymbol('CURRENT_DATE', today);
// add symbols for the filters of the project
wizard.AddSymbol('MY_SOURCE_FOLDER_NAME', 'Source Files');
wizard.AddSymbol('MY_HEADER_FOLDER_NAME', 'Header Files');
wizard.AddSymbol('MY_RESOURCE_FOLDER_NAME', 'Resource Files');
selProj = CreateCustomProject(strProjectName, strProjectPath);
AddCommonConfig(selProj, strProjectName);
AddConfig(selProj, strProjectName);
AddFilters(selProj);
var InfFile = CreateCustomInfFile();
AddFilesToCustomProj(selProj, strProjectName, strProjectPath,
InfFile);
PchSettings(selProj);
InfFile.Delete();
selProj.Object.Save();
}
catch(e)
{
if (e.description.length != 0)
SetErrorInfo(e);
return e.number
}
}
AddFilters
This function is called to create the filters for the project. Your project will have three filters:
- Source Files: Contains files with the extensions .cpp, .cxx, .c
- Header Files: Contains files with the extensions .h, .hpp
- Resource Files: Contains files with the extensions .rc, .rc2, .bmp, .ico, .gif
In this function, you will retrieve the values of the symbols generated in OnFinish() to use as the filter names.
function AddFilters(proj)
{
try
{
var group1 = proj.Object.AddFilter
(wizard.FindSymbol('MY_SOURCE_FOLDER_NAME'));
group1.Filter = ".cpp;.cxx;.c";
var group2 = proj.Object.AddFilter
(wizard.FindSymbol('MY_HEADER_FOLDER_NAME'));
group2.Filter = ".h;.hpp";
var group3 = proj.Object.AddFilter
(wizard.FindSymbol('MY_RESOURCE_FOLDER_NAME'));
group3.Filter = '.rc;.rc2;.ico;.gif;.bmp;';
}
catch(e)
{
throw e;
}
}
AddConfig
Use this function to set the various compiler and linker settings for the available configurations. You can access the configuration object in this manner:
var config = proj.Object.Configurations('Debug');
The members of the VCConfiguration interface are listed here. You can set properties such as the IntermediateDirectory, OutputDirectory, or ConfigurationType. An important property is Tools that gives the available tools for the configuration that include:
In the sample code, the compiler, linker, manifest, and browser tools are customized with different settings. The code is listed below:
function AddConfig(proj, strProjectName)
{
try
{
// ----------------------------------------------------------
// settings for the DEBUG configuration
// ----------------------------------------------------------
var config = proj.Object.Configurations('Debug');
config.IntermediateDirectory = '.\\Debug';
config.OutputDirectory = '.\\Debug';
config.InheritedPropertySheets =
'$(VCInstallDir)VCProjectDefaults\\UpgradeFromVC60.vsprops';
config.ConfigurationType = ConfigurationTypes.typeApplication;
var CLTool = config.Tools('VCCLCompilerTool');
CLTool.AdditionalIncludeDirectories = './;./src/';
CLTool.Detect64BitPortabilityProblems = false;
CLTool.PreprocessorDefinitions = 'WIN32;_DEBUG;_CONSOLE';
CLTool.UsePrecompiledHeader = pchOption.pchCreateUsingSpecific;
CLTool.PrecompiledHeaderThrough = "StdAfx.h";
CLTool.PrecompiledHeaderFile = '.\\Debug/' +
strProjectName + '.pch';
CLTool.AssemblerListingLocation = '.\\Debug/';
CLTool.ObjectFile = '.\\Debug/';
CLTool.ProgramDataBaseFileName = '.\\Debug/';
var LinkTool = config.Tools('VCLinkerTool');
LinkTool.OutputFile = '$(OutDir)\$(ProjectName).exe';
LinkTool.GenerateManifest = false;
LinkTool.ProgramDatabaseFile = '.\\Debug/' +
strProjectName + 'D.pdb';
LinkTool.SubSystem = subSystemOption.subSystemNotSet;
LinkTool.ImportLibrary = '.\\Debug/' + strProjectName + 'D.lib';
var ManifestTool = config.Tools('VCManifestTool');
ManifestTool.EmbedManifest = false;
ManifestTool.OutputManifestFile = '$(TargetPath).manifest';
var BrowseTool = config.Tools('VCBscMakeTool');
BrowseTool.OutputFile = '.\\Debug/' + strProjectName + 'D.bsc';
// ----------------------------------------------------------
// settings for the RELEASE configuration
// ----------------------------------------------------------
config = proj.Object.Configurations('Release');
config.IntermediateDirectory = '.\\Release';
config.OutputDirectory = '.\\Release';
config.InheritedPropertySheets =
'$(VCInstallDir)VCProjectDefaults\\UpgradeFromVC60.vsprops';
config.ConfigurationType = ConfigurationTypes.typeApplication;
var CLTool = config.Tools('VCCLCompilerTool');
CLTool.AdditionalIncludeDirectories = './;./src/';
CLTool.DebugInformationFormat = debugOption.debugDisabled;
CLTool.Detect64BitPortabilityProblems = false;
CLTool.InlineFunctionExpansion = expandOnlyInline;
CLTool.WholeProgramOptimization = false;
CLTool.PreprocessorDefinitions = 'WIN32;NDEBUG;_CONSOLE';
CLTool.StringPooling = true;
CLTool.EnableFunctionLevelLinking = true;
CLTool.UsePrecompiledHeader = pchOption.pchCreateUsingSpecific;
CLTool.PrecompiledHeaderThrough = "StdAfx.h";
CLTool.PrecompiledHeaderFile = '.\\Release/' +
strProjectName + '.pch';
CLTool.AssemblerListingLocation = '.\\Release/';
CLTool.ObjectFile = '.\\Release/';
CLTool.ProgramDataBaseFileName = '.\\Release/';
var LinkTool = config.Tools('VCLinkerTool');
LinkTool.OutputFile = '$(OutDir)\$(ProjectName).exe';
LinkTool.GenerateManifest = false;
LinkTool.GenerateDebugInformation = false;
LinkTool.ProgramDatabaseFile = '.\\Release/' +
strProjectName + '.pdb';
LinkTool.SubSystem = subSystemOption.subSystemNotSet;
LinkTool.OptimizeReferences =
optRefType.optReferencesDefault;
LinkTool.EnableCOMDATFolding =
optFoldingType.optFoldingDefault;
LinkTool.LinkTimeCodeGeneration =
LinkTimeCodeGenerationOption.
LinkTimeCodeGenerationOptionDefault;
var ManifestTool = config.Tools('VCManifestTool');
ManifestTool.EmbedManifest = false;
ManifestTool.OutputManifestFile = '$(TargetPath).manifest';
var BrowseTool = config.Tools('VCBscMakeTool');
BrowseTool.OutputFile = '.\\Release/' +
strProjectName + 'D.bsc';
}
catch(e)
{
throw e;
}
}
GetTargetName
This function gets called for every file listed in the Templates.inf file. You can use this method to change the name of the files or the path. That is exactly what we need to do to follow the requirements:
- Sampleclass.h must be renamed to <CLASS_NAME>.h and put in the src subfolder
- Sampleclass.cpp must be renamed to <CLASS_NAME>.h and put in the src subfolder
- BaseSample.cpp must be renamed to main.cpp
All the other file names remain unchanged and they will be placed in the directly under the project folder.
function GetTargetName(strName, strProjectName)
{
try
{
var strTarget = strName;
// get the name of the class
var strClassName = wizard.FindSymbol('CLASS_NAME');
if (strName == 'sampleclass.h')
{
strTarget = 'src\\' + strClassName + '.h';
}
else if(strName == 'sampleclass.cpp')
{
strTarget = 'src\\' + strClassName + '.cpp';
}
else if(strName == 'BaseSample.cpp')
strTarget = 'main.cpp';
return strTarget;
}
catch(e)
{
throw e;
}
}
AddFilesToCustomProj
This function parses the templates file and adds files to the project. For each file name, it calls GetTargetName() to get the name (and relative path) of the file in the project. In the templates, one of the files is ReadMe.txt, but you don't want this file to be added to the project file, only to be degenerated in the project folder. A custom function called KeepOutsideProject() takes the name of each file (the same name passed to GetTargetName, and not the name returned by this function) and returns true if the file should not be listed in the project file.
function KeepOutsideProject(strName)
{
try
{
var add = false;
if(strName == 'ReadMe.txt')
add = true;
return add;
}
catch(e)
{
throw e;
}
}
Once a file is generated in the project folder, it is added to the project to one of the available filters, depending of the extension. Complete source code is listed below:
function AddFilesToCustomProj(proj, strProjectName, strProjectPath,
InfFile)
{
try
{
var projItems = proj.ProjectItems
// get references to the filters of the project
var projFilters = proj.Object.Filters;
var filterSrc =
projFilters.Item(wizard.FindSymbol('MY_SOURCE_FOLDER_NAME'));
var filterHdr =
projFilters.Item(wizard.FindSymbol('MY_HEADER_FOLDER_NAME'));
var filterRc =
projFilters.Item(wizard.FindSymbol('MY_RESOURCE_FOLDER_NAME'));
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")
bCopyOnly = true;
wizard.RenderTemplate(strTemplate, strFile, bCopyOnly);
if(!KeepOutsideProject(strName))
{
if(strExt == '.cpp' || strExt == '.cxx' ||
strExt == '.c')
{
filterSrc.AddFile(strTarget);
}
else if(strExt == '.hpp' || strExt == '.h')
{
filterHdr.AddFile(strTarget);
}
else if(strExt == '.rc2' ||
strExt == '.ico' || strExt == '.gif' ||
strExt == '.bmp')
{
filterRc.AddFile(strTarget);
}
else if(strExt == '.rc')
{
filterRc.AddFile(strFile);
}
else
proj.Object.AddFile(strFile);
}
}
}
strTextStream.Close();
}
catch(e)
{
throw e;
}
}
All the other functions in Default.js should remain unmodified for this sample project.
Comments
There are no comments yet. Be the first to comment!