dotnet pack - defining output folders - asp.net-core

I'm using dotnet pack to create a nuget package for a .net core project but have 2 issues:
The .nupkg file when inspected always has a lib folder which contains the contents of the project's bin folder. Can you prevent the lib folder from being created in the output?
In the project.json packOptions how do you map an include folder to an output directory? I've tried using the mappings property as detailed below to output the contents of the publish directory into the wwwroot directory below but no luck so far.
"packOptions": {
"files": {
"include": [
"publish"
],
"mappings": {
"wwwroot": "publish"
}
}
},

I was unable to remove the lib folder but the mapping works with slight modifications to the example above as displayed below:
"packOptions": {
"files": {
"include": [
"publish"
],
"mappings": {
"wwwroot/": "publish/"
}
}
}

Related

Nunit.framework dll from the package doesnt get copied to bin folder

I'm currently running .net framework 4.6 and added the NUnit, NUnit3TestAdapter nuget package to the test project that i am working on and I see that the test's are not being discovered.
Upon some investigtion, I see that nunit.framework.dll is not being copied to the bin folder. Did some more research and I see the following in the assets.json file, and Nunit's build/NUnit.props file which is supposed to have the MSBuild settings is empty and does not have any.
"NUnit/3.13.3": {
"type": "package",
"compile": {
"lib/net45/nunit.framework.dll": {}
},
"runtime": {
"lib/net45/nunit.framework.dll": {}
},
"build": {
"build/NUnit.props": {}
}
},
However Nbuild/Unit3TestAdapter.props file which is installed from the official Nunit has the MSBuild settings. Is there a reason why build/NUnit.props does not have the build setting where as build/Unit3TestAdapter.props has these?
Also, Is there a work-around to get these copied to local or should I add the reference's manually than getting these from the package?

Libman copying folders from subdirectories but not root directory while using filesystem provider

I am using libman For managing client side libraries, and using the fileSystem provider.
Facing this issue:
{
"library": "Content/.",
"destination": "wwwroot/lib/animate/",
"files": [ "animate.css" ]
},
{
"library": "../../fonts/font-awesome/css",
"destination": "wwwroot/lib/font-awesome/",
"files": [ "font-awesome.css" ]
},
The font-awesome library is copied to the destination while the animate is not copied to the destination. The source libs exist.
I also tried Content instead of Content/. but this isn't working too. This issue is causing the build to fail as the BundlerMinifier.BundlerBuildTask is failing.
This is happening with other libs too like:
{
"library": "../../Scripts/.",
"destination": "wwwroot/lib/modernizr/",
"files": [ "modernizr-2.6.2.js" ]
}
What is the appropriate solution for this?

How to setup tern-vim in my project?

I want to setup tern-vim in my project and followed this link https://github.com/ternjs/tern_for_vim/blob/master/doc/tern.txt.
After installation, I created a .tern-project file under the root directory of my project as below.
{
"libs": [
"browser",
"jquery"
],
"loadEagerly": [
"importantfile.js"
],
"plugins": {
"requirejs": {
"baseURL": "./",
"paths": {}
}
}
}
When I open a js file by vim command, I can't use any tern command as below:
|:TernDoc|...................... Look up Documentation
|:TernDocBrowse|................ Browse the Documentation
|:TernType|..................... Perform a type look up
|:TernDef|...................... Look up definition
|:TernDefPreview|............... Look up definition in preview
|:TernDefSplit|................. Look up definition in new split
|:TernDefTab|................... Look up definition in new tab
|:TernRefs|..................... Look up references
|:TernRename|................... Rename identifier
I will get Not an editor command error. Do I need to do any other configuration?

.NET Core project.json copyToOutput file in parent directory

I need to include a file in my build output. This file is two folders above project.json. If I specify this using "../../", the config file is copied over, but to the wrong location. Instead of being copied over to /bin/Debug/net461, it's placed two folders higher in /bin.
How do I get a file in a parent directory to be copied over to actual output folder where the dlls are placed?
"buildOptions": {
"copyToOutput": { "includeFiles": [ "../../config.json" ] }
},
Try use this instead, it's tested way and working very well:
"buildOptions": {
"copyToOutput": "project.json",
"emitEntryPoint": true,
"preserveCompilationContext": true
}
You can use copy single file "copyToOutput":"name" or array "copyToOutput":["name1","name2"]. To copy directory need to use "copyToOutput":"name\\"

Exclude non-minified files from publish in `project.json` with ASP.NET Core

I am trying to find a proper configuration for the publishOptions inside project.json (ASP.NET Core 1.0 / Full Framework) so that non-minified files are not published.
Official documetation doesn't help much: project.json reference.
Searching for globbing patterns, and finding some artilcles with gulp examples, I came up with this wwwroot/js/**/*!(*.min.js), but it doesn't seem to work.
Is my syntax wrong? Or, it's just that project.json and dotnet publish don't support this syntax?
"publishOptions": {
"include": [
"wwwroot",
"Views",
"Areas/**/Views",
"appsettings.json",
"web.config"
],
"exclude": [
"wwwroot/lib",
"wwwroot/js/**/*!(*.min.js)",
"wwwroot/css/*.less",
"wwwroot/_references.js"
],
"includeFiles": [],
"excludeFiles": []
},
The typical workflow for JavaScript files/libraries management is to use gulp or grunt tasks to copy over the necessary files into the wwwroot folder which may happen on certain events (prebuild, postbuild, project open, clean).
In the latest tooling, the default MVC doesn't include gulpfile.js anymore as the most common usage was to minify and bundle js files, even when no external libraries were used so gulp may be a bit overwhelming for new users.
But it can easily be brought back, when you right-click the bundleconfig.json file in the solution explorer and choose "Bundler & Minifier" > "Convert to Gulp".
This creates a gulpfile.js and package.json (nodejs dependencies) in the root of your project and adds npm folder to the "Dependencies" section of Solution Explorer. When you watch in the Windows Explorer, you'll see a node_modules folder in the project root folder. That's where npm will download all packages and it's dependencies.
The generated gulpfile.js looks like this and has a few predefined tasks. i won't use this file as example, as it is strongly based on the bundleconfig.json and it's structure and use my gulpfile.json which used to be shipped with older templates.
"use strict";
var gulp = require("gulp"),
rimraf = require("rimraf"),
concat = require("gulp-concat"),
cssmin = require("gulp-cssmin"),
uglify = require("gulp-uglify");
var webroot = "./wwwroot/";
var paths = {
app: webroot + "app/",
libs: webroot + "lib/",
js: webroot + "js/**/*.js",
minJs: webroot + "js/**/*.min.js",
css: webroot + "css/**/*.css",
minCss: webroot + "css/**/*.min.css",
concatJsDest: webroot + "js/app.min.js",
concatCssDest: webroot + "css/app.min.css"
};
gulp.task("clean:js", function (cb) {
rimraf(paths.concatJsDest, cb);
});
gulp.task("clean:libs", function (cb) {
rimraf(paths.libs, cb);
});
gulp.task("clean:css", function (cb) {
rimraf(paths.concatCssDest, cb);
});
gulp.task("clean", ["clean:js", "clean:css", "clean:libs"]);
gulp.task("min:js", function () {
return gulp.src([paths.js, "!" + paths.minJs], { base: "." })
.pipe(concat(paths.concatJsDest))
.pipe(uglify())
.pipe(gulp.dest("."));
});
gulp.task("min:css", function () {
return gulp.src([paths.css, "!" + paths.minCss])
.pipe(concat(paths.concatCssDest))
.pipe(cssmin())
.pipe(gulp.dest("."));
});
gulp.task("min", ["min:js", "min:css"]);
gulp.task("libs", function (cb) {
gulp.src([
'bootstrap/**/*.js',
'bootstrap/**/*.css',
'jquery/**/*.js`, // we can also limit this to `jquery/dist/**/*.js to only include distribution files
'jquery/**/*.css'
], {
cwd: "node_modules/**"
})
.pipe(gulp.dest(paths.libs));
});
gulp.task("app", function (cb) {
gulp.src([
'app/**.js'
])
.pipe(gulp.dest(paths.app));
});
gulp.task("default", ['clean', 'libs']);
It looks more complicated than it actually is. There are several minizier tasks (min:js, min:css) and one general minifier task min which just runs all others in sequence.
A clean task which deletes the output file(s) from wwwroot. When converting from the template, it deletes only the default wwwroot/js/site.min.js file.
Since there are no javascript libraries used in the default template, except of what's inside the wwwroot/lib folder already the packages are not handled that way.
So first thing you may want is to grab bootstrap and jquery from npm rather than the static versions provided by the template. So we add the dependencies to the package.json.
{
"name": "app",
"version": "0.0.0",
"private": true,
"dependencies": {
"bootstrap": "3.3.6",
"jquery": "2.2.0"
},
"devDependencies": {
"gulp": "3.8.11",
"gulp-concat": "2.5.2",
"gulp-cssmin": "0.1.7",
"gulp-uglify": "1.2.0",
"rimraf": "2.2.8"
}
}
The libs task from the gulpfile.js above for example will copy over all required files of a package to wwwroot. I said required, because in the packages there are often unbundled files for debugging and stuff, which we usually don't want inside wwwroot (they can grow quite big).
gulp.task("libs", function (cb) {
gulp.src([
'bootstrap/**/*.js',
'bootstrap/**/*.css'
], {
cwd: "node_modules/**"
})
.pipe(gulp.dest(paths.libs));
});
It will look for all *.js and *.css files within the bootstrap folder in node_modules folder and copy them over to path.libs which is configured as wwwroot/lib/.
The app task does the same for our own code. clean clears the folders and (i.e. before switching from debug to release build or before publishing).
Finally you can bind the tasks to certain VS Events. You need to open the "Task Runner Explorer" View (View > Other Window > Task Runner Explorer). There you can choose a task and right-click it, then "Binding" and choose one of the binding (Before Build, After Build, Clean, Projct Open). They are pretty self explaining, "Clean" means when you do "Build > Clean Solution".
Now to the publishing part. You can run certain command, when you publish your application (either via dotnet or Visual Studio).
In the project.json there is a scripts section for this.
"scripts": {
"prepublish": [ "npm install", "bower install", "gulp clean", "gulp min", "gulp libs" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
Each of the entries "prepublish" is one command to be executed. In this example, before the publishing begins, npm install will be executed first in order to restore all npm dependencies. Then bower install to install the dependencies managed by bower (remove it if you don't use bower and do all via npm).
The next three commands are the interesting ones, they will execute gulp tasks. We can also simplify this by adding a "publish" task.
gulp.task("publish", ['clean', 'libs', 'min']);
"scripts": {
"prepublish": [ "npm install", "bower install", "gulp publish" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
This will copy all the necessary files for publishment into the wwwroot folder, publish the files and then call the "postpublish" scripts.
That's a rough introduction in gulp. It's has a learning curve, but once you get it working it imrpoves the overall workflow.
What's not covered here is to add a watch task which may look into a certain folder (I usually use app folder in the project root) and when any file changes there run the app task, so the our code gets minifed and copied over to wwwroot and is available when we debug it.
A simple alternative is:
Rename unminified source to *.debug.js and *.debug.css
They are now easier to exclude:
"publishOptions": {
"include": [
"wwwroot"
],
"exclude": [
"wwwroot/**/*.debug.*"
]
}
IMHO unminified source should stick out and look abnormal. It is more deserving of a file name wart than minified code.
This also brings the build output files into line with the rest of the .Net build configuration. It makes it clear that your debug versions may well include extra logging and debug utilities and that its not for production.