Scripting in the Cross-Platform Command-Line Client for Team Foundation Server
Would you like to automate tasks, such as checking in and checking out source code? You can use either external or internal automation to get the job done. First, let’s review these two modes of automation. Or, you can skip directly to the pros and cons.
External automation
External automation configures another process to invoke the tf command-line tool repeatedly with fully contextual arguments each time. You don’t need any special syntax to enable this mode. You can use one of the following external processes to implement this method:
A Unix shell, such as sh, ksh, bash, or csh
Perl or another programming language that is used for scripting
Apache Ant or another build-oriented tool
Any other tool that can start the tf program and supply arguments
Internal automation
Internal automation configures the tf command-line tool to drive the automation process and interpret a command file that is passed as an argument. You can invoke internal automation by supplying the @ symbol immediately followed by a local path to a command file. Any additional arguments that you supply in this command line are available to commands in the command file as positional arguments.
Note
To process standard input as your command file, supply the @ symbol with no file name following it (tf@). The input stream is interpreted as text according to your platform's default encoding and character set.
The following command file gets the specified version of a file and prints detailed historical information about that version.
# This line is ignored because it is a comment.
get "-version:%2" -force "%1"
history -format:detailed "-version:%2" "%1"
The command file, which is named /home/john/get-and-history.tfc, expects two positional arguments, so you can run it like the following example shows:
tf @/home/john/get-and-history.tfc README.doc C5087
The first argument, README.doc, is substituted for %1 when each line is interpreted. C5087 is substituted for %2.
Note
In the command file, both positional arguments are surrounded by double quotation marks because their substituted values may contain spaces (for example, a file name or a date version specification).
The format of the command file can be described by the Extended Backus-Naur Form (EBNF):
command file ::= { line } ;
line ::= comment line | blank line | action line , EOL ;
comment-line ::= "#" | "rem" , { ? any non-EOL character ? } ;
blank line ::= { ? any whitespace character ? };
action line ::= tf command , [ { white space , tf option } ] , [ { white space , tf free argument } ] ;
tf command ::= ? any tf command ?
tf option ::= ? any tf option ? | positional argument ;
tf free argument::= ? any tf free argument ? | positional argument ;
positional argument::= "%" , non-zero digit , [ { digit } ] ;
EOL ::= ? your platform's EOL character or sequence ?
white space ::= { ? any non-EOL whitespace character ? }
digit ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
non-zero digit ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
tf command can be any valid tf command, such as get, workfold, and workspace.
tf option can be any valid tf option, such as -login, -profile, and -format.
tf free argument can be any string that is intended as a free argument to tf, such as a server path and a local path.
positional argument is a placeholder for text that is substituted from the outer tf command-line arguments. The number is a positive integer that corresponds to the index of the command-line argument that you want to substitute, such as "%1" and "%2". %1 refers to the first argument. You can use positional arguments as options or free arguments and in any order after the command.
Note
Use double quotation marks to surround options and arguments that contain spaces. For example, use “-version:LRelease 2.1” for a label specifier, “$/Inventory/Client Project/main.c” for a free argument, and "%1" for a positional argument that may contain spaces. Only double quotations denote option boundaries in command files. Single quotations are interpreted literally. To specify a literal double quotation in your action line, you must use two double quotations together (""). Quoting rules inside command files may differ from the rules that your shell uses because your shell does not parse the lines in the command file.
Pros and cons of the two methods
So which method should you use—the external or internal automation? Let’s look at the pros and cons of each method:
External automation |
Internal automation |
|
Pros |
|
|
Cons |
|
It’s less flexible than external automation. Here’s why:
|