Trouble Resolving Types from DomainModelTemplate - code-generation

I am working on adding a base class to specific entities in my object model. To that end I have followed the samples in the following links:
http://drc.ideablade.com/devforce-2012/bin/view/Documentation/model-custom-base-class
http://drc.ideablade.com/devforce-2012/bin/view/Documentation/custom-code-generation-template
My *.tt file looks exactly like the example in the first link with the Assembly includes listed in the second link.
Currently I am getting two errors compiling the transformation:
Compiling transformation: The type or namespace name 'EntityOrComplexTypeWrapper' could not be found (are you missing a using directive or an assembly reference?)
Compiling transformation: The type or namespace name 'EntityTypeWrapper' could not be found (are you missing a using directive or an assembly reference?)
Can you provide any assistance resolving this matter?

So I dug into the IdeaBlade.VisualStudio.OM.CodeGenerator.EF5.dll using ildasm and was able to solve my problem by fully qualifying the objects' namespaces.
IdeaBlade.VisualStudio.OM.CodeGenerator.Metadata.EntityOrComplexTypeWrapper
IdeaBlade.VisualStudio.OM.CodeGenerator.Metadata.EntityTypeWrapper
I also had to add a few assembly imports to the header. Mine now looks like:
<## template language="C#" debug="true" hostSpecific="true" #>
<## output extension=".ReadMe" #>
<## Assembly Name="Microsoft.VisualStudio.TextTemplating.12.0" #>
<## Assembly Name="IdeaBlade.VisualStudio.OM.CodeGenerator.EF5.dll" #>
<## import namespace="IdeaBlade.VisualStudio.OM.CodeGenerator" #>
<## import namespace="IdeaBlade.VisualStudio.OM.CodeGenerator.Metadata" #>
<## Assembly Name="IdeaBlade.Core.dll" #>
<## import namespace="IdeaBlade.Core" #>
<## Assembly Name="IdeaBlade.EntityModel.Edm.Metadata.dll" #>
<## import namespace="IdeaBlade.EntityModel.Edm.Metadata" #>
The DevForce documentation was not complete for this feature.

Related

Referencing addins from secondary script file

I have a cake file with my Tasks defined.
I also have a second cake file with some classes defined.
In the second file, I would like to reference e.g. Cake.Json, and maybe Cake.Svn. But I cannot figure out how reference them.
How would I do that?
Both addins and utility Cake scripts are references using pre-processor directives.
Addin directive
Usage
#addin nuget:?package={NuGet Package Id}&version={NuGet package version}
Example:
#addin nuget:?package=Cake.Json&version=3.0.1
Reference directive
Usage
#r "Path to assembly / dll"
Example:
#r "bin/Cake.Json.dll"
Load directive
Usage
#load "Path to cake file"
Example:
#load "scripts/common.cake"
Reference
Read more about pre-processor directives at
https://cakebuild.net/docs/fundamentals/preprocessor-directives

How to setup TypeLite to build types in other projects

I'm working in MS Dynamics CRM, and I have a WebResources project that contains all of my TypeScript. I have Plugin .net library dll that contains some POCO objects that will be returned via Rest to my javascript in my WebResources project. How do I setup TypeLite to build my Typescript files?
This is my assumption.
Install Typescript NuGet Package in my Plugin project
Decorate the POCO classes I need Typescript files for, with the [TsClass] attribute
Upon build, it will generate the Typescript Files to the Scripts folder that it will create in the Plugin project
I create a postbuild event that copies the Typescript files over to my WebResources Project
Is there an easier way?
Did I make an incorrect assumption?
Update
From Lukas's answer, I've updated my TypeLite.Net4.tt file to look like this:
<## template debug="false" hostspecific="True" language="C#" #>
<## assembly name="$(TargetDir)TypeLite.dll" #>
<## assembly name="$(TargetDir)TypeLite.Net4.dll" #>
<## assembly name="$(TargetDir)Xrm.Plugins.dll" #>
<## assembly name="$(TargetDir)$(TargetFileName)" #>
<## import namespace="TypeLite" #>
<## import namespace="TypeLite.Net4" #>
<##output extension=".d.ts"#>
<##include file="Manager.ttinclude"#>
<# var manager = Manager.Create(Host, GenerationEnvironment); #>
<# var ts = TypeScript.Definitions()
.WithReference("Enums.ts")
.For<Xrm.Plugins.Rest.Poco.AccountLookupResponse>();
#>
<#= ts.Generate(TsGeneratorOutput.Properties) #>
<# manager.StartNewFile("Enums.ts"); #>
<#= ts.Generate(TsGeneratorOutput.Enums) #>
<# manager.EndBlock(); #>
<# manager.Process(true); #>
This generates the correct definition in the TypeLite.Net4.d.ts file. I'm not currently using any enums in my class, so the Enums.ts is not necessary currently, but it doesn't hurt to have it.
There is another way, but I am not sure, whether it is better.
install TypeLITE.Lib package to your Plugin project (you need reference TypeLITE library there, because you want to use the [TsClass] attribute)
install TypeLITE package to your WebResources project and modify TypeLite.tt file to include a reference to your plugin.dll
<## assembly name="$(TargetDir)plugin.dll" #>
alternatively you can skip the first step and use fluent configuration TypeScript.Definitions().For<YourClass>() to include specific classes in the output file

How do I reference an assembly from the GAC in a PowerShell module?

I'm writing a PowerShell module which depending on the SMO assemblies in SQL Server. (The only one I need to reference is Microsoft.SqlServer.Smo.dll)
When I've been developing module. I've just taken a copy of the assembly I need and referenced it in my manifest file similar to this:
RequiredAssemblies = #(
"$env:userprofile\Documents\WindowsPowerShell\Modules\Dependencies\Microsoft.SqlServer.Smo.dll"
)
I would assume that in a production environment you'd want to reference assemblies from a standard location. I'd assume a standard location would be the GAC.
If I reference the assembly from the GAC, I get this, but this looks like it could break if a new version of the assembly is installed:
RequiredAssemblies = #(
'C:\WINDOWS\assembly\gac_msil\Microsoft.SqlServer.Smo\12.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Smo.dll'
)
I could also reference the assembly directly from the SDK:
RequiredAssemblies = #(
'C:\Program Files\Microsoft SQL Server\120\SDK\Assemblies\Microsoft.SqlServer.Smo.dll'
)
Another option I've considered (but possibly might be breaking a EULA somewhere) is to copy the assemblies to a server share and reference that like so:
RequiredAssemblies = #(
'\\MyServer\PowerShellDependencies\Microsoft.SqlServer.Smo.dll'
)
But how should I be doing this?
If it's relevant, all computers that this module will be installed on are 64-bit and will have the SMO libraries installed. Also, this isn't a publicly available piece of software, it's being deployed on a company network.
Update: I've tried only specifying the name of the assembly in the manifest and this appears to work.
RequiredAssemblies = #(
'Microsoft.SqlServer.Smo.dll'
)
Unless you're going to deploy the SMO assemblies as private assemblies, then I wouldn't recommend loading them from direct references.
To load from the GAC, use the Add-Type cmdlet with the fully qualified assembly name...
Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
Related:
How do I use Add-Type to load Microsoft.Web.Deployment?
You can also specify just the assembly without the version info:
Add-Type -AssemblyName "System.Xml.Linq"

32 bit Powershell: Use the ServerManager module?

I'm working with a 32 bit application that can run powershell snippets. I need to load the ServerManager module, which I would normally do with:
Import-Module ServerManager
But I get this error:
The specified module 'ServerManager' was not loaded because no valid module file was found in any module directory.
I assume, this is because the ServerManager module does not exist in the 64 bit modules directory, so I have tried the following:
Import-Module "C:\Windows\sysnative\WindowsPowerShell\v1.0\Modules\ServerManager"
But now I get the error:
Import-Module : Cannot load Windows PowerShell snap-in C:\Windows\assembly\GAC_MSIL\Microsoft.Windows.ServerManager.PowerSh
ell\6.1.0.0__31bf3856ad364e35\Microsoft.Windows.ServerManager.PowerShell.dll because of the following error: Unable to load
one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Loader Exceptions:
Could not load file or assembly 'Microsoft.Windows.ServerManager, Version=6.1.0.0, Culture=neutral, PublicKeyToken=31bf3856
ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'Microsoft.Windows.ServerManager, Version=6.1.0.0, Culture=neutral, PublicKeyToken=31bf3856
ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'Microsoft.Windows.ServerManager, Version=6.1.0.0, Culture=neutral, PublicKeyToken=31bf3856
ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'Microsoft.Windows.ServerManager, Version=6.1.0.0, Culture=neutral, PublicKeyToken=31bf3856
ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'Microsoft.Windows.ServerManager, Version=6.1.0.0, Culture=neutral, PublicKeyToken=31bf3856
ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'Microsoft.Windows.ServerManager, Version=6.1.0.0, Culture=neutral, PublicKeyToken=31bf3856
ad364e35' or one of its dependencies. The system cannot find the file specified.
At line:1 char:14
Any suggestions on how I could use the ServerManager module from within 32 bit powershell? Or another suggestion on how I could install the 'Desktop Experience' feature on Server 2008 R2 (without using the UI)?
Your only real choice here is to spawn a 64 bit instance of powershell.exe to process your server manager commands. Because the parent process is 32 bit, you'll have to use the same %windir%\sysnative trick to launch powershell.exe.
%windir%\sysnative\windowspowershell\v1.0\powershell.exe
-command '& { ipmo servermanager; add-windowsfeature foo }'
(wrapped for clarity)

How can I load WPFToolkit assembly in Powershell

I have installed WPF Toolkit:
Location: C:\Program Files\WPF Toolkit\v3.5.40320.1\WPFToolkit.dll
Name: WPFToolkit, Version=3.5.40128.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35
Type: Library
I can load it by full path:
[System.Reflection.Assembly]::LoadFrom("C:\Program Files\WPF Toolkit\v3.5.40320.1\WPFToolkit.dll")
But can't load by assembly name:
[System.Reflection.Assembly]::LoadWithPartialName("WPFToolkit, Version=3.5.40128.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
[System.Reflection.Assembly]::Load("WPFToolkit, Version=3.5.40128.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
What is a solution?
Loading by assembly name doesn't work because the WPFToolkit assembly is neither in GAC nor in the PowerShell directory. There are several options:
load it by path
add it to the GAC
change powershell.exe.config to look
in the WPF Toolkit directory
handle the AppDomain.AssemblyResolve
event (not particulary easy in
PowerShell V1)