<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[Visual Studio - Bacon Applications]]></title><description><![CDATA[Follow me while I share with the world useful things I have learned and projects I am working on.]]></description><link>http://www.baconapplications.com/</link><image><url>http://www.baconapplications.com/favicon.png</url><title>Visual Studio - Bacon Applications</title><link>http://www.baconapplications.com/</link></image><generator>Ghost 3.7</generator><lastBuildDate>Sat, 22 Mar 2025 18:50:25 GMT</lastBuildDate><atom:link href="http://www.baconapplications.com/tag/visual-studio/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Custom Publishing with Visual Studio 2013]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>If you read my <a href="http://www.baconapplications.com/running-bowergrunt-in-visual-studio-2013/" target="_blank">last post</a>, hopefully you implemented Bower into your next Visual Studio project.  If you are anything like me though, you probably noticed some files being published that you really didn't want to publish.  This caused me to do some digging to figure out how to remove</p>]]></description><link>http://www.baconapplications.com/custom-publish-with-visual-studio-2013/</link><guid isPermaLink="false">5e535ef4ba89970001e0b05d</guid><category><![CDATA[Visual Studio]]></category><category><![CDATA[Code]]></category><dc:creator><![CDATA[Randy Bacon]]></dc:creator><pubDate>Wed, 08 Oct 2014 18:30:38 GMT</pubDate><media:content url="http://www.baconapplications.com/content/images/2020/02/vspublishingCover.jpg" medium="image"/><content:encoded><![CDATA[<!--kg-card-begin: markdown--><img src="http://www.baconapplications.com/content/images/2020/02/vspublishingCover.jpg" alt="Custom Publishing with Visual Studio 2013"><p>If you read my <a href="http://www.baconapplications.com/running-bowergrunt-in-visual-studio-2013/" target="_blank">last post</a>, hopefully you implemented Bower into your next Visual Studio project.  If you are anything like me though, you probably noticed some files being published that you really didn't want to publish.  This caused me to do some digging to figure out how to remove these files from publishing.  Additionally I learned how to copy some files as well.  This is useful for custom file publishing such as replacing a globals.js file.</p>
<h3 id="tothecode">To the Code</h3>
<p>To get started create a blank MVC application in Visual Studio.  If you want to cheat and get a complete kick start, <a href="https://github.com/baconapplications/VS2013PublishFiles" target="_blank">click here</a> to pull an example project from my GitHub.</p>
<h4 id="examplepublishing">Example Publishing</h4>
<p>Once you have your example project set up, create a publishing profile.  I am only using file based publishing in this post (maybe a follow up post is needed...).  First start by publishing your project and selecting custom profile:</p>
<img src="http://www.baconapplications.com/content/images/posts/vspublish/publish.png" alt="Custom Publishing with Visual Studio 2013">
<p>Now select the publish method, file system, and enter the location where you would like your files to be published.</p>
<img src="http://www.baconapplications.com/content/images/posts/vspublish/publishMethod.png" alt="Custom Publishing with Visual Studio 2013">
<p>On the final screen, select the configuration you would like to use for publishing.  I also like to delete all my existing files and make sure not to include files from the App_Data folder.</p>
<img src="http://www.baconapplications.com/content/images/posts/vspublish/publishConfig.png" alt="Custom Publishing with Visual Studio 2013">
<p>When you are done, click publish to publish the files.</p>
<h3 id="timetocustomize">Time to Customize</h3>
<p>At this point, VS will create a publishing profile for you.  This is where you want to dig in and manually customize your publishing process.  Expand the Properties directory and then the PublishProfiles directory to see the publishing profile XML.</p>
<img src="http://www.baconapplications.com/content/images/posts/vspublish/publishSolutionExpl.png" alt="Custom Publishing with Visual Studio 2013">
<p>Once you open this file, you will see something like the below as the default:</p>
<img src="http://www.baconapplications.com/content/images/posts/vspublish/publishxmlPre.png" alt="Custom Publishing with Visual Studio 2013">
<p>First you will create a publishing action to remove some files and a directory.  To do this, you will be adding two new XML elements, ExcludeFilesFromDeployment and ExcludeFoldersFromDeployment to your pubxml file.  The first element removes files you do not wish to deploy and the second removes directories you do not wish to deploy.  The paths in both elements are relative to the project root.  For this example I added an ignoreme.txt file and a RemoveFolder directory to the root of the project.  Once you have made your edits, your pubxml file should look like the following:</p>
<script src="https://gist.github.com/baconapplications/e520ff6824a9bf22f504.js"></script>
<p>Save your changes and publish your project using this publishing profile.  You should see that the ignoreme.txt file, packages.config file and the RemoveFolder are not published to the publish directory.  SWEET!</p>
<p>The final example of custom publishing involves copying a file from an external location to the final published location.  This could be useful for something like replacing a global.js file.  Inside your solution but outside of your project, create 2 files in a solution folder - global.js and renamefile.js.  The contents of these do not matter.  You only need to be able to tell what file it is.  Additionally, in your scripts directory in your MVC project create another global.js file with different content in it.</p>
<p>Now you will be adding two additional targets and property groups to your pubxml file.  Add the &lt;Target Name=&quot;CopyGlobal&quot;&gt; and &lt;Target Name=&quot;CopyAndRename&quot;&gt; elements and the associated &lt;PropertyGroup&gt; elements to your pubxml file.  The result should look like the following:</p>
<script src="https://gist.github.com/baconapplications/f7d18b132c1b780fc740.js"></script>
<p>Assuming your project is structured the same as mine (you might need to verify the path of your global.js file), when you publish you should notice the global.js file in the scripts directory is your external file.  Additionally, you should now have the renamed config.js file.</p>
<p>If you want to copy more than one file, you can add additional &amp;;lt_CustomFiles&gt; elements to your target group.  If you use this method however, you will need to use this line</p>
<pre><code>&lt;DestinationRelativePath&gt;scripts\%(RecursiveDir)%(Filename)%(Extension)&lt;/DestinationRelativePath&gt;
</code></pre>
<p>to ensure the files are placed correctly.</p>
<p>Also note that <i>scripts&lt;/i&gt; directory is where the copied files will be placed.  If you want to use a different location you will need to adjust that.</i></p>
<h3 id="closingthoughts">Closing Thoughts</h3>
<p>Hopefully this will save you some time in publishing your project with custom build actions.  Although this isn't the Grunt of Visual Studio, it does allow some custom build actions without an additional bat file or script.  Until next time - happy coding!</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Running Bower and Grunt in Visual Studio 2013]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>Yes the title of this post is correct.  A recent extension for Visual Studio 2013 will let you run Grunt inside of Visual Studio.  Scott Hanselman gave a quick overview <a href="http://www.hanselman.com/blog/introducinggulpgruntbowerandnpmsupportforvisualstudio.aspx" target="_blank">here</a>, but honestly I had to stumble through implementing it the first time.  To save you some time read on!</p>]]></description><link>http://www.baconapplications.com/running-bower-grunt-in-visual-studio-2013/</link><guid isPermaLink="false">5e535ef4ba89970001e0b05a</guid><category><![CDATA[Grunt]]></category><category><![CDATA[Visual Studio]]></category><category><![CDATA[Bower]]></category><dc:creator><![CDATA[Randy Bacon]]></dc:creator><pubDate>Tue, 16 Sep 2014 18:32:35 GMT</pubDate><media:content url="http://www.baconapplications.com/content/images/2020/02/vsGrunt.jpg" medium="image"/><content:encoded><![CDATA[<!--kg-card-begin: markdown--><img src="http://www.baconapplications.com/content/images/2020/02/vsGrunt.jpg" alt="Running Bower and Grunt in Visual Studio 2013"><p>Yes the title of this post is correct.  A recent extension for Visual Studio 2013 will let you run Grunt inside of Visual Studio.  Scott Hanselman gave a quick overview <a href="http://www.hanselman.com/blog/introducinggulpgruntbowerandnpmsupportforvisualstudio.aspx" target="_blank">here</a>, but honestly I had to stumble through implementing it the first time.  To save you some time read on!</p>
<h3 id="preparingyourenvironment">Preparing Your Environment</h3>
<p>If you haven't done any work with NodeJS on your machine, you will need to do a few things to get your environment ready for this extension.  This extension is a wrapper for NodeJS, Git, Bower, Grunt, Gulp and npm.  If you haven't heard of or used any of these tools, I would recommend reading the following:</p>
<ul>
<li>
<p><a href="http://nodejs.org/" target="_blank">NodeJS</a> - Everyone seems to be talking about it.  It's a JavaScript based language that runs server-side.  Grunt uses Node to run its tasks.</p>
</li>
<li>
<p><a href="http://git-scm.com/" target="_blank">Git</a> - Git is a version control system that is distributed.  The Windows install is pretty unobtrusive.  If you have never used Git, give it a shot.  You may love it, I do now!</p>
</li>
<li>
<p><a href="http://bower.io/" target="_blank">Bower</a> - Bower installs client-side packages for the web, much like Nuget.  Bower uses the Git repository for the client-side packages instead of the package having to be maintained by someone for Nuget.</p>
</li>
<li>
<p><a href="http://gruntjs.com/" target="_blank">Grunt</a> - Grunt is a JavaScript task runner used to help with builds, downloading packages, minifying files, all sorts of fun things!</p>
</li>
<li>
<p><a href="http://gulpjs.com/" target="_blank">Gulp</a> - Gulp is another JavaScript task runner, but is code-driven instead of declaration driven.</p>
</li>
<li>
<p><a href="https://www.npmjs.org/" target="_blank">NPM</a> - Node packaged modules manager.  Think of NPM as something kind of like the Nuget of Node.  NPM is more powerful though as it allows you to install modules you can you use system wide, not just in your project.</p>
</li>
</ul>
<h3 id="whattoinstall">What to Install</h3>
<p>Here is the install order I would use for a fresh install.  As a guy who dabbles in Node, I had most of these installed.  You don't need to re-install any item below.  I would just make sure they are up-to-date.  For all the installers, the default options will be fine.</p>
<ul>
<li><a href="http://git-scm.com/downloads" target="_blank">Git</a></li>
</ul>
<p>A note about your Git install: if you choose to install and use the Git bash, like I do, you will need to add Git to your path.  Follow the directions <a href="http://blog.countableset.ch/2012/06/07/adding-git-to-windows-7-path/" target="_blank">here</a> to do that.  If installed correctly, typing git in a command prompt will display the git help screen.</p>
<ul>
<li><a href="http://nodejs.org/" target="_blank">NodeJS</a></li>
</ul>
<p>Once node is installed the following can be installed via NPM.  NPM is installed with Node on Windows.  Open a new command prompt and run the following commands.</p>
<pre><code>npm install -g bower
</code></pre>
<p>The above command will install the bower package globally (-g) so any Node script can access the package on your machine.  Now run the following 2 commands to finish all the installs we need.</p>
<pre><code>npm install -g grunt-cli

npm install -g gulp
</code></pre>
<p>Once that batch of nitty gritty installs is done, you can finally install the extension for Visual Studio.  <b><i>You must install Visual Studio 2013 service pack 3</i></b> to install this package.  If you haven't done that yet, check for updates in Visual Studio and install it.  Once service pack 3 is installed, install the extension by downloading it <a href="http://visualstudiogallery.msdn.microsoft.com/8e1b4368-4afb-467a-bc13-9650572db708" target="_blank">here</a>.</p>
<p>You will also need to install the following extensions to make your life a little easier:</p>
<ul>
<li><a href="http://visualstudiogallery.msdn.microsoft.com/65748cdb-4087-497e-a394-2e3449c8e61e" target="_blank">Package Intellisense</a> Adds NPM and Bower package Intellisense right in Visual Studio.</li>
<li><a href="http://visualstudiogallery.msdn.microsoft.com/dcbc5325-79ef-4b72-960e-0a51ee33a0ff" target="_blank">Grunt Launcher</a> Adds a right click option for Grunt and Gulp files.  Also allows you to do a right click, NPM install.</li>
</ul>
<h3 id="usinggruntandbowerinvisualstudio">Using Grunt and Bower in Visual Studio</h3>
<p>It's time for learning by example.  To see why anyone would go through this much work to set up an extension, check out the following sample.  First create a new <i>empty</i> web project.  You can use Grunt and Bower in any web project, but for this example, keep it simple.  If you want to cheat, you can check out the github for this example project <a href="https://github.com/baconapplications/BowerGruntInVS2013" target="_blank">here</a>.</p>
<h4 id="packagejson">package.json</h4>
<p>Once you have an empty web project, create the following package.json file.  The package.json file is used by Grunt to provide information to Grunt about your project.  These include the dev dependencies (what is needed to run your Grunt tasks) and the production (project) dependencies.  The package below will tell NPM to install grunt and some grunt related packages that will be used in your grunt file.</p>
<pre><code>{
    &quot;name&quot;: &quot;vs-grunt-example&quot;,
    &quot;version&quot;: &quot;0.0.1&quot;,
    &quot;description&quot;: &quot;Just a simple Grunt example - filled with fun and joy.&quot;,
    &quot;devDependencies&quot;: {
        &quot;grunt&quot;: &quot;~0.4.5&quot;,
        &quot;grunt-bower-task&quot;: &quot;~0.4.0&quot;,
        &quot;grunt-contrib-uglify&quot;: &quot;~0.5.1&quot;,
        &quot;grunt-contrib-concat&quot;: &quot;~0.5.0&quot;
    }
}
</code></pre>
<p><b>Once this package file is done,</b> you will need to run NPM.  Running NPM will install these packages and allow you to run your grunt script.  I am in the habit of running NPM right after I make changes to my package.json script.  To run NPM for your project, right click the package.json file and choose &quot;NPM install packages&quot;.</p>
<h4 id="bowerjson">bower.json</h4>
<p>Now create a bower.json file.  This file tells bower what you want your project to download and install in your project.  More information about the bower.json file can be found <a href="https://github.com/bower/bower.json-spec" target="_blank">here</a>.</p>
<pre><code>{
    &quot;name&quot;: &quot;vs-grunt-example&quot;,
    &quot;version&quot;: &quot;0.0.1&quot;,
    &quot;authors&quot;: [
        &quot;Randy Bacon &lt;randy@hirerandybacon.com&gt;&quot;
    ],
    &quot;description&quot;: &quot;Example using grunt and bower in VS2013&quot;,
    &quot;homepage&quot;: &quot;www.hirerandybacon.com/blog&quot;,
    &quot;dependencies&quot;: {
        &quot;jquery&quot;: null,
        &quot;leaflet&quot;: null,
        &quot;leaflet-plugins&quot;: null
    },
    &quot;exportsOverride&quot;: {
        &quot;leaflet-plugins&quot;: {
            &quot;control&quot;: &quot;control/*.js&quot;,
            &quot;css&quot;: &quot;css/*&quot;,
            &quot;layer&quot;: &quot;layer/*.js&quot;,
            &quot;layer/tile&quot;: &quot;layer/tile/*.js&quot;,
            &quot;layer/vector&quot;: &quot;layer/vector/*.js&quot;
        }
    },
    &quot;private&quot;: true
}
</code></pre>
<p>Note: When I tried a fresh solution on another machine, bower could not read the bower.json file.  It turned out the encoding was an issue.  To fix it open the bower.json file, choose &quot;Save As&quot;, click the arrow next to &quot;Save&quot;, choose &quot;Save with Encoding...&quot; and finally select &quot;Unicode (UTF-8 without signature)&quot; for the encoding and &quot;Unix (LF)&quot; for the line endings.</p>
<h4 id="gruntfilejs">Gruntfile.js</h4>
<p>Now create the Gruntfile.js.  This is where the magic happens.  If you are still with me, then my virtual hat is off to you.</p>
<pre><code>module.exports = function (grunt) {
    grunt.initConfig({
    	//this loads our packages for our grunt file
        pkg: grunt.file.readJSON('package.json'),
        
        //this section does our bower installs for us
        bower: {
            install: {
                options: {
                    targetDir: './scripts/vendor',
                    layout: 'byComponent',
                    install: true,
                    verbose: true,
                    cleanTargetDir: false,
                    cleanBowerDir: false,
                    bowerOptions: {}
                }
            }
        },
        
        //this section is used to join our js files together
        concat: {
            options: {
                separator: ';',
                stripBanners: true,
                banner: '/*! &lt;%= pkg.name %&gt; - v&lt;%= pkg.version %&gt; - ' +
                  '&lt;%= grunt.template.today(&quot;mm-dd-yyyy&quot;) %&gt; */',
            },
            devSrc: {
                src: ['scripts/vendor/jquery/*.js',
            'scripts/vendor/leaflet/*.js',
                    'scripts/vendor/leaflet-plugins/layer/tile/Google.js'],
                dest: 'scripts/vendor/dist/project-vendor-src.js',
            },
        },
        
        //this section is used to for our minify action
        uglify: {
            options: {
                banner: '/*! &lt;%= pkg.name %&gt; &lt;%= grunt.template.today(&quot;yyyy-mm-dd&quot;) %&gt; */\n'
            },
            build: {
                src: 'scripts/vendor/dist/project-vendor-src.js',
                dest: 'scripts/vendor/dist/project-vendor.min-&lt;%= pkg.version %&gt;.js'
            }
        }
    });

	//npm modules need for our task
    grunt.loadNpmTasks('grunt-bower-task');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-concat');

    //run bower for package install
    grunt.registerTask('install-bower-packages', ['bower']);

    //build vendor file src
    grunt.registerTask('merge-js-files', ['concat']);

    //build min vendor file from above
    grunt.registerTask('min-js-file', ['concat', 'uglify']);
};
</code></pre>
<h3 id="usingtaskrunnerexplorer">Using Task Runner Explorer</h3>
<p>Once your Gruntfile.js is complete, you can run the fancy Visual Studio Task Runner Explorer that this post is all about and watch the magic...</p>
<p>Right click on your Gruntfile.js file and choose Task Runner Explorer.  This will open up the tool that allows you to run your Grunt tasks. Note: if this option isn't showing up for you, try to quit and restart Visual Studio.   You should see something like the below:</p>
<img src="http://www.baconapplications.com/content/images/posts/vs-grunt-example/taskRunnerScreen.png" alt="Running Bower and Grunt in Visual Studio 2013">
<h4 id="runningbower">Running Bower</h4>
<p>Right click the install-bower-packages Alias Task and click run.</p>
<img src="http://www.baconapplications.com/content/images/posts/vs-grunt-example/taskRunnerInstallBower.png" alt="Running Bower and Grunt in Visual Studio 2013">
<p>The right window should now let you know bower is doing some work.  When this is done you probably will not see any changes in your Solution Explorer.  You will need to show all files in the Solution Explorer via this button:</p>
<img src="http://www.baconapplications.com/content/images/posts/vs-grunt-example/solutionExplorerShowAll.png" alt="Running Bower and Grunt in Visual Studio 2013">
<p>In order to see changes after running your grunt tasks, you will need to refresh your Solution Explorer. After refreshing, right click the scripts directory and select &quot;Include in Project&quot;. Now your solution should now look something like this:</p>
<img src="http://www.baconapplications.com/content/images/posts/vs-grunt-example/solutionExplorer.png" alt="Running Bower and Grunt in Visual Studio 2013">
<p>Congratulations!  You have just used Grunt to download and install js files in your project via bower.  Any time the Git repository changes, you can run this command to update the JS files you have referenced.  This is a very easy way to keep your JS files in sync with the Git for new versions and bug fixes.</p>
<h4 id="joiningourjsfiles">Joining Our JS Files</h4>
<p>The second task you created will join your vendor JS files you just installed into a single JS file.  This is helpful in dev (you only need to include a single file) and also makes creating a minified file of this file much faster.</p>
<p>Again go to the Task Runner Explorer and right click the merge-js-files Alias Task.  Again you will see a few messages in your command window to the right.  You can now refresh your scripts/vendor directory and you should see the dist directory.  Make sure to refresh your Solution Explorer and include the files in your project to use them.</p>
<h4 id="minifyingourjsfile">Minifying Our JS File</h4>
<p>The final task you created will minify your JS file for use in production.  Right click the min-js-file task and click run.  Again refresh the scripts/vendor/dist and you should see your new project-vendor.min-0.0.1.js file.  Sweet!</p>
<h3 id="gotchas">Gotchas</h3>
<p>A few issues I ran into while working on my grunt scripts:</p>
<ul>
<li>
<p>bower.json file encoding.  Selecting the default .json template in Visual Studio will not work with bower and you will have to make adjustments to the encoding per above.</p>
</li>
<li>
<p>Pretty bad error messages.  If you have an error in your package.json file, the command output is not really too helpful.</p>
</li>
<li>
<p>No bower search.  From the command line you can run <code>bower search item-to-search-for</code> to find matching gits.  There doesn't appear to be a way to do that via this tool.</p>
</li>
<li>
<p>Solution Explorer does not automatically add new files.  As you probably experienced above, the Solution Explorer does not show your bower installed files automatically.  This is a minor annoyance, but still a 'gotcha'.</p>
</li>
<li>
<p>NPM uninstalls - I was playing with different bower running packages and didn't like the first couple of packages I had installed.  I was unable to delete them via Windows explorer because the path of some of the nested files was too long.  To uninstall them, open a command prompt and browse to your web project directory.  Run the following to remove a package:</p>
<pre><code>	npm uninstall &lt;package name&gt;
</code></pre>
</li>
</ul>
<h3 id="closingthoughts">Closing Thoughts</h3>
<p>Hopefully you can see the power of using bower and grunt in your project.  It's a new world out there and Visual Studio is closing the gap between tools that are usually just used in Node or with Open Source tools.  I find this change exciting and I'm very happy to be able to use bower inside of Visual Studio.  Until next time - happy coding!</p>
<!--kg-card-end: markdown-->]]></content:encoded></item></channel></rss>