Previous walkthrough in the Getting Started with BimlScript series: Getting Started with BimlScript - Part 2
In this walkthrough, we’ll learn about BimlScript directives, template processing tier groups, and the Biml API. To demonstrate these, we are going to create a BimlScript template that automatically generates a package that executes the five packages created in the Getting Started with BimlScript - Part 2 walkthrough.
BimlScript directives provide instructions to the Biml engine on how to process a BimlScript template. In this walkthrough, we are going to learn about the template directive. Note that in the previous walkthrough, the BimlScript file didn’t have a directive. This is because the default template directive was used. The default template directive is:
<#@ template language="C#" tier="1" #>
This directive instructs the BimlEngine to interpret the code in the control blocks as C# code, and process the template in the tier 1 processing group. If you would like to use Visual Basic.NET, instead of C#, then set language="VB"
in the template directive. The tier attribute comes into play when you want to combine the execution of BimlScript files that are dependent on the output of other BimlScript files. To accomplish this, template processing tier groups are required.
Template processing groups guarantee that all the output from templates in tier 1 will be compiled before templates in tier 2, and tier 2 will be compiled before tier 3, and so on. In other words, tier n is always compiled before tier n+1. The power of tiers is that tier n+1 can apply Biml APIs to the compiled output of tier n.
Let’s try using a tier:
- Open the Getting Started with BimlScript project.
- Right click on the SSIS Packages folder inside Solution Explorer. Then click Add New Biml File in the context menu.
- Rename the added file to My Package Driver.biml.
- Double click My Package Driver.biml to open it.
- In the editor, add a line above the
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
tag and type the following directive:
<#@ template language="C#" tier="2" #>
- Next, add the following Biml between the Biml tags:
<Packages> <Package Name="My Package Driver" ConstraintMode="Linear" AutoCreateConfigurationsType="None"> <Tasks>
- Under the tasks tag, add the following code nugget to loop through the packages created in the My Packages.biml file :
<# foreach (var package in RootNode.Packages) { #>
- Then, add the following Biml block:
<ExecutePackage Name="Run <#=package.Name#>"> <Package PackageName="<#=package.Name#>" /> </ExecutePackage>
- Close the foreach loop by typing the following on the next line:
<# } #>
- Finally, type the following to close the open Biml elements:
</Tasks> </Package> </Packages>
Your My Package Driver.biml file should match the following:
<#@ template language="C#" tier="2" #> <Biml xmlns="http://schemas.varigence.com/biml.xsd"> <Packages> <Package Name="My Package Driver" ConstraintMode="Linear" AutoCreateConfigurationsType="None"> <Tasks> <# foreach (var package in RootNode.Packages) { #> <ExecutePackage Name="Run <#=package.Name#>"> <Package PackageName="<#=package.Name#>" /> </ExecutePackage> <# } #> </Tasks> </Package> </Packages> </Biml>
With these changes in place, try generating the SSIS packages for this Biml:
- Right-click My Package Driver.biml and click Generate SSIS Packages.
When you do, you will see the Biml Validation Items dialog box displaying one error regarding the Tasks element. The gist of this message is that a Tasks element needs 1 or more task elements inside of it.
Inside the Tasks element, we have a control block that loops through RootNode.Packages, and a Biml block with the Execute Package task. Since the Biml engine did not find any packages in RootNode.Packages
, a validation error was thrown since the Tasks collection is empty. So, where do we obtain packages to add to RootNode.Packages? The answer is from our My Packages.biml file.
Let’s retry generating a package from My Package Driver.biml file, but this time doing the following:
- Press and hold the <Ctrl> key.
- Click My Package Driver.biml
- Click My Packages.biml and release the
key. - Right-click either of the selected files and click Generate SSIS Packages.
- The Confirm Overwritten Items dialog box will appear. Click Commit to overwrite the current SSIS packages with the new ones.
- Open the new My Package Driver.dtsx package to see what was generated.
By including My Packages.biml and My Package Driver.biml when generating assets, the Biml engine followed a different procedure:
- Both of the BimlScript files were sent to Biml engine to be compiled.
- The files were evaluated and the execution order was determined by the value of the tier attribute in each file’s template directive.
- The My Packages.biml file was compiled first and added to an in-memory Biml model.
- Next, the My Package Driver.biml file was compiled and the compiler used the in-memory model to evaluate and compile the BimlScript.
By compiling My Packages.biml first, its packages were added to the RootNode’s Packages collection. Thus, the Biml engine was able to loop through the RootNode’s package elements when compiling the My Package Driver.biml file. As a result, our desired My Package Driver.dtsx package was generated.
In this walkthrough, we used a template directive to assign a BimlScript to a tier group, along with learning how template tier groups are processed. We also began using the Biml API by iterating the RootNode.Packages collection.
To learn more about the structure of a BimlScript file, please read Basic BimlScript Structure.
Comments
Paul6
12:59pm 10.31.12
Is it possible that I missed some setup tasks? All the directives are underlined with the red squiggle, the following line '<#@ template="" language="C#" tier="2" #> has the error message "Illegal Syntax Expecting valid start name character" It does generate the packages though
Paul S. Waters
3:48pm 04.29.13
Paul6
This has to do with how VS handles pasted text when it thinks it is getting valid XML. Please see the http://bimlscript.com/Walkthrough/Details/45 walk through to learn how to remedy this.
Thanks.
Paul
Zsombor
8:31am 01.30.15
Hi Paul W.
I have the same problem as Paul6. I made the settings you recommend on the page but the problem persists. It is not about the copy/paste, I wrote the code myself. The problem is that apparently these <#@ are not interpreted correctly by the syntax highlighter and the BIML parser. I could send a screenshot but I have no possibility to upload here.
Thanks.
Adrian Chacon
10:14pm 03.04.15
Awesome Tutorial! I only got a taste of SSIS development but I already know how many headaches I'll save by implementing BIML. copy+paste code causes too many problems... might be a better idea to just type it all out...
john6
9:55pm 01.06.17
I have a fresh install of VS2015 and Bimlexpress 1.0 I have walked through this setup and everything seems to work as expected, until I click box of the biml files and generate the ssis packages, it asks for me to confirm to over write the files and then I get object reference is not set to an instance of an object. thoughts?
L
3:35pm 01.13.17
John6, did you ever figure this out? I have the exact same issue, VS2015 along with that Error.
Pavel6
10:22am 07.18.17
I have the same issue as john6. Please, can someone explain to me what is a problem? Thanks. Pavel
Tankepaus
5:11pm 01.08.20
After A LOT of troubleshooting I found out that writing tier = "0" will result in the tier being NULL even though BIML will not complain about the error. The correct way is tier="0".