Embedding Project with Typescript into another project - visual-studio

I have a Visual Studio project that uses Typescript. This projects gets compiled into a dll and then referenced in the main project. This works fine for all normally compiled files, but I am hitting an issue when it comes to transpiled javascript files.
The sub project has the following in the .csproj file
<Target Name="AfterClean">
<ItemGroup>
<EmbeddedResource Include="**\*.html;**\*.cshtml;**\*.css;**\*.js;**\*.map;**\*.jpg;**\*.png" />
</ItemGroup>
</Target>
This will include the .js files in the project when a Rebuild Solution is run, but it will not include the .js files when a normal build or a "Run" from Visual studio is used.
I believe the issue is to do with timings, I want the embedding to occur after the typescript transpile has happened, but before the dll is included in the main project.
I have tried the following options "BeforeBuild", "AfterBuild", "BeforeResolveReference", "AfterResolveReferences", "BeforeResGen" and "AfterResGen". - Found from a msdn article here
Ideally I would like to add a DependsOnTargets=TypeScript compile to my embedding task so it forced the embed to happen after the transpile, but the typescript compile does not appear to be a target as it just appears like this in the .csproj file, so I don't believe this is possible
<ItemGroup>
<TypeScriptCompile Include="app\app.module.ts" />
...
</ItemGroup>
Any ideas would be greatly appreciated
(I am using Visual Studio 2015 Update 3 and Typescript 1.8)
EDIT: The build server does not have tsc on the PATH so I am unable to call tsc from a prebuild event

I have tried to do similar thing, the solution works for me is use TypeScript command line to compile TypeScript in the pre-build event.
<PropertyGroup>
<PreBuildEvent>
tsc $(ProjectDir)\Scripts\references.d.ts
// or compile tsconfig.json if you use TypeScript 1.8
// tsc --project $(ProjectDir)\Scripts\
</PreBuildEvent>
</PropertyGroup>
Then add following target element for BeforeBuild:
<Target Name="BeforeBuild" DependsOnTargets="PreBuildEvent">
<ItemGroup>
<EmbeddedResource Include="**\*.js" />
</ItemGroup>
</Target>
You can find more information about tsconfig.json here.

Related

How should I include Package References in a VSIX project

My solution creates a Visual Studio Package from multiple projects, using multiple NuGet packages.
All of the Nuget packages are specified in the project files using PackageReference (rather than the older packages.config file). I am using Visual Studio 2019.
I have had a problem, that the DLLs referenced by NuGet Packages are not included in the VSIX installation.
There is a solution to this problem, described in this article by Daniel Cazzulino, by adding the following code to the project file:
<PropertyGroup>
<GetVsixSourceItemsDependsOn>$(GetVsixSourceItemsDependsOn);IncludeNuGetResolvedAssets</GetVsixSourceItemsDependsOn>
</PropertyGroup>
<Target Name="IncludeNuGetResolvedAssets" DependsOnTargets="ResolveNuGetPackageAssets">
<ItemGroup>
<VSIXCopyLocalReferenceSourceItem Include="#(ReferenceCopyLocalPaths)" />
</ItemGroup>
</Target>
This does work, but it blows up the size of the installation from about 20MB to about 40MB.
The installation now includes a lot of PDB files, which I don't really need.
More significantly, it brings in about 46MB of Visual Studio DLLs which are not necessary, because they are part of Visual Studio.
Is there a better way to ensure that the referenced NuGet packages are included in the VSIX, without inflating the installation with these other files?
You can use a simple script like this:
<Target Name="IncludeNuGetPackageReferences" AfterTargets="GetVsixSourceItems">
<ItemGroup>
<VSIXSourceItem Include="#(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.NuGetPackageId)' == 'Newtonsoft.Json'" />
<VSIXSourceItem Include="#(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.NuGetPackageId)' == 'xxx'" />
... </ItemGroup>
</Target>
You can specify what assemblies should be included into .vsix . And it won't copy the unnecessary VS assemblies after tests in my machine. Hint from smourier, thanks to him.
Hope it helps:)

ASP.NET Core (NET Framework) Teamcity build fails, VS builds properly

I have an ASP.NET Core project that builds properly with VS but fails with TeamCity.
It is a project that compiles to a library, but TeamCity tries to build it as an executable, and complains about the lack of 'main':
CSC error CS5001: Program does not contain a static 'Main' method suitable for an entry point
The content of the .csproj file are as follow:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
<OutputTypeEx>library</OutputTypeEx>
<StartupObject />
<AssemblyName>Test</AssemblyName>
<RootNamespace>Test</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup>
</Project>
Visual studio has no problem building the dll file.
To reproduce:
Create an ASP.NET Core (.NET Framework) project
Change the output type to library
Remove the program.cs / startup.cs files
Compile with Visual Studio to confirm a library is being built
Build with Team City and an error will appear
To avoid that error, Please look into this SO post or this
You should be using the dotnet core plugin or you can easily
configure dotnet build command(if dotnet is present in your build
servers).
Or you can refer the MusicStore build.cmd file for reference. This basically downloads and installs the dotnet and all the dependencies and then builds the project.
Hope it helps!
I found a workaround; in the project file, VS puts this:
<OutputTypeEx>library</OutputTypeEx>
I need to add one line:
<OutputType>Library</OutputType>
<OutputTypeEx>library</OutputTypeEx>
So it looks like the build with TeamCity is not handling the OutputTypeEx propery but it handles the OutputType one.
I still see this as a bug, but at least there is a workaround.

TypeScript files compiling on save but not on build

Problem
I've found that my TypeScript files only compile when I save them. However, when I build or rebuild the project, they do not compile. I'm using TypeScript 0.9.1.1.
When I check the build output of a new TypeScript project, it includes the following entry:
CompileTypeScript:
C:\Program Files\Microsoft SDKs\TypeScript\tsc.exe --module AMD --sourcemap --target ES3 "app.ts"
But my project doesn't produce this in its build output.
Setup
Here are the relevant parts of the project file:
<ItemGroup>
<TypeScriptCompile Include="Scripts\app\example.ts" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptIncludeComments>true</TypeScriptIncludeComments>
<TypeScriptSourceMap>true</TypeScriptSourceMap>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptIncludeComments>false</TypeScriptIncludeComments>
<TypeScriptSourceMap>false</TypeScriptSourceMap>
</PropertyGroup>
<Import Project="$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
What I've checked
The files compile on build in new TypeScript projects, so it should be possible.
The targets file exists in the correct location
The project file configuration shown above appears to be correct when compared with a new TypeScript project
Compiling a new TypeScript project, which works fine
Cannot compile TypeScript files in Visual Studio 2012, but the symptoms of the problem are different to mine, and it's not clear if the author was using the same setup
Typescript will not properly compile in VS2012, but the symptoms of the problem are different to mine
The build action for each TypeScript file is set to TypeScriptCompile as shown above
After comparing the project file with a new TypeScript project file, I narrowed the problem down to the following:
The TypeScript targets file must be imported after the C# targets file.
In the code in the question, fix the problem by moving the following line up:
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

How do I use an MSBuild file from Visual Studio 2012?

I have a simple MSBuild file that I'm learning with.
Here it is:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Clean" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>{D5A16164-962E-4A6D-9382-240F31AB6C50}</ProjectGuid>
</PropertyGroup>
<Target Name="Clean">
<ItemGroup>
<BinFiles Include="bin\*.*" />
<fff Include="f\*.*" />
</ItemGroup>
<Delete Files="#(BinFiles)" />
<Delete Files="#(fff)" />
</Target>
</Project>
Now I want to include this in a Visual Studio solution and be able to run the "clean" target from Visual Studio 2012. I tried naming it testproject.msbuildproj like the internet seems to suggest "works", but it doesn't work. When I run the clean command I just get "unexpected error".
If I rename the project to testproject.csproj, it does some unintuitive things like creating compilation directories, but it does actually run my clean command properly. However, this is undesireable because it creates obj and bin/x86/debug type directories. It also looks goofy in Visual Studio because it still gives the References drop down.
How can I use just a plain vanilla MSBuild project from Visual Studio without random errors or false assumptions?
Note I only am having a problem with this from Visual Studio. Using msbuild from the command line it works perfectly
Visual Studio creates bin / obj folders when it opens csproj file. When you click Build / Rebuild / Clean it just uses appropriate targets from the project file.
You cannot stop VS from creating these folders, but you can ask it to create them in say temp folder by setting appropriate properties - refer this MSDN article for details.
So the steps are to rename your project to csproj, and add the following lines into project:
<PropertyGroup>
<OutputPath>$(Temp)\bin</OutputPath>
<IntermediateOutputPath>$(Temp)\obj</IntermediateOutputPath>
</PropertyGroup>
I usually use a bit different approach to work with MSBUILD files from VS:
I use regular csproj file with removed Import ... CSharp.targets part as pure container for my Build projects.
I add actual build files with targets and logic, and all properties, necessary artifacts like XSLT etc using "Include into project", so I can manage hierarchy and change any file from within VS.Net.
I redefine Build / Rebuild targets in csproj file for whatever I need, for example Build may contain minimum output, and while rebuild diagnostic one.
Like this:
<Target Name="Build">
<Exec Command="%windir%\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe Builds\build.proj /t:Build /v:m" />
</Target>
<Target Name="Rebuild">
<Exec Command="%windir%\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe Builds\build.proj /t:Build /v:d" />
</Target>

Visual Studio setup project prebuild event

I have a Visual Studio Setup project where some of the deployed files are created by a pre-build event. However, when I build the project Visual Studio first does pre-build validation and then runs the pre-build event. Thus pre-build validation fails, with the error message "ERROR: Unable to find file ...".
Does anyone know a work-around for this?
(The details may not matter, but it is a Windows Installer for a Python app. The pre-build event calls PyInstaller which packages the py files as a single exe file. This exe file and some DLL's and resource files are then packaged by the Setup project as a Windows Installer.)
You must be able to use MSBuild Targets instead of a Prebuild Event. I am not sure about the specifics, but I guess the following link might explain your similar situation.
Edited - July 2017 (due to relocated link):
http://pradeepc.net/using-tfs-teambuild-to-build-setup-projects-in-visual-studio
Sample copied from that link is pasted below - you may want to edit to fit the need:
<Target Name="AfterDropBuild">
<Exec Command="devenv.exe MySolution.sln /Build &quot;Release|Any CPU&quot;" WorkingDirectory="$(SolutionRoot)" />
<ItemGroup>
<SetupFiles Include="$(SolutionRoot)/MySetup/Release/MySetup.msi" />
<SetupFiles Include="$(SolutionRoot)/MySetup/Release/Setup.exe" />
</ItemGroup>
<Copy SourceFiles="#(SetupFiles)" DestinationFolder="\Build-MachineBuild_Drop_FoldersMyProjectMSI$(BuildNumber)" />
<Copy SourceFiles="#(SetupFiles)" DestinationFolder="\Build-MachineBuild_Drop_FoldersMyProjectMSILatest_MSI" />
</Target>

Resources