How to include js files in a Visual Studio project on build - visual-studio-2013

In our project there are some front-end developers that use a different IDE from VS to write JS, CSS and HTML templates. However our build process does not include the files in their appropriate folders, but only the ones that are specified in the .csproj files.
Front-end developers should be able to edit their files in the separate IDEs and before they commit to source control they would build the VS Solution once. I want their files to be included in the .csproj file of the project on build.

You can include files to project by mask, so front-end developers can build solution with these files from command line.
For example:
<ItemGroup>
<Content Include="..\..\Client\*.css">
<Link>%(Filename)%(Extension)</Link>
<!-- <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> -->
</Content>
<Content Include="..\..\..\..\Bin\Client\Drivers\**\*.*">
<Link>Drivers\%(RecursiveDir)%(Filename)%(Extension)</Link>
<!-- <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> -->
</Content>
</ItemGroup>

Related

Visual Studio - Prevent Creation of <Compile> and <None> tags when moving files in CSPROJ

In my CSPROJ I have a subfolder ›None‹. It is setup like this:
<ItemGroup Label="None">
<Compile Remove="None/**/*.cs" />
<None Include="None/**/*.cs" />
</ItemGroup>
In there I have CS files, that I don't want to compile.
No, I want to move a file (lets say ‌›CompileIt.cs‹ from ›None‹ back to the project, because now I want to compile it again.
If I move it outside of Visual Studio (like directly in the explorer or via PowerShell) it does exactly what I want, the file is now in a different folder inside the project and the Build-Action changes from ›None‹ to ›C# Compiler‹.
But if I move the file in Visual Studio, something different happens. Visual Studio changes the CSPROJ file like this:
<ItemGroup Label="None">
<Compile Remove="None/**/*.cs" />
<None Include="None/**/*.cs" />
</ItemGroup>
<ItemGroup>
<None Include="CompileIt.cs" />
</ItemGroup>
Now I have to either delete the ItemGroup from the CSPROJ again or change the "Build Action" in the Solution Explorer`s Properties page of the file.
Of course I know, this is usually exactly what you would expect, because normally a build action shouldn't change if you only move the file.
But anyway, is there a way to let Visual Studio know to not change CPSROJ file if I move a file inside it?
Or alternatively, is there a way to mark a subfolder in Visual Studio so that Visual Studios knows it should ignore all files in it?
PS: Oh, I forgot to say this, it is a Core-Style CSPROJ file, that means, all CS files are included automatically.

ExcludeFoldersFromDeployment not working in publish profile

I am trying to exclude a folder in publish profile using ExcludeFoldersFromDeployment but it is not wxcluding while publishing to azure-app service
folder location : a\b\c\foldername
Please let me know if anyone can help!!
Add an additional answer, in case someone got confused by different EXCLUDE methodologies, if you are working in this environment:
Visual Studio 2017 + ASP.NET core 2 + Azure App Service.
try this:
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-2.1#exclude-files
Edit YourProject.csproj file,
<ItemGroup>
<Content Update="wwwroot/content/**/*.txt" CopyToPublishDirectory="Never" />
</ItemGroup>
BTW, I also tried these posts, which do not work:
use .wpp.targets file: Excluding Files and Folders from Deployment
Use Web Deploy Profile: Exclude unwanted binaries from azure package
Use ExcludeFilesFromDeployment or ExcludeFoldersFromDeployment: Web Deployment: Excluding Files and Folders via the Web Application’s Project File
By all those methods, web.config could not be excluded, even following this post:
How to exclude web.config when publishing with Visual Studio 2013?
According to this article, you need to edit the .pubxml or the .wpp.targets file and add an ExcludeFilesFromDeployment element or an ExcludeFoldersFromDeployment element (or both) in the PropertyGroup element. Check whether you have correct setting as the example shows:
<PropertyGroup">
<ExcludeFilesFromDeployment>
File1.aspx;File2.aspx
</ExcludeFilesFromDeployment>
<ExcludeFoldersFromDeployment>
Folder1;Folder2
</ExcludeFoldersFromDeployment>
</PropertyGroup>
Another option to exclude files or folders is to use the PublishIgnore NuGet package. This option is explained in Web Publishing a simpler way to exclude files/folders from being published on the .NET Web Development and Tools blog.
In order to exclude folders from a Blazor hosted app (like a PWA) you need to add the exclusion to the Client project, not to the Server.
I.e., you have a solution with a project named Server and a project named Client.
The Client.csproj should contains the exclusion, like:
<ItemGroup>
<Content Update="wwwroot\images\contents\**" CopyToPublishDirectory="Never" />
</ItemGroup>
Now, the wwwroot folder created by the Server.pubxml will not contains the images/contents folder and sulfolders.
I was also struggling with the issue and after looking at the #Dongdong answer I came to following conclusion:
Edit YourProject.csproj file and include the following
<ItemGroup>
<Content Update="web.Debug.config;web.Release.config;web.QA.config;appsettings.Debug.json;appsettings.Release.json;" CopyToPublishDirectory="Never" />
</ItemGroup>
You can also apply some condition as well.
<ItemGroup>
<Content Condition="'$(Configuration)|$(Platform)'=='QA|AnyCPU'" Update="appsettings.json;" CopyToPublishDirectory="Never" />
</ItemGroup>
You can extend it as per your requirement. Excluded files from the directory won't be published.

Make Visual Studio minify css and js on publish\compile

I have a project that works that works as a subapplication on a site (actually multiple sites). Among other things, I have several static css and js files in this project. I need to keep those as separate js and css files, but to keep google happy, I'd like to minify those. Those files are likely to be modified in the future, so I'd like to avoid manually minifying them every time they are changed.
Is there some option that would allow me to minify those files on compile or publish (other than writing build events to minify those files using external tools).
The following pertains to VS2015, which has much better support for this scenario.
VS2015 introduces support for task runners like gulp or grunt. You could use one of those, and have a task triggered on project open (i.e. always runs while you're working on the project) which will itself monitor for file changes and run the minimizer (i.e. do it on save).
An intro to gulp in VS2015 is available here.
A simpler alternative on VS2015 is to use the Web Compiler Extension, which basically does the above automagically with less work on your side to set it up.
All solutions I found required using different filenames for the minimized versions, and a lot of extra work to switch between using the normal/minified versions.
Instead, I wanted the compressed JavaScript files to have the original names so I didn't have to change the references in my HTML markup. I could use the normal Javascript files in my development environment, then minimized versions would be automatically deployed when publishing.
I found a simple solution that does just that.
First, install Microsoft Ajax Minifier.
Then, in your Visual Studio project file, just before the closing </Project> tag add the following :
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Microsoft Ajax Minifier\ajaxmin.tasks" />
<Target Name="AfterBuild" Condition="'$(ConfigurationName)'=='Release'">
<ItemGroup>
<JS Include="**\*.js" Exclude="**\*.min.js;obj\**\*.*" />
<CSS Include="**\*.css" Exclude="**\*.min.css;obj\**\*.*" />
</ItemGroup>
<AjaxMin
JsSourceFiles="#(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".jsMIN"
CssSourceFiles="#(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".cssMIN" />
</Target>
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
</PropertyGroup>
<Target Name="CustomCollectFiles">
<ItemGroup>
<MinJS Include="**\*.jsMIN" />
<FilesForPackagingFromProject Include="%(MinJS.Identity)">
<DestinationRelativePath>%(RecursiveDir)%(Filename).js</DestinationRelativePath>
</FilesForPackagingFromProject>
<MinCSS Include="**\*.cssMIN" />
<FilesForPackagingFromProject Include="%(MinCSS.Identity)">
<DestinationRelativePath>%(RecursiveDir)%(Filename).css</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
What does the above code do? When you publish in Visual Studio, this code will find every .js and .css file in your source, and create a minified copy using the extension .jsMIN and .cssMIN. It will ignore files that are already minified. Then it will copy these minified files to the deployment folder, using the original file names.
Voilà! You just published minified JS/CSS files, while your original files stay intact on your development environment.
Optional:
Want Ajax Minifier to be packaged with your project? From the Ajax Minifier install folder, you can move AjaxMin.dll and AjaxMinTask.dll directly into your source directory. I put them in my App_Data folder. Once they're somewhere in your source, in Visual Studio right-click them, select Include in Project, and also change their Build Action property to None.
Then in the code I included above, change
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Microsoft Ajax Minifier\ajaxmin.tasks" />
to
<UsingTask TaskName="AjaxMin" AssemblyFile="$(MSBuildProjectDirectory)\App_Data\AjaxMinTask.dll" />
Done.
A troubleshooting tip:
My main code above executes AfterBuild and only when the configuration is Release. That's so it will only run during a publish. If your configuration is named something else, or you want it to run in other circumstances, modify the code as needed.
A nice tool can be downloaded from Visual studio Marketplace.
Bundler & Minifier

Visual Studio Project - Automatically include externally created files?

Is there a nice solution to be able to automatically refresh/include externally created files in the asp.net application project?
The majority of the time I'm programming front end javascript in Sublime Text 2 editor. When integrating with the rest of the team I have to manually make sure that any new files i've created externally are included in the asp.net project.
Is there an automatic option to refresh the files in a particular folder or something?
Thanks
Figured it out!
in the proj files you can edit the itemgroup includes so rather than just including individual files you can use glob patterns. such as
app\**\*.js
<ItemGroup>
<Content Include="app\**\*.js" />
<Content Include="scripts\jquery.js" />
</ItemGroup>

Conditionally embed ASP.NET MVC2 Views as resources during build in Visual Studio 2010

I have a ASP.NET MVC2 project in VS2010 that can be deployed in two modes: standalone or plugin. In standalone mode, the views should live outside the compiled assembly as .aspx files (the default setup). In plugin mode, the views are switched (currently by hand) to embedded resources and the entire assembly is dropped into a host project folder.
Currently, this requires the developer to go through each view and switch it from Build Action: "Content" to "Embedded Resource" and vice versa. I would like to create a new solution configuration to automatically grab all .aspx files and build them as resources.
This SO post seems like the solution, but I would prefer not to have to edit the .csproj every single time I add a new view to the project. Is there a way to use a wild cards or some other batch/global conditionally statement to change resources from content to embedded?
Well, sometimes I should experiment before I post.
I modified my .csproj file and just went ahead and tried a wild card:
Views\*\*.aspx
...and it worked great. I posted a snippet of my reconfigured project file below. One thing to note: adding a new view puts it in the "always content" category at the top of the snippet below. You can either live with having .aspx files deployed even when the views are embedded as resources (not an issue for me) or you can move them from the first ItemGroup below to the Otherwise section each time by hand.
<ItemGroup> <-- Always included as content
<Content Include="Global.asax" />
<Content Include="Web.config">
<SubType>Designer</SubType>
</Content>
<Content Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</Content>
<Content Include="Web.Release.config">
<DependentUpon>Web.config</DependentUpon>
</Content>
</ItemGroup>
<Choose> <--- Only added to assembly in "Plugin Mode"
<When Condition=" '$(Configuration)'=='Plugin' ">
<ItemGroup>
<EmbeddedResource Include="Views\*\*.aspx">
</EmbeddedResource>
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<Content Include="Views\Comment\Create.aspx" />
<Content Include="Views\Record\Create.aspx" />
</ItemGroup>
</Otherwise>
</Choose>

Resources