How do I reference instead of copy js files from a Nuget package at build time in TeamCity?

前端 未结 3 1897
迷失自我
迷失自我 2021-02-09 17:39

I\'ve got a packages.config file checked into source control. This specifies the exact version of the Nuget dependency I want. We have our own NuGet repository. We are creating

相关标签:
3条回答
  • 2021-02-09 18:04

    If you are the owner of the package then you could use the nuget package i've created to be able to have a folder called "Linked" in the package and have a simple Install.ps1 and Uninstall.ps1 (one liners) to add every file in the nuget package's linked folder as existing to the project.

    https://github.com/baseclass/Contrib.Nuget#baseclasscontribnugetlinked

    I didn't try out how publication treats linked files, the problem is debugging the Project, as the JavaScript files will be missing in the directories.

    If you are using git as source control you could try my nuget package which ignores all the nuget content files and automatically restores them before building.

    Step by step example in my blog: http://www.baseclass.ch/blog/Lists/Beitraege/Post.aspx?ID=9&mobile=0

    0 讨论(0)
  • 2021-02-09 18:28

    The underlying problem here is Visual Studio's relatively poor support of JavaScript projects and JavaScript's lack of built-in module loader.

    For C#, when you install a package it adds a reference in your .csproj file to the assembly on disk. When you build, MSBuild knows to copy the thing referenced to the bin directory. Since you aren't checking in your bin directory, this all works great.

    Unfortunately for JavaScript, the build system isn't nearly as matured and there aren't well defined guidelines for NuGet to follow. Ideally (IMO), Visual Studio would not run web sites directly from your source directory. Instead, when you built it would copy the JavaScript files, CSS and HTML files to a bin directory from which they would be executed. When debugging, it would map those back to the original JavaScript or TypeScript files (so if you make a change it isn't to a transient file). If that were to happen then there is now a well-defined build step and presumably a well-defined tag for JavaScript files (rather than just "content"). This means that NuGet would be able to leverage that well-defined MSBuild tag and package authors could leverage the NuGet feature to do the right thing.

    Unfortunately, none of the above is true. JavaScript files are run in-place, If you did copy them to bin on build Visual Studio would do the wrong thing and editing from a debugger would edit the transient files (not the originals). NuGet therefore has no well-defined place to put files so it leaves the decision up to the package author. Package authors know that the average user is just going to be running directly from source (no build-step) so they dump files into the source folder where they must be checked in to version control.

    The entire system is very archaic if you are coming from a modern ecosystem like C# where someone took time to think these things through a bit.

    What you could do is create an MSBuild task that, before build, would go through all of your packages, look for content, and copy that content to the desired location. This wouldn't be terribly difficult, though would take a bit of work.

    Also, package authors could include a build-task that does this in their package so that before-build all of their content was copied local. Unfortunately, if only some package authors do this then you end up with weird fragmentation where some packages need to be committed to version control and others do not.

    0 讨论(0)
  • 2021-02-09 18:29

    When a package is installed into a project, NuGet in fact performs these operations,

    1. Download the package file from source;
    2. Install the package into the so called packages folder, which is $(SolutionDir)\packages by default;
    3. Install the package into the project, which consists of adding references to DLLs, copying content files into the project directory etc.

    When a package is restored, only the first two steps are executed. Projects will not be touched by nuget package restore. Which is why the js files in your project will not be "restored".

    The only solution for now is to check in the js files in your project.

    0 讨论(0)
提交回复
热议问题