4

I have a Shared Project project which contains some content files like JSON, etc.

I have a number of ASP.Net core projects and I want to include/share some of these JSON files. Very simple with the "Link to File" option in VS but it's not available in ASP.Net core projects.

I've tried playing with the buildOptions/copyToOutput as follows but this does not seem to be working and no error or warning logged in the build process.

"copyToOutput": {
    "include": [ "../Common.Includes/mysettings*.json" ] }

Any ideas gratefully accepted?

Thanks

Donal

Ismail RBOUH
  • 10,292
  • 2
  • 24
  • 36
Donal McWeeney
  • 241
  • 2
  • 14

1 Answers1

0

The copyToOutput setting works fine for me (at least in dotnet 1.0.0-preview3-003171 on Windows). Note however that copyToOutput is not transitive - meaning tath if the files are copied in project A, they won't be copied to output in project B that references project A.

Another method is to use underlying file system and create a symbolic link to the shared file or folder. On Linux it would be:

> ln -s ../Common.Includes common

Windows has a similar (but less commonly used) feature:

> mklink /J common ..\Common.Includes

You can then configure dotnet to run it on a specific event:

"scripts": {
    "precompile": "cmd /C mklink /J common ..\\Common.Includes"
}

In RC1 there were also events like 'prepare' or 'prerestore', which would be more suitable for this, but for now they're gone and dotnet only supports 'precompile' and 'postcompile'.

It will be even better to put this command into a separate script along with some logic to check if the link already exists. On windows, I would create init.ps1 Powershell script:

if (!(Test-Path "common")) {
    cmd /C mklink /J "common" "..\Common.Includes"
}

Then reference it from "scripts":

"scripts": {
    "precompile": "init.ps1"
}
qbik
  • 5,502
  • 2
  • 27
  • 33
  • Hi qbik, Thanks for taking the time to put together such a consise answer. I have the Preview 2 Sdk and tools installed - my version is 1.0.0-preview2-003121. And I'm running from within VS2015. My source control system is VSTS/TFS and from what I've read I dont think symbolic links and TFS work perfect together. So that leaves some form of copy process via scripts. I might wait until v1 of the tools to see if it works in that and if not go the script approach. Thanks Donal – Donal McWeeney Jul 22 '16 at 13:43
  • @DonalMcWeeney: `copyToOutput` works also in `1.0.0-preview2-003121` (I checked). Note however that those files will be copied to *bin/Debug/netcoreapp1.0* directory (or something similar, depending on project settings), so you won't see them in VS. – qbik Jul 23 '16 at 06:53
  • As of the script approach: I recommend adding the symlink (*common* directory in the answer above) to ignored files in Version Control, so it won't get checked-in (in git it would be an entry in *.gitignore*, I'm not sure how to [ignore directory in TFS](http://stackoverflow.com/questions/922798/how-to-ignore-files-directories-in-tfs-for-avoiding-them-to-go-to-central-source)). This way you avoid duplication in VCS and it should still work on other developers' machines - the symlink will be automatically recreated on build. – qbik Jul 23 '16 at 06:55