PrimordialCodeJekyll2021-12-02T10:57:54+01:00https://www.primordialcode.com/Alessandro Giorgettihttps://www.primordialcode.com/alessandro.giorgetti@live.comhttps://www.primordialcode.com/blog/post/referenced-project-targeted-different-framework-family2021-12-02T00:00:00+01:002021-12-02T00:00:00+01:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>The project ‘Acme.Core’ cannot be referenced. The referenced project is targeted to a different framework family (.NETCoreApp)</p>
<p>In a multitarget solution that also contains an old .Net Full Framework Web Project sometimes we might face that error.</p>
<p>The scenario is:</p>
<ul>
<li>A solution with multiple multitarget Library Projects and an Old Web Project.</li>
<li>Acme.Core: a multitarget Library Project with the following target frameworks: net6.0, net5.0, netstandard2.0, net472</li>
<li>Acme.Web: an old net472 Asp.Net MVC Web Project referencing Acme.Core.</li>
</ul>
<p>If it happens that you get the above mentioned error, and you see a yellow warning mark in Visual Studio reference node tree of Acme.Web project, the problem might be the order of the target frameworks in the Acme.Core.csproj file.</p>
<p>The original .csproj file that caused the warning was like:</p>
<figure class="highlight"><pre><code class="language-xml" data-lang="xml"> <span class="nt"><PropertyGroup></span>
<span class="nt"><TargetFrameworks></span>net6.0;net5.0;netstandard2.0;net472<span class="nt"></TargetFrameworks></span>
<span class="nt"></PropertyGroup></span></code></pre></figure>
<p>To solve the problem just change the order of target frameworks:</p>
<figure class="highlight"><pre><code class="language-xml" data-lang="xml"> <span class="nt"><PropertyGroup></span>
<span class="nt"><TargetFrameworks></span>net472;net6.0;net5.0;netstandard2.0<span class="nt"></TargetFrameworks></span>
<span class="nt"></PropertyGroup></span></code></pre></figure>
<p>The first of the list is the one used by old Web Projects when making project references.</p>
<p>The hint to figure out the thing was in the warning message itself: <code class="language-plaintext highlighter-rouge">The referenced project is targeted to a different framework family (.NETCoreApp)'</code></p>
<p>Old Web Projects that target full frameworks belongs to another framework family: <code class="language-plaintext highlighter-rouge">(.NETFramework)</code>.</p>
<p>For more informations on multi targeting and cross platform targeting head to the official docs:</p>
<ul>
<li><a href="https://docs.microsoft.com/en-us/dotnet/standard/frameworks">Target frameworks in SDK-style projects</a></li>
<li><a href="https://docs.microsoft.com/en-us/dotnet/standard/library-guidance/cross-platform-targeting">Cross-platform targeting</a></li>
</ul>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/referenced-project-targeted-different-framework-family">The referenced project is targeted to a different framework family</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on December 02, 2021.</p>https://www.primordialcode.com/blog/post/microsoft-webapplication-targets-not-found2021-07-02T00:00:00+02:002021-07-02T00:00:00+02:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>Do you want to compile old Website Projects (csproj / vbproj) file with the new “dotnet build” CLI command?</p>
<p>In case of build failure an error similar to the following one might have been reported:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Sample.Web.csproj(2620,3): error MSB4019: The imported project "C:\Program Files\dotnet\sdk\5.0.301\
Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets" was not found.
Confirm that the path in the Import "C:\Program Files\dotnet\sdk\5.0.301\Microsoft\VisualStudio\v16.0\
WebApplications\Microsoft.WebApplication.targets" is correct, and that the file exists on disk.
</code></pre></div></div>
<p>Paths might change but the problem is the CLI is not able to properly locate the required <code class="language-plaintext highlighter-rouge">.targets</code> files (this can often happen on a build server).</p>
<p>The easy solution:</p>
<p>Add the MSBuild.Microsoft.VisualStudio.Web.targets NuGet package (a newer version can be used if available) to the Website project:</p>
<figure class="highlight"><pre><code class="language-xml" data-lang="xml"><span class="nt"><package</span> <span class="na">id=</span><span class="s">"MSBuild.Microsoft.VisualStudio.Web.targets"</span> <span class="na">version=</span><span class="s">"14.0.0.3"</span> <span class="na">targetFramework=</span><span class="s">"net48"</span> <span class="nt">/></span></code></pre></figure>
<p>The following line of code will be added to your .csproj file:</p>
<figure class="highlight"><pre><code class="language-xml" data-lang="xml"><span class="nt"><Import</span> <span class="na">Project=</span><span class="s">"..\..\packages\MSBuild.Microsoft.VisualStudio.Web.targets.14.0.0.3\build\MSBuild.Microsoft.VisualStudio.Web.targets.props"</span> <span class="na">Condition=</span><span class="s">"Exists('..\..\packages\MSBuild.Microsoft.VisualStudio.Web.targets.14.0.0.3\build\MSBuild.Microsoft.VisualStudio.Web.targets.props')"</span> <span class="nt">/></span></code></pre></figure>
<p>The following code is not strictly needed, but if you want you can also find the following lines in your .csproj file:</p>
<figure class="highlight"><pre><code class="language-xml" data-lang="xml"> <span class="nt"><PropertyGroup></span>
<span class="nt"><VisualStudioVersion</span> <span class="na">Condition=</span><span class="s">"'$(VisualStudioVersion)' == ''"</span><span class="nt">></span>10.0<span class="nt"></VisualStudioVersion></span>
<span class="nt"><VSToolsPath</span> <span class="na">Condition=</span><span class="s">"'$(VSToolsPath)' == ''"</span><span class="nt">></span>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)<span class="nt"></VSToolsPath></span>
<span class="nt"></PropertyGroup></span>
<span class="nt"><Import</span> <span class="na">Project=</span><span class="s">"$(MSBuildBinPath)\Microsoft.CSharp.targets"</span> <span class="nt">/></span>
<span class="nt"><Import</span> <span class="na">Project=</span><span class="s">"$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets"</span> <span class="na">Condition=</span><span class="s">"'$(VSToolsPath)' != ''"</span> <span class="nt">/></span>
<span class="nt"><Import</span> <span class="na">Project=</span><span class="s">"$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets"</span> <span class="na">Condition=</span><span class="s">"false"</span> <span class="nt">/></span>
<span class="nt"><Target</span> <span class="na">Name=</span><span class="s">"MvcBuildViews"</span> <span class="na">AfterTargets=</span><span class="s">"AfterBuild"</span> <span class="na">Condition=</span><span class="s">"'$(MvcBuildViews)'=='true'"</span><span class="nt">></span>
<span class="nt"><AspNetCompiler</span> <span class="na">VirtualPath=</span><span class="s">"temp"</span> <span class="na">PhysicalPath=</span><span class="s">"$(WebProjectOutputDir)"</span> <span class="nt">/></span>
<span class="nt"></Target></span></code></pre></figure>
<p>and change them like this:</p>
<figure class="highlight"><pre><code class="language-xml" data-lang="xml"> <span class="nt"><PropertyGroup></span>
<span class="nt"><VisualStudioVersion</span> <span class="na">Condition=</span><span class="s">"'$(VisualStudioVersion)' == ''"</span><span class="nt">></span>10.0<span class="nt"></VisualStudioVersion></span>
<span class="nt"><VSToolsPath</span> <span class="na">Condition=</span><span class="s">"'$(VSToolsPath)' == ''"</span><span class="nt">></span>..\..\Packages\MSBuild.Microsoft.VisualStudio.Web.targets.14.0.0.3\tools\VSToolsPath<span class="nt"></VSToolsPath></span>
<span class="nt"></PropertyGroup></span>
<span class="nt"><Import</span> <span class="na">Project=</span><span class="s">"$(MSBuildBinPath)\Microsoft.CSharp.targets"</span> <span class="nt">/></span>
<span class="nt"><Import</span> <span class="na">Project=</span><span class="s">"$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets"</span> <span class="na">Condition=</span><span class="s">"'$(VSToolsPath)' != ''"</span> <span class="nt">/></span>
<span class="nt"><Import</span> <span class="na">Project=</span><span class="s">"$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets"</span> <span class="na">Condition=</span><span class="s">"false"</span> <span class="nt">/></span>
<span class="nt"><Target</span> <span class="na">Name=</span><span class="s">"MvcBuildViews"</span> <span class="na">AfterTargets=</span><span class="s">"AfterBuild"</span> <span class="na">Condition=</span><span class="s">"'$(MvcBuildViews)'=='true'"</span><span class="nt">></span>
<span class="nt"><AspNetCompiler</span> <span class="na">VirtualPath=</span><span class="s">"temp"</span> <span class="na">PhysicalPath=</span><span class="s">"$(WebProjectOutputDir)"</span> <span class="nt">/></span>
<span class="nt"></Target></span></code></pre></figure>
<p>You can now compile old Websites .csproj / .vbproj using: <code class="language-plaintext highlighter-rouge">dotnet build</code>.</p>
<p>Have fun!</p>
<p><a href="https://www.primordialcode.com/blog/post/microsoft-webapplication-targets-not-found">dotnet build - Microsoft.WebApplication.targets not found</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on July 02, 2021.</p>https://www.primordialcode.com/blog/post/nuget-credentialprovider-timeout2021-06-17T00:00:00+02:002021-06-17T00:00:00+02:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>Plugin ‘CredentialProvider.Microsoft’ can fail due to timeouts!</p>
<p>Sometimes when you try to restore packages via “nuget restore” or “dotnet restore” in your build pipelines or in your everyday development job, you can get the following error (text has been formatted to increase readability):</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat">...
##<span class="o">[</span><span class="kd">error</span><span class="o">]</span><span class="kd">The</span> <span class="kd">nuget</span> <span class="kd">command</span> <span class="kd">failed</span> <span class="kd">with</span> <span class="k">exit</span> <span class="kd">code</span><span class="o">(</span><span class="m">1</span><span class="o">)</span> <span class="kd">and</span> <span class="kd">error</span><span class="o">(</span><span class="kd">Problem</span> <span class="kd">starting</span> <span class="kd">the</span> <span class="kd">plugin</span>
<span class="s1">'X:\DevOpsAgents\2.179.0\CredentialProviderV2\plugins\netfx\CredentialProvider.Microsoft\CredentialProvider.Microsoft.exe'</span>.
<span class="kd">Plugin</span> <span class="s1">'CredentialProvider.Microsoft'</span> <span class="kd">failed</span> <span class="kd">within</span> <span class="m">5</span><span class="o">,</span><span class="m">378</span> <span class="kd">seconds</span> <span class="kd">with</span> <span class="k">exit</span> <span class="kd">code</span> <span class="o">-</span><span class="m">1</span>.
<span class="kd">Problem</span> <span class="kd">starting</span> <span class="kd">the</span> <span class="kd">plugin</span> <span class="s1">'X:\DevOpsAgents\2.179.0\CredentialProviderV2\plugins\netfx\CredentialProvider.Microsoft\CredentialProvider.Microsoft.exe'</span>.
<span class="kd">Plugin</span> <span class="s1">'CredentialProvider.Microsoft'</span> <span class="kd">failed</span> <span class="kd">within</span> <span class="m">5</span><span class="o">,</span><span class="m">378</span> <span class="kd">seconds</span> <span class="kd">with</span> <span class="k">exit</span> <span class="kd">code</span> <span class="o">-</span><span class="m">1</span>.
<span class="kd">NuGet</span>.Protocol.Plugins.PluginException: <span class="kd">Problem</span> <span class="kd">starting</span> <span class="kd">the</span> <span class="kd">plugin</span> <span class="s1">'X:\DevOpsAgents\2.179.0\CredentialProviderV2\plugins\netfx\CredentialProvider.Microsoft\CredentialProvider.Microsoft.exe'</span>.
<span class="kd">Plugin</span> <span class="s1">'CredentialProvider.Microsoft'</span> <span class="kd">failed</span> <span class="kd">within</span> <span class="m">5</span><span class="o">,</span><span class="m">378</span> <span class="kd">seconds</span> <span class="kd">with</span> <span class="k">exit</span> <span class="kd">code</span> <span class="o">-</span><span class="m">1</span>.
<span class="k">in</span> <span class="kd">NuGet</span>.Protocol.Core.Types.PluginResource.<GetPluginAsync>d__5.MoveNext<span class="o">()</span>
...</code></pre></figure>
<p>One of the problems is the slowness of the pipeline itself in obtaining authentication request responses (especially if you use multiple nuget feeds): the default timeout for the CredentialProvider is 5 seconds, which can sometimes be too low.</p>
<p>The solution is to increate the timeouts, just add the following code in your azure-pipelines.yml build files:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>variables:
- name: NUGET.PLUGIN.HANDSHAKE.TIMEOUT.IN.SECONDS
value: 30
- name: NUGET.PLUGIN.REQUEST.TIMEOUT.IN.SECONDS
value: 30
</code></pre></div></div>
<p>Or define the following environment variables:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$env:NUGET_PLUGIN_HANDSHAKE_TIMEOUT_IN_SECONDS=30
$env:NUGET_PLUGIN_REQUEST_TIMEOUT_IN_SECONDS=30
</code></pre></div></div>
<p><a href="https://www.primordialcode.com/blog/post/nuget-credentialprovider-timeout">NuGet - Problem starting the plugin CredentialProvider.Microsoft (timeout)</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on June 17, 2021.</p>https://www.primordialcode.com/blog/post/akka-dot-net-1012018-03-12T00:00:00+01:002018-03-12T00:00:00+01:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>In the recent years I had a chance to work with highly concurrent systems using several development practices and patterns, like DDD, Event Sourcing, CQRS and the Actor Model, just to name a few.</p>
<p>I tested and worked with some Actor Frameworks in the .NET world: <a href="https://getakka.net/">Akk.Net</a> and <a href="http://proto.actor/">Proto.Actor</a>.</p>
<p>I used to have a private project with the source code of an Akka.Net Introductory Course I’ve elaborated over the months.</p>
<p>With the occasion of the “Un Actor (Model) per amico - Multithreading made easy” <a href="http://dev.marche.it/">DevMarche</a> User Group event we held in the last week of February, I’ve decided to “reshape” the course source code and open source it.</p>
<p>So here it is: a 4 Units Introductory Course that will guide you through most of the features Akka.Net has.</p>
<p><a href="https://github.com/PrimordialCode/akkadotnet101">Akka.Net 101</a></p>
<p>You can take on all the lessons following the tutorials, from the starting point to the completion of each unit.</p>
<p>During the course we’ll implement a simple application that can be used to test the features of the framework, showing what happens when we create, start and stop actors, when an exception is raised inside an actor and the supervision strategy kicks in.</p>
<p>We’ll also take advange of the routing mechanics to distribute the workload amongst multiple working actors; we’ll see how we can have remote process communicate (using Akka.Remote) and configure a simple cluster of nodes with Akka.Cluster.</p>
<p>The idea is to have an Open Source Couse that will possibly evolve over time, maybe taking advantage of your contributions.</p>
<p>cya next!</p>
<p><a href="https://www.primordialcode.com/blog/post/akka-dot-net-101">Akka.Net 101 - an Open Source introductory course to Akka.Net</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on March 12, 2018.</p>https://www.primordialcode.com/blog/post/journey-dotnetcore-application-types2016-07-29T00:00:00+02:002016-07-29T00:00:00+02:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>An important concept when it comes to .NET Core is Application Portability, in short where and how we are able to distribute and run our applications.</p>
<p>.NET Core has two types of applications:</p>
<ul>
<li>Portable applications.</li>
<li>Self Contained Applications.</li>
</ul>
<p>To illustrate the difference between these two let’s start creating a “multiple projects solution” to use with Visual Studio Code:</p>
<ul>
<li>Create a new folder which will contain the project hierarchy.</li>
<li>Create a “global.json” file in the folder with this content:</li>
</ul>
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
</span><span class="nl">"projects"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
</span><span class="s2">"PortableApp"</span><span class="p">,</span><span class="w"> </span><span class="s2">"SelfContainedApp"</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">}</span></code></pre></figure>
<p>This file will be used by the <a href="https://docs.microsoft.com/en-us/dotnet/articles/core/tools/dotnet">dotnet utility</a> to work on all the projects of the solution.</p>
<ul>
<li>Create two subfolders named: “PortableApp” and “SelfContainedApp” and initialize the new ‘empty’ applications executing these bash command in each folder:</li>
</ul>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">dotnet</span> <span class="kd">new</span>
<span class="kd">dotnet</span> <span class="nb">restore</span></code></pre></figure>
<p>To compile and publish the application you can issue:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">dotnet</span> <span class="kd">build</span>
<span class="kd">dotnet</span> <span class="kd">publish</span></code></pre></figure>
<h2 id="portable-applications">Portable Applications</h2>
<p>This is the default type of application and it essentially means that to be able to run it <strong>you need to have .NET Core installed on the machine</strong>.</p>
<p>A portable application is a .dll file that can be launched using the <a href="https://docs.microsoft.com/en-us/dotnet/articles/core/tools/dotnet">dotnet tool</a>.</p>
<p>Let’s look at a typical project.json file:</p>
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
</span><span class="nl">"version"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0-*"</span><span class="p">,</span><span class="w">
</span><span class="nl">"buildOptions"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"debugType"</span><span class="p">:</span><span class="w"> </span><span class="s2">"portable"</span><span class="p">,</span><span class="w">
</span><span class="nl">"emitEntryPoint"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"dependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{},</span><span class="w">
</span><span class="nl">"frameworks"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"netcoreapp1.0"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"dependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"Microsoft.NETCore.App"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"platform"</span><span class="p">,</span><span class="w">
</span><span class="nl">"version"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"imports"</span><span class="p">:</span><span class="w"> </span><span class="s2">"dnxcore50"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">}</span></code></pre></figure>
<p>You can check my previous post <a href="http://www.primordialcode.com/blog/post/journey-dotnetcore-framework-tfm-dependency">Journey to the .NET Core - Frameworks, TFM and Dependencies</a> to have some detailed information on the project.json file format.</p>
<p>Look at the <em>dependecies</em> section: any dependency marked with the property <code class="language-plaintext highlighter-rouge">"type":"platform"</code> will not be included in your publish folder, as .NET Core tooling will assume that the runtime (or any other marked library) will be already installed in the system and globally available.</p>
<p>You can only have one dependency marked with the <code class="language-plaintext highlighter-rouge">"type":"platform"</code> attribute.</p>
<p>It’s important to launch ‘dotnet restore’ every time we change the dependencies in the project.json file to let NuGet download and install any missing package, otherwise the build could fail.</p>
<p>If you build and publish the application and then look at the files that are generated you will see something like this:</p>
<p><img src="https://www.primordialcode.com/UserFiles/images/dotnetcore-application-types/01.PNG" alt="Portable Application" /></p>
<p>None of the files included in the referenced package Microsoft.NETCore.App are present, they are all part of the .NET Core library and are supposed to be generally available on the target machine.</p>
<p>Once the application is built and published you can deploy that to any machine that has the .NET Core framework installed and execute it using this command from the console:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">dotnet</span> <span class="kd">PortableApp</span>.dll</code></pre></figure>
<p>If a Portable application has some native dependencies to other libraries, it will be as portable as all of its dependencies are portable; that means that you’ll be able to run the application on any platform that those dependencies can run on.</p>
<h2 id="self-contained-applications">Self Contained Applications</h2>
<p>In this kind of application everything, including the runtime, is packaged together; this means you will be able to run it on any machine that runs an OS compatible with the one you used to build the application itself.</p>
<p>You’ll need to make it <em>explicit</em> which <em>platforms</em> you’re going to build the application for.</p>
<p>Switch over to the “SelfContainedApp” folder project; to create a Self Contained application you need to:</p>
<ul>
<li>Remove the <code class="language-plaintext highlighter-rouge">"type":"platform"</code> attribute off any dependency in the project.json file.</li>
<li>Add a <code class="language-plaintext highlighter-rouge">runtimes</code> node in the project.json that will list all the <a href="https://docs.microsoft.com/it-it/dotnet/articles/core/rid-catalog">Runtime Identifiers / RIDs</a> of the platforms to support.</li>
</ul>
<p>Here’s a modified project.json file that will produce a Self Contained Application for a Windows 10 and for a Linux Ubuntu machines:</p>
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
</span><span class="nl">"version"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0-*"</span><span class="p">,</span><span class="w">
</span><span class="nl">"buildOptions"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"debugType"</span><span class="p">:</span><span class="w"> </span><span class="s2">"portable"</span><span class="p">,</span><span class="w">
</span><span class="nl">"emitEntryPoint"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"dependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{},</span><span class="w">
</span><span class="nl">"frameworks"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"netcoreapp1.0"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"dependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"Microsoft.NETCore.App"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"version"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"imports"</span><span class="p">:</span><span class="w"> </span><span class="s2">"dnxcore50"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"runtimes"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"win10-x64"</span><span class="p">:</span><span class="w"> </span><span class="p">{},</span><span class="w">
</span><span class="nl">"ubuntu.14.04-x64"</span><span class="p">:</span><span class="w"> </span><span class="p">{}</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">}</span></code></pre></figure>
<p>If you now issue the commands:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">dotnet</span> <span class="nb">restore</span>
<span class="kd">dotnet</span> <span class="kd">build</span>
<span class="kd">dotnet</span> <span class="kd">publish</span></code></pre></figure>
<p>It will compile a version of the application that is compatible with the system you are using. To build and publish a specific platform you need to specify which RID to build with the <em>–runtime</em> switch:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">dotnet</span> <span class="nb">restore</span>
<span class="kd">dotnet</span> <span class="kd">build</span> <span class="na">--runtime </span><span class="kd">ubuntu</span>.14.04<span class="na">-x</span><span class="m">64</span>
<span class="kd">dotnet</span> <span class="kd">publish</span> <span class="na">--runtime </span><span class="kd">ubuntu</span>.14.04<span class="na">-x</span><span class="m">64</span></code></pre></figure>
<h2 id="application-deploy">Application Deploy</h2>
<p>Chosing one application type over the other will impact how you distribute the application.</p>
<p>At its core deploying the application consists in just copying the files in the ‘publish’ folder to the target machines.</p>
<p>Portable applications will require the target machines (or containers) to have the .NET Core framework already installed, so you need to configure them or use preconfigured images for those machines. Self Contained applications do not require that, because they carry everything over with them, but they need to run on the very specific OS there compiled for.</p>
<p>There’s of course a difference in the size of the applications and you’ll need to consider if that can impact your distribution transport strategy.</p>
<p><strong>cya next</strong></p>
<p><a href="https://www.primordialcode.com/blog/post/journey-dotnetcore-application-types">Journey to the .NET Core - Application Types & Distribution Strategies</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on July 29, 2016.</p>https://www.primordialcode.com/blog/post/journey-dotnetcore-framework-tfm-dependency2016-07-20T00:00:00+02:002016-07-20T00:00:00+02:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>When you deal with .NET projects you should keep the following concepts in mind:</p>
<ul>
<li>Framework: it’s a stand alone implementation exposing a well-defined Runtime environment, an API and utility functions. Several versions of the .NET framework exist each or them adding new feature and breaking changes over the previous one.</li>
<li><a href="https://docs.nuget.org/create/targetframeworks">Target Framework Moniker</a>: it’s a short name to identify a specific version of the framework.</li>
<li>Dependency: with this term we typically intend a part of the features exposed by the framework (or by a library) and packaged together as a whole. The package is then made available with a package manager (like: <a href="https://www.nuget.org/">NuGet</a>).</li>
</ul>
<p>Let’s start by creating an empty project using the Command Line tool, open up a console, create a new folder for the project and launch this command:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">dotnet</span> <span class="kd">new</span></code></pre></figure>
<p>Now look at the project.json file that was created for us:</p>
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
</span><span class="nl">"version"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0-*"</span><span class="p">,</span><span class="w">
</span><span class="nl">"buildOptions"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"debugType"</span><span class="p">:</span><span class="w"> </span><span class="s2">"portable"</span><span class="p">,</span><span class="w">
</span><span class="nl">"emitEntryPoint"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"dependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{},</span><span class="w">
</span><span class="nl">"frameworks"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"netcoreapp1.0"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"dependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"Microsoft.NETCore.App"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"platform"</span><span class="p">,</span><span class="w">
</span><span class="nl">"version"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"imports"</span><span class="p">:</span><span class="w"> </span><span class="s2">"dnxcore50"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">}</span></code></pre></figure>
<p>This file will contain some project metadata and information on all the frameworks we would use to compile and run the application alongside with all the references to any dependency packages that are needed by the application to work.</p>
<p>The complete reference to this file can be found here: <a href="https://docs.microsoft.com/it-it/dotnet/articles/core/tools/project-json">project.json reference</a> and here: <a href="https://docs.nuget.org/consume/projectjson-format">NuGet project.json format</a></p>
<p>The most important bits are:</p>
<p><strong>frameworks</strong>: it’s a json object that specifies the frameworks supported by this project, they must be valid <a href="https://docs.nuget.org/create/targetframeworks">Target Framework Monikers</a>; here we have selected “netcoreapp1.0” which means we are targeting the 1.0 release of .NET Core. To target multiple frameworks just add them above or under “netcoreapp1.0”.</p>
<p>Some of the most commonly used frameworks (up to this date are):</p>
<ul>
<li>“netcoreapp1.0” For .NET Core 1.0.</li>
<li>“net45”, “net451”, “net452”, “net46”, “net461” for .NET Framework versions.</li>
<li>“portable-net45+win8” for Portable Class Library.</li>
<li>“netstandard1.0”, … “netstandard1.6” for the <a href="https://github.com/dotnet/corefx/blob/master/Documentation/architecture/net-platform-standard.md">.NET Platform Standard</a>.</li>
</ul>
<p><strong>dependencies</strong>: it’s a json object that’s used to specify all the ‘global’ dependencies of the project. A dependency is usually identified by a name and a version tag. The packages specified here are meant to be available and used by all the different target frameworks (you should specify the minimum package version that can be used by all the different runtime). All the dependencies declared in this section refer to NuGet packages. If you need to use a package specifically designed for a particular framework you need to include it in the “dependencies” section of the framework object itself.</p>
<p><strong>frameworks/TFM/imports</strong>: this setting is useful if you need to be able to reference and use packages compiled with previous versions of the framework (like preview or RC versions). “dnxcore50” in the example above refers to the preview releases of .NET Core.</p>
<p><strong>frameworks/TFM/dependencies</strong>: this json object is used to specify all the dependencies that are specific to this version of the framework and that cannot be used by other runtime.</p>
<p>Let’s use our NuGet package manager and ‘restore’ our dependencies, from the project root folder execute this command:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">dotnet</span> <span class="nb">restore</span></code></pre></figure>
<p>This command will check your dependency tree, it will download any missing package from the configured NuGet feeds and in the end it will create a new file named ‘project.lock.json’. This file will contain an exploded version of all the packages used by the application, together with the information needed by the build and runtime system to find those files in global location where they are downloaded by NuGet.</p>
<p>To build and publish you project for the specified target framework you can issue the following commands:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">dotnet</span> <span class="kd">build</span>
<span class="kd">dotnet</span> <span class="kd">publish</span></code></pre></figure>
<p>If you decide to support multiple frameworks you can use Visual Studio or the Command Line tool to decide for which of them compile and run the application.</p>
<p>Here’s the same project.json file slightly modified to also support the .NET Framework 4.6 (the full framework):</p>
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
</span><span class="nl">"version"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0-*"</span><span class="p">,</span><span class="w">
</span><span class="nl">"buildOptions"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"debugType"</span><span class="p">:</span><span class="w"> </span><span class="s2">"portable"</span><span class="p">,</span><span class="w">
</span><span class="nl">"emitEntryPoint"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"dependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{},</span><span class="w">
</span><span class="nl">"frameworks"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"netcoreapp1.0"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"dependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"Microsoft.NETCore.App"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"platform"</span><span class="p">,</span><span class="w">
</span><span class="nl">"version"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"imports"</span><span class="p">:</span><span class="w"> </span><span class="s2">"dnxcore50"</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"net461"</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"dependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">}</span></code></pre></figure>
<p>When you execute the same build and publish commands saw before, the dotnet Command Line tool will compile a different version of the application for each framework you decided to support.</p>
<p>Here’s how the folder structure will look like:</p>
<p><img src="https://www.primordialcode.com/UserFiles/images/dotnetcore-framework-tfm-dependency/01.PNG" alt="Image" /></p>
<p>You can see specific folders for each target framework (they have the very same name) and the related nested publish folder.</p>
<p>If you want compile and publish just a single version of the application you can do that using the <em>-f</em> switch from the command line specifying which TFM to use:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">dotnet</span> <span class="kd">build</span> <span class="na">-f </span><span class="kd">net461</span>
<span class="kd">dotnet</span> <span class="kd">publish</span> <span class="na">-f </span><span class="kd">net461</span></code></pre></figure>
<p>This command will compile only the .NET Framewrok 4.6 version of the application.</p>
<p>For more information on the command line available options you can issue these commands</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">dotnet</span> <span class="na">--help
</span><span class="kd">dotnet</span> <span class="nb">restore</span> <span class="na">--help
</span><span class="kd">dotnet</span> <span class="kd">build</span> <span class="na">--help
</span><span class="kd">dotnet</span> <span class="kd">publish</span> <span class="na">--help</span></code></pre></figure>
<p>Edit:
To select which version of the application to run, you can issue the following command from your project folder:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">dotnet</span> <span class="nb">run</span> <span class="na">-f </span><span class="kd">net461</span></code></pre></figure>
<p>if you just type “dotnet run” it will pick the first framework of the list.</p>
<p>That’s all for now!</p>
<p><strong>cya next</strong></p>
<p>Additional Resources:</p>
<ul>
<li><a href="https://docs.microsoft.com/it-it/dotnet/articles/core/tools/project-json">project.json reference</a></li>
<li><a href="https://docs.nuget.org/consume/projectjson-format">NuGet project.json format</a></li>
<li><a href="https://github.com/dotnet/corefx/blob/master/Documentation/architecture/net-platform-standard.md">.NET Platform Standard</a></li>
<li><a href="https://blogs.msdn.microsoft.com/cesardelatorre/2016/06/28/running-net-core-apps-on-multiple-frameworks-and-what-the-target-framework-monikers-tfms-are-about/">Running .NET Core apps on multiple frameworks and What the Target Framework Monikers (TFMs) are about</a></li>
</ul>
<p><a href="https://www.primordialcode.com/blog/post/journey-dotnetcore-framework-tfm-dependency">Journey to the .NET Core - Frameworks, TFM and Dependencies</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on July 20, 2016.</p>https://www.primordialcode.com/blog/post/journey-dotnetcore-dev-env-setup2016-07-14T00:00:00+02:002016-07-14T00:00:00+02:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>On the journey to learn .NET Core our entry point should be this link:
<a href="https://dotnet.github.io/">.NET Core</a></p>
<p>There we’ll find some very easy to follow tutorials on how to install it and get started using our OS of choice.</p>
<p>To start developing your .NET Core application on a Windows machine we basically have two choices:</p>
<ul>
<li>The Command Line tool (dotnet.exe) and our favorite editor - this approach is very lightweight and useful for small projects and on systems that do not have Visual Studio.</li>
<li>Visual Studio 2015 (and up) - if you like to use a fully featured IDE loaded with tools and facilities.</li>
</ul>
<p><strong>Command Line and Visual Studio Code</strong></p>
<p>If you opted for the Command Line tools and a text editor, a good candidate is Visual Studio Code. It is available on all major Operating Systems; here’s what’s needed to install:</p>
<ul>
<li><a href="https://go.microsoft.com/fwlink/?LinkID=809122">.NET Core SDK for Windows</a></li>
<li><a href="https://code.visualstudio.com/">Visual Studio Code</a></li>
</ul>
<p>You can also add some Visual Studio Code extensions to improve your coding experience (you can install them directly from within Visual Studio Code using the “ext install <em>extensionname</em>” command, or the brand new Extensions tab). I usually add:</p>
<ul>
<li><a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp">C#</a></li>
<li><a href="https://marketplace.visualstudio.com/items?itemName=eg2.tslint">TSLint</a></li>
<li><a href="https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome">Debugger for Chrome</a></li>
<li><a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell">Powershell</a></li>
</ul>
<p>To start a new .NET Core Project we will need to setup the solution structure almost all manually:</p>
<ol>
<li>Create an empty “dotnetcore” folder, this will be our solution folder.</li>
<li>Inside that directory create an “src” folder, it will contain all our projects.</li>
<li>Create a “global.json” file, it will be used by the command line tool to gather metadata and configurations, at its minimum this file should contain something like:</li>
</ol>
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
</span><span class="nl">"projects"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"src"</span><span class="p">,</span><span class="w"> </span><span class="s2">"test"</span><span class="p">]</span><span class="w">
</span><span class="p">}</span></code></pre></figure>
<ol>
<li>Create another folder named “ConsoleApp” inside “src” and open up a Command Prompt at that location; execute these commands:</li>
</ol>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">dotnet</span> <span class="kd">new</span>
<span class="kd">dotnet</span> <span class="nb">restore</span>
<span class="kd">dotnet</span> <span class="kd">build</span>
<span class="kd">dotnet</span> <span class="nb">run</span></code></pre></figure>
<p>A new empty .NET Core Console Project will be created, nuget packages will be restored, then the project will be compiled and executed.</p>
<p>Two new files will be created for us:</p>
<ul>
<li>“project.json”: it will contain references to the runtime and framework version and any dependencies we’ll later add to the project along with other metadata.</li>
<li>“program.cs”: it will act as the starting point of the application itself.</li>
</ul>
<p>If you open up the folder in Visual Studio Code, it will automatically recognize this as a .NET Core project and it will create all the support files and settings needed by the editor to be able to build, debug and run the project.</p>
<p><strong>Visual Studio 2015</strong></p>
<p>If you want to use Visual Studio 2015 to author your projects and have a pleasant development and debugging experience you have to install the following updates:</p>
<ul>
<li><a href="https://www.visualstudio.com/news/releasenotes/vs2015-update3-vs">Visual Studio 2015 Update 3</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkId=817245">.NET Core for Visual Studio</a></li>
</ul>
<p>These two packages will add all the editor extensions and templates needed to manage the new projects created for .NET Core.</p>
<p>To start a new .NET Core project using Visual Studio 2015:</p>
<ol>
<li>Launch Visual Studio 2015</li>
<li>Select “File” -> “New Project”</li>
<li>Navigate to “Templates \ Visual C# \ .NET Core”</li>
</ol>
<p><img src="https://www.primordialcode.com/UserFiles/images/dotnetcore-dev-env-setup/01.PNG" alt="Visual STudio 2015 new .NET Core project" /></p>
<p>Select “Console Application (.NET Core)” project, name it “DotNetCoreConsole” and hit “OK”.
The complete solution structure will be created for you:</p>
<p><img src="https://www.primordialcode.com/UserFiles/images/dotnetcore-dev-env-setup/02.PNG" alt="Visual STudio 2015 - Solution Structure" /></p>
<p>If you navigate there, you will find some of the files we previously talked about (global.json and project.json) and some new file specifically designed to work with Visual Studio:</p>
<ul>
<li>DotNetCoreConsole.sln - the usual Visual Studio solution definition file.</li>
<li>DotNetCoreConsole.xproj - this is the new project file that will contain Visual Studio specific settings when it comes to deal with .NET Core projects.</li>
</ul>
<p>We will look at the content of “global.json” and “project.json” in more detail in a future post.</p>
<p><strong>cya next</strong></p>
<p><a href="https://www.primordialcode.com/blog/post/journey-dotnetcore-dev-env-setup">Journey to the .NET Core - Windows Development Environment Setup</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on July 14, 2016.</p>https://www.primordialcode.com/blog/post/migrate-svn-to-git2016-07-01T00:00:00+02:002016-07-01T00:00:00+02:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>I know there are plenty of articles on the subject, but I needed to keep track how I did it. To migrate my current <a href="https://subversion.apache.org/">SVN</a> repositories to <a href="https://git-scm.com">Git</a> I started to look around for some documentation and ended up with this process in mind:</p>
<ul>
<li>Clone the SVN repository to a local Git Repository while filtering the data and converting the users.</li>
<li>Create a Remote (bare) Git repository that will be my new ‘master’ repository.</li>
<li>Choose which branches of the local repository push to the new remote Git repository.</li>
</ul>
<p>These are the steps I followed:</p>
<p><strong>Step 1 - Install</strong></p>
<p>Install <a href="https://git-scm.com/">Git</a> if you do not have it already.</p>
<p><strong>Step 2 - Map the users</strong></p>
<p>Create an ‘authors.txt’ file that will be used to map your SVN users to Git users; it’s just a plain text file that will look like this:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">SVN_Username</span> <span class="o">=</span> <span class="kd">Git_Username</span> <span class="o"><</span><span class="kd">user</span>@mail.com>
...</code></pre></figure>
<p><strong>Step 3 - SVN clone</strong></p>
<p>Clone the SVN repository with <a href="https://git-scm.com/docs/git-svn">git svn</a>:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">git</span> <span class="kd">svn</span> <span class="kd">clone</span> <span class="na">--stdlayout --authors-file</span><span class="o">=</span><span class="kd">authors</span>.txt <span class="na">--ignore-paths</span><span class="o">=</span><span class="s2">"packages/|packages$"</span> <span class="na">--prefix</span><span class="o">=</span><span class="s2">"svn/"</span> <span class="kd">http</span>://your.svn/repo <span class="kd">LocalGitFolder</span></code></pre></figure>
<p>The parameters used are:</p>
<ul>
<li>stdLayout: use this if you are using the standard SVN folder layout.</li>
<li>authors-file=authors.txt: you need this file to map your users.</li>
<li>ignore-paths=”packages/|packages$”: regex that specify which paths you’ll like to ignore (I am ignoring every path that contains the chars ‘packages/’ or ends wit the word ‘packages’.</li>
<li>prefix=”svn/”: setup a prefix that will be used to map your SVN remotes (otherwise ‘origin/’ will be used).</li>
</ul>
<p><strong>Step 4 - Checkout branches</strong></p>
<p>List all the available branches and choose the ones you want to push to your new remote Git repository:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">git</span> <span class="kd">branch</span> <span class="na">-a</span></code></pre></figure>
<p>You will see a brand new and shiny ‘master’ branch (a local branch) and some remote branches (coloured in red) named something like: remotes/svn/trunk, remotes/svn/branches, etc…</p>
<p>By default the master branch will point to your SVN trunk branch; the others (the remotes/svn/something) are all your other branches and tags you had in your SVN repository.</p>
<p>To choose which of them you want to push to the remote Git repository you have to checkout each and every one of them:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">git</span> <span class="kd">checkout</span> <span class="kd">svn</span><span class="na">/branchName
</span><span class="kd">git</span> <span class="kd">checkout</span> <span class="na">-b </span><span class="kd">svn</span><span class="na">/branchName </span></code></pre></figure>
<p>These commands do two things: the first checkout a new detached branch and the second creates a new branch at that very same point so you can push them.</p>
<p>If you want to import all of them, you can issue this commands to the Git bash:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="k">for</span> <span class="kd">remote</span> <span class="k">in</span> <span class="sb">`git branch -r`</span><span class="o">;</span> <span class="k">do</span> <span class="kd">git</span> <span class="kd">checkout</span> $remote<span class="o">;</span> <span class="kd">git</span> <span class="kd">checkout</span> <span class="na">-b </span>$remote<span class="o">;</span> <span class="kd">done</span></code></pre></figure>
<p>If you now list all your branches again, your will see some more local branches (in white).</p>
<p>You can push to a remote Git repository only your local branches, not the ‘remote/svn’ ones.</p>
<p><strong>Step 4 [alternative] - there’s also another way</strong></p>
<p>There’s also another way to deal with the remote <em>svn branches</em> and <em>svn tags</em> and how to ‘change’ them so they will appear as proper Git Branches and Git Tags.</p>
<p>Take a look at the page <a href="https://git-scm.com/book/it/v2/Git-and-Other-Systems-Migrating-to-Git">Git and Other Systems - Migrating to Git</a> of the Git Documentation.</p>
<p><strong>Step 5 - Push</strong></p>
<p>Create your new remote Git repository, in my case I decided to use <a href="https://www.visualstudio.com/products/visual-studio-team-services-vs.aspx">Visual Studio Team Services</a>, and attach it as a remote to the local Git repository (I used ‘vsts’ as a name for the remote reference) and push your local commits to it:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">git</span> <span class="kd">remote</span> <span class="kd">add</span> <span class="kd">vsts</span> <span class="kd">https</span>://myAccount.visualstudio.com/DefaultCollection/myColl/_git/myRepo
<span class="kd">git</span> <span class="kd">push</span> <span class="kd">vsts</span> <span class="na">--mirror -u </span></code></pre></figure>
<p>Done!</p>
<p><strong>Step 6 - bonus: keep it in sync</strong></p>
<p>If you want to keep the things in sync for a while and keep using your old SVN repository while experimenting and getting familiar with Git, you can issue the following commands to the Git bash:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">git</span> <span class="kd">svn</span> <span class="kd">fetch</span>
<span class="kd">git</span> <span class="kd">rebase</span> <span class="kd">remotes</span><span class="na">/svn/trunk </span><span class="kd">svn</span><span class="na">/trunk
</span><span class="kd">git</span> <span class="kd">rebase</span> <span class="kd">remotes</span><span class="na">/svn/trunk </span><span class="kd">master</span>
<span class="kd">git</span> <span class="kd">push</span> <span class="kd">vsts</span></code></pre></figure>
<p>With these commands you will:</p>
<ul>
<li>Update all your local ‘remotes/svn’ branches.</li>
<li>Merge (ok it’s a rebase, I know, <a href="https://git-scm.com/docs/git-rebase">check the docs</a>) all the new commits to your current ‘master’ and the imported ‘svn/trunk’ branches.</li>
<li>Push the changes to the remote repository.</li>
</ul>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/migrate-svn-to-git">Migrate from SVN to GIT</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on July 01, 2016.</p>https://www.primordialcode.com/blog/post/typescript-visualstudio-msbuild-conditional-compilation2016-01-12T00:00:00+01:002016-01-12T00:00:00+01:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>Conditional Compilation is not yet available in TypeScript, some requests and proposals have been made:</p>
<ul>
<li><a href="https://github.com/Microsoft/TypeScript/issues/449">Support conditional compilation</a></li>
<li><a href="https://github.com/Microsoft/TypeScript/issues/3538">Proposal: Conditional Compilation</a></li>
<li><a href="https://github.com/Microsoft/TypeScript/issues/4691">Proposal: Preprocessor Directives</a></li>
</ul>
<p>If you are using Visual Studio and MSBuild there’s however a very simple workaround to completely exclude some TypeScript files from being compiled.</p>
<p>The solution is to use <a href="https://msdn.microsoft.com/en-us/library/7szfhaft.aspx">MSBuild Conditions</a> and/or <a href="https://msdn.microsoft.com/en-us/library/ms164307.aspx">MSBuild Conditional Constructs</a>, it all depends on how complex your conditional logic will be.</p>
<p>In the simplest case you just have to open your .csproj (or .vbproj) file in a text editor and add the proper <strong>Condition</strong> attribute to the TypeScriptCompile entry; something like:</p>
<figure class="highlight"><pre><code class="language-xml" data-lang="xml">...
<span class="nt"><TypeScriptCompile</span> <span class="na">Condition=</span><span class="s">"'$(CONFIG)'=='DEBUG'"</span> <span class="na">Include=</span><span class="s">"UiComponentFeature\UiComponent.ts"</span> <span class="nt">/></span>;
...</code></pre></figure>
<p>With code like this we will compile that TypeScript file only if the DEBUG symbol has been defined for the current build configuration; that’s all we really need to do!</p>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/typescript-visualstudio-msbuild-conditional-compilation">TypeScript - Visual Studio / MSBuild conditional compilation</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on January 12, 2016.</p>https://www.primordialcode.com/blog/post/incredibuild-first-look2015-12-17T00:00:00+01:002015-12-17T00:00:00+01:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>Visual Studio 2015 Update 1 come with a new <em>free</em> bonus: You can download and install a new version of <a href="http://www.incredibuild.com">IncrediBuild</a>.</p>
<p>If you install it through Visual Studio you’ll get a version of IncrediBuild specifically designed for the environment that will allow you to keep using 8 cores for parallel builds even when the trial license expires (normally it will be limited to 4 cores).</p>
<p>To install it use the “File \ New \ Project…” menu, then look for a link called “Build Accelerators”, inside it you’ll find the template used to install IncrediBuild.</p>
<p>Once you ‘open’ the link, the installation process starts; you will presented with a few choises:</p>
<p><img src="https://www.primordialcode.com/UserFiles/images/incredibuild-first-look/1.PNG" alt="IncrediBuild Setup 1" /></p>
<p>Let’s install it on our local machine selecting option 1: “Activate IncrediBuild locally”.</p>
<p>After that you will be be offered the option to request a free license, do that if you don’t already own one.</p>
<p><img src="https://www.primordialcode.com/UserFiles/images/incredibuild-first-look/2.PNG" alt="IncrediBuild Setup 2" /></p>
<p>Filling in the form will result in an e-mail containing the license file you’ll need to use in order to enable the software</p>
<p><img src="https://www.primordialcode.com/UserFiles/images/incredibuild-first-look/3.PNG" alt="IncrediBuild Setup 3" /></p>
<p>The disclaimer states it’s free for life; however the licenses details states it’s a 30 days trial (for the full featured version) and a 1 year licence for all the rest. Once the trial period expires you can keep using the product with some limitations in single machine mode (take a look at the official <a href="https://www.incredibuild.com/microsoft-incredibuild-partnership.html">IncrediBuild</a> website for more information).</p>
<p>Once installed you are ready to use it.</p>
<p>Now you can choose to go with two different scenarios:</p>
<ul>
<li>Use multiple (already configured) helpers machines to assist you during the build process.</li>
<li>Go for a single machine installation and fine tune the settings for it.</li>
</ul>
<p>Let’s start with this second option, which is easier to start with if you are using IncrediBuild for the first time (like me).</p>
<p>Open the IncrediBuild Agent Settings.
Go to the ‘Initiator \ General’ tab and check ‘Enable Standalone mode’.</p>
<p><img src="https://www.primordialcode.com/UserFiles/images/incredibuild-first-look/4.PNG" alt="IncrediBuild Setup 4" /></p>
<p>Now swtich to the ‘Visual Studio Builds \ Advanced’ tab and:</p>
<ul>
<li>Check “Predictive Execution - enanche throughtput using out-of-order tasks spawing”.</li>
<li>Uncheck “PDB File Allocation - Limit concurrent PDB”.</li>
</ul>
<p><img src="https://www.primordialcode.com/UserFiles/images/incredibuild-first-look/5.PNG" alt="IncrediBuild Setup 5" /></p>
<p>Good to go, click on the top menu: “IncrediBuild - Build Solution” to do the dirty job!</p>
<p>You will get a report like this:</p>
<p><img src="https://www.primordialcode.com/UserFiles/images/incredibuild-first-look/6.PNG" alt="IncrediBuild Report" /></p>
<p>You can now start using this insight look (it’s always useful to have a graphical view of what is happening) to hunt for compilation bottlenecks and possible ways to refactor your code; as an example: start giving a look at projects that cannot be built in parallel early in the compilation chain, you can possibly refactor or relocate your code in order to improve the overrall build time.</p>
<p>The rule of thumb is always to keep the dependecies among projects as low as possible, limiting the chain lenght in order to favor parallelism.</p>
<p>After playing a bit with the tool I was able cut the total compilation time by 10-15%, not that bad!</p>
<p>A deeper look at the report shows an initial ‘preparing build task’ period which is quite long (around 10s or more); I assume that this is the setup phase of IncrediBuild which tries to reach its compilation ‘helpers’ or creates a plan on how to parallelize the building tasks. However since I setup for ‘Standalone mode’ I expected this step to be shorter.</p>
<p>Once I refactored my code I switched back to the usual Visual Studio compilation pipeline (which does some parallelized compilation itself) and I found out it to be faster, mostly due to not having to deal with IncrediBuild initial setup task.</p>
<p>After using it for a couple of hours I have the strong feeling that on a single machine - with a reasonably limited number of projects in your solution - you can use its graphical reports to tweak your compilation at start, then switch back to the usual compilation pipeline.</p>
<p>I’ll test it in a multi machine environment, in this scenario I expect it to have a bigger impact on the overral compilation experience (especially in case of build automation).</p>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/incredibuild-first-look">A first look at IncrediBuild</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on December 17, 2015.</p>https://www.primordialcode.com/blog/post/pygments-jekyll-build-fail2015-12-09T00:00:00+01:002015-12-09T00:00:00+01:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>Jekyll on GitHub uses liquid tags and <a href="http://pygments.org/">Pygments</a> as its default syntax highlighter.
It generally works great, but sometimes it can cause troubles to the Jekyll build process, especially if you mistype the language it should output.</p>
<p>This is the syntax you should use:</p>
<p>{% highlight languageTag %}
… your code goes here …
{% endhighlight %}</p>
<p>The problem is: <strong>if you mistype the ‘languageTag’ chances are that Jekyll is not able to build the site anymore</strong> and the returning message from GitHub isn’t of much help;
in the emails sent to me I could read:</p>
<blockquote>
<p>“The page build failed with the following error:
Page build failed. For more information, see https://help.github.com/articles/troubleshooting-github-pages-build-failures.
If you have any questions you can contact us by replying to this email.”</p>
</blockquote>
<p>A little bit more details would have been appreciated.</p>
<p>It took me some time of investigation, trials and errors to spot the problem, because I wasn’t using pygments in my local Jekyll installation.</p>
<p>To test any change I don’t use ‘Pygments’, but ‘Rouge’ as my syntax highlighter (I don’t like the idea to install Pyton just to have this one running).
It turns out that if you mistype the language tag, Rouge doesn’t complain with an error.</p>
<p>For a complete list of the languages supported by Pygments - and their short name tag - you can look at this <a href="http://pygments.org/docs/lexers/">page</a>.</p>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/pygments-jekyll-build-fail">Jekyll - Pygments, mistyping the syntax highlight language tag results in blog build fail</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on December 09, 2015.</p>https://www.primordialcode.com/blog/post/fluent-interface-class-hierarchy2015-12-04T00:00:00+01:002015-12-04T00:00:00+01:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>Do you like to have <a href="https://en.wikipedia.org/wiki/Fluent_interface">fluent interfaces</a> in your code because they make it more readable?</p>
<p>Do you have a ‘deep’ class hierarchy and you’d like to have a fluent interface without the need of always returning the base class type? and possibly avoid all the cast(s) in the methods’ chain?</p>
<p>Here’s the standard way of approaching Fluent Interfaces with inherited classes in C#:</p>
<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><span class="k">public</span> <span class="k">class</span> <span class="nc">Calculator</span>
<span class="p">{</span>
<span class="k">public</span> <span class="n">Calculator</span> <span class="nf">Add</span><span class="p">()</span>
<span class="p">{</span>
<span class="c1">// do the job</span>
<span class="k">return</span> <span class="k">this</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">class</span> <span class="nc">ScientificCalculator</span> <span class="p">:</span> <span class="n">Calculator</span>
<span class="p">{</span>
<span class="k">public</span> <span class="n">ScientificCalculator</span> <span class="nf">Sin</span><span class="p">()</span>
<span class="p">{</span>
<span class="c1">// do the job</span>
<span class="k">return</span> <span class="k">this</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">class</span> <span class="nc">FluentInterfacesAndClassHierarchies</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">void</span> <span class="nf">FluentInterface_And_ClassHierarchy</span><span class="p">()</span>
<span class="p">{</span>
<span class="kt">var</span> <span class="n">scientificCalculator</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">ScientificCalculator</span><span class="p">();</span>
<span class="n">scientificCalculator</span>
<span class="p">.</span><span class="nf">Add</span><span class="p">();</span>
<span class="c1">// .Sin(); // you cannot call this method here! the return value of Add() is a normal Calculator!</span>
<span class="c1">// You will need to cast, and that will break the chain!</span>
<span class="p">}</span>
<span class="p">}</span></code></pre></figure>
<p>As you can see it just doesn’t work out of the box! You can overcome the issue using <em>extension methods</em>, here’s how:</p>
<figure class="highlight"><pre><code class="language-csharp" data-lang="csharp"><span class="k">public</span> <span class="k">class</span> <span class="nc">Calculator</span>
<span class="p">{</span>
<span class="k">public</span> <span class="n">Calculator</span> <span class="nf">Add</span><span class="p">()</span>
<span class="p">{</span> <span class="p">...</span> <span class="n">same</span> <span class="k">as</span> <span class="n">above</span> <span class="p">...</span> <span class="p">}</span>
<span class="c1">// we change the implementation to internal and we add an extension method.</span>
<span class="k">internal</span> <span class="n">Calculator</span> <span class="nf">internalSubtract</span><span class="p">()</span>
<span class="p">{</span>
<span class="c1">// do the job</span>
<span class="k">return</span> <span class="k">this</span><span class="p">;</span> <span class="c1">// actually there's no real need to return the instance of the object here, it will be handled in the Extension Method below.</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="c1">/// <summary></span>
<span class="c1">/// Extension Methods for Fluent Interfaces to the rescue</span>
<span class="c1">/// </summary></span>
<span class="k">public</span> <span class="k">static</span> <span class="k">class</span> <span class="nc">CalculatorFluentInterface</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">static</span> <span class="n">T</span> <span class="n">Subtract</span><span class="p"><</span><span class="n">T</span><span class="p">>(</span><span class="k">this</span> <span class="n">T</span> <span class="n">calculator</span><span class="p">)</span> <span class="k">where</span> <span class="n">T</span> <span class="p">:</span> <span class="n">Calculator</span>
<span class="p">{</span>
<span class="n">calculator</span><span class="p">.</span><span class="nf">internalSubtract</span><span class="p">();</span>
<span class="k">return</span> <span class="n">calculator</span><span class="p">;</span> <span class="c1">// this is the trick!</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">class</span> <span class="nc">ScientificCalculator</span> <span class="p">:</span> <span class="n">Calculator</span>
<span class="p">{</span> <span class="p">...</span> <span class="n">same</span> <span class="k">as</span> <span class="n">above</span> <span class="p">...</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">class</span> <span class="nc">FluentInterfacesAndClassHierarchies</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">void</span> <span class="nf">FluentInterface_And_ClassHierarchy</span><span class="p">()</span>
<span class="p">{</span>
<span class="kt">var</span> <span class="n">scientificCalculator</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">ScientificCalculator</span><span class="p">();</span>
<span class="n">scientificCalculator</span>
<span class="p">.</span><span class="nf">Subtract</span><span class="p">()</span> <span class="c1">// Hell Yeah! Type inference guarantees that the return value of the extension method is the original type.</span>
<span class="p">.</span><span class="nf">Sin</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">}</span> </code></pre></figure>
<p>The trick is returning the same instance passed in as the ‘this’ argument in the extension method (instead of using the return value of the internal object method, which will return the wrong base type); that will have the correct type to let you go on with the method chaining.</p>
<p>Surely defining the api in this way is not that pretty, but that will be hidden inside your library and the users will be happy!</p>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/fluent-interface-class-hierarchy">Fluent Interface and Class Hierarchy in C#</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on December 04, 2015.</p>https://www.primordialcode.com/blog/post/angular-typescript-bindToController2015-11-24T00:00:00+01:002015-11-24T00:00:00+01:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>One of my best practices when creating new Angular components using TypeScript is to use classes when defining Service(s) and Controller(s) and to use the ‘controllerAs’ syntax for the bindings.</p>
<p>You can take a look at the material from my session at the Italian AngularConf2015 in the previous posts to have a better idea of what I mean. Some code samples are also available to better illustrate the matter.</p>
<p>There’s however a problem when it comes to the ‘controllerAs’ syntax and directives that define an isolated scope and use properties on that scope to bind values from the outside world.
When using controllerAs Angular ‘hides’ the $scope object and binds directly to the properties of the controller, the $scope is however still there and you can define properties on it like this:</p>
<figure class="highlight"><pre><code class="language-js" data-lang="js"><span class="kd">class</span> <span class="nx">controller</span> <span class="p">{</span>
<span class="kr">public</span> <span class="nx">test</span><span class="p">:</span> <span class="nx">string</span> <span class="c1">// a property on the controller</span>
<span class="p">}</span>
<span class="kd">function</span> <span class="nx">directive</span><span class="p">():</span> <span class="nx">ng</span><span class="p">.</span><span class="nx">IDirective</span> <span class="p">{</span>
<span class="k">return</span> <span class="o"><</span><span class="nx">ng</span><span class="p">.</span><span class="nx">IDirective</span><span class="o">></span><span class="p">{</span>
<span class="na">controller</span><span class="p">:</span> <span class="dl">"</span><span class="s2">controller</span><span class="dl">"</span><span class="p">,</span>
<span class="na">controllerAs</span><span class="p">:</span> <span class="dl">"</span><span class="s2">vm</span><span class="dl">"</span><span class="p">,</span>
<span class="c1">// an isolated scope!</span>
<span class="na">scope</span><span class="p">:</span> <span class="p">{</span>
<span class="na">test</span><span class="p">:</span> <span class="dl">"</span><span class="s2">=</span><span class="dl">"</span> <span class="c1">// a property of the scope</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span></code></pre></figure>
<p>This is the usual way you have to define a two-way binding on the ‘test’ property with the outside boundary of the directive.</p>
<p>You can run the code and using a tool like <a href="https://chrome.google.com/webstore/detail/angularjs-batarang/ighdmehidhipcmcojjgiloacoafjmpfk">Batarang</a> or <a href="http://ng-inspector.org/">ng-inpector</a> you can easily verify that you’ll end up having 2 distinct copies of the ‘test’ property: one on the controller and one on the $scope. Both being out of sync.</p>
<p>One way to overcome this issue is to use the <a href="https://docs.angularjs.org/api/ng/service/$compile">bindToController</a> property:</p>
<figure class="highlight"><pre><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">directive</span><span class="p">():</span> <span class="nx">ng</span><span class="p">.</span><span class="nx">IDirective</span> <span class="p">{</span>
<span class="k">return</span> <span class="o"><</span><span class="nx">ng</span><span class="p">.</span><span class="nx">IDirective</span><span class="o">></span><span class="p">{</span>
<span class="na">controller</span><span class="p">:</span> <span class="dl">"</span><span class="s2">controller</span><span class="dl">"</span><span class="p">,</span>
<span class="na">controllerAs</span><span class="p">:</span> <span class="dl">"</span><span class="s2">vm</span><span class="dl">"</span><span class="p">,</span>
<span class="c1">// an isolated scope!</span>
<span class="na">scope</span><span class="p">:</span> <span class="p">{</span>
<span class="na">test</span><span class="p">:</span> <span class="dl">"</span><span class="s2">=</span><span class="dl">"</span> <span class="c1">// a property of the scope</span>
<span class="p">},</span>
<span class="na">bindToController</span><span class="p">:</span> <span class="kc">true</span>
<span class="p">}</span>
<span class="p">}</span></code></pre></figure>
<p>With this declaration the properties on the controller class will be kept in sync with the related properties on the underlying $scope object.</p>
<p>Good practice: <strong>if you use a TypeScript class to define the controller of a directive, don’t forget to set ‘bindToController: true’ when using the ‘controllerAs’ syntax.</strong></p>
<p>Angular 1.4 supports an extended syntax for the ‘bindToController’ property that allow us to specify the binding information using an object literal:</p>
<figure class="highlight"><pre><code class="language-js" data-lang="js"><span class="kd">function</span> <span class="nx">directive</span><span class="p">():</span> <span class="nx">ng</span><span class="p">.</span><span class="nx">IDirective</span> <span class="p">{</span>
<span class="k">return</span> <span class="o"><</span><span class="nx">ng</span><span class="p">.</span><span class="nx">IDirective</span><span class="o">></span><span class="p">{</span>
<span class="na">controller</span><span class="p">:</span> <span class="dl">"</span><span class="s2">controller</span><span class="dl">"</span><span class="p">,</span>
<span class="na">controllerAs</span><span class="p">:</span> <span class="dl">"</span><span class="s2">vm</span><span class="dl">"</span><span class="p">,</span>
<span class="na">scope</span><span class="p">:</span> <span class="p">{},</span> <span class="c1">// an isolated scope!</span>
<span class="na">bindToController</span><span class="p">:</span> <span class="p">{</span>
<span class="na">test</span><span class="p">:</span> <span class="dl">"</span><span class="s2">=</span><span class="dl">"</span> <span class="c1">// a property of the scope</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span></code></pre></figure>
<p>This is important because bindToController has been extended to be used with every kind of directive that introduce a new scope (even the non isolated ones).</p>
<p>My Best Practice: <strong>when defining a directive that uses an isolate scope, always specify the bindable properties using the extensive ‘bindToController’ syntax.</strong></p>
<p>This article was based on:</p>
<ul>
<li>AngularJs version 1.4+</li>
<li>TypeScript version 1.5+</li>
</ul>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/angular-typescript-bindToController">AngularJs 1.x and TypeScript best practices - bindToController</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on November 24, 2015.</p>https://www.primordialcode.com/blog/post/angularconf2015-slides-code2015-10-26T00:00:00+01:002015-10-26T00:00:00+01:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>Last week I had the honor and the priviledge to give speech at the italian <a href="http://2015.angularconf.it/">AngularConf 2015</a>, which was held at the Environment Park in Turin (Italy). It was a very good event with many interesting sessions on Angular 1.x and a look some aspects of Angular 2.0.</p>
<p>I’ve hosted a session on which benefits you have when using TypeScript to develop Angular 1.x applications and how your code will change when you move to Angular 2.0.</p>
<p>Here are the slides I used as a reference:</p>
<div style="text-align: center; width: 100%; margin-bottom: 1em">
<iframe src="//www.slideshare.net/slideshow/embed_code/key/CQ0sZloSTZ2Dmt" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen=""> </iframe> <div style="margin-bottom:5px"> <strong> <a href="//www.slideshare.net/giorgettialessandro/angularconf2015" title="AngularConf2015" target="_blank">AngularConf2015</a> </strong> from <strong><a href="//www.slideshare.net/giorgettialessandro" target="_blank">Alessandro Giorgetti</a></strong> </div>
</div>
<p>The public repositories with the code samples are hosted on GitHub:</p>
<p>Angular 1.x: <a href="https://github.com/AGiorgetti/AngularConf2015">https://github.com/AGiorgetti/AngularConf2015</a></p>
<p>Angular 2.0: <a href="https://github.com/AGiorgetti/AngularConf2015_ng2">https://github.com/AGiorgetti/AngularConf2015_ng2</a></p>
<p>The full material of all the sessions will be available soon through the conference <a href="http://2015.angularconf.it/">website</a>.</p>
<p>Video records of the whole conference will be available at a later time courtesy of <a href="http://www.html.it/">HTML.it</a>.</p>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/angularconf2015-slides-code">AngularConf2015 - Slides and Code Samples</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on October 26, 2015.</p>https://www.primordialcode.com/blog/post/add-disqus-jekyll-blog2015-10-19T00:00:00+02:002015-10-19T00:00:00+02:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<ol>
<li>Setup a <a href="https://disqus.com">Disqus</a> account.</li>
<li>Hit ‘Settings’ - ‘add Disqus to Site’.</li>
<li>Fill in the required informations for the site you’ll like to add Disqus to.
<img src="https://www.primordialcode.com/UserFiles/images/JekyllDisquss01.PNG" alt="Disqus settings" /></li>
<li>After that you are asked to choose the platform in which ‘install’ the service; for Jekyll just pick ‘Universal Code’. This will send you to a page in which you’ll see instructions on which scripts to add to your templates and how to configure them.</li>
</ol>
<p>Look for the proper position on your blog template (in the Notepad theme I used the file is: _includes/disqus_comments.html) and check/update the code if needed.</p>
<p>Done.</p>
<p>It was pretty simple right ?!</p>
<p>Yes!… But by default Disqus configuration uses the page url as the key to identify the comments related to a specific blog post,
if you are migrating your data from another platform or you want the freedom to migrate away from Jekyll with an easy way to keep using your comments on Disqus,
a good idea can be the following tip:</p>
<p><strong>tip: use disqus_identifier and disqus_url</strong></p>
<p>Looking at the Disqus <a href="https://help.disqus.com/customer/portal/articles/472098-javascript-configuration-variables">JavaScript configuration variables</a> documentation,
they strongly encourage the use of these two variables: <strong>disqus_identifier</strong> and <strong>disqus_url</strong>.</p>
<p>Assigning a specific Disqus identifier to any post will make the very things easy as Disqus is going to use that id to retrieve your comments instead of the page url.
We can change our Jekyll templates again to include this values for us, let’s design a solution:</p>
<ul>
<li>we want to be able to specify the disqus_identifier in the metadata (YAML front matter) of the post or page.</li>
<li>if an explicit identifier was specified it will be used, otherwise we fall back to the page url.</li>
<li>disqus_url variable must be explictly set to the page absolute path.</li>
</ul>
<p>Here’s how the YAML metadata will look like:</p>
<figure class="highlight"><pre><code class="language-yaml" data-lang="yaml"><span class="nn">---</span>
<span class="na">layout</span><span class="pi">:</span> <span class="s">post</span>
<span class="na">title</span><span class="pi">:</span> <span class="s">How to add Disqus to your Jekyll Blog</span>
<span class="na">comments</span><span class="pi">:</span> <span class="no">true</span>
<span class="na">disqus_identifier</span><span class="pi">:</span> <span class="s">A7655498-AB9E-40BF-A0D5-E5C6DE6BBF28</span>
<span class="na">tags</span><span class="pi">:</span> <span class="pi">[</span><span class="nv">jekyll</span><span class="pi">,</span> <span class="nv">disqus</span><span class="pi">]</span>
<span class="nn">---</span></code></pre></figure>
<p>The Disqus template will be changed adding these lines:</p>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kd">var</span> <span class="nx">disqus_identifier</span> <span class="o">=</span> <span class="dl">'</span><span class="s1">EE3032CD-BAF7-4C15-9FB5-795F634DBC2C</span><span class="dl">'</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">disqus_url</span> <span class="o">=</span> <span class="dl">'</span><span class="s1">https://www.primordialcode.com/blog/post/add-disqus-jekyll-blog</span><span class="dl">'</span><span class="p">;</span></code></pre></figure>
<p>Specifying these identifiers explicitly (I like to use GUIDs) will guarantee that they will be always consistent, even if you add a trailing backslash or some query string parameters to the browser address
(the browser will navigate to the correct post anyway; but to Disqus, the two addresses are different and your comments could not be loaded).</p>
<p>After doing these two more steps you have Disqus correctly configured to work with your Jekyll website.</p>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/add-disqus-jekyll-blog">How to add Disqus to your Jekyll Blog</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on October 19, 2015.</p>https://www.primordialcode.com/blog/post/webnextconf2015-slides-code2015-10-05T00:00:00+02:002015-10-05T00:00:00+02:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>Here are the slides of my session: ‘TypeScript - the JavaScript developer best friend’:</p>
<div style="text-align: center; width: 100%; margin-bottom: 1em">
<iframe src="//www.slideshare.net/slideshow/embed_code/key/J4ss6YuuJYH7nD" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen=""> </iframe> <div style="margin-bottom:5px"> <strong> <a href="//www.slideshare.net/giorgettialessandro/typescript-the-javascript-developer-best-friend" title="TypeScript . the JavaScript developer best friend!" target="_blank">TypeScript . the JavaScript developer best friend!</a> </strong> from <strong><a href="//www.slideshare.net/giorgettialessandro" target="_blank">Alessandro Giorgetti</a></strong> </div>
</div>
<p>The source code can be found at this GitHub repository:</p>
<p><a href="https://github.com/AGiorgetti/AngularConf2015">GitHub AngularConf2015</a></p>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/webnextconf2015-slides-code">Web European Conference 2015 - Slides and Code</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on October 05, 2015.</p>https://www.primordialcode.com/blog/post/webnextconf2015-personal-toughts2015-09-30T00:00:00+02:002015-09-30T00:00:00+02:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>The <a href="http://webnextconf.eu/">Web European Conference 2015</a> is over.</p>
<p>I’m on my way back home and I’m writing these lines; not knowing when, in the next few days, I’ll publish this post… not too late I hope.</p>
<p>I’m doing it right now to capture the moment and not let the feeling fade away; it has been a great experience, I had the chance to meet some very cool guys and see some friends of old again.</p>
<p>I had the chance to see a natural born showman like <a href="http://www.hanselman.com/">Scott Hansleman</a> open up the conference with his keynote, and I learned a lot on what the terms ‘having stage presence’ mean; it’s definitely not an easy job if you wanna do it well.</p>
<p>It was also good to be back on a stage again after quite some time; I was a little scared at start: this was my first public talk in english after two plus years and I didn’t knew how rusty I was with the language and if I could keep it up.</p>
<p>I ‘survived’! And I’m feeeling good, I had a couple of indecision, but overral, it went way better than I tought it could have be!</p>
<p>I really have to say thank you to all the girls and guys involved in the organization of the event with special mention to the three major orginizers: <a href="https://twitter.com/imperugo">Ugo Lattanzi</a>, <a href="https://twitter.com/simonech">Simone Chiaretta</a> and <a href="https://twitter.com/centulani">Gianluca Centulani</a>; their dedication and passion made it possibile.</p>
<p>A personal thank goes to <a href="http://tostring.it">Ugo</a>, he did hammered me so much to be part of the conference that eventually I couldn’t say no and I answered the call for papers just in time. Luckily one of my sessions passed the screening and was in!</p>
<p>So I ‘rebooted’ myself and here we are! I’m blogging again and I’ll be out there taking part in more events to meet new guys and have fun, exchanging knowledge and learning from the great professionals that spend their time sharing their experiences, most of the time <strong>for free</strong>.</p>
<p>Thanks to the Web European Conference for igning the flame again, see you next year! I’ll be there once again (in a way or another)!</p>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/webnextconf2015-personal-toughts">Web European Conference 2015 - personal thoughts</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on September 30, 2015.</p>https://www.primordialcode.com/blog/post/using-typescript-gulp2015-09-22T00:00:00+02:002015-09-22T00:00:00+02:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>Gulp is a task runner that works on top of <a href="https://nodejs.org/">Node.js</a>.
As a prerequisite you should have Node.js already installed on your system.</p>
<p>Once you have Node.js installed (it will also install ‘npm’ - Node Package Manager), you can open a console prompt and install Gulp globally:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">npm</span> <span class="kd">install</span> <span class="kd">gulp</span> <span class="na">-g</span></code></pre></figure>
<p>Installing it globally is a requirement for some IDE and editors like Visual Studio 2013 and Visual Studio Code, other IDEs - like Visual Studio 2015 - can operate with private copies.</p>
<p>The next step is to initialize npm for the new project, so head to the project’s root folder and type:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">npm</span> <span class="kd">init</span></code></pre></figure>
<p>You will be asked some questions and as a result a package.json file will be created. This file is used to define all the packages that will be installed and used locally by node.js.</p>
<p>We can start adding some plugins and tasks that we’ll use to build our TypeScript files; let’s start simple: edit the package.json file to add some dependencies.
To correctly compile TypeScript files using gulp we need the <a href="https://github.com/ivogabe/gulp-typescript">gulp-typescript</a> plugin.</p>
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
</span><span class="nl">"name"</span><span class="p">:</span><span class="w"> </span><span class="s2">"tsgulp"</span><span class="p">,</span><span class="w">
</span><span class="nl">"version"</span><span class="p">:</span><span class="w"> </span><span class="s2">"1.0.0"</span><span class="p">,</span><span class="w">
</span><span class="nl">"description"</span><span class="p">:</span><span class="w"> </span><span class="s2">""</span><span class="p">,</span><span class="w">
</span><span class="nl">"main"</span><span class="p">:</span><span class="w"> </span><span class="s2">"final.js"</span><span class="p">,</span><span class="w">
</span><span class="nl">"scripts"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"test"</span><span class="p">:</span><span class="w"> </span><span class="s2">"echo </span><span class="se">\"</span><span class="s2">Error: no test specified</span><span class="se">\"</span><span class="s2"> && exit 1"</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"author"</span><span class="p">:</span><span class="w"> </span><span class="s2">"alessandro giorgetti"</span><span class="p">,</span><span class="w">
</span><span class="nl">"license"</span><span class="p">:</span><span class="w"> </span><span class="s2">"ISC"</span><span class="p">,</span><span class="w">
</span><span class="nl">"devDependencies"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"gulp"</span><span class="p">:</span><span class="w"> </span><span class="s2">"^3.9.0"</span><span class="p">,</span><span class="w">
</span><span class="nl">"gulp-typescript"</span><span class="p">:</span><span class="w"> </span><span class="s2">"^2.8.2"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">}</span></code></pre></figure>
<p>Once you’we done editing the file, run the following command to download and install a local copy on the packages:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">npm</span> <span class="kd">install</span> </code></pre></figure>
<p>It’s now time to define the task that will be used to compile the .ts files.
Create a <em>gulpfile.js</em> file in the root folder of the project, then add this code to it:</p>
<figure class="highlight"><pre><code class="language-js" data-lang="js"><span class="kd">var</span> <span class="nx">gulp</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">"</span><span class="s2">gulp</span><span class="dl">"</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">ts</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">"</span><span class="s2">gulp-typescript</span><span class="dl">"</span><span class="p">);</span>
<span class="nx">gulp</span><span class="p">.</span><span class="nx">task</span><span class="p">(</span><span class="dl">"</span><span class="s2">build-ts</span><span class="dl">"</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">tsResult</span> <span class="o">=</span> <span class="nx">gulp</span><span class="p">.</span><span class="nx">src</span><span class="p">(</span><span class="dl">"</span><span class="s2">*.ts</span><span class="dl">"</span><span class="p">)</span>
<span class="p">.</span><span class="nx">pipe</span><span class="p">(</span><span class="nx">ts</span><span class="p">({</span>
<span class="na">noImplicitAny</span><span class="p">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="na">out</span><span class="p">:</span> <span class="dl">"</span><span class="s2">final.js</span><span class="dl">"</span>
<span class="p">}));</span>
<span class="k">return</span> <span class="nx">tsResult</span><span class="p">.</span><span class="nx">js</span><span class="p">.</span><span class="nx">pipe</span><span class="p">(</span><span class="nx">gulp</span><span class="p">.</span><span class="nx">dest</span><span class="p">(</span><span class="dl">'</span><span class="s1">.</span><span class="dl">'</span><span class="p">));</span>
<span class="p">});</span></code></pre></figure>
<p>That’s all that is needed to do in order to compile all your TypeScript files.
You can already see the advantages of using task runners: you can define a build pipeline made of different actions that will change and trasform the artifacts according to your desire.
You can chose which files to include and exclude from the compilation using the filtering capabilities of node.js and gulp (even using wildcards, a feature not yet supported by the plain TypeScript compiler).</p>
<p>Using the TypeScript compiler through Node.js and Gulp (or any other task runner) you can also take advantage of the advanced features of the compiler,
like watching for every change made to the files and recompile them on the fly, all you need to do is add some more tasks to the gulpfile.js:</p>
<figure class="highlight"><pre><code class="language-js" data-lang="js"><span class="kd">var</span> <span class="nx">gulp</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">"</span><span class="s2">gulp</span><span class="dl">"</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">ts</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">"</span><span class="s2">gulp-typescript</span><span class="dl">"</span><span class="p">);</span>
<span class="c1">// just build all the typescript files</span>
<span class="nx">gulp</span><span class="p">.</span><span class="nx">task</span><span class="p">(</span><span class="dl">"</span><span class="s2">build-ts</span><span class="dl">"</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">tsResult</span> <span class="o">=</span> <span class="nx">gulp</span><span class="p">.</span><span class="nx">src</span><span class="p">(</span><span class="dl">"</span><span class="s2">*.ts</span><span class="dl">"</span><span class="p">)</span>
<span class="p">.</span><span class="nx">pipe</span><span class="p">(</span><span class="nx">ts</span><span class="p">({</span>
<span class="na">noImplicitAny</span><span class="p">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="na">out</span><span class="p">:</span> <span class="dl">"</span><span class="s2">final.js</span><span class="dl">"</span>
<span class="p">}));</span>
<span class="k">return</span> <span class="nx">tsResult</span><span class="p">.</span><span class="nx">js</span><span class="p">.</span><span class="nx">pipe</span><span class="p">(</span><span class="nx">gulp</span><span class="p">.</span><span class="nx">dest</span><span class="p">(</span><span class="dl">'</span><span class="s1">.</span><span class="dl">'</span><span class="p">));</span>
<span class="p">});</span>
<span class="c1">// watch the files for changes and rebuild everything</span>
<span class="nx">gulp</span><span class="p">.</span><span class="nx">task</span><span class="p">(</span><span class="dl">"</span><span class="s2">watch</span><span class="dl">"</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">gulp</span><span class="p">.</span><span class="nx">watch</span><span class="p">(</span><span class="dl">"</span><span class="s2">*.ts</span><span class="dl">"</span><span class="p">,</span> <span class="p">[</span><span class="dl">'</span><span class="s1">build-ts</span><span class="dl">'</span><span class="p">]);</span>
<span class="p">});</span></code></pre></figure>
<p>However keep in mind that if you’re using wildcards in your paths, any new file created since running the ‘tsc’ command may not be compiled, verify the behavior on your system because you might need to stop the watcher and start it again.</p>
<p>Starting from here you can expand your build pipeline adding support for sourcemaps, automatic file references and reordering and so on…</p>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/using-typescript-gulp">Using TypeScript with Gulp</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on September 22, 2015.</p>https://www.primordialcode.com/blog/post/typescript-command-line-compiler2015-09-16T00:00:00+02:002015-09-16T00:00:00+02:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p>Let’s have another introductory post on TypeScript. This time it’s the command-line Compiler turn.</p>
<p>If you use a fully featured IDE with TypeScript support - like Visual Studio 2013+ - you will probably do not have to worry about the command-line compiler.
It’s nonetheless usefull to have an overview on some of its basic functionalities;
it can come in handy when you do not have an IDE available or just to automate some tasks with a task runner.</p>
<p><strong>Compile a single file:</strong></p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">tsc</span> <span class="kd">app</span>.ts</code></pre></figure>
<p>Generates app.js.</p>
<p><strong>Compile multiple files</strong></p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">tsc</span> <span class="kd">file1</span>.ts <span class="kd">file2</span>.ts<span class="o">,</span> <span class="kd">file</span> <span class="m">3</span>.ts</code></pre></figure>
<p>Multiple .js files will be created.</p>
<p>Wildcards are not supported, so to compile multiple files you need to do something like:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="nb">dir</span> <span class="o">*</span>.ts <span class="na">/b /s </span><span class="o">></span> <span class="kd">ts</span><span class="na">-files</span>.txt
<span class="kd">tsc</span> @ts<span class="na">-files</span>.txt</code></pre></figure>
<p>This 2 steps will create a configuration file containing the arguments (the list of files in this case) that will be passed in to the TypeScript compiler.</p>
<p><strong>Source Map</strong></p>
<p>We all like a good debugging experience, since many browsers now support sourcemap, let’s instruct TypeScript to generete them for us:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">tsc</span> <span class="kd">app</span>.ts <span class="na">--sourcemap</span></code></pre></figure>
<p>This command will generate .map (sourcemap) files.</p>
<p><strong>Joining files</strong></p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">tsc</span> <span class="kd">file1</span>.ts <span class="kd">file2</span>.ts <span class="na">--out </span><span class="kd">final</span>.js</code></pre></figure>
<p>It will concatenate all the generated JavaScript on a single file called final.js.</p>
<p><strong>ECMAScript target version</strong></p>
<p>By default the compiler will emit ES3 compatible JavaScript, to change it you need the ‘-t’ switch (allowed values ES3, ES5, ES6).</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">tsc</span> <span class="kd">app</span>.ts <span class="na">-t </span><span class="kd">ES5</span></code></pre></figure>
<p><strong>Watch for changes</strong></p>
<p>Runing the compiler manually is quite tedious, we can ask the compiler to watch for file changes and recompile them on the fly:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">tsc</span> <span class="kd">app</span>.ts <span class="na">--watch</span></code></pre></figure>
<p>In a Windows environment, to use the watch feature you will need to run the compiler you installed using npm and node; the version of the standard SDK will not provide watch support.
See my previous post <a href="http://www.primordialcode.com/blog/post/setup-typescript/">Setup TypeScript</a> on how to setup TypeScript to see how to find the version of the compiler you need.</p>
<p>A typical command-line usage combines some or all of these switches:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">tsc</span> <span class="kd">file1</span>.ts <span class="kd">file2</span>.ts <span class="na">--sourcemap --watch
</span><span class="kd">tsc</span> <span class="kd">file1</span>.ts <span class="kd">file2</span>.ts <span class="na">--out </span><span class="kd">combined</span>.js <span class="na">--sourcemap --watch</span></code></pre></figure>
<p><strong>tsconfig.json</strong></p>
<p>TypeScript 1.5 also introduced support for a configuration file called <a href="https://github.com/Microsoft/TypeScript/wiki/tsconfig.json">tsconfig.json</a> that can be used to configure all the parameters the compiler supports and to provide a list of files to compile; once again wildcards cannot be used in path definitions.</p>
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
</span><span class="nl">"compilerOptions"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"target"</span><span class="p">:</span><span class="w"> </span><span class="s2">"ES5"</span><span class="p">,</span><span class="w">
</span><span class="nl">"out"</span><span class="p">:</span><span class="w"> </span><span class="s2">"./final.js"</span><span class="p">,</span><span class="w">
</span><span class="nl">"sourceMap"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"files"</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
</span><span class="s2">"./app.ts"</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">}</span></code></pre></figure>
<p>Working with the command line compiler while using a tsconfig.json file is pretty straightforward, the only real problem is the lack of wildcards support to define the paths of the files to be compiled.
This problem can be easily solved using task runner like <a href="http://gruntjs.com/">Grunt</a> or <a href="http://gulpjs.com/">Gulp</a>, and will be the subject of another post.</p>
<p>However keep in mind that, even if using those solution, if you’re using wildcards in your paths, any new files created since running the ‘tsc’ command won’t get compiled, you’ll need to stop the watcher and start it again.</p>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/typescript-command-line-compiler">TypeScript command-line Compiler</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on September 16, 2015.</p>https://www.primordialcode.com/blog/post/setup-typescript2015-09-09T00:00:00+02:002015-09-09T00:00:00+02:00Alessandro Giorgettihttps://www.primordialcode.comalessandro.giorgetti@live.com
<p><strong>Prerequisites</strong></p>
<p>Node and NPM have to be installed, so navigate to <a href="https://nodejs.org/">NodeJs official website</a> and grab the installer best suited for your system.</p>
<p>To test if it’s installed and if it work you can open your command prompt(or bash or whatever you use) and type these two commands:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">node</span> <span class="na">-v
</span><span class="kd">npm</span> <span class="na">-v</span></code></pre></figure>
<p>both will show the version number of the installed Node and NPM.</p>
<p><strong>Installing TypeScript</strong></p>
<p>To install TypeScript open a command pront and type:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">npm</span> <span class="kd">install</span> <span class="na">-g </span><span class="kd">typescript</span></code></pre></figure>
<p>The -g option will install the package globally, so that the ocmpiler can be used in any project.
To test if it works just type:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">tsc</span> <span class="na">-v
</span><span class="kd">tsc</span> <span class="na">-h</span></code></pre></figure>
<p>To print out the compiler version number and an help of all the available options.</p>
<p><strong>Fixing the Issue with multiple versions installed</strong></p>
<p>It’s always a good idea to check if you have the latest version available: if the displayed version number it’s not what you expect it to be - especially after having updated all your global packages -
chances are you have multiple copies of the package installed on your machine, and the default path is pointing you to an outdated one.
Fix the issue on a Windows machine is not that hard, just look and the paths defined in the environment system variables and point the to the newly installed version of typescript.</p>
<p>In my case I had to open the ‘Control Panel’, select ‘System’ and then ‘Advanced System Settings’; looking and the System enviroment variables, under the PATH option I could read:
…C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0...
Which was pointing to an outdated version of the compiler.
Find where the newly installed version is on your system (usually inside C:\Program Files (x86)\Microsoft SDKs\TypeScript) and fix the path.</p>
<p>As a hint you can:</p>
<ul>
<li>Install a propert SDK from Microsoft, and look under C:\Program Files (x86)\Microsoft SDKs\TypeScript.</li>
<li>Manually update the npm package (npm install -g typescript) and take note of the paths that will be printed out by the installer:</li>
</ul>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">C</span>:\Temp\typescript>npm <span class="kd">install</span> <span class="na">-g </span><span class="kd">typescript</span>
<span class="kd">C</span>:\Users\Username\AppData\Roaming\npm\tsserver <span class="o">-></span> <span class="kd">C</span>:\Users\Username\AppData\Roaming\npm\node_modules\typescript\bin\tsserver
<span class="kd">C</span>:\Users\Username\AppData\Roaming\npm\tsc <span class="o">-></span> <span class="kd">C</span>:\Users\Username\AppData\Roaming\npm\node_modules\typescript\bin\tsc
<span class="kd">typescript</span>@1.5.3 <span class="kd">C</span>:\Users\Username\AppData\Roaming\npm\node_modules\typescript</code></pre></figure>
<p><strong>Keep TypeScript Updated</strong></p>
<p>To update Node.js global packages, you can use:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">npm</span> <span class="kd">install</span> <span class="na">-g </span><span class="kd">typescript</span></code></pre></figure>
<p>To find out which packages need to be updated, you can use:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">npm</span> <span class="kd">outdated</span> <span class="na">-g --depth</span><span class="o">=</span><span class="m">0</span></code></pre></figure>
<p>To update all global packages, you can use:</p>
<figure class="highlight"><pre><code class="language-bat" data-lang="bat"><span class="kd">npm</span> <span class="kd">update</span> <span class="na">-g</span></code></pre></figure>
<p><strong>Grab a good editor, possibly with TypeScript support</strong></p>
<p>There are many editors out there, some are listed on the <a href="http://www.typescriptlang.org/">TypeScript</a> Website.</p>
<p>On a Windows OS I’d like to recommend:</p>
<ul>
<li><a href="https://www.visualstudio.com/">Visual Studio</a> - the community edition is free.</li>
<li><a href="https://code.visualstudio.com/">Visual Studio Code</a></li>
</ul>
<p>On other operating systems you can check Visual Studio Code as well, it’s currently available for Linux and Mac OSX too.</p>
<p><em>cya next</em></p>
<p><a href="https://www.primordialcode.com/blog/post/setup-typescript">Setup TypeScript</a> was originally published by Alessandro Giorgetti at <a href="https://www.primordialcode.com">PrimordialCode</a> on September 09, 2015.</p>