I'm building a NuGet package that delivers some T4 templates into the CodeTemplates directory. When I install the NuGet package, the T4 templates all have the Custom Tool property set to "TextTemplatingFileGenerator". This isn't right.
I know I can disable this by altering my registry so that new T4 templates aren't added this way, but since this is a NuGet package, that is not an option.
I've looked into PowerShell, but I'm having trouble understanding what I would do to achieve my goal.
I've looked at the .csproj file xml and found this:
<None Include="CodeTemplates\AddController\Controller.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Controller.cs</LastGenOutput>
</None>
If I remove "TextTemplatingFileGenerator" from this node, then the file will work as I desire.
Where should I go from here?
I'm not sure there's a nice way to do this. In a blog post, David Ebbo wrote:
One last thing I’ll mention about this
model is that the .tt file is normally
not part of your project. Instead, it
lives somewhere else, and only its
output becomes part of your project.
Well, technically, the .tt file can be
in your project for easy editing, but
you then have to remove the
‘TextTemplatingFileGenerator’ custom
tool, because you really don’t want it
to execute on its own (it would surely
fail with the custom host).
This makes it sound like this is Visual Studio behavior when a .tt file is added to the project.
That said, Scott Hanselman's AddMvc3ToWebForms makes some changes to a GUID in the csproj file to add MVC functionality (Add Controller / Add View, etc.), so it's possible you could do something similar to his code and remove the Generator section for files in your package and reload the project?
I had the same problem and I solved it using "install.ps1" which executes everytime the nuget package is installed or updated.
Your install.ps1 should look like:
param($installPath, $toolsPath, $package, $project)
$addControllerFolder = $project.ProjectItems.Item("CodeTemplates").ProjectItems.Item("AddController")
$addControllerFolder.ProjectItems.Item("Controller.tt").Properties.Item("CustomTool").Value = ""
Related
I need to use a NuGet package containing a utility for my project. It contains several binaries (EXEs and DLLs).
I've added it to my project successfully but I suspect the nupkg isn't formed correctly because I cannot use any of its DLLs or EXEs in my project without manually pointing to the package in my local NuGet cache. When compiling, none of its resources are added to the output (I assume this is because nothing is referenced in my code).
I'd like to create a wrapper project to call the binaries but I'd also like other project devs to be able to compile the solution without adjusting directory variables. Ideally, I could configure the csproj to pull in the bits directly from the local package cache. I think this would be possible by setting the Generate Path Property value to Yes in Visual Studio, but the variable cannot be found when I attempt to use an <Include/> statement in the csproj file.
Is what I'm asking possible? Namely, reference the NuGet package bits within my csproj to ensure the binaries are dropped in the compilation output? Can I do this with the Path Property, or is there something else I can do without directly committing the package's binaries into my project?
(I realize I need to work with the developer to fix whatever issue they have with their package, but I have no direct influence at the moment so this is the best I can do at the moment).
I figured this out, mostly due to misunderstanding how some of the different tags and attributes are meant to be used.
To achieve the desired effect, I did the following:
<ItemGroup>
<Content Include="$(Pkg{PackageId})\**">
<Link>{NameOfSolutionDirectory}\%(RecursiveDir)%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
Where {PackageId} is the name of the NuGet package (this step requires setting 'Generate Path Property' to 'Yes' in the package properties via Solution Explorer), and {NameOfSolutionDirectory} is the name of a folder within the solution I'd like to use for containing those bits, if you're as concerned about keeping the project as organized as I am. The {} should be excluded when replacing these values.
If you want to scope to a specific directory within the package contents, do it within the Include attribute. The ** is necessary if you want to include all files within that directory, or else you can scope by extension or whatever additional pattern you'd like.
I wrote a class library in C# that I need to push to a private NuGet server (v3.4.1.0). I decorated my classes and methods with XML documentation comments.
XML documentation file option is checked on the Build tab of the project properties panel, the project builds successfully and the xml file gets generated on the project's root folder with the same name as the assembly.
In the .csproj file the related section looks like this:
<PropertyGroup>
<DocumentationFile>absolutePathTo\assemblyName.xml</DocumentationFile>
</PropertyGroup>
IntelliSense (VS2019 16.9.0) recognises the documentation and shows it properly even in other projects under the same solution.
When I generate the NuGet package it gets created in the project's bin\Debug folder. If I open it as a zip archive the DLL and the documentation XML can be found in the lib\netstandard2.1 folder having matching names.
Once I install this package to another project from the private NuGet server it works properly but loses the complete documentation. IntelliSense does not show my comments anymore and the assembly metadata seems not to have it either.
Could anyone support me on this one?
That is normal. For xml document, it is special under new-sdk style projects. The xml document could only be copied into the non-sdk net framework projects but new-sdk net core projects cannot. More similar to this issue I handled before.
So you should try these steps additionally to get what you want:
1) enter these node under csproj file of your nuget project.
<ItemGroup>
<None Include="xxx\absolutePathTo\assemblyName.xml"(the path of the xml file under your project folder) Pack="true">
<PackageCopyToOutput>true</PackageCopyToOutput>
</None>
</ItemGroup>
2) after that, re-pack your nuget project and before you install the new version, please clean nuget caches first or just delete all files under C:\Users\xxx\.nuget\packages
Personally, I let Visual Studio handle things for me.
If you right-click in the project, and choose Properties,
In the Build -> Output area, you should see a checkbox under Documentation file labelled Generate a file containing API documentation..
When you check this, a new option appears underneath: XML documentation file path. But the file selector is labelled Optional path for the API documentation file. Leave blank to use the default location..
FYI: The default location is alongside your EXE / DLL that is generated when you build your project.
When you next build your code (for anyone else reading, I assume you've got the Generate NuGet package on build in the Package area checked too) it will also package up the XML documentation into the NuGet package generated.
From the perspective of users of this new package, Visual Studio will pick up on the XML Documentation inside.
I have a solution with an application project (ASP.NET Core) and multiple library projects. I want to separate some of the library projects into a separate solution and turn them into NuGet packages.
With the libraries in the same solution I could of course simply edit something in a library, run the application and see how it works (and debug, if necessary).
However, when I turn the libraries into a NuGet package, the application references the packages from our private NuGet feed instead of the project file.
My question is: is it possible to locally "override" the package reference and use the local source code instead? That way I could still edit the libraries and see the effects in the application. This is a lot easier than having to publish a new package for every small change (especially when trying to fix an issue or implementing a new feature).
DNT (Dot Net Tools) does this. You can specify which packages to switch and where they are.
See the 'switch-to-packages' and 'switch-to-projects' command line switches.
Its a bit fiddley as (when I last tried) you had to create a config file that holds the mapping, and it seems to be easy to break the switching. But its something.
https://github.com/RicoSuter/DNT
I've not tried it, but maybe you can use it to switch to packages on a commit for the build server to work correctly? (Or to ensure the references are correct in source control?)
If you want to use nuget in your project and debug, even modify the source files of the nuget packages, this is not a good choice because you should build the nuget project(generate the new changed dll) and repack it as a nuget package, then reinstall, to enable the changes. It is too complex.
Once you install the nuget, no matter how many changes you make, it’s useless. The nuget installed at this time is the version you made before any changes. No matter how you change it, it is the previous version. The version stays at that timestamp, unless you repackage the project. Generate nupkg and update the nuget version.
So nuget is not a good choice for your situation, you should use ProjectReference.
Directly use the ProjectReference to reference two source projects, build at the same time, and get the changed parts at the same time.
ProjectReference could cross two different solutions.
Add this on the main project:
<ItemGroup>
<!--add any nuget project'csproj file like this to debug its source code-->
<ProjectReference Include="..\xxx\xxx.csproj">
</ProjectReference>
</ItemGroup>
If the proejct is out of the solution, you could directly use the full path of the nuget project's csproj to connect it.
I'm not sure what you mean by "override" but you can always add the library project to your ASP.NET Core solution and reference it like normal project references. A project referenced within a solution doesn't have to be physically placed in the same folder as the solution itself.
This, however, does require that any developer on the project has both GIT repositories cloned locally (given your two solutions are located in separate GIT repos) in order to be able to build the ASP.NET Core solution. But I don't really see that as a downside.
If there is a code library in a single file, is it possible for Visual Studio to "include" it remotely?
Consider a library I wrote -- Nemmet. Basically, the entire thing exists in a single file, by design. It's a very limited library, with barely any dependencies.
For people to include the source (even myself, in other projects), they would have to do one of the following:
Download the repo and add the project to their solution
Create the file, and copy and paste Nemmet.cs into it
Yes, they could install a Nuget package, but then you get the compiled DLL in your project, not the source. Nuget is really about functionality, not source code. Additionally, it requires me as the library owner to create and maintain the package, which I'm really not that interested in doing.
What I'm looking for is a more "casual" way of including raw source code into a project.
What would be nice is if Visual Studio could do a "remote include" or something. I'd love to be able to bind a source file to a URL (the "raw" URL at Github) and have VS update the contents of the file every once in a while, or on-demand (right click > "Update from URL").
(Yes, this assumes you trust the source. Let's assume you do. You'd have the same issue with any included library.)
Is there anything like this available in some way? Should I just WGet it? Am I not thinking of a more obvious way to achieve the same end?
You can use Paket which is a dependency manager for .NET projects. Many of us in the F# community use it quite a lot but there is no reason it isn't equally applicable to the rest of the .NET world.
Basically, you create a paket.dependencies file for your project to list your dependenies. It supports:
Nuget dependencies:
nuget EntityFramework
git repositories
git https://github.com/fsprojects/Paket.git
Single files
http http://www.fssnip.net/raw/1M/test1.fs
Github dependencies
github forki/FsUnit FsUnit.fs
Here is the getting started guide.
This is not quite the same thing, but I found a way to "Add as Link" in a Visual Studio project. You can add a source code file without actually adding it to your project.
When adding an "existing item," dropped the "Add" button down to "Add as Link." It will add a placeholder to the file to your project, and compile it in, but leave the file where it is. This means you could have a central project on your file system, and use that code in all sorts of projects without having multiple copies of the file sitting around. Changing the file in your central project would change it in all the other projects.
Again, not the same thing, but still helpful.
I need to create csproj file that will be usable as project reference in VS2013 and will output prebuilt binary as it's "Build" result.
We use referenced projects for build, however company policy doesn't allow access to some of that projects for everyone. As a result projects need to be updated manually to make them build. This is really a major inconvenience when switching branches and when making edits to project files, so I want to create dummy project that will be bound to pre-built binaries as their "output" and will be placed instead of real projects.
EDIT: Moving that assembly to Nuget package is not an option for now since Nuget has some issues with dev flow (when you need to debug/test/develop package). I saw some VS extension that implements switching between Nuget package and local project which might solve this issue, but I'm not sure if it will be accepted and want to explore other options.
To be clear - the thing I want to avoid is editing project in any way, so that project can be built cleanly after pulling it from Git, and I don't have to clean it every time before commit.
I haven't properly tested it, but the solution seems really simple (if I understand the question properly).
Just add this to the existing .csproj, overriding the Build target to just give the path to the pre-built assembly.
<Target
Name="Build"
Returns="$(TargetPath)" />
This assumes the TargetPath property already defined, and it should automatically be if you're modifying the original .csproj. Otherwise just define it yourself in a <PropertyGroup> before the Build task.
Note that having TargetPath defined is important for the ProjectReferences in your own project to resolve.
How about having those restricted (binary only) projects reside in an internal Nuget package feed, so that Nuget can install the packages as needed, on build?