This class allows you to manipulate projects. The topics covered in this section include:
This lets you retrieve the name of a project, as shown in Listing 3.20.
BSTR* pval - the project name.
'create an instance of CodeWarrior
set CW = CreateObject("CodeWarrior.CodeWarriorApp")
projectname = "c:\testing\Test.mcp"
'*** open a project (path, visible, ask if
`*** converting, allow revert)
set project = CW.OpenProject(projectName, true, 2, 1)
'***get the default project and name
projectName = project.Name
This lets you retrieve the file spec of a project, as shown in Listing 3.21.
IFileSpec** pval - the project file spec.
'***create an instance of CodeWarrior
set CW = CreateObject("CodeWarrior.CodeWarriorApp")
projectname = "c:\testing\Test.mcp"
'*** open a project (path, visible, ask if
`*** converting, allow revert)
set project = CW.OpenProject(projectName, true, 2, 1)
'*** get the project path
result = project.FileSpec.FullPath
This allows you to get a collection of all the designs. Refer to Listing 3.26 for a detailed example of working with targets.
ICodeWarriorDesignCollection** pval - the collection of targets.
This allows you to get a collection of all the targets in a project. Refer to Listing 3.26 for a detailed example of working with targets.
ICodeWarriorTargetCollection** pval - the collection of targets.
This lets you see if the window of a project window is visible, as shown in Listing 3.22.
VARIANT_BOOL* pval - the visibility value.
Seeing if a Project is Visible:
'***create an instance of CodeWarrior
set CW = CreateObject("CodeWarrior.CodeWarriorApp")
projectname = "c:\testing\Test.mcp"
'*** open a project (path, visible, ask if
`*** converting, allow revert)
set project = CW.OpenProject(projectName, true, 2, 1)
'*** get the project visibility state
result = project.IsVisible
This lets you obtain the version control settings for a project.
ICodeWarriorVersionControl** VersionControl - the version control object.
This closes a project window, as demonstrated in Listing 3.23.
'***create an instance of CodeWarrior
set CW = CreateObject("CodeWarrior.CodeWarriorApp")
projectname = "c:\testing\Test.mcp"
'*** open a project (path, visible, ask if
`*** converting, allow revert)
set project = CW.OpenProject(projectName, true, 2, 1)
'*** close the project
project.Close
This creates an XML file containing the details of the project file. An example is shown in Listing 3.24.
ICodeWarriorProject.Export Arguments:
' file: groups.vbs
' author: Kurt Marley, AG Communication Systems
' created: Jan. 23, 2000
' This script was created as an exercise for the students in
' the Scripting CodeWarrior course available from Metrowerks.
' This script creates an application object, gets the default
' project. It then Exports the entire project in XML format
' inorder to read the XML and find all Groups in the File View.
' All Groups are displayed along with the files which are
' contained within each Group.
option explicit
dim CW 'ICodeWarrior
dim project 'default project
dim textDocument 'text document object to hold report
dim textEngine 'the object for dealing with text
dim eol 'end-of-line character for formatting
dim result 'returned values
dim projectName 'name of default project
dim FileSpecFor_xml 'XML file name
dim FSO 'IFileSystem
dim TS 'ITextStream
dim fileLine 'text line read from xml file
dim i 'loop; math
dim j 'math
dim GroupName 'group name
dim File 'file within current group
dim GroupLevel 'group nesting level
dim GroupCount 'number of groups
dim foundit 'loop flag
eol = chr(13) 'set end of line character
' create an instance of CodeWarrior
set CW = CreateObject("CodeWarrior.CodeWarriorApp")
'create text document and get engine
set textDocument = CW.OpenUntitledTextDocument()
set textEngine = textDocument.TextEngine
'get the default project
set project = CW.DefaultProject
'do some error control here
if TypeName(project) = "Nothing" then
textEngine.InsertText("Script operates on default project." &eol)
textEngine.InsertText("There must be at least one open project." &eol)
else ' valid project
' Export the project as XML, open that XML file,
' read down to the <GROUPLIST> line and then parse
' for:
' <GROUP><NAME>GroupName</NAME> = beginning of group
' with name of group;
' </GROUP> = end of group; and
' <PATH>Filename</PATH> = file within current
' group.
projectName = project.Name
textEngine.InsertText("Structure of project: " &projectName &eol)
textEngine.InsertText("====================================" &eol)
' *** get fullpath filespec for project and append ".xml"
FileSpecFor_xml = project.FileSpec.FullPath & ".xml"
' *** export entire project in XML format
project.Export(FileSpecFor_xml)
' *** Xml file exists, try to open it
set FSO = CreateObject("Scripting.FileSystemObject")
if ( NOT FSO.FileExists( FileSpecFor_xml ) ) then
textEngine.InsertText("**Couldn''t open " &FileSpecFor_xml &eol)
end if
set TS = FSO.OpenTextFile( FileSpecFor_xml )
GroupLevel = 0 ' group Nesting Level
GroupCount = 0 ' number of Groups
' *** find beginning of <GROUPLIST> in xml file
foundit = False
do
fileLine = TS.Readline
if( instr( fileLine, "<GROUPLIST>") <> 0 ) then
foundit = True
end if
loop until( foundit OR TS.AtEndOfStream )
do while( NOT TS.AtEndOfStream ) ' if Not EOF
fileLine = TS.Readline
if( instr( fileLine, "<GROUP>" ) <> 0) then ' if group
i = 6 + instr( fileLine, "<NAME>" )
j = instr( fileLine, "</NAME>" )
GroupName = mid( fileLine, i, j-i) ' extract group name
for i=0 to GroupLevel
textEngine.InsertText("|--")
next
textEngine.InsertText("Group: " &GroupName &eol)
GroupLevel = GroupLevel + 1
GroupCount = GroupCount + 1
elseif( instr(fileLine, "</GROUP>") <> 0 ) then ' if end of group
if(GroupLevel>0) then
GroupLevel = GroupLevel - 1 ' decr. level if non-zero
end if
elseif( instr(fileLine, "<PATH>") <> 0) then ' if file
i = 6 + instr( fileLine, "<PATH>")
j = instr( fileLine, "</PATH>")
File = mid( fileLine, i, j-i) ' extract file name
for i=0 to GroupLevel
textEngine.InsertText("|--")
next
textEngine.InsertText("File: " &File &eol)
end if
loop ' while not EOF
TS.close
textEngine.InsertText(" " &eol)
textEngine.InsertText("..Total number of Groups: " &GroupCount &eol)
end if ' valid project
This creates an XML file containing the details of the project file.
Argument
Functionality
IFileSpec* FileSpec
ICodeWarriorProject.ExportByFileSpec Arguments:
This removes binaries from a project , as demonstrated in Listing 3.25.
Removing Binaries from a Project:
'***create an instance of CodeWarrior
set CW = CreateObject("CodeWarrior.CodeWarriorApp")
projectname = "c:\testing\Test.mcp"
'*** open a project (path, visible, ask if
`*** converting, allow revert)
set project = CW.OpenProject(projectName, true, 2, 1)
'*** purge binaries from the project
project.RemoveBinaries
This lets you choose which target is the current target of a project. Refer to Listing 3.26 for a detailed example of working with targets.
ICodeWarriorProject.SetCurrentTarget Arguments:
This lets you create a new target in a project. Refer to Listing 3.26 for a detailed example of working with targets.
Argument
Functionality
ICodeWarriorDesign* Design
ICodeWarriorProject.CreateTarget Arguments:
ICodeWarriorTarget** Target - the target that was created.
This lets you remove an existing target from a project. Refer to Listing 3.26 for a detailed example of working with targets.
Argument
Functionality
ICodeWarriorTarget* Target
ICodeWarriorProject.RemoveTarget Arguments:
This lets you find an existing target in a project. Refer to Listing 3.26 for a detailed example of working with targets.
ICodeWarriorProject.FindTarget Arguments:
ICodeWarriorTarget** Target - the target that was created.
This lets you clone an existing target in a project. Refer to Listing 3.26 for a detailed example of working with targets.
Argument
Functionality
ICodeWarriorTarget* srcTarget
ICodeWarriorProject* srcProject
ICodeWarriorDesign* Design
ICodeWarriorProject.CloneTarget Arguments:
ICodeWarriorTarget** Target - the target that was created.
This lets you get the current target in a project. Refer to Listing 3.26 for a detailed example of working with targets.
ICodeWarriorTarget** Target - the target that was created.
This allows you to create a new design in your project file.
ICodeWarriorProject.CreateDesign Arguments:
' file: 03Targets.vbs
' author: Jim Trudeau, Metrowerks
' created: August 23, 1999
' modification history
'
' This script was created as an exercise for the students in
' the Scripting CodeWarrior course available from Metrowerks.
' This script creates an application object and gets the default
' project. It then creates a new target, clones an existing
' target, and adds a file to the cloned target. It then displays
' a complete list of all targets in this project. After that,
' the script gets the current target. It displays the output
' file information for the current target.
' The course project "ScriptTest.mcp" must be the default
' project for all aspects of this script to work.
option explicit 'all variables must be declared
dim CW
dim project 'default project
dim textDocument 'text document object to hold report
dim textEngine 'the object for dealing with text
dim eol 'end-of-line character for formatting
dim result 'returned values
eol = chr(13) 'set end of line character
'create an instance of CodeWarrior
set CW = CreateObject("CodeWarrior.CodeWarriorApp")
'create text document and get engine
set textDocument = CW.OpenUntitledTextDocument()
set textEngine = textDocument.TextEngine
'get the default project
set project = CW.DefaultProject
'do some error control here
if TypeName(project) = "Nothing" then
textEngine.InsertText("Script operates on default project." &eol)
textEngine.InsertText("There must be at least one open project." &eol)
else
dim theTarget 'target
dim design 'null design
dim file 'file added to a target
dim targetList 'collection of targets
'remove targets to ensure they don't exist
'this is here to allow students to run the script repeatedly
RemoveNewTargets
'*** create a target (name, linker, design)
set theTarget = project.CreateTarget("New Target", "Win32 x86 Linker", nothing)
'*** clone the new target (copy files and settings, no design)
set theTarget = project.CloneTarget(theTarget, project, "Cloned Target", true, true, nothing)
'*** add a file to the cloned target
set file = theTarget.FindAndAddFile ("Sample.c", "Source")
'*** get all targets for this project
set targetList = project.Targets
'list the name of all targets
ListTargets targetList
'*** get the current target
set theTarget = project.GetCurrentTarget
'*** get the browser state for the current target
result = theTarget.BrowserEnabled
DisplayOutputInfo theTarget
end if
'=========================================================
' ListTargets - display name of each target in collection
' receives target collection interface
'=========================================================
sub ListTargets (targets)
dim index
dim target
textEngine.InsertText("Targets:" &eol)
for index = 0 to targets.Count-1
'get the individual target
set target = targets.Item(index)
'display the target name
textEngine.InsertText(" " &target.Name &eol)
next
'skip a line
textEngine.InsertText(eol)
end sub
'=========================================================
' DisplayOutputInfo - display target output info
' receives target interface
'=========================================================
sub DisplayOutputInfo (target)
dim output 'interface to ICodeWarriorTargetOutput
dim string 'array of strings
dim result
string = Array("none", "file", "directory")
textEngine.InsertText("Output Information for ")
textEngine.InsertText(target.name &eol)
'*** get the output object
set output = target.GetTargetOutput
'*** get the kind of output
result = output.OutputKind
textEngine.InsertText(" Output Kind: " &string(result) &eol)
'*** get the name of the output file
result = output.FileSpec.Name
textEngine.InsertText(" Output Name: " &result &eol)
'*** get the path to the output file
result = output.FileSpec.FullPath
textEngine.InsertText(" Location: " &result &eol)
end sub
'=========================================================
' RemoveNewTargets - check to see if targets exist
' remove them if they do to avoid generating errors
' on multiple runs of this demo script
' receives project interface
'=========================================================
sub RemoveNewTargets
dim theTarget
'find the New Target
set theTarget = project.FindTarget("New Target")
'if it exists
if TypeName(theTarget) <> "Nothing" then
'remove it
project.RemoveTarget theTarget
end if
'find the cloned target
set theTarget = project.FindTarget("Cloned Target")
'if it exists
if TypeName(theTarget) <> "Nothing" then
'remove it
project.RemoveTarget theTarget
end if
end sub