<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[CodingCoyote Blog]]></title><description><![CDATA[I'm a self taught Software Developer focused on cross-platform developer tools.]]></description><link>https://blog.codingcoyote.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Nov 2026 06:57:55 GMT</lastBuildDate><atom:link href="https://blog.codingcoyote.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Optimizing a React SPA Part 3: Clean Out Your Functional Components]]></title><description><![CDATA[A general overview of the dangers of nested functional components and how applying Lift and Shift development strategies provide a low risk fix]]></description><link>https://blog.codingcoyote.dev/optimizing-a-react-spa-part-3-clean-out-your-functional-components</link><guid isPermaLink="true">https://blog.codingcoyote.dev/optimizing-a-react-spa-part-3-clean-out-your-functional-components</guid><category><![CDATA[react js]]></category><category><![CDATA[React]]></category><category><![CDATA[Functional Components]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Tue, 08 Apr 2025 13:00:44 GMT</pubDate><content:encoded><![CDATA[<p>I’ve had a run through a decent number of interesting React issues since I’ve started this and the biggest performance loss I’ve seen is from doing stuff more often than necessary. While this could extend to render loops which are their own special torture for your application, I’m targeting low hanging fruit today.</p>
<h2 id="heading-nested-component-functions">Nested Component Functions</h2>
<p>To understand why it’s bad to nest component functions inside each other you need to think about what a functional component really is. It’s the render() function that runs to generate the component code inside it. Any code defined inside it is only defined when the function runs. So if you define a function, a constant, or a component it’s discarded after the render completes. React internally tracks which components have rendered to be able to tell when it needs to render them again.</p>
<p>Let’s look at this minimal example based on the vite react template:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> {useState} <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> reactLogo <span class="hljs-keyword">from</span> <span class="hljs-string">'./assets/react.svg'</span>
<span class="hljs-keyword">import</span> viteLogo <span class="hljs-keyword">from</span> <span class="hljs-string">'/vite.svg'</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>)

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Innercomponent</span>(<span class="hljs-params">{setCount, count}</span>): <span class="hljs-title">JSX</span>.<span class="hljs-title">Element</span> </span>{
        <span class="hljs-keyword">return</span> &lt;button onClick={<span class="hljs-function">() =&gt;</span> setCount(<span class="hljs-function">(<span class="hljs-params">count: <span class="hljs-built_in">number</span></span>) =&gt;</span> count + <span class="hljs-number">1</span>)}&gt;
            count is {count}
        &lt;/button&gt;
    }

    <span class="hljs-keyword">return</span> (
        &lt;&gt;
            &lt;div&gt;
                &lt;a href=<span class="hljs-string">"https://vite.dev"</span> target=<span class="hljs-string">"_blank"</span>&gt;
                    &lt;img src={viteLogo} className=<span class="hljs-string">"logo"</span> alt=<span class="hljs-string">"Vite logo"</span>/&gt;
                &lt;/a&gt;
                &lt;a href=<span class="hljs-string">"https://react.dev"</span> target=<span class="hljs-string">"_blank"</span>&gt;
                    &lt;img src={reactLogo} className=<span class="hljs-string">"logo react"</span> alt=<span class="hljs-string">"React logo"</span>/&gt;
                &lt;/a&gt;
            &lt;/div&gt;
            &lt;h1&gt;Vite + React&lt;/h1&gt;
            &lt;div className=<span class="hljs-string">"card"</span>&gt;
                &lt;Innercomponent count={count} setCount={setCount}/&gt;
                &lt;p&gt;
                    Edit &lt;code&gt;src/App.tsx&lt;/code&gt; and save to test HMR
                &lt;/p&gt;
            &lt;/div&gt;
            &lt;p className=<span class="hljs-string">"read-the-docs"</span>&gt;
                Click on the Vite and React logos to learn more
            &lt;/p&gt;
        &lt;/&gt;
    )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1744077803107/a40e836f-12d1-4429-96f4-177812f378d4.png" alt class="image--center mx-auto" /></p>
<p>As seen here, when clicking on the button, the InnerComponent is rendered as a fresh function on each render despite being effectively the same component. Now let’s move it and the counter function out of the component:</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// imports...</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Innercomponent</span>(<span class="hljs-params">{setCount,count}</span>): <span class="hljs-title">JSX</span>.<span class="hljs-title">Element</span></span>{
    <span class="hljs-keyword">return</span>    &lt;button onClick={<span class="hljs-function">() =&gt;</span> setCount(<span class="hljs-function">(<span class="hljs-params">count: <span class="hljs-built_in">number</span></span>) =&gt;</span> count + <span class="hljs-number">1</span>)}&gt;
        count is {count}
    &lt;/button&gt;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>)
  <span class="hljs-keyword">return</span> (
    &lt;&gt;
      &lt;div&gt;
        &lt;a href=<span class="hljs-string">"https://vite.dev"</span> target=<span class="hljs-string">"_blank"</span>&gt;
          &lt;img src={viteLogo} className=<span class="hljs-string">"logo"</span> alt=<span class="hljs-string">"Vite logo"</span> /&gt;
        &lt;/a&gt;
        &lt;a href=<span class="hljs-string">"https://react.dev"</span> target=<span class="hljs-string">"_blank"</span>&gt;
          &lt;img src={reactLogo} className=<span class="hljs-string">"logo react"</span> alt=<span class="hljs-string">"React logo"</span> /&gt;
        &lt;/a&gt;
      &lt;/div&gt;
      &lt;h1&gt;Vite + React&lt;/h1&gt;
      &lt;div className=<span class="hljs-string">"card"</span>&gt;
          &lt;Innercomponent count={count} setCount={setCount} /&gt;
        &lt;p&gt;
          Edit &lt;code&gt;src/App.tsx&lt;/code&gt; and save to test HMR
        &lt;/p&gt;
      &lt;/div&gt;
      &lt;p className=<span class="hljs-string">"read-the-docs"</span>&gt;
        Click on the Vite and React logos to learn more
      &lt;/p&gt;
    &lt;/&gt;
  )
}
<span class="hljs-comment">///...</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1744077702526/1cc8b0f5-cff0-45e5-b9ff-d798085064b0.png" alt class="image--center mx-auto" /></p>
<p>Now we can see in the React Profiler that it is indeed tracking it as the same component. Don’t be mislead by the execution times though. In this case it’s a relatively simple component and function call, but more complex sets can have devestating performance implications when the inner component’s state resets.</p>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Innercomponent</span>(<span class="hljs-params">{onClick}</span>): <span class="hljs-title">JSX</span>.<span class="hljs-title">Element</span> </span>{
        <span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>)

        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">counter</span>(<span class="hljs-params">setCount</span>) </span>{
            setCount(<span class="hljs-function">(<span class="hljs-params">count: <span class="hljs-built_in">number</span></span>) =&gt;</span> count + <span class="hljs-number">1</span>);
            onClick(<span class="hljs-function">(<span class="hljs-params">clickCount:<span class="hljs-built_in">number</span></span>) =&gt;</span> clickCount + <span class="hljs-number">1</span>);
        }

        <span class="hljs-keyword">return</span> &lt;button onClick={<span class="hljs-function">() =&gt;</span> {
            counter(setCount);}}&gt;
            inner component is at {count}
        &lt;/button&gt;
    }

    <span class="hljs-keyword">const</span> [state, setState] = useState(<span class="hljs-number">0</span>)

    <span class="hljs-keyword">return</span> (
        &lt;&gt;
            &lt;div&gt;
                &lt;a href=<span class="hljs-string">"https://vite.dev"</span> target=<span class="hljs-string">"_blank"</span>&gt;
                    &lt;img src={viteLogo} className=<span class="hljs-string">"logo"</span> alt=<span class="hljs-string">"Vite logo"</span>/&gt;
                &lt;/a&gt;
                &lt;a href=<span class="hljs-string">"https://react.dev"</span> target=<span class="hljs-string">"_blank"</span>&gt;
                    &lt;img src={reactLogo} className=<span class="hljs-string">"logo react"</span> alt=<span class="hljs-string">"React logo"</span>/&gt;
                &lt;/a&gt;
            &lt;/div&gt;
            &lt;h1&gt;Vite + React&lt;/h1&gt;
            &lt;div className=<span class="hljs-string">"card"</span>&gt;
                &lt;Innercomponent onClick={<span class="hljs-function">() =&gt;</span> setState(<span class="hljs-function">(<span class="hljs-params">state</span>) =&gt;</span> state + <span class="hljs-number">1</span>)}/&gt;
                &lt;p&gt;
                    But the outer component has is at {state}
                &lt;/p&gt;
            &lt;/div&gt;
            &lt;p className=<span class="hljs-string">"read-the-docs"</span>&gt;
                Click on the Vite and React logos to learn more
            &lt;/p&gt;
        &lt;/&gt;
    )
}
</code></pre>
<p>Notice in the code above that if we run it, the outer component will update it’s state and increase the counter, but since the inner component is freshly rendered on each render of the outer component it will never increase the count. In practice, I’ve seen deeply nested components that use redux or contexts to consistently recalculate complex state and cause render loops or delays of hundreds of milliseconds for a single render.</p>
<p>On the function side of things, the function counter() is treated as a new function and can cause unwanted rerenders even when no other state changes for memoized components. A simple lift and shift of these functions to somewhere outside of the component function causes React to treat them appropriately. If your function has dependencies from the inner function, they need to be passed to the component in some other fashion such as via props or contexts.</p>
<h2 id="heading-how-much-does-this-help">How Much Does This Help?</h2>
<p>The answer is very dependent on your app. With a set of deeply nested components that make use of useEffect(), React.memo, useState() and useMemo() it can be very impactful preventing extra data processing and even api calls. For simpler components with only a few layers deep, it may be a millisecond or less of savings. For functions defined in another function, the improvement is even lower, but across a large SPA it becomes mildly significant.</p>
<p>But, it’s effectively a free performance improvement with little to no risk. Where the waters muddy is when you are using state from the parent component in the child component without passing it in as props. Ideally you keep this in mind and fix the issues as you work near them instead of overhauling an app just to make this change. Lift and Shift development should be treated as low hanging fruit, but not a cure-all.</p>
]]></content:encoded></item><item><title><![CDATA[Optimizing a React SPA Part 2: Profilers and Finding Bottlenecks]]></title><description><![CDATA[Tools of the Trade]]></description><link>https://blog.codingcoyote.dev/optimizing-a-react-spa-part-2-profilers-and-finding-bottlenecks</link><guid isPermaLink="true">https://blog.codingcoyote.dev/optimizing-a-react-spa-part-2-profilers-and-finding-bottlenecks</guid><category><![CDATA[performance]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Mon, 10 Apr 2023 01:14:40 GMT</pubDate><content:encoded><![CDATA[<p>Knowing how to find the bottlenecks in your code can be more important than knowing how to fix them. After all it's impractical to attempt to optimize everything in one shotgun blast. This is where the right tools will make a huge difference.</p>
<h1 id="heading-chrome-performance-profiler">Chrome Performance Profiler</h1>
<p>If you use Google Chrome (or a browser based on the <a target="_blank" href="https://www.chromium.org/chromium-projects/">Chromium Project</a>, you should have access to a version of Chrome's performance profiler.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1653583194204/lwi8Q8AM0.png" alt="image.png" /></p>
<p>That middle section is the "Flame Graph". Each bar represents a function call and the width indicates how long it took to run. Each bar below another bar is a function that was called in the bar above it. Now in React, a lot of this will not be your code so you need to look for function names that you recognize.</p>
<p>Running this in production will likely present you with obfuscated function names. If possible, try a a development build pointed to the same data, or deploy non-minified build to make it easier to read. Otherwise you will end up guessing based on behaviors and built-in functions like map or filter.</p>
<h2 id="heading-chrome-task-manager">Chrome Task Manager</h2>
<p>When you're knee-deep in memory and the waters are rising, you need to find out if it's your app taking up all that memory, or if some extensions or even another tab is the culprit.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1681088759367/9207cc5f-361b-434c-a55a-9de13377094a.png" alt class="image--center mx-auto" /></p>
<p>There are several key columns here and the one we are most interested in is not available by default. With developer tools open, right-click and select JS Heap to see the actual heap (memory used by your code) size. You can watch this as you trigger suspect regions of code. Generally heavy allocations are slower, and sudden rises that don't fall away could mean frequent allocations, memory leaks, or inefficient code.</p>
<p>But, pay attention to differences in native memory and JS Heap. The native memory size includes detached Dom Elements (elements referenced by code, but not part of the page).</p>
<h2 id="heading-edge-detached-elements-tool">Edge Detached Elements Tool</h2>
<p>This is a tool that is so far exclusive to Edge but is just a simplified view of the memory snapshots in Chrome. If you choose to go this route, only worry about Detached elements that stick around or build up quickly. Any elements created in javascript (including elements from past renders in React) will detach before being cleaned up by the next garbage collection pass. If you see them building up, you may be holding references to these elements somewhere in your code.</p>
<h2 id="heading-single-threaded-environment">Single Threaded Environment</h2>
<p>Remember above all that the web page runs on a single Thread. Web Workers and Service Workers get around this using background threads, but async and synchronous code all run on the same thread. So SignalR updates, async API calls and long-running functions are all dependent on thread running them. Frequent async operations to update the model or cache can delay other calls or cause stuttering depending on what is being done.</p>
]]></content:encoded></item><item><title><![CDATA[Optimizing a React SPA Part 1: Collections, Loops, and Filters]]></title><description><![CDATA[Know Your Data]]></description><link>https://blog.codingcoyote.dev/optimizing-a-react-spa-part-1-collections-loops-and-filters</link><guid isPermaLink="true">https://blog.codingcoyote.dev/optimizing-a-react-spa-part-1-collections-loops-and-filters</guid><category><![CDATA[C#]]></category><category><![CDATA[TypeScript]]></category><category><![CDATA[React]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Wed, 25 May 2022 19:22:30 GMT</pubDate><content:encoded><![CDATA[<p>So I've been writing code in C# for the last 7 years or so, and this past year the decision was made to use Typescript and React for a new project. And we hit some surprise scalability issues pretty quickly. This article will largely assume you have an idea where the bottleneck is and I'll try to cover profiling tips/tricks in another article.</p>
<p>Typescript/JavaScript are different. And some of that is good. I like using falsy checks in place of my normal null checks, and I enjoy the ability to easily make a monster with the objects:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> a = {...s, Rank = s.Rank+<span class="hljs-number">1</span>, Level = getLevel(s.User)
</code></pre>
<p>But some things are just close enough they feel less intuitive, like making a dictionary. How do you grab a specific item from a collection?</p>
<h1 id="heading-where-is-where-and-select">Where is .Where() and .Select()?</h1>
<p>.Where in C# is effectively.filter() in  Typescript.</p>
<p>C#'s .First() doesn't have an exact equivalent but .FirstOrDefault() does: .find()</p>
<p>.find() returns undefined if it doesn't find an object that satisfies the condition.</p>
<p>.map() takes the place of our .Select() and the following .ToList()/.ToArray().</p>
<p>Now from C# you probably know that arrays are generally cheaper to iterate over, but slower to grow (and require more ceremony), but in Typescript arrays can be pushed onto like a List in C#.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> arrayOfObjects = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Array</span>(<span class="hljs-number">10000</span>).fill(<span class="hljs-built_in">Math</span>.random());

  objArray.push(...arrayOfObjects);
</code></pre>
<p>So what kinds of "array-like" objects can we store data in? </p>
<p>Array: bare bones basic, but likely what you need for overall readability. </p>
<p>Map: The hashmap in Typescript similar to the Dictionary in C# with some interesting caveats.</p>
<p>Set: like the array, but every object is required to be unique.</p>
<p>Object: yes, the plain old value that you may associate with JSON more than a proper data type. If you deserialize an array from a C# API, you may find it shows up as an object like this:</p>
<pre><code class="lang-typescript">{
     <span class="hljs-string">"0"</span>: <span class="hljs-string">"Value0"</span>,
     <span class="hljs-string">"1"</span>: <span class="hljs-string">"Value1"</span>,
     <span class="hljs-string">"2"</span>: <span class="hljs-string">"Value2"</span>,
     <span class="hljs-string">"3"</span>: <span class="hljs-string">"Value3"</span>,
     <span class="hljs-string">"4"</span>: <span class="hljs-string">"Value4"</span>,
}
</code></pre>
<p>You can access the members as of each index is a property. And with a an interface specified, you can still have type safety:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> ObjectDictionary {
  [key: <span class="hljs-built_in">number</span>]: <span class="hljs-built_in">string</span>
}
</code></pre>
<p>While researching for this article, I came across a great site that details collections I didn't even know existed. I'll probably end up covering some of them if I end up using them, but you should check it out: https://www.collectionsjs.com/</p>
<h1 id="heading-reducing-loops">Reducing Loops</h1>
<p>Often a .FirstOrDefault() is a reasonable way to find an item in a large dataset being pulled from a database. But Dictionaries are much faster for accessing an item you know the key for (Benchmark: https://jsbench.me/29l3kt2jgq/1) . So your goto may be the find. And in fact, trying to build a Map for short-lived or single use searches is too expensive (Benchmark: https://jsbench.me/29l3kt2jgq/2). Even a small array is costly in comparison to using find().</p>
<p>But for the sake of performance, regardless of whether the data you got came in from an API call or is being pulled from a local cache, if you are reusing it, keep it around. If you have a call bringing in 50,000 items for a dashboard or to precache for reporting, you should keep anything useful and reuse it. Larger datasets take significantly longer to loop over. And thats a good place to start optimizing. If you know you are going to constantly search the collection based on that key, you're probably better off accepting the slight hit to build the map, then use it as you repeatedly search for objects. The access is much faster and doesn't require looping over the collection and with a condition although it can be somewhat more limited.</p>
<p>Just remember the syntax for getting an item from a map by key is <code>map.get(key)</code> not <code>map[key]</code>.</p>
<p>An alternative you may read about is object/key access. instead of building a Map, you just make a new object and assign each key with a value. It's much faster than a dictionary (Benchmark: https://jsbench.me/29l3kt2jgq/6)</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> obj = {};

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arrayOfObjects.length; i++) {
  obj[arrayOfObjects[i].Key] = arrayOfObjects[i]
}

  obj[<span class="hljs-number">9999</span>]
</code></pre>
<p>Funny thing about using find(), the item you want is always in the last place you look for it. That means that if the item you want is in a collection of thousands, you are going to run that lambda at least once and potentially thousands of times. And if you're going to run it twice, you may double that or more. (Benchmark: https://jsbench.me/29l3kt2jgq/5) As long as the lamda's you are using aren't too expensive consider combining the calls into a single lambda on a filter(). You'll be left with an array that you'll need to run find on, but this could save you from having to traverse the original collection.</p>
<pre><code class="lang-typescript">arrayOfObjects.find(<span class="hljs-function"><span class="hljs-params">o</span> =&gt;</span> o.Key === <span class="hljs-number">9999</span>)
arrayOfObjects.find(<span class="hljs-function"><span class="hljs-params">o</span> =&gt;</span> o.Key === <span class="hljs-number">9997</span>)
arrayOfObjects.find(<span class="hljs-function"><span class="hljs-params">o</span> =&gt;</span> o.Key === <span class="hljs-number">9998</span>)

<span class="hljs-comment">// could be faster combined</span>

<span class="hljs-keyword">const</span> objectsFiltered = arrayOfObjects.filter(<span class="hljs-function"><span class="hljs-params">o</span> =&gt;</span> o.Key === <span class="hljs-number">9999</span> || o.Key === <span class="hljs-number">9997</span> || o.Key === <span class="hljs-number">9998</span>)

<span class="hljs-comment">// but you'll need to search the resulting array, although the find will only run 10,006 times instead of 30,000 times</span>
objectsFiltered.find(<span class="hljs-function"><span class="hljs-params">o</span> =&gt;</span> o.Key === <span class="hljs-number">9999</span>)
objectsFiltered.find(<span class="hljs-function"><span class="hljs-params">o</span> =&gt;</span> o.Key === <span class="hljs-number">9999</span>)
objectsFiltered.find(<span class="hljs-function"><span class="hljs-params">o</span> =&gt;</span> o.Key === <span class="hljs-number">9997</span>)
</code></pre>
<h1 id="heading-use-state-to-your-advantage">Use State to Your Advantage</h1>
<p>Caching intermediate results or partitioning your data can be a good strategy as well. If their are any common filters, such as only looking at active Orders after a certain date, filter down to that first, then you run filters looking into tax amount, total order amounts, sum of all orders, orders per customer, etc, and loop over the smaller dataset.</p>
<p>Look for any components that search collections or run loops on every render. Is it possible to cache the results in State? If I am only looking at orders since October of last year, I can filter those down once, store the result in state and then only search and loop over the cached state anytime I need to access it on future renders.</p>
<h1 id="heading-use-the-right-kind-of-loop">Use the Right Kind of Loop</h1>
<p>There are a few ways to loop in Typescript/Javascript, but as in C# the humble for Loop is uglier and usually faster than most (Benchmark: https://jsbench.me/29l3kt2jgq/7).</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arrayOfObjects.length; i++) {
  <span class="hljs-comment">// Do stuff here</span>
}
</code></pre>
<p>For loops have the advantage of of being flexible as long as you can access the items by index. As long as you don't throw another allocation in there like <code>const obj = arrayOfObjects[i];</code>. </p>
<p>if you can't or don't need to access by index, .forEach is probably you're next best bet since the performance varies more on the other methods. </p>
<pre><code class="lang-typescript">arrayOfObjects.forEach(<span class="hljs-function">(<span class="hljs-params">obj</span>) =&gt;</span>{
 <span class="hljs-comment">//do stuff with obj</span>
})
</code></pre>
<p>I reran the above benchmark many times and small changes had fairly drastic effects on performance though, so you should probably pick the one that's most readable and only approach the others when you feel the need to benchmark them and squeeze the every drop of speed out of it you can.</p>
<h1 id="heading-know-your-data">Know Your Data</h1>
<p>Overall, you need to understand your dataset to make any meaningful improvements regardless of your platform. And by understanding the relationships between the data your component needs and where it gets it, you can make optimizations without ever looking at a profiler.</p>
]]></content:encoded></item><item><title><![CDATA[Project Jengo Redux]]></title><description><![CDATA[Why Patent Trolls Are a Problem]]></description><link>https://blog.codingcoyote.dev/project-jengo-redux</link><guid isPermaLink="true">https://blog.codingcoyote.dev/project-jengo-redux</guid><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Tue, 24 May 2022 02:57:19 GMT</pubDate><content:encoded><![CDATA[<p>The original <a target="_blank" href="https://blog.cloudflare.com/project-jengo/">Project Jengo</a> began after Blackbird Technologies sued CloudFlare over old and very vague patents that probably shouldn't have been granted in the first place. Instead of paying the troll to leave them alone, they fought back. They didn't do it because it was easier, or because it was cheaper. They did it for the same reason I participated in Round 2. To secure a brighter future for the world.</p>
<h2 id="heading-what-makes-a-bad-patent">What Makes a Bad Patent</h2>
<p>Patents grant the right to make/use/license a particular technological design. And are a good motivator for innovators who do not have the means to profit directly from their ideas. But not all patents are good patents.</p>
<p>In particular, the U.S. Patent Office requires that a patent is specific enough to actually describe an idea. You can't patent "do x faster", but you could patent "do x faster by doing y, followed by z".  And each patent is supposed to be a new idea and sufficiently different that anyone skilled in the area of the patent would not consider it an obvious change.  That's where vague patents come in.</p>
<p>Overly vague patents cover an idea in a way that it may not be obviously different, or sufficiently precise to actually describe a new process or improvement. And since we cannot reasonably put people in the patent office that have an expertise in every possible field, or require them to remember every patent that has ever been registered sometimes things slip through.</p>
<h2 id="heading-why-bad-patents-are-dangerous-and-not-just-a-nuisance">Why Bad Patents Are Dangerous and Not Just a Nuisance</h2>
<p>Patent trolls often buy older companies, rights to older patents, etc in order to use those patents to sue people who may or may not be infringing on the patent. In the case of Blackbird Technologies, they sued over 100 companies in the span of 3 years with a set of weak patents. </p>
<p>If you are a small enough company, this could put you under. Larger companies may still shy away from innovating on an existing idea to avoid the possibility of being sued even if they know they would win the court case (defending yourself in court isn't cheap).</p>
<h2 id="heading-what-you-can-do-to-help">What You Can Do To Help</h2>
<p><a target="_blank" href="https://blog.cloudflare.com/arkansas-engineer-wins-round-3-of-project-jengo-and-cloudflare-continues-to-win-at-the-patent-office/">I won round 3 of Project Jengo Redux</a>. This time CloudFlare was sued by Sable Networks ("a company that doesn’t appear to have operated a real business in nearly ten years"). Over what? Patents like "Identifying flows based on behavior characteristics and applying user-defined actions". I don't know about you, but that sounds like general Quality of Service that you'd see in almost any networking stack. And another patent "Mechanism for identifying and penalizing misbehaving flows in a network" sounds very similar and is 2 years older.</p>
<p>Participating in Project Jengo isn't hard. Pick a patent like I did with the ones above, and search for similar keywords and alternate jargon either in older research papers, blogs, or even <a target="_blank" href="https://patents.google.com/">Google Patent Search</a>. The goal is to find anything sufficiently similar and hopefully even more specific that predates the earliest priority date on the patents in question.</p>
<p>If you're a small business owner or an Open Source maintainer, this could be valuable practice for finding information to defend yourself if you end up in court with a patent troll.</p>
]]></content:encoded></item><item><title><![CDATA[Programmer Enhancing Supplements]]></title><description><![CDATA[NOTE: If you read nothing else, read the last paragraph If you haven't heard of this, it's a good thing to read up on for entertainment, but not for use in…]]></description><link>https://blog.codingcoyote.dev/programmer-enhancing-supplements</link><guid isPermaLink="true">https://blog.codingcoyote.dev/programmer-enhancing-supplements</guid><category><![CDATA[software development]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Tue, 15 Mar 2022 00:09:26 GMT</pubDate><content:encoded><![CDATA[<p><strong><em>NOTE: If you read nothing else, read the last paragraph</em></strong></p>
<h1 id="heading-the-ballmer-peak">The Ballmer Peak</h1>
<p><img src="https://imgs.xkcd.com/comics/ballmer_peak.png" alt="xkcd comic about the Ballmer Peak" /></p>
<p>If you haven't heard of this, it's a good thing to read up on for entertainment, but not for use in production. The idea is that as you get slightly more impaired, you are less likely to inhibit yourself from more creative solutions to problems. So a complex issue that you can't seem to get working reliable would conceivably be easier to work out with a the blood-alcohol levels associated with <a target="_blank" href="https://www.techtroupe.io/ballmers-peak/">Ballmer's Peak</a>. </p>
<h3 id="heading-does-it-work">Does It Work?</h3>
<p>I've heard people attribute the same effects to everything from marijuana to sleep deprivation and I don't buy it. I've had a drink or two to help me destress before approaching a project, but I've never found the code produced to be more efficient/maintainable than code I write without them. And if you look at <a target="_blank" href="https://skeptics.stackexchange.com/a/2841">studies and anecdotes</a> you may see a trend in its inconsitency.</p>
<p>Personally, the happy medium might be to have a couple away from the project (assuming it's legal for you to do so), take notes, and then come back to do the work unimpaired.</p>
<p><strong>So please, do not write important code under the influence.</strong></p>
<h1 id="heading-energy-drinks-andamp-sports-drinks">Energy Drinks &amp; Sports Drinks</h1>
<p>Most of my personal experience lies in the realm of Energy Drinks and the chemicals that go in them. Instead of recommending or discussing any brand of the beverages, let's talk about the individual chemicals in the "energy blends" and the perceived effects they have. I have started mixing my own at this point and I have included some affiliate links to the products I buy from Amazon in some of the headers below.</p>
<h3 id="heading-sugar">Sugar</h3>
<p>Not all energy drinks have sugar, but maintaining your blood sugar while coding is a must. <a target="_blank" href="https://www.mayoclinic.org/diseases-conditions/hypoglycemia/symptoms-causes/syc-20373685">Hypoglycemia</a> is bad for you in a number of ways (anxiety, confusion, etc). There is no reason to include sugar though and many energy drinks don't. Consider your diet and make sure you don't have issues in the middle of the next Death March.</p>
<h3 id="heading-caffeine">Caffeine</h3>
<p>The glorious stimulant that has powered many lines of code late into the night is not a good choice for everyone. Although studies suggest that levels of safe caffeine intake may be higher than previously thought, you should limit yourself. Personally caffeine helps me focus. On mornings where I don't have a cafeinated beverage of some kind, I feel sluggish and distracted. By 9 A.M. I'm usually headed to make tea if not grab a soda.</p>
<p> <a target="_blank" href="https://www.verywellmind.com/what-are-the-effects-of-caffeine-on-the-brain-21842#toc-common-side-effects">Side Effects</a> of caffeine can be fairly disruptive to writing code (ex. "excessive urination"). And you should be aware that some people are more sensitive to caffeine and should avoid it for health reasons.</p>
<h3 id="heading-l-theaninehttpswwwamazoncomgpproductb07w8ylhs5refppxyodtbsearchasintitleieutf8andamppsc1andampencodingutf8andamptagcodingcoyote-20andamplinkcodeur2andamplinkid0dd1b804d362206871c72108d36c5411andampcamp1789andampcreative9325"><a target="_blank" href="https://www.amazon.com/gp/product/B07W8YLHS5/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&amp;psc=1&amp;_encoding=UTF8&amp;tag=codingcoyote-20&amp;linkCode=ur2&amp;linkId=0dd1b804d362206871c72108d36c5411&amp;camp=1789&amp;creative=9325">L-Theanine</a></h3>
<p>A key component of my own drinks, L-Theanine is supposed to be helpful for countering the "jittery" effects of caffeine and it seems to work well fo me. Aside from anecdotal evidence though, studies have shown that L-Theanine reduces the effects of stress. It's <a target="_blank" href="https://draxe.com/nutrition/l-theanine/#Benefits">benefits</a> also include helping with sleep and anxiety. That's why it's often  combined with melatonine in sleep aids.</p>
<p>If you don't like the idea of trusting a supplement, you can get a good dose of caffeine and L-Theanine naturally with green tea. When I'm looking to sleep after coding, I brew a pitcher of green tea instead of relying on any mix I culd make myself.</p>
<h3 id="heading-l-carnitinehttpsamznto3ineeen"><a target="_blank" href="https://amzn.to/3inEEeN">L-Carnitine</a></h3>
<p>You probably <a target="_blank" href="https://www.webmd.com/vitamins/ai/ingredientmono-1026/l-carnitine">don't need L-Carnitine</a>. It's a somewhat common ingredient in energy drinks and I'm interested in adding it to my pre-workout mix, but it won't help you code. It's used to lower cholesterol, potentially for weight loss, etc. There's also some safety concerns about what medicines it may interact with.</p>
<h3 id="heading-taurinehttpsamznto3kc1wji"><a target="_blank" href="https://amzn.to/3KC1WJI">Taurine</a></h3>
<p>You probably <a target="_blank" href="https://www.webmd.com/vitamins/ai/ingredientmono-1024/taurine">don't need Taurine</a>. It's very comon in energy drinks and is supposed to help with the effectiveness of a workout. Althought it has "neurological benefits" it's not going to help you code better. I generally use some in my pre-workout mix.</p>
<h3 id="heading-inositolhttpsamznto3jbg8ef"><a target="_blank" href="https://amzn.to/3JbG8EF">Inositol</a></h3>
<p>You probably <a target="_blank" href="https://www.verywellmind.com/inositol-what-should-i-know-about-it-89466">don't need Inositol</a>. Another ingredient in some energy drinks meant to be used as a pre-workout (for wight loss apparently). I intend to try adding it to my pre-workout eventually.</p>
<p>###Choline
You most likely <a target="_blank" href="https://www.verywellmind.com/what-is-acetylcholine-2794810#toc-choline-and-acetylcholine">don't need Choline</a>. I have never seen a valid reason to take choline supplements. I saw this listed as an energy drink additive a few times, but from all the research I have seen, it does nothing for you unless you have a deficiency.</p>
<h3 id="heading-guarana">Guarana</h3>
<p>Guarana is just another source of caffeine.</p>
<h3 id="heading-ginseng">Ginseng</h3>
<p>Ginseng is a bit of a mixed bag in some of the studies they suggested possible effects with mental tasks, but generally I don't think there is enough evidence to support it's use.</p>
<h3 id="heading-b-vitamins">B Vitamins</h3>
<p><a target="_blank" href="https://www.verywellfit.com/b-complex-vitamins-89411">Supporting brain function</a> is one of many things B vitamins do for you. They are in most Energy drinks due to their role in turning food into energy, but they do have neurological benefits. I was recommended them to help with migraines (seems to work) and there isn't much <a target="_blank" href="https://www.drweil.com/vitamins-supplements-herbs/vitamins/can-any-b-vitamins-be-harmful/">risk</a> in the dosage typically found in energy drinks.</p>
<h2 id="heading-do-they-work">Do They Work?</h2>
<p>Some of them seem to. Anecdotally I can say that I feel like I code better with some of them in my system. And some of the research seems to promote that view. I'd advise extreme caution if you decide to mix your own, and a least a little consideration before picking up an Energy Drink before heading into the office. I sip on them over the course of the day the same way I would coffee or tea.</p>
<h2 id="heading-why-list-pre-workouts-at-all">Why List Pre-Workouts at all?</h2>
<p>I like them, and it seems like it helps me get a better workout in the mornings. Which leads me to...</p>
<h1 id="heading-excercise">Excercise</h1>
<p>The best code I've written is after a good 30+ minutes of cardio. Note that I do use a pre-workout before I get started. So usually I'm still feeling some of the effects from the energy drink ingredients above and feeling energized from the workout itself. </p>
<h3 id="heading-anxiety-and-stress">Anxiety and Stress</h3>
<p>I generally stop caring about 30 minutes in. That feeling of needing to focus on my feet and hands keeps me out of my own head long enough to think about the projects I'm working on or planning without as much self-doubt or other problems that are holding me back. </p>
<h3 id="heading-sleep">Sleep</h3>
<p>A good workout routine will improve your sleep and that will help you wake up without feeling like you just logged off. And good sleep is better for maintanable code. I've had many late night sessions where everything seemed fine only to wake up in the morning and barely remember what I was trying to do. And then not being able to read the code I'd written the night before.</p>
<h1 id="heading-conclusion">Conclusion</h1>
<p>There are no magic mixtures that will improve your code. Alcohol and other impairing substances have a very blunted benefit if any. Energy drinks are generally just not made for Cognitive tasks. Unless you just crave the flavor, Green Tea can get you caffeine and L-Theanine. And a good 
workout will put your head in a better space to code long-term.</p>
<p>On a side note, the energy dress mid targeting professional gamers and streamers have overlap with developers, but I've not found the couple I've tried to be better than caffeine with some L-theanine. </p>
]]></content:encoded></item><item><title><![CDATA[Easy Docs with Docusaurus]]></title><description><![CDATA[A brief recommendation and tutorial for Docusaurus. React.js power or simple Markdown files.]]></description><link>https://blog.codingcoyote.dev/easy-docs-with-docusaurus</link><guid isPermaLink="true">https://blog.codingcoyote.dev/easy-docs-with-docusaurus</guid><category><![CDATA[documentation]]></category><category><![CDATA[React]]></category><category><![CDATA[markdown]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Sun, 13 Feb 2022 21:55:09 GMT</pubDate><content:encoded><![CDATA[<p>It's been a rough few months since I last wrote anything. I normally did most of my work in C# with .Net, but at the end of October that changed. I've been through a crash course on React.js and the following death march. Then I finally came down with covid (managed to dodge it for most of the pandemic). But with all that, when I finally got back to updating the <a target="_blank" href="https://digitalcoyote.github.io/NuGetDefense/">documentation for NuGetDefense</a>, I wanted to be able to leverage my new familiarity with React while keeping the docs themselves simple enough that anyone could contribute. With that in mind, I found Docusaurus.</p>
<h2 id="heading-what-is-docusaurus">What Is Docusaurus?</h2>
<p><a target="_blank" href="https://docusaurus.io/">Docusaurus</a> is a framework for React.js that focuses on <a target="_blank" href="https://www.markdownguide.org/">MarkDown</a>. This means all the documentation is as easy to edit as a text file. Docusaurus doesn't even require React.knowledge to use. Settings are made in a well documented <code>docusaurus.config.js</code> file (which ends up being mostly JSON) and all the doc entries are .md or .mdx (the markdown equivalent of JSX) files.</p>
<h2 id="heading-why-not-just-build-a-doc-site-with-react">Why Not Just Build A Doc Site With React?</h2>
<p>I originally started with Blazor. I loved it, but .Net Core 3.1 it was too slow when used as a static site on GitHub. I switched to Gatsby because it has a lot of nice features and a good plugin system. But trying to write a doc website in 10 minute bursts was not great (and getting packages up to date was a hassle). </p>
<p>I completely configured, and deployed docusaurus in an hour initially. I'm not using any of the more advanced features, but that makes contributions to the base documents more approachable while still letting me handle routing, the home page, etc however I feel. It does support items like "Versioned Documentation", a built in Blog for the project, etc. But even the basic features include Dark Mode, a Basic Themes, Code Highlighting via <a target="_blank" href="https://prismjs.com/">prism.js</a>, and of course a menu with Table of Contents. </p>
<h2 id="heading-geting-started">Geting Started</h2>
<p>I like to use Yarn and Typescript, so I'll cover that here. The rest is mostly editing the markdown files and switching out images for your project. </p>
<h3 id="heading-setting-up-yarn">Setting Up Yarn</h3>
<p>Yarn's <a target="_blank" href="https://yarnpkg.com/getting-started/install">official documentation</a> indicates the preferred method now to use <a target="_blank" href="https://nodejs.org/dist/latest/docs/api/corepack.html">Corepack</a>. As of this writing Corepack is documented as <em>experimental</em> though. So you may choose not to use (shouldn't be difficult to switch later). I am assuming you have Node installed. If not, you can grab it from <a target="_blank" href="https://nodejs.org/">nodejs.org</a>.</p>
<h4 id="heading-without-corepack">Without Corepack:</h4>
<p>If you've never used Yarn before on this machine, youll need to install it globally  first with:  </p>
<pre><code><span class="hljs-built_in">npm</span> install yarn -g
</code></pre><p>Next we need to setup the project's yarn: </p>
<pre><code>yarn <span class="hljs-keyword">set</span> <span class="hljs-keyword">version</span> berry
</code></pre><p>You can run <code>yarn set version stable</code> to update this, but without running the first command, it will only install the latest version of <a target="_blank" href="https://yarnpkg.com/getting-started/migration">Yarn Classic</a>.</p>
<h4 id="heading-with-corepack">With Corepack:</h4>
<p>Dependant on your version of Node, you may need to install Corepack:</p>
<p>Node.js 16.10 and above: </p>
<pre><code>corepack <span class="hljs-keyword">enable</span>
</code></pre><p>Node.js 16.9 and below: </p>
<pre><code><span class="hljs-built_in">npm</span> install corepack -g
</code></pre><h3 id="heading-setting-up-docusaurus">Setting up Docusaurus</h3>
<p>Docusaurus has a CLI setup tool that takes care of getting you started:</p>
<pre><code>npx create<span class="hljs-operator">-</span>docusaurus@latest my<span class="hljs-operator">-</span>website classic <span class="hljs-operator">-</span><span class="hljs-operator">-</span>typescript
</code></pre><p>The important bit there is <code>--typescript</code>. Without that, you'll have to manually change any configurations or be stuck using javascript.</p>
<p>The run:</p>
<pre><code>yarn add <span class="hljs-operator">-</span><span class="hljs-operator">-</span>dev typescript @docusaurus<span class="hljs-operator">/</span>module<span class="hljs-operator">-</span><span class="hljs-keyword">type</span><span class="hljs-operator">-</span>aliases @tsconfig<span class="hljs-operator">/</span>docusaurus
</code></pre><p>Finally, the official docs say to add an "extends" to your tsconfig , but in my case it was added automatically so you can probably just double check the tsconfig for:</p>
<pre><code><span class="hljs-string">"extends"</span>: <span class="hljs-string">"@tsconfig/docusaurus/tsconfig.json"</span>
</code></pre><h4 id="heading-dont-use-typescript-for-configuration">Don't Use Typescript for Configuration</h4>
<p>Docusaurus and many other tools do not support using Typescript for your config files. You can transpile them yourself, but usually config files don't gain enough from Typescript usage to be worth adding an extra build step.</p>
<h2 id="heading-rebranding">Rebranding</h2>
<p>You probably don't want a website with docusaurus branding all over the place. There are a few key files and configs to change to match the project it covers.</p>
<h3 id="heading-images">Images</h3>
<p>These are the main three images used for branding. <a target="_blank" href="https://www.gimp.org/">GIMP - GNU Image Manipulation Program</a> can be used to generate them if you have a base image to start with.</p>
<ul>
<li><code>static/img/logo.svg</code></li>
<li><code>static/img/docusaurus.png</code></li>
<li><code>static/img/favicon.ico</code> </li>
</ul>
<h3 id="heading-settings">Settings</h3>
<p>Settings as a whole are likely to evolve more as the project does, but there are a few to look for. All of these are found in the <code>docusaurus.config.js</code> file in the root of the project.</p>
<ul>
<li>GitHub links: there are a few by default</li>
<li>Edit URL's: most of these link back to Docusaurus or FaceBook repos</li>
<li>Titles: A find/replace for Docusaurus will make finding these pretty easy</li>
</ul>
<h2 id="heading-deploy-and-enjoy">Deploy And Enjoy</h2>
<p>When you get ready to deploy:</p>
<pre><code><span class="hljs-attribute">yarn</span> build
</code></pre><p>after it finishes, the build folder is everything your static site needs to run. </p>
]]></content:encoded></item><item><title><![CDATA[Throw VS Throw e VS Throw new Exception()]]></title><description><![CDATA[Confusing yourself and others for the sake of hiding implementation details.]]></description><link>https://blog.codingcoyote.dev/throw-vs-throw-e-vs-throw-new-exception</link><guid isPermaLink="true">https://blog.codingcoyote.dev/throw-vs-throw-e-vs-throw-new-exception</guid><category><![CDATA[C#]]></category><category><![CDATA[.NET]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Sun, 10 Oct 2021 01:54:22 GMT</pubDate><content:encoded><![CDATA[<p>Throwing exceptions seems pretty simple to anyone experienced with C#. And yet I've seen misuse of them too many times. If used incorrectly you could send someone on a wild goose chase while trying to investigate an issue or confuse your end user til they end up opening s ticket or contacting support for what they could easily handle themselves.</p>
<p>There are three main methods of rethrowing an exception. Each has it's use, but the TL;DR is you should probably just be using <code>throw</code> and this is why:</p>
<h1 id="heading-throw"><code>throw</code></h1>
<p>You should almost always just use <code>throw</code>.  This passes the exception on so it can "bubble up" like it was never caught at all. This can be very useful for instances where you need to clean up anything that's an immediate concern but still allow the calling code to handle the error.</p>
<p>More importantly, <code>throw</code> does nothing else (except change the line number). As you'll see with the other usages below, <code>throw</code> is the only variant that throws the original exception. This means the stack trace is intact and usable by anyone investigating any issues. </p>
<p>Say you're making a library that provides methods for encrypting and decrypting files. If the file that is passed in doesn't exist, it eventually throws a null reference deep in your library. The stack trace bubbles up and you catch it, clean up any resources and rethrow the exception like this:</p>
<pre><code><span class="hljs-keyword">try</span>
{
     <span class="hljs-comment">// ...Some internal calls to encrypt/decrypt</span>
}
<span class="hljs-keyword">catch</span> (<span class="hljs-built_in">Exception</span> e)
{
     <span class="hljs-comment">// ...Clean up</span>
     <span class="hljs-keyword">throw</span>;
}
</code></pre><p>What do we get for a stack trace? A complete trail from calling code to the site of the null reference.  Useful for open source projects or closed source projects where internal details leaking out aren't an issue.</p>
<h1 id="heading-throw-e"><code>throw e</code></h1>
<p>There is one legitimate use for this: hiding details to sensitive implementations in a closed source app. It's not a great use and is more likely to cause you and your users pain in the future.</p>
<p>There is never a reason to use <code>throw e</code> in an open source application.</p>
<p>See <a target="_blank" href="https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2200">Warning CA2200</a>.</p>
<h1 id="heading-throw-new-customexceptione"><code>throw new CustomException(e)</code></h1>
<p>This is the better alternative to <code>throw e</code>. This is technically the same syntax you use to initially throw an exception anyway, and is perfect for situations where you need to hide implementation details of point the user towards a solution. </p>
<p>You've probably seen a stack trace before that had the term "innerException" in it. This is how you do that. </p>
<pre><code><span class="hljs-keyword">try</span>
{
     <span class="hljs-comment">// ...Some internal calls to encrypt/decrypt</span>
}
<span class="hljs-keyword">catch</span> (<span class="hljs-built_in">Exception</span> e)
{
     <span class="hljs-comment">// ...Clean up</span>
     <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> EncryptionException(e);
}
</code></pre><p>By creating a custom exception or using an existing exception that can use the original exception as an inner exception, we can provide more accurate details on what occurred and still preserve the original stacktrace. Or if we need to hide the implementation details, we can give an exception that describes the problem in enough detail that we aren't scratching heads later wondering what on the entire call chain was null.</p>
<p>Generally this is a better practice. If you are catching the exception, do something. Even if all you do is add some details that make it more obvious what went wrong. </p>
<h1 id="heading-caveats">Caveats</h1>
<h2 id="heading-throw-is-not-good-for-large-trycatch-blocks"><code>throw</code> is not good for large try/catch blocks</h2>
<p>I generally recommended using just <code>throw</code> and while that's generally true, it does have one side effect that can cause some pain. The line number is changed to the <code>throw</code> statement instead of the line that actually threw the exception. This is generally fine, but anyone wrapping a larger section of code (for whatever reason) should always wrap the exception with a custom exception type. It's more work to write and maintain, but the first time you have to hunt for an exception in 500+ lines of code, you'll thank yourself.</p>
<h2 id="heading-dont-use-exceptions-to-control-application-flow">Don't use exceptions to control application flow</h2>
<p>If you are handling exceptions and throwing them to bypass logic, you are playing a dangerous game. Consider handling exceptions in all but the unexpected scenarios and returning a value that indicates whether or not there was a problem. In my experience people pay more attention to return types than possible exceptions. If your return includes a case that handles the failure, they are more likely to plan for it and less likely to accidentally bypass important business logic because of an exception.</p>
]]></content:encoded></item><item><title><![CDATA[Testing Websites with Selenium]]></title><description><![CDATA[A Guide to Writing Easy To Read and Maintain Parallel Tests]]></description><link>https://blog.codingcoyote.dev/testing-websites-with-selenium</link><guid isPermaLink="true">https://blog.codingcoyote.dev/testing-websites-with-selenium</guid><category><![CDATA[selenium]]></category><category><![CDATA[Testing]]></category><category><![CDATA[C#]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Wed, 25 Aug 2021 22:55:35 GMT</pubDate><content:encoded><![CDATA[<p><a target="_blank" href="https://www.selenium.dev/">Selenium</a> facilitates running automated E2E tests as if you were a user accessing the website using the any browser with a supported WebDriver. This enables you to test things not normally available to unit and integration tests such as cross browser support. And using a well thought out strategy, can make it easy to maintain tests even for a complex difficult to test legacy code base. I'll be writing my examples in C# and some of it will be C# specific, but the concepts can apply to multiple languages.</p>
<h1 id="selenium-testing-architecture">Selenium Testing Architecture</h1>
<h2 id="webdriver-management">WebDriver Management</h2>
<p>The WebDriver is the object that actually sends commands to the browser. You have to have the webdriver that matches your browser (type and version). In C# this can be installed using the NuGet package for FirefoxDriver (controls Firefox) of ChromeDriver (controls Chrome) but other languages may require you to manually download a webdriver. <code>npm</code> hosts a <a target="_blank" href="https://www.npmjs.com/package/webdriver-manager">webdriver-manager</a> tool that can be used to keep your driver executables up to date.</p>
<p>I prefer to keep a single global webdriver for the entire test. I've been told that this seems like a code smell, but generally you won't need to control more than one browser at once, and each webdriver instance requires an executable to be launched, so creating new ones isn't cheap. This is meant to make it easier to write and maintain tests.</p>
<pre><code><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Browser</span>
{
       <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-title">ThreadLocal</span>&lt;<span class="hljs-title">IWebDriver</span>&gt; webDriver</span> = <span class="hljs-keyword">new</span> ThreadLocal&lt;IWebDriver&gt;();
       <span class="hljs-keyword">internal</span> <span class="hljs-keyword">static</span> IWebDriver WebDriver 
       {
              <span class="hljs-keyword">get</span> =&gt; webDriver.Value ?? Initialize();
              <span class="hljs-keyword">set</span> =&gt; webDriver.Value = <span class="hljs-keyword">value</span>;
       }

       <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> IWebDriver <span class="hljs-title">Initialize</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> TestSiteURL, <span class="hljs-keyword">string</span> PathToTestInputVideo = <span class="hljs-string">""</span>, <span class="hljs-keyword">bool</span> Headless = <span class="hljs-literal">true</span></span>)</span>
       {
              <span class="hljs-keyword">var</span> driverService = ChromeDriverService.CreateDefaultService();
              <span class="hljs-keyword">var</span> options = <span class="hljs-keyword">new</span> ChromeOptions();
              options.AddArgument(<span class="hljs-string">"--start-maximized"</span>);
              options.AddArgument(<span class="hljs-string">"--use-fake-device-for-media-stream"</span>);
              options.AddArgument(<span class="hljs-string">"--use-fake-ui-for-media-stream"</span>);
              <span class="hljs-keyword">if</span>(!<span class="hljs-keyword">string</span>.IsNullOrWhiteSpace(PathToTestInputVideo)) options.AddArgument(<span class="hljs-string">$"--use-file-for-fake-video-capture <span class="hljs-subst">{PathToTestInputVideo}</span>"</span>);
            <span class="hljs-keyword">if</span> (Headless)
            {
                options.AddArgument(<span class="hljs-string">"--headless"</span>);
                driverService.HideCommandPromptWindow = <span class="hljs-literal">true</span>;
            }

            options.AddArgument(<span class="hljs-string">"--incognito"</span>);
            options.AddArgument(TestSiteURL);
            <span class="hljs-keyword">var</span> driver = <span class="hljs-keyword">new</span> EventFiringWebDriver(<span class="hljs-keyword">new</span> ChromeDriver(driverService, options, TimeSpan.FromMinutes(<span class="hljs-number">5</span>)));

       }

<span class="hljs-comment">//... Static Wrappers for methods like FindBy</span>
}
</code></pre><p>What does ThreadLocal do? If written as a static wrapper for you webdriver, it enables centralizing all of your setup logic such that any test can access it and always gets the Webdriver that is owned by the current thread thanks to <a target="_blank" href="https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadlocal-1">ThreadLocal</a>. This means no passing around instances of a webdriver, you current test will always have access to it's own browser. And thanks to the property, when you go to get your webdriver, it will create a new one if the current one is null.</p>
<p>Why didn't we just use <code>new ChromeDriver()</code>? Most of that is to get Chrome to run completely silently in the background. And the media stream stuff allows us to use a video as fake webcam input in case it's needed for a test. <a target="_blank" href="https://peter.sh/experiments/chromium-command-line-switches/">Peter Beverloo</a> put together a page I reference quite often when I need to dig into more Chrome Switches. Firefox likely has similar documentation, but the only links I found were invalid and outdated.</p>
<h2 id="page-object-model">Page Object Model</h2>
<p>A design commonly used with Selenium is to treat each page as it's own class. These page objects make it clear what elements and code are usable where. And clarity is key in maintainable tests. Name your elements based on how you would describe them (ex. The text on a button or the location of a textbox) not what their ID/xpath is. This keeps your tests maintainable, readable and DRY.</p>
<pre><code><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LoginPage</span>
</span>{
       <span class="hljs-keyword">public</span> <span class="hljs-built_in">static</span> IWebElement UserName =&gt; Driver.FindElement(By.Id(<span class="hljs-string">"username"</span>));

       <span class="hljs-keyword">public</span> <span class="hljs-built_in">static</span> IWebElement Password =&gt; Driver.FindElement(By.Id(<span class="hljs-string">"pwid234wn"</span>));

}
</code></pre><p>Note that above I used ReadOnly properties. You could just as well Use GetUsername or GetPassword, but you do not want to store these element references for any length of time. As you browser goes about its business these references will become stale and will not work (depending on how your website is built this may vary).</p>
<h2 id="component-object-model">Component Object Model</h2>
<p>For components of a SPA such as a <a target="_blank" href="https://reactjs.org/">React</a> or <a target="_blank" href="https://angular.io/">Angular</a> website, the Page Object Model doesn't make immediate sense. In comes the Component Object Model. Each reused component of significant size has it's own class. If the component is a button, don't worry about it unless it has very peculiar behavior. Name each component in a way that makes it readily identifiable by behavior/purpose and any visual identifiers that make sense. The Component Object Model is about larger repeatable pieces that show up on various "Screens" of the SPA. </p>
<p>It can look pretty similar as well:</p>
<pre><code><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LoginDiv</span>
</span>{
       <span class="hljs-keyword">public</span> <span class="hljs-built_in">static</span> IWebElement UserName =&gt; Driver.FindElement(By.Id(<span class="hljs-string">"username"</span>));

       <span class="hljs-keyword">public</span> <span class="hljs-built_in">static</span> IWebElement Password =&gt; Driver.FindElement(By.Id(<span class="hljs-string">"pwid234wn"</span>));

}
</code></pre><p>This is also handy for websites build using the Page Object Model. If the Navigation Bar or Footer of your website is on every page, using the Component Object Model to Describe these is better than repeating the code on every page.</p>
<h2 id="action-flows">Action Flows</h2>
<p>Writing tests as a series of static calls to Pages and Components is repetitive and provides no guidance to the test writer as to what they can do next. By designing a framework of <a target="_blank" href="https://dotnettutorials.net/lesson/fluent-interface-design-pattern/">fluent</a> Action Flows for your site, you can facilitate readable tests that are written in a way that is simple enough that non-developers should be able to read, understand and modify the tests. This means if a method/element is missing from a Page Object, or an unexpected redirect occurs, you know instead of wondering why it cannot find an object or where to look for it's definition.</p>
<p>Checkout the fluent tests below:</p>
<pre><code><span class="hljs-selector-tag">public</span> <span class="hljs-selector-tag">void</span> <span class="hljs-selector-tag">Test</span>()
{
       <span class="hljs-selector-tag">User</span><span class="hljs-selector-class">.Login</span>(<span class="hljs-string">"SomePassword1"</span>);
       <span class="hljs-selector-tag">User</span><span class="hljs-selector-class">.OpenNotifications</span>()
              <span class="hljs-selector-class">.Click</span>(<span class="hljs-string">"Test Notification"</span>);
}

<span class="hljs-selector-tag">public</span> <span class="hljs-selector-tag">void</span> <span class="hljs-selector-tag">Test</span>()
{
       <span class="hljs-selector-tag">User</span><span class="hljs-selector-class">.Login</span>(<span class="hljs-attribute">username</span>: <span class="hljs-string">"UserPerson123"</span>, <span class="hljs-attribute">password</span>: <span class="hljs-string">"SomePassword1"</span>);
       <span class="hljs-selector-tag">User</span><span class="hljs-selector-class">.BrowseCategory</span>(<span class="hljs-string">"Shoes"</span>)
              <span class="hljs-selector-class">.FilterColor</span>(<span class="hljs-string">"Blue"</span>)
              <span class="hljs-selector-class">.FilterSize</span>(<span class="hljs-number">9</span>)
              <span class="hljs-selector-class">.FilterPriceMin</span>(<span class="hljs-number">4.50</span>)
              <span class="hljs-selector-class">.FilterPriceMax</span>(<span class="hljs-number">450</span>)
              <span class="hljs-selector-class">.ApplyFilters</span>();
       <span class="hljs-selector-tag">User</span><span class="hljs-selector-class">.AddToCart</span>(<span class="hljs-attribute">itemnumber</span>: <span class="hljs-string">"12345"</span>);
}
</code></pre><p>Not knowing what code was used to implement these, it's fairly obvious what the user is doing in both tests. There was a reason our classes from earlier weren't static. In addition to the static ReadOnly properties containing the element definitions, we are going to add some instance methods that use these definitions, but always return an object for the page/component that the user is looking at when they get done. </p>
<p>Our User class acts as a static entry point to the various Action Flows we might be entering:</p>
<pre><code><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span>
{</span>
      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> HomePage <span class="hljs-title">Login</span><span class="hljs-params">(<span class="hljs-built_in">string</span> username, <span class="hljs-built_in">string</span> password)</span>
      </span>{
           NavBar.LogIn.Click();
           LoginPage.UserName.SendKeys(username);
           LoginPage.Password.SendKeys(password);
           <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> HomePage();
      }

     <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-title">BrowseCategory</span><span class="hljs-params">(<span class="hljs-built_in">string</span> categoryName)</span>
     </span>{
          NavBar.Categories.Click(categoryName);
          <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> ProductSearchPage();
     }

     <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> NotificationsPage <span class="hljs-title">OpenNotifications</span><span class="hljs-params">()</span>
     </span>{
          NavBar.NotificationsButton.Click();
          <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> NotificationsPage();
     }
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ProductSearchPage</span>
{</span>
     <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> SelectElement FilterColor =&gt; <span class="hljs-keyword">new</span> SelectElement(Driver.FindElement(By.Id(<span class="hljs-string">"colorFilter"</span>)));

     <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">FilterColor</span><span class="hljs-params">(<span class="hljs-built_in">string</span> color)</span>
     </span>{
          FilterColor.SelectByText(Color);
          <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
     }
}
</code></pre><p>If any methods or elements are not likely to be useful outside of action flows, mark them internal. This will allow them to be used in the test library, but not in the actual test project. More importantly it means that it won't clutter their auto-complete.</p>
<h2 id="utility-methods">Utility Methods</h2>
<p>Anything that does not specifically interact with the website should go into a separate set of Static Utility Classes. These classes have no state and only host methods that are helpful for doing specific formula for constructing values to compare with website data. This could be calculating tax, getting the order total, parsing a file etc. These utility methods should generally be <a target="_blank" href="https://www.freecodecamp.org/news/what-is-a-pure-function-in-javascript-acb887375dfe/">Pure Functions</a>. Calling webservices or accessing files is fine as long as the method it's obvious and the data accessed will always be the same (or at least reasonably equivalent) for the test it is called in.</p>
<h2 id="running-tests-in-parallel">Running Tests in Parallel</h2>
<p>The key thing in this design is to never store anything at a static level. All the fluent methods will be thread safe because we are using static methods that invoke a ThreadLocal WebDriver. Now the only worries as far as parallel activity goes is the website being tested. If you start changing product prices in one test while trying to assert that they are correct in another, it will conflict eventually and produce false positive rest results.</p>
<h2 id="gotchas-and-pitfalls">Gotchas and Pitfalls</h2>
<h3 id="conditional-flows">Conditional Flows</h3>
<p>Methods within a single page that depend on conditionals can be time consuming and messy and remove control front the test. If you have to have them, do not clutter up the page or component models. Ideally you have explicit test cases where the path through the website is known and you expect each test to know where it will go and how it will behave.</p>
<p>For example, you have a store that offers free shipping when the subtotal is over $75. Any tests for totals should not use conditional logic to automatically exclude expected shipping cost. Yes it is convenient, but the first time you change that free shipping requirement or items that are never eligible for free shipping, you will have to go back and change those tests. </p>
<p>Utility methods that have are aware of these conditions can keep the conditions out of your tests and out of the page/components. It keeps your code DRY and makes updating tests easier when specifics of a condition change.</p>
<pre><code><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">ViewCartPage</span>
{
     <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> IEnumerable&lt;IWebElement&gt; ProductsElement =&gt; 
          Driver.FindElement(By.Id(<span class="hljs-string">"orderdIds"</span>))
               .FindElements(By.ClassName(<span class="hljs-string">"cartLineItem"</span>));

     <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> IEnumerable&lt;IWebElement&gt; SubtotalElement=&gt; 
          Driver.FindElement(By.Id(<span class="hljs-string">"subtotal"</span>));

     <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">decimal</span> Subtotal=&gt; 
          Decimal.Parse(SubtotalElement.GetText());

     <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-title">GetProductPrice</span>(<span class="hljs-params"></span>)</span>
     {
          <span class="hljs-comment">// Some logic to extract the price from each product's element</span>
     }
}

<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title">ShippingCost</span>
{
     <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">decimal</span> <span class="hljs-title">CalculateTotalShipping</span>(<span class="hljs-params"></span>)</span>
     {
          <span class="hljs-comment">// Some logic to check the products against preset data to see</span>
          <span class="hljs-comment">// which are eligible for shipping and what the total should be</span>
          <span class="hljs-comment">// and to check against the subtotal.</span>
     }
}
</code></pre><h3 id="external-dependencies">External Dependencies</h3>
<p>If your tests depend on an external database, webservice, etc you may be tempted to mock it. If this external resource is not under your control or is not part of your test environment, you should probably do it. Otherwise, consider what is not being tested by skipping it. </p>
<p>If your site generally uses an <a target="_blank" href="https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping">ORM</a> to target <a target="_blank" href="https://www.microsoft.com/en-us/sql-server">SQL Server</a>, but you add  a class with Oracle specific SQL and use a mock, you may miss the fact that a production database can't use that query. Or if you are calling a webservice that has recent changes, you may miss a breaking change due to mocks. When possible, setup your own backend systems that can have controlled state that are up to date with what will be used in production scenarios. When not reasonable (such as when targeting multiple Databases through ORM), mock it. The more time and complexity cost that get's added, the more coverage you could have added to your tests.</p>
<h3 id="test-state">Test State</h3>
<p>You will be likely want to store state about a test somewhere at some point. Preferrably this should be stored in a local variable inside the test method itself. Storing it on the Test object could cause it to affect other tests (depending on how your test runner works) and storing it in the framework as a static variable can massively increase the complexity (since you would need to use ThreadStatic or ThreadLocal in C#).</p>
]]></content:encoded></item><item><title><![CDATA[Using NUKE  for Cross-Platform Build Scripts]]></title><description><![CDATA[A short introduction to intuitive, IDE-native build and test setup scripts for CI and manual with NUKE]]></description><link>https://blog.codingcoyote.dev/using-nuke-for-cross-platform-build-scripts</link><guid isPermaLink="true">https://blog.codingcoyote.dev/using-nuke-for-cross-platform-build-scripts</guid><category><![CDATA[Build tool]]></category><category><![CDATA[Open Source]]></category><category><![CDATA[.NET]]></category><category><![CDATA[C#]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Sun, 15 Aug 2021 18:35:27 GMT</pubDate><content:encoded><![CDATA[<blockquote>
<p><a target="_blank" href="https://github.com/nuke-build/nuke">NUKE</a> is the most intuitive and productive build automation system for C#/.NET that runs cross-platform on .NET Core, .NET Framework, and Mono. </p>
</blockquote>
<p>Ever had to rig up a build script that felt more like a <a target="_blank" href="https://en.wikipedia.org/wiki/Rube_Goldberg_machine">Rube Goldberg machine</a>. Build this project, run this script, move these files, now publish that site, reset the app pool and then upload some changes, etc.  In a good CI server it may be as simple as putting in the steps one by one, and then hitting save. The display shows what will happen and the build server does the rest. But what happens when the build server goes down and someone needs to manually push a change? </p>
<p>In comes NUKE build scripts to save the day. The runner is packaged as a <a target="_blank" href="https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools">dotnet tool</a>. It runs your builds using a series of libraries and console applications written in pure .Net. And the build scripts you write in it are just .Net console applications. This means your build scripts can live in your solution, in your source control repo, and in your IDE. No more worrying if that syntax was right or switching context for msbuild tasks. </p>
<p>Define Targets and their relationships that are executed.</p>
<pre><code>    Target BuildProject =&gt; _ =&gt; _
        .DependsOn(Restore)
        .Executes(() =&gt;
        {
            DotNetBuild(s =&gt; s
                .SetProjectFile(Solution)
                .SetConfiguration(Configuration)
                .SetAssemblyVersion(GitVersion.AssemblySemVer)
                .SetFileVersion(GitVersion.AssemblySemFileVer)
                .SetInformationalVersion(GitVersion.InformationalVersion)
                .EnableNoRestore()
                .SetProperty(<span class="hljs-string">"PackageVersion"</span>, GitVersion.NuGetVersionV2));
        });
<span class="hljs-comment">//... any other tasks such as Restore</span>

<span class="hljs-keyword">public</span> <span class="hljs-built_in">static</span> <span class="hljs-keyword">int</span> Main() =&gt; Execute&lt;Build&gt;(x =&gt; x.BuildProject);
</code></pre><p>One of my own personal uses is setting up or resetting testing environment resources (copying/deleting/downloading files or publishing websites) prior to running the build and it's tests.</p>
<h3 id="the-nuclear-option">The Nuclear Option</h3>
<p>You have the option of getting rid of most of your CI server's build scripts and rewriting everything in C#. Changes to the build scripts can go through code review and slow steps or things that can be done in parallel can be handled just as you would when optimizing any other piece of code.</p>
<p>If you're still stuck on a legacy build server like Cruise Control. NUKE can offer a way around the many limitations imposed by those systems reducing the entire script down to a readable glob of C#. </p>
<h3 id="only-for-manual-builds">Only For Manual Builds</h3>
<p>Not ready to switch things over entirely? You can write up build scripts for individuals to use when testing their code or when a special deploy is needed. Perhaps it's currently a manual process that you don't want eating up your build server minutes? Save those minutes and avoid downtime and human errors with a reliably engineered build script.</p>
<h2 id="open-source-projects">Open Source Projects</h2>
<p>Open Source projects greatly benefit from having a simple build script. Not having to look up msbuild options and download specific resources prior to a build can make things much easier for new contributors. In fact the <a target="_blank" href="https://dotnetfoundation.org/projects/net-foundation-project-committee#eligibility-criteria">.Net Foundation</a> requires a reproducible build script:</p>
<blockquote>
<p>The project contains a build script that can produce deployable artifacts that are identical to the official deployable artifacts, with the exception of code signing (Exception may be granted for strong name keys, though strongly encouraged to be committed. Exception relies on OSS signing being in the build script for public builds).</p>
</blockquote>
<p>This can be especially important for testing as well. If you require them to run all the applicable tests prior to merging a PR (and you don't have a CI option that will run their PR for you) you can provide a target through NUKE that enables running the scripts first and eases your confirmation of this result.</p>
<h2 id="the-catch">The Catch</h2>
<p>Currently NUKE does not inherently support parallel build targets (see <a target="_blank" href="https://github.com/nuke-build/nuke/pull/526">PR 526</a> and <a target="_blank" href="https://github.com/nuke-build/nuke/issues/162">Issue 162</a> ). To get something in paralle you will need to use a feature such as tasks to do this:</p>
<pre><code>    Target Build =&gt; <span class="hljs-function"><span class="hljs-params">_</span> =&gt;</span> _
        .DependsOn(Restore)
        .Executes(<span class="hljs-function">() =&gt;</span>
        {
            <span class="hljs-keyword">var</span> BuildTask = Task.Run(<span class="hljs-function">() =&gt;</span> DotNetBuild(<span class="hljs-function"><span class="hljs-params">s</span> =&gt;</span> s
                .SetProjectFile(Solution)
                .SetConfiguration(Configuration)
                .SetAssemblyVersion(GitVersion.AssemblySemVer)
                .SetFileVersion(GitVersion.AssemblySemFileVer)
                .SetInformationalVersion(GitVersion.InformationalVersion)
                .EnableNoRestore()
                .SetProperty(<span class="hljs-string">"PackageVersion"</span>, GitVersion.NuGetVersionV2)));

            <span class="hljs-keyword">var</span> GetFilesForPublish = Task.Run(<span class="hljs-function">() =&gt;</span> Task.Delay(<span class="hljs-number">5</span>)); <span class="hljs-comment">///Some Arbitrary task that needs to happen before package can be deployed</span>
            BuildTask.Wait();
            GetFilesForPublish.Wait();
        });
</code></pre><h2 id="support-this-project">Support This Project</h2>
<p>NUKE is provided as an Open Source project. If NUKE makes your life easier, consider sponsoring <a target="_blank" href="https://github.com/sponsors/matkoch">matkoch</a> the maintainer. </p>
]]></content:encoded></item><item><title><![CDATA[Crash Course for the Self Taught Developer]]></title><description><![CDATA[A guide to things a self taught software developer should know about after learning to code]]></description><link>https://blog.codingcoyote.dev/crash-course-for-the-self-taught-developer</link><guid isPermaLink="true">https://blog.codingcoyote.dev/crash-course-for-the-self-taught-developer</guid><category><![CDATA[coding]]></category><category><![CDATA[software development]]></category><category><![CDATA[learn coding]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Sat, 14 Aug 2021 20:53:28 GMT</pubDate><content:encoded><![CDATA[<p>I learned to code the hard way, but it was worth every minute. Unfortunately, not all of us who teach ourselves are able to pick up the same terminology and patterns used by others. In fact, I hadn't heard about "Design Patterns" until I first listened to a podcast about it a few years after I started studying. Not all of these just apply to self-taught coders either. I've had junior developers a year out of college look confused when I've mentioned a few of these.</p>
<h2 id="design-patterns">Design Patterns</h2>
<p>What is a design pattern? Why should you care? And will it change how you code? </p>
<p>A design pattern is a simple or complex pattern that naturally evolves to solve some problem.  I'll cover a couple here, but there are tons or resources on individual patterns, variations on these patterns and libraries and frameworks that help to achieve these patterns for almost any language if you give a quick search.</p>
<h3 id="factory-pattern">Factory Pattern</h3>
<p>The Factory Pattern is one such pattern that you may have already used without knowing what it was. Factories are useful for creating objects where the consumer doesn't have to know much about the object it's creating. It's especially useful when you have need to generate an object or some other data structure, but that structure may change over time. </p>
<p>Maybe it's user creation for a website or a desktop client. The UI shouldn't need to declare every last detail of a user and it shouldn't have direct access to the database where the user is stored (for security). So what do we do? We make a method on our back end (server) that the front end (UI) can call to get a user created. The "Factory" in the back end can look up any preferences the UI needs to know about and grab extra details like the user's display name or avatar before sending the object back to the front end. And then the management software you use can also use this same factory to retrieve user details that need to be edited. Thanks to the factory, you can add methods to make the user an admin, moderator or guest all without having to change how much either UI needed to know about the user and how it's stored in the database.</p>
<h3 id="visitor-pattern">Visitor Pattern</h3>
<p>The visitor pattern is less concerned about making low level updates easier and more concerned with not breaking legacy code. By making a special construct that only "visits" legacy code, you can extend it's functionality without altering the behavior of legacy code.</p>
<p>Say our website has new features for selling products that some users can opt into. It could be quite reckless to jam this new set of behaviors into our existing product, storefront, and user classes. But at the same time, rewriting all of that code or, even copying some of it could cause issues with our new code that were already solved in the old. The visitor class we create can use our old constructs (users, products, etc) but add new functional behaviors on top of them. It's not fool-proof, but it's safer for brittle legacy code and an option when changing the source code of what we are visiting is not an option.</p>
<h2 id="jargon">Jargon</h2>
<p>Jargon is any special word or that only has meaning within a specific field. You likely came across some of it while learning. "Compile", "variable", and "function" are all jargon.</p>
<h3 id="copy-pasta">Copy-Pasta</h3>
<p>Despite anything you may have heard, copying code is OK much of the time. Copying code with little to no modifications is what we call "Copy-Pasta" (like Copy and Paste).  Generally this is used with negative connotations and implies that the copied code was not thoroughly examined or adapted to it's new usage.</p>
<h3 id="allocation">Allocation</h3>
<p>You may have heard someone tell you that your code or some method is slow because of all the "allocations". An allocation is when memory is set aside for use. This takes time and generally slows down the code. Zero-allocation code usually uses some tricks to pass the data around without allocating more memory addresses to it. This means better performance all around.</p>
<h3 id="garbage-collection">Garbage Collection</h3>
<p>This doesn't apply to every language, but many such as C# and JavaScript (EcmaScript) have mechanisms built in to "free" memory that was allocated. This takes time, but also helps to prevent memory leaks and lower the amount of memory the program uses.</p>
<h3 id="runtime">Runtime</h3>
<p>This can have different meanings depending on the context. Runtime can literally be the amount of time it took to run or more commonly the program that manages and runs your code. Kotlin and C#  both have runtimes. C# uses a version of the .Net runtime called the <a target="_blank" href="https://docs.microsoft.com/en-us/dotnet/standard/clr">CLR (Common Language Runtime)</a> and Kotlin uses the  <a target="_blank" href="https://www.ibm.com/cloud/learn/jre">JRE (Java Runtime Environment)</a>. The runtime is responsible for a number of things such as garbage collection or memory allocation.</p>
<h3 id="stack-and-heap">Stack and Heap</h3>
<p>These are rather low level terms that may not matter much to your early learning, but knowing what they are will help as you grow. Stack is the memory that the processor has at hand and is generally the fastest place to put things. It is much smaller than the "Heap" though. For larger objects, you generally put them in the Heap.</p>
<h3 id="references-pointers-and-values">References, Pointers, and  Values</h3>
<p>These are different ways to describe a bit of data. A reference or a pointer is more like the name of a person or their home address than the person themselves. A value can be considered the actual person. So if I gave you Bob's address, you could go tell him what you needed, but I can also go his house and tell him what I need. If Bob wanted to give you his house, he'd have to actually make a new house since he is still living in his. Because of this, it's generally better for performance to not make Bob build a new house every time someone wants to look at his living room and instead just give them a pointer or a reference.</p>
<h3 id="kiss">KISS</h3>
<p>"<strong>K</strong>eep <strong>I</strong>t <strong>S</strong>imple <strong>S</strong>tupid" - We all have a tendency to over engineer things. Want to a sandwich? Balance on one foot, flatten a spoon into a knife, juggle the condiments...etc.  KISS is one of the most important philosophies you will ever follow. Simple code is easier to read, easier to maintain, and less likely to break. Consider trying to make that sandwich by adding bread and the filings with no ceremony or hoop jumping. In more realistic terms, this is avoiding the drive to "do something interesting" or bleed as much performance as possible out of you code when you don't need to. That function may demonstrate the neatest quirk, but if it's difficult to understand, you or whoever has to read it next will hate you.</p>
<h3 id="solid">SOLID</h3>
<p>SOLID is an acronym for a tried and true set of philosophies to guide your code:</p>
<p><strong>S</strong>ingle Responsibility Principle
Each piece of code should do one thing and do it well. A class/function for reading a file should not also attempt to parse it out and display it. Split the functionality up and use higher level functions/classes to "handle the file". And this also means it's OK to have higher levels of responsibility. A Manager runs a store, but the employees handle the sale and the register handles the transaction.</p>
<p><strong>O</strong>pen-Closed Principle
Code should be <em>open</em> for extension but <em>closed</em> for modification. Extending existing code with new fields and functions is much safer than changing existing fields and functions. And the more higher level classes/functions that depend on it, the less it should be changed. If you made a Pizza class, it's OK to extend it with more toppings, but if you change the pepperoni to salami, the consumers of that pizza are still going to expect pepperoni. Much like with pizza however, this is not always safe. In code, if a switch is expecting 1 of 3 values and errors out if it's not one of those, adding a fourth possible value could cause unexpected errors.</p>
<p><strong>L</strong>iskov Substitution Principle
The idea here is that if you request a specific type, you need to be able to handle any derived type without knowing what the type is. Example, a Pizza Place delivery person should be able to deliver any kind of Pizza Place product without knowing exactly what he is delivering. Specialty pizza, wings, soda, etc. All the delivery person needs to know is that it has or has not been paid for and where it needs to go. If I'm calling a method to get an integer, I need to be able to handle that integer without knowing whether or not it was in a trusted./expected range. This means having logic to handle unexpected numbers, but not having to worry about decimals and other fractions.</p>
<p><strong>I</strong>nterface Segregation Principle
If you have similar functionalities and interfaces shared by a number of consumers, you should split them up into individual interfaces/behaviors. At the Pizza Delivery Place, Vegetarian and Meat pizzas both share toppings, crust and sauce interfaces. Grouping these into a single pizza interface would work for calzones as well. But what about those wings? Now we would have to re-implement the interface for sauce to leave out the crust and toppings interface. But now we also sell Cookies that have toppings but no sauce. These all have to be menu items to be sold. So by separating these out, a Cookie just sets the same toppings used by the pizza, but knows that it can only have chocolate chips, sprinkles, or bacon (because the world demands bacon) and a pizza knows it can also have chocolate chips, but not with a marinara sauce.</p>
<p><strong>D</strong>ependency Inversion Principle
Your code should always depend on abstractions instead of the concrete implementations. This is especially important when you need to switch out something. If I had a class that was responsible for storing objects in a database, I should be able to switch it out with a class that stores them on disk or in memory without the rest of my code caring. This can be even more important when switching between databases. If my User object is sanitizing the name field for SQL Injections, it may pull back that same sanitized name instead of the correct name on the next go around. And worse, sanitizing it for one database may leave out necessary changes in another. </p>
<p>Rereading these once a year helps me to internalize some of the concerns and catch myself anytime I would violate them. I can never remember the actual terms for each letter, but knowing what they are can keep you up to speed if someone mentions them</p>
<h3 id="dry">DRY</h3>
<p><strong>D</strong>on't <strong>R</strong>epeat <strong>Y</strong>ourself. When someone wants to DRY their code, they didn't just spill that caffeinated nectar from the gods on their laptop. DRY is an acronym for making sure you reuse code whenever possible. Got code for notifying users about an update? Extract a function/class to notify users of anything instead of rewriting that bit every time you need to send a notification. This is most important when it comes time to fix bugs. Ever played whack-a-mole? Try getting bug reports about missing notifications or bad format in notifications the day after you just fixed it. And again and again. Reusing the code makes it easier to fix it in one place then move on.</p>
<h3 id="spaghetti-code">Spaghetti Code</h3>
<p>Much like that saucy pasta, some code strings down into the depths only to spiral back up again. This means anytime you have a error message saying where the error came from, it points to a few hundred lines of possible failure points. This is generally a violation of the Single Responsibility Principle and can be very difficult to clean up,</p>
<h3 id="lift-and-shift">Lift and Shift</h3>
<p>Lift and shift is an alteration to existing code that <em>should</em> be low risk. This could be moving behaviors into a library so it can be shared with another application or moving the code to a newer version of the framework/runtime.</p>
<h2 id="not-everyone-knows-x">Not Everyone Knows X</h2>
<p>Sometimes those who taught themselves do not understand how difficult some concepts are because they hit them earlier than a structured system would teach them. Multi-threading and parallel programming came easier to me because that's what I started with. In fact working in synchronous only code was a bit of a struggle at first because long running IO had to be waited on and optimized differently. But at the same time, a lot of people talk about it being easier. Package management is the same way. I've talked to developers that don't know how npm or nuget package resolution works because they were taught to ignore that and focus on the "Fundamentals" that I had to learn later. Always approach subjects assuming everyone knows about it but don't downplay it no matter how simple it is.</p>
<h2 id="testing">Testing</h2>
<h3 id="unit-integration-e2e-and-black-box-testing">Unit, Integration, E2E and Black box Testing</h3>
<p>Unit tests are tests that target the smallest testable portion of the code. They offer the least immediate protection overall and take the most time, but ensure the very foundations of the code work. They often require faking out any dependencies and as such can be used to test scenarios that are not possible with current code but could be in the future.</p>
<p> Integration tests combine these units and offers the greatest amount of  immediate protection for the least effort. This covers all possible interactions of units and does little to no faking of interfaces and dependencies.</p>
<p>End to End or E2E tests focus on flows from the top of the program and offer a lot of protection for a little more work than integration tests. This can overlap a lot with integration test cases and black box test cases.</p>
<p>Black box is interesting as it almost requires the tests to have no insight into the implementations of the code itself. These are generally the same as E2E tests but ignore the limits and restrictions of the code. This is most useful for scenarios where you really want to know how a proprietary piece of software (such as Chrome or Firefox) interacts with your software. By ignoring implementation and focusing on how the user will approach the software, you can catch some interesting or rare issues that are not likely to occur in tightly controlled E2E test cases. This also means they can be much flakier than E2E tests. This can be compared to the type of testing that is often done by manual QA testers.</p>
<h3 id="not-everyone-tests">Not Everyone Tests</h3>
<p>This one came as a bit of a shock to me. After reading the importance of testing on many sites and in countless books, I found almost immediately that testing can be seen as a waste of time by some. Automated testing is even less appreciated to some as they can be seen as a drag on resources instead of adding value. This includes security testing. While it's not deadly to avoid testing, it certainly makes adherence to best practices that much more important. That said testing does slow down production and has an impact on the bottom line (disregarding the cost of bugs).</p>
<h2 id="it-doesnt-need-to-be-optimized-yet">It Doesn't Need to Be Optimized Yet</h2>
<p>Premature Optimization is the source for a lot of headache. Learning those optimal methods til they become second nature is great, but recognizing when you should avoid them is better. If a user facing program reacts in under 150ms, you probably don't need to spend hours shaving that down. If a high-traffic method on a server is taking over a second per call, you could probably save money and get better throughput with some tweaks, tricks and dirty hacks. Also remember, that many languages optimize themselves to a degree when they run. If you just really enjoy optimization and nothing else needs your attention, still give it a second though if it's frequently modified code. Optimized code can be harder to read, more brittle and sometimes nullified by changes down the line.</p>
<h2 id="seniority">Seniority</h2>
<p>If you stop learning now, no matter how long you code, you will stay at the same level. I have worked with people with a year of experience that were well qualified to be called an Intermediate Developer and I have seen those 2-3 years in who are still Junior. At the same time 15 years of experience does not a Senior Developer make. Many positions will give a length of experiences as a reference for how long you need to have worked in a field or which degree you have to have. Give them enough work to prove you know what you are talking about and many will waive those requirements. And some will not. Find a place that values you that you taught yourself and you will be far happier than you could be somewhere that has a hard degree requirement.</p>
<h2 id="you-dont-need-x">You Don't Need X</h2>
<p>New languages, frameworks, features, and so on are often unnecessary. <a target="_blank" href="https://github.com/you-dont-need">You Don't Need</a> is an awesome GitHub account with a large number of examples of why you don't need things. I've seen websites using JQuery just to get functionality that is part of modern JavaScript and BootStrap used in place of css just for the grid. Advancements and innovations are great, but rewrites are costly and dependencies often have a hidden cost (like load times or file size).</p>
<h2 id="security">Security</h2>
<p>By the headlines in the news, we should all know that we need to be concerned with security. This includes internalizing safe coding practices and accepting when others point out potential security flaws in our code. We also need to learn about any features our chose stack offers to increase security.</p>
<h2 id="and-more">And More</h2>
<p>I could spend a lot more time writing things I learned early on and things I wish I had known earlier but I've written a good bit. If anyone has suggestions, I'll add em. If anyone just feels like they need to learn more: listen to podcasts, read blogs, and work on side projects. That will keep you going.</p>
<iframe src="https://github.com/sponsors/digitalcoyote/button" height="35" width="116" style="border:0"></iframe>]]></content:encoded></item><item><title><![CDATA[NuGetDefense v3.x Update]]></title><description><![CDATA[NuGetDefense 3.0.0-pre0004 has been released]]></description><link>https://blog.codingcoyote.dev/nugetdefense-v3x-update</link><guid isPermaLink="true">https://blog.codingcoyote.dev/nugetdefense-v3x-update</guid><category><![CDATA[.NET]]></category><category><![CDATA[C#]]></category><category><![CDATA[Security]]></category><category><![CDATA[dotnet]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Tue, 10 Aug 2021 00:56:49 GMT</pubDate><content:encoded><![CDATA[<p>3.x is coming together. What does the <a target="_blank" href="https://github.com/digitalcoyote/NuGetDefense/releases/tag/3.0.0-pre-0004">4th prerelease of NuGetDefense 3.0.0</a> bring?</p>
<h4 id="300-pre0004-changes">3.0.0-pre0004 Changes</h4>
<ul>
<li>Added a <code>ScanOptions</code> object for NuGetDefense.Lib to make it less likely that new options can be added and defaulted without breaking consumers.</li>
<li>Added two subcommands to NuGetDefense<ul>
<li><strong>Update</strong> | Triggers an update of the Embedded NVD Source</li>
<li><strong>Recreate-NVD</strong> | Recreates the NVD Source in case updating is not an option (for edge case updates requiring a full rebuild)</li>
</ul>
</li>
</ul>
<h2 id="whats-next">What's Next?</h2>
<p>Next to be worked will be a long awaited caching solution. I'm looking at a per machine sqlite database that is stored in an overrideable location on each machine. For organizations, there will be a premium offering of a cloud based cache and a server that can be run on the local network to handle caching. Now is the time to speak out if you have any suggestions or concerns about caching.</p>
<p>Additionally, the query for the GitHub Advisory Database is ready and a new remote source will be added to facilitate it's usage.</p>
<p>Once 3.0.0 is ready, I'm going to overhaul the doc site. While I'm a huge fan of server side blazor, the client side version is generally slow to startup. I'm looking at simplifying it a lot and expanding the documentation to better cover the old features as well as the new ones coming in.</p>
<p>I greatly appreciate everyone who helps to give feedback on the prereleases.</p>
<iframe src="https://github.com/sponsors/digitalcoyote/button" height="35" width="116" style="border:0"></iframe>]]></content:encoded></item><item><title><![CDATA[Generating Realistic Test Data with Bogus.Net]]></title><description><![CDATA[A short introduction to generating realistic test data with Bogus.Net.]]></description><link>https://blog.codingcoyote.dev/generating-realistic-test-data-with-bogusnet</link><guid isPermaLink="true">https://blog.codingcoyote.dev/generating-realistic-test-data-with-bogusnet</guid><category><![CDATA[Testing]]></category><category><![CDATA[.NET]]></category><category><![CDATA[C#]]></category><category><![CDATA[Open Source]]></category><category><![CDATA[opensource]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Mon, 09 Aug 2021 02:04:39 GMT</pubDate><content:encoded><![CDATA[<p>Bogus is a .Net library for generating test data that uses the datasets of faker.js. This goes a few steps beyond simply accessing random entries from a collection and using them in a test. Bogus facilitates writing generators with complex rulesets to generate realistic and contextual test data for demos or E2E tests. </p>
<h4 id="license">License?</h4>
<p>Before using or suggesting any library, you need to understand it's license. Luckily the <a target="_blank" href="https://github.com/bchavez/Bogus/blob/master/LICENSE">license for Bogus.Net</a> is MIT/ BSD(3-clause) meaning you can use it personally, use it for commercial use, and even modify and redistribute it without worrying about the license on your own project.</p>
<h2 id="what-can-bogus-do-for-me">What Can Bogus Do For Me?</h2>
<p>Let's cover a simple object that most people probably have: User.</p>
<pre><code><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span>
</span>{
   <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Username;
   <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> FirstName;
   <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> LastName;
   <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> UserAddress;
}
</code></pre><p>We can use bogus to create a faker that generates users on demand. There's a couple syntax options for this but I generally use the Rules method to add a set of complex rules for data generation.</p>
<pre><code><span class="hljs-built_in">public</span> Faker&lt;<span class="hljs-keyword">User</span>&gt; testDataGenerator = <span class="hljs-built_in">new</span> Faker&lt;<span class="hljs-keyword">User</span>&gt;((faker, <span class="hljs-keyword">user</span>) =&gt; {
   var person = <span class="hljs-built_in">new</span> Bogus.Person();
   <span class="hljs-keyword">user</span>.UserName = person.Email;
   <span class="hljs-keyword">user</span>.FirstName = person.FirtName;
   <span class="hljs-keyword">user</span>.LastName = person.LastName;
<span class="hljs-keyword">user</span>.Address = faker.Address.FullAddress();
};
</code></pre><p>Why did we make a Person instead of just using values from the Email and Name datasets? This will generate an email address based on the Person created. </p>
<h2 id="why-not-roll-my-own-fake-data">Why Not Roll My Own Fake Data?</h2>
<p>Fake data generation is easy and somewhat trivial. Ransome numbers and text could be created for almost any class. So why should you bother learning a specific API that offers realistic data? Demoing a project to users, investors, or your coworkers is the more apparent use. </p>
<p>Realistic data makes a project look more professional. Think about what Netflix would feel like if all the pictures were randomly colored blocks and all the titles and descriptions were just random lengths of text? Not very, pretty is it? What about Twitter? Random keyboard mashing instead of realistic Tweets? The image is more clear when you see data that makes sense. Even <a target="_blank" href="https://lipsum.com/">Lorem Ipsum</a> text is better since at a glance it looks like real text. Without having to imagine real text and imagery and deciphering where real images would sit, they can focus on the look, feel and function of your app.</p>
<p>And even if you're just making something for yourself, the benefit to testing is colossal. Not using realistic data can cause you to not notice unexpected truncation, displaying the wrong field etc. You may be tempted to set all your products up with names like "Product 1", "Product 2" and prices such as $0.01 and $0.02 respectively. but with Bogus you can have "Tasty Fresh Cheese", "Handcrafted Steel Chair" and "Ergonomic Granite Shirt". Ok, so not all of them are super realistic. Still better than "Product 4052". My coworkers regularly get a laugh from some of our more absurd bits of data and have even caught issues due to the attention these caused. </p>
<p>Bogus has a whole host of <a target="_blank" href="https://github.com/bchavez/Bogus#bogus-api-support">datasets</a> for almost any use. And Premium Bogus extensions add even more support for specific fields such as healthcare or movies and TV shows and even GPS data that offer contextual data for demoing projects or testing.</p>
<p>Can't get approval to purchase a license? The community has written several extensions that add datasets such as <a target="_blank" href="https://github.com/SimonCropp/WaffleGenerator">seemingly realistic random prose</a> . And Bogus provides all the tools you need to add your own datasets instead of engineering your own system to handle them.</p>
<h2 id="but-how-do-i-test-classes-with-this">But How Do I Test Classes With This</h2>
<p>If your first instinct was to add a Faker field to  every object you want to test, STOP! Polluting production code with test code is rarely a good idea. </p>
<p>I personally have static "generators" that I use to generate data with Bogus:</p>
<pre><code>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title">TestData</span>
    {
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-title">Faker</span>&lt;<span class="hljs-title">User</span>&gt; UserGenerator</span> = <span class="hljs-keyword">new</span> Faker&lt;User&gt;().Rules((faker, user)=&gt;
        {
            <span class="hljs-keyword">var</span> person = <span class="hljs-keyword">new</span> Bogus.Person();
            user.Username = person.UserName;
            user.Email = person.Email;
            user.FirstName = person.FirstName;
            user.LastName = person.LastName;
            user.UserAddress = faker.Address.FullAddress();
        });
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">TestClass</span>
    {
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">GenreateUsers</span>(<span class="hljs-params"></span>)</span>
        {
            <span class="hljs-keyword">var</span> user = TestData.UserGenerator.Generate();
            <span class="hljs-keyword">var</span> userArray = TestData.UserGenerator.GenerateLazy(<span class="hljs-number">500</span>).ToArray();
            <span class="hljs-keyword">var</span> userList = TestData.UserGenerator.GenerateBetween(<span class="hljs-number">1</span>, <span class="hljs-number">5000</span>).ToList();
        }
    }
</code></pre><p>Using Generate, I can get a single test user. I can plug this user into the test and try creating an account, adding items to the cart, or starting a collaborative session (or whatever your applicatin does). But what if I need to run a report for 500 users? GenerateLazy Lets you Generate a specific number of users using LINQ's deferred execution just like a calling to a database so you can take any other actions before retrieving an array (consider using a db mocking framework for actual database related test). Generate forever would give you the same results, but generates the users up front. </p>
<p>Not sure how many Users to create? Need to make sure no one "fixes" a test by targeting the 500 users you are creating. GenerateBetween can get a quantity within your limits. Keeping it random while still large enough to matter and small enough to not pushing the limits of scalability.</p>
<p>What if you use the same user class for free and paid users in addition to users that are staff, admins and moderators? Maybe you just need more targeted data for some tests. Bogus supports that too. Bogus RuleSets allow writing specific rules for each set of generators you need. ("default" is internally applied to any rules without a ruleset)</p>
<pre><code><span class="hljs-built_in">public</span> static Faker&lt;<span class="hljs-keyword">User</span>&gt; UserGenerator = <span class="hljs-built_in">new</span> Faker&lt;<span class="hljs-keyword">User</span>&gt;().Rules((faker, <span class="hljs-keyword">user</span>) =&gt;
    {
         var person = <span class="hljs-built_in">new</span> Bogus.Person();
         <span class="hljs-keyword">user</span>.Username = person.UserName;
         <span class="hljs-keyword">user</span>.Email = person.Email;
         <span class="hljs-keyword">user</span>.FirstName = person.FirstName;
         <span class="hljs-keyword">user</span>.LastName = person.LastName;
         <span class="hljs-keyword">user</span>.UserAddress = faker.Address.FullAddress();
        <span class="hljs-keyword">user</span>.Status = faker.Random.Enum&lt;Statuses&gt;();
     }).RuleSet("FreeUsers", <span class="hljs-keyword">set</span> =&gt;
        {
            <span class="hljs-keyword">set</span>.Rules((faker, <span class="hljs-keyword">user</span>) =&gt;
            {
                var person = <span class="hljs-built_in">new</span> Bogus.Person();
                <span class="hljs-keyword">user</span>.Username = person.UserName;
                <span class="hljs-keyword">user</span>.Email = person.Email;
                <span class="hljs-keyword">user</span>.FirstName = person.FirstName;
                <span class="hljs-keyword">user</span>.LastName = person.LastName;
                <span class="hljs-keyword">user</span>.UserAddress = faker.Address.FullAddress();
                <span class="hljs-keyword">user</span>.Status = Statuses.Free;
            });
    }).RuleSet("DeactivatedUsers", <span class="hljs-keyword">set</span> =&gt;
   {
      <span class="hljs-keyword">set</span>.Rules((faker, <span class="hljs-keyword">user</span>) =&gt;
      {
          var person = <span class="hljs-built_in">new</span> Bogus.Person();
          <span class="hljs-keyword">user</span>.Username = person.UserName;
          <span class="hljs-keyword">user</span>.Email = string.Empty;
          <span class="hljs-keyword">user</span>.FirstName = person.FirstName;
          <span class="hljs-keyword">user</span>.LastName = person.LastName;
          <span class="hljs-keyword">user</span>.UserAddress = faker.Address.FullAddress();
         <span class="hljs-keyword">user</span>.Status = Statuses.Deactivated;
    });
});

<span class="hljs-built_in">public</span> <span class="hljs-keyword">class</span> TestClass
{
    <span class="hljs-built_in">public</span> <span class="hljs-type">void</span> GenreateUsers(string ruleset = "default")
    {
        var <span class="hljs-keyword">user</span> = TestData.UserGenerator.Generate();
        var userArray = TestData.UserGenerator.GenerateLazy(<span class="hljs-number">500</span>).ToArray();
        var userList = TestData.UserGenerator.GenerateBetween(<span class="hljs-number">1</span>, <span class="hljs-number">5000</span>).ToList();
        var freeUser = TestData.UserGenerator.Generate("FreeUsers");
        var deactivatedUser = TestData.UserGenerator.Generate("DeactivatedUser");
        var RulesetPassedInUser = TestData.UserGenerator.Generate(ruleset);
    }
}
</code></pre><h2 id="the-community-is-awesome">The Community Is Awesome</h2>
<p>Having trouble? Found a bug? Want to request a feature? The Bogus community is awesome. Although the maintainer is picky (as one should be) about what gets pulled into Bogus itself, both bchavez and the rest of the community is active in helping users to extend Bogus for their own purposes or in publishing an extension that is versatile enough for other projects to consume as well.</p>
<h3 id="support-bogusnethttpsgithubcombchavezbogus-development">Support <a target="_blank" href="https://github.com/bchavez/Bogus">Bogus.Net</a> Development</h3>
<p>Normally at the end of each post, I include my GitHub sponsor link. Bogus has been one of my favorite open source projects for some time and was the source rod one of my first Pull Requests. Show the project some love and <a target="_blank" href="https://github.com/sponsors/bchavez">sponsor bchavez</a>.</p>
]]></content:encoded></item><item><title><![CDATA[Works On My Machine]]></title><description><![CDATA[Preventing and dealing with errors that are difficult to reproduce.]]></description><link>https://blog.codingcoyote.dev/works-on-my-machine</link><guid isPermaLink="true">https://blog.codingcoyote.dev/works-on-my-machine</guid><category><![CDATA[software development]]></category><category><![CDATA[Bugs and Errors]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Sat, 07 Aug 2021 19:32:16 GMT</pubDate><content:encoded><![CDATA[<h1 id="the-code-cant-be-wrong">The Code Can't Be Wrong</h1>
<p>How often have you heard "the code is fine, must be your setup"? Or perhaps you yourself are working on a "bug" 🐞 but every time you run it, it works. How could this happen? What can we do to stop it?</p>
<h2 id="what-can-maintainers-do">What Can Maintainers Do?</h2>
<h3 id="dev-setup-scripts">Dev Setup Scripts</h3>
<p>Pick your poison. <a target="_blank" href="https://chocolatey.org/">Chocolatey</a>, <a target="_blank" href="https://scoop.sh/">Scoop</a>, <a target="_blank" href="https://manpages.ubuntu.com/manpages/xenial/man8/apt.8.html">apt</a>,<a target="_blank" href="https://brew.sh/">HomeBrew</a>, <a target="_blank" href="https://packaging.python.org/tutorials/installing-packages/">pip</a>, <a target="_blank" href="https://www.npmjs.com/">npm</a>, or <a target="_blank" href="https://yarnpkg.com/">yarn</a> the package manager doesn't matter as long as it works across the target platforms And includes the build tools it should do fine. A reproducible setup script removes the possibility that someone is using the wrong version of a build tool or  has a configuration set wrong. </p>
<p>But what if the setup script fails? It happens, and I've had it happen to me. But luckily we have the script itself to see what should be done and how. Using that we can work through the setup manually.</p>
<p>I had an issue a while back with a Silverlight project that required a specific installation order of plugins, SDK's and proprietary client libraries with license managers. Any of this was done without the proper reboots, in the wrong order or with the wrong update installed and it would never work. Setting up a new Dev environment took days of trial and error then updating the documentation with anything that the previous victim had forgotten to list. It was dreadful and costly. </p>
<p>There are pitfalls a plenty in this route, but the rewards are great. You're dependent in part on shiver maintains the scripts for those tools and problems on individual machines can still cause issues if they have residual configurations or cache problems.</p>
<h3 id="build-scripts">Build Scripts</h3>
<p>So the dev environment is setup and I have the code opened. But the executables aren't getting created when I build?</p>
<p>A good build script, whether created by your IDE or hand written, is a lifesaver when it comes to building software. My first forest into .Net was filled with people expecting me to know that I needed to copy files from A to B or a lot of the features would crash  the application without so much as a log file or event viewer entry. These were a set of pre-req files referenced by the code but not copied to the output for various reasons.</p>
<p>That's not good. It makes it harder to onboard new developers and it takes longer to produce working builds from a fresh dev environment. It used build scripts (project files) but specific requirements for a working build were documented but only as a manual requirement. </p>
<h3 id="use-cicd">Use CI/CD</h3>
<p>Developer tests their code, confirms with QA that their tests passed as well and pushes it up. Release starts and...💥</p>
<p>Forgetting to commit a file, or not building in the right configuration could cause unforeseen problems that can't be tested in the same environment that authored the changes. A good CI/CD setup will fail tests or fail to build/deploy if these problems occur.</p>
<h3 id="deterministic-builds">Deterministic Builds</h3>
<p>Package managers such as Yarn, npm, or NuGet do not always pull down the exact same dependencies. Using a build system that always resolves the same way will go a long way to help reduce unexplainable build issues and unexpected end user issues.</p>
<p>Yarn provides an options like <code>Zero-Installs</code> that prevent any changes to dependencies before building. This way you know the versions you tested are the same as the ones that get deployed.</p>
<h3 id="write-the-manual">Write The Manual</h3>
<p>Good documentation can cover a lot of problems as a last resort. Listing environment dependencies and advice for troubleshooting both the dev environment and the usage of the software will go a long way towards helping them help themselves.</p>
<h2 id="what-about-end-users">What About End Users?</h2>
<p>End users may often get the short end of the stick here. The developer may have done everything above and you still have an issue they can't reproduce.</p>
<h3 id="read-the-manual">Read The Manual</h3>
<p>Documentation is often forgotten and neglected but there is little to wonder about when many forego reading the supplied documentation before filing a bug ticket or reaching out to support. Many times reading the documentation or even a spelunking through the code can lead you to an answer that lets you fix the issue or file a ticket that makes the fix trivial for the maintainers.</p>
<h3 id="clean-environment">Clean Environment</h3>
<p>If possible, test the problem you have in a virtual machine that had just been setup using the instructions from the maintainer. This way you can even build the tool yourself (hopefully using the build scripts provided by the maintainer).</p>
<p>If you don't know enough to build their project (they probably didn't read the sections above), then you will at least be able to test it in a fresh OS untainted by your normal setup.</p>
<p>If you don't have a virtual machine or can't set one up, try on a different computer. Compare them and see what the difference is. Sometimes it's as simple as realizing your desktop is on a different subnet or that your laptop isn't on the domain.</p>
<h3 id="be-as-verbose-as-possible">Be As Verbose As Possible</h3>
<p>Sometimes the problem isn't related to the environment. It could be a miscommunication. Giving as much detail as you can about what the error is and what you are doing when it occurs will help them to reproduce edge case issues that they aren't testing for. They may not even consider it a use case at all.</p>
<p>The worst thing that can happen is you report a bug and either side just stops responding. Constant pestering with no additional details isn't helpful either.</p>
<p>Try listing:</p>
<ul>
<li>Current version you are using</li>
<li>An example (video, gif, code snippet).</li>
<li>Other methods of attempting the behavior</li>
<li>Other versions of the software tried</li>
<li>Any errors or logs that you can find that seem related</li>
</ul>
<iframe src="https://github.com/sponsors/digitalcoyote/button" height="35" width="116" style="border:0"></iframe>]]></content:encoded></item><item><title><![CDATA[Reporting Yarn Audit Results to Team City]]></title><description><![CDATA[Secure Team City deployments against known supply chain vulnerabilities using Powershell and Yarn.]]></description><link>https://blog.codingcoyote.dev/reporting-yarn-audit-results-to-team-city</link><guid isPermaLink="true">https://blog.codingcoyote.dev/reporting-yarn-audit-results-to-team-city</guid><category><![CDATA[Yarn]]></category><category><![CDATA[npm]]></category><category><![CDATA[ci-cd]]></category><category><![CDATA[ci]]></category><category><![CDATA[Security]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Sat, 07 Aug 2021 02:17:05 GMT</pubDate><content:encoded><![CDATA[<h1 id="parsing-yarn-audit">Parsing Yarn Audit</h1>
<p><code>yarn npm audit -R -A</code> is a useful command to check your dependencies for vulnerabilities before using them in production and it displays a neat human readable list of problems and advice on how to correct them.</p>
<pre><code><span class="hljs-string">├─</span> <span class="hljs-attr">browserslist:</span> <span class="hljs-number">4.14</span><span class="hljs-number">.2</span>
<span class="hljs-string">│</span>  <span class="hljs-string">├─</span> <span class="hljs-attr">Issue:</span> <span class="hljs-string">Regular</span> <span class="hljs-string">Expression</span> <span class="hljs-string">Denial</span> <span class="hljs-string">of</span> <span class="hljs-string">Service</span>
<span class="hljs-string">│</span>  <span class="hljs-string">├─</span> <span class="hljs-attr">URL:</span> <span class="hljs-string">https://npmjs.com/advisories/1747</span>
<span class="hljs-string">│</span>  <span class="hljs-string">├─</span> <span class="hljs-attr">Severity:</span> <span class="hljs-string">moderate</span>
<span class="hljs-string">│</span>  <span class="hljs-string">├─</span> <span class="hljs-attr">Vulnerable Versions:</span> <span class="hljs-string">&gt;=4.0.0</span> <span class="hljs-string">&lt;4.16.5</span>
<span class="hljs-string">│</span>  <span class="hljs-string">├─</span> <span class="hljs-attr">Patched Versions:</span> <span class="hljs-string">&gt;=4.16.5</span>
<span class="hljs-string">│</span>  <span class="hljs-string">├─</span> <span class="hljs-attr">Via:</span> <span class="hljs-string">gatsby,</span> <span class="hljs-string">webpack</span>
<span class="hljs-string">│</span>  <span class="hljs-string">└─</span> <span class="hljs-attr">Recommendation:</span> <span class="hljs-string">Upgrade</span> <span class="hljs-string">to</span> <span class="hljs-string">version</span> <span class="hljs-number">4.16</span><span class="hljs-number">.5</span> <span class="hljs-string">or</span> <span class="hljs-string">later</span>
<span class="hljs-string">│</span>
<span class="hljs-string">└─</span> <span class="hljs-attr">trim:</span> <span class="hljs-number">0.0</span><span class="hljs-number">.1</span>
   <span class="hljs-string">├─</span> <span class="hljs-attr">Issue:</span> <span class="hljs-string">Regular</span> <span class="hljs-string">Expression</span> <span class="hljs-string">Denial</span> <span class="hljs-string">of</span> <span class="hljs-string">Service</span> <span class="hljs-string">in</span> <span class="hljs-string">trim</span>
   <span class="hljs-string">├─</span> <span class="hljs-attr">URL:</span> <span class="hljs-string">https://npmjs.com/advisories/1700</span>
   <span class="hljs-string">├─</span> <span class="hljs-attr">Severity:</span> <span class="hljs-string">high</span>
   <span class="hljs-string">├─</span> <span class="hljs-attr">Vulnerable Versions:</span> <span class="hljs-string">&lt;0.0.3</span>
   <span class="hljs-string">├─</span> <span class="hljs-attr">Patched Versions:</span> <span class="hljs-string">&gt;=0.0.3</span>
   <span class="hljs-string">├─</span> <span class="hljs-attr">Via:</span> <span class="hljs-string">gatsby</span>
   <span class="hljs-string">└─</span> <span class="hljs-attr">Recommendation:</span> <span class="hljs-string">Upgrade</span> <span class="hljs-string">to</span> <span class="hljs-string">version</span> <span class="hljs-number">0.0</span><span class="hljs-number">.3</span> <span class="hljs-string">or</span> <span class="hljs-string">later</span>
</code></pre><p>Yarn even offers a machine parsable format to for any local tools to consume or to use for reporting via <code>--json</code>. It's got more detail and looks very much like yarn spilled a bucket of JSON on you desk.</p>
<pre><code>{
  <span class="hljs-attr">"actions"</span>: [],
  <span class="hljs-attr">"advisories"</span>: {
    <span class="hljs-attr">"1700"</span>: {
      <span class="hljs-attr">"findings"</span>: [
        {
          <span class="hljs-attr">"version"</span>: <span class="hljs-string">"0.0.1"</span>,
          <span class="hljs-attr">"paths"</span>: [<span class="hljs-string">"gatsby&gt;gatsby-cli&gt;gatsby-recipes&gt;remark-parse&gt;trim"</span>]
        }
      ],
      <span class="hljs-attr">"id"</span>: <span class="hljs-number">1700</span>,
      <span class="hljs-attr">"created"</span>: <span class="hljs-string">"2021-05-10T18:48:44.988Z"</span>,
      <span class="hljs-attr">"updated"</span>: <span class="hljs-string">"2021-05-10T18:53:15.621Z"</span>,
      <span class="hljs-attr">"deleted"</span>: <span class="hljs-literal">null</span>,
      <span class="hljs-attr">"title"</span>: <span class="hljs-string">"Regular Expression Denial of Service in trim"</span>,
      <span class="hljs-attr">"found_by"</span>: { <span class="hljs-attr">"link"</span>: <span class="hljs-string">""</span>, <span class="hljs-attr">"name"</span>: <span class="hljs-string">"Anonymous"</span>, <span class="hljs-attr">"email"</span>: <span class="hljs-string">""</span> },
      <span class="hljs-attr">"reported_by"</span>: { <span class="hljs-attr">"link"</span>: <span class="hljs-string">""</span>, <span class="hljs-attr">"name"</span>: <span class="hljs-string">"Anonymous"</span>, <span class="hljs-attr">"email"</span>: <span class="hljs-string">""</span> },
      <span class="hljs-attr">"module_name"</span>: <span class="hljs-string">"trim"</span>,
      <span class="hljs-attr">"cves"</span>: [<span class="hljs-string">"CVE-2020-7753"</span>],
      <span class="hljs-attr">"vulnerable_versions"</span>: <span class="hljs-string">"&lt;0.0.3"</span>,
      <span class="hljs-attr">"patched_versions"</span>: <span class="hljs-string">"&gt;=0.0.3"</span>,
      <span class="hljs-attr">"overview"</span>: <span class="hljs-string">"Versions of `trim` lower than 0.0.3 are vulnerable to Regular Expression Denial of Service (ReDoS) via trim()."</span>,
      <span class="hljs-attr">"recommendation"</span>: <span class="hljs-string">"Upgrade to version 0.0.3 or later"</span>,
      <span class="hljs-attr">"references"</span>: <span class="hljs-string">"- [CVE](https://nvd.nist.gov/vuln/detail/CVE-2020-7753)\n- [GitHub Advisory](https://github.com/advisories/GHSA-w5p7-h5w8-2hfq)\n"</span>,
      <span class="hljs-attr">"access"</span>: <span class="hljs-string">"public"</span>,
      <span class="hljs-attr">"severity"</span>: <span class="hljs-string">"high"</span>,
      <span class="hljs-attr">"cwe"</span>: <span class="hljs-string">"CWE-400"</span>,
      <span class="hljs-attr">"metadata"</span>: {
        <span class="hljs-attr">"module_type"</span>: <span class="hljs-string">""</span>,
        <span class="hljs-attr">"exploitability"</span>: <span class="hljs-number">7</span>,
        <span class="hljs-attr">"affected_components"</span>: <span class="hljs-string">""</span>
      },
      <span class="hljs-attr">"url"</span>: <span class="hljs-string">"https://npmjs.com/advisories/1700"</span>
    },
    <span class="hljs-attr">"1747"</span>: {
      <span class="hljs-attr">"findings"</span>: [
        {
          <span class="hljs-attr">"version"</span>: <span class="hljs-string">"4.14.2"</span>,
          <span class="hljs-attr">"paths"</span>: [
            <span class="hljs-string">"gatsby&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-cli&gt;gatsby-recipes&gt;remark-mdxjs&gt;@babel/plugin-proposal-object-rest-spread&gt;@babel/helper-compilation-targets&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;babel-preset-gatsby&gt;@babel/preset-env&gt;@babel/plugin-proposal-object-rest-spread&gt;@babel/helper-compilation-targets&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;babel-preset-gatsby&gt;@babel/plugin-transform-runtime&gt;babel-plugin-polyfill-corejs2&gt;@babel/helper-define-polyfill-provider&gt;@babel/helper-compilation-targets&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;babel-preset-gatsby&gt;@babel/preset-env&gt;babel-plugin-polyfill-corejs2&gt;@babel/helper-define-polyfill-provider&gt;@babel/helper-compilation-targets&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;babel-preset-gatsby&gt;@babel/plugin-transform-runtime&gt;babel-plugin-polyfill-corejs3&gt;@babel/helper-define-polyfill-provider&gt;@babel/helper-compilation-targets&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;babel-preset-gatsby&gt;@babel/preset-env&gt;babel-plugin-polyfill-corejs3&gt;@babel/helper-define-polyfill-provider&gt;@babel/helper-compilation-targets&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;babel-preset-gatsby&gt;@babel/plugin-transform-runtime&gt;babel-plugin-polyfill-regenerator&gt;@babel/helper-define-polyfill-provider&gt;@babel/helper-compilation-targets&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;babel-preset-gatsby&gt;@babel/preset-env&gt;babel-plugin-polyfill-regenerator&gt;@babel/helper-define-polyfill-provider&gt;@babel/helper-compilation-targets&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;babel-preset-gatsby&gt;@babel/preset-env&gt;@babel/helper-compilation-targets&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-legacy-polyfills&gt;core-js-compat&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;babel-preset-gatsby&gt;gatsby-legacy-polyfills&gt;core-js-compat&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;babel-preset-gatsby&gt;@babel/plugin-transform-runtime&gt;babel-plugin-polyfill-corejs3&gt;core-js-compat&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;babel-preset-gatsby&gt;@babel/preset-env&gt;babel-plugin-polyfill-corejs3&gt;core-js-compat&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;babel-preset-gatsby&gt;@babel/preset-env&gt;core-js-compat&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;react-dev-utils&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;@pmmmwh/react-refresh-webpack-plugin&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;terser-webpack-plugin&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;@pmmmwh/react-refresh-webpack-plugin&gt;webpack-dev-server&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;webpack-dev-server&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;@pmmmwh/react-refresh-webpack-plugin&gt;webpack-dev-server&gt;webpack-dev-middleware&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;webpack-dev-server&gt;webpack-dev-middleware&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;webpack-dev-middleware&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;babel-loader&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;css-loader&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;css-minimizer-webpack-plugin&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-webpack-plugin&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;file-loader&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;url-loader&gt;file-loader&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;mini-css-extract-plugin&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;null-loader&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;postcss-loader&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;raw-loader&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;style-loader&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;url-loader&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;webpack&gt;browserslist"</span>,
            <span class="hljs-string">"webpack&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;autoprefixer&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;css-minimizer-webpack-plugin&gt;cssnano&gt;cssnano-preset-default&gt;postcss-colormin&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;css-minimizer-webpack-plugin&gt;cssnano&gt;cssnano-preset-default&gt;postcss-colormin&gt;caniuse-api&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;css-minimizer-webpack-plugin&gt;cssnano&gt;cssnano-preset-default&gt;postcss-merge-rules&gt;caniuse-api&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;css-minimizer-webpack-plugin&gt;cssnano&gt;cssnano-preset-default&gt;postcss-reduce-initial&gt;caniuse-api&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;css-minimizer-webpack-plugin&gt;cssnano&gt;cssnano-preset-default&gt;postcss-merge-longhand&gt;stylehacks&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;css-minimizer-webpack-plugin&gt;cssnano&gt;cssnano-preset-default&gt;postcss-merge-rules&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;css-minimizer-webpack-plugin&gt;cssnano&gt;cssnano-preset-default&gt;postcss-minify-params&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;css-minimizer-webpack-plugin&gt;cssnano&gt;cssnano-preset-default&gt;postcss-normalize-unicode&gt;browserslist"</span>,
            <span class="hljs-string">"gatsby&gt;css-minimizer-webpack-plugin&gt;cssnano&gt;cssnano-preset-default&gt;postcss-reduce-initial&gt;browserslist"</span>
          ]
        }
      ],
      <span class="hljs-attr">"id"</span>: <span class="hljs-number">1747</span>,
      <span class="hljs-attr">"created"</span>: <span class="hljs-string">"2021-05-24T19:56:39.062Z"</span>,
      <span class="hljs-attr">"updated"</span>: <span class="hljs-string">"2021-05-24T19:59:05.419Z"</span>,
      <span class="hljs-attr">"deleted"</span>: <span class="hljs-literal">null</span>,
      <span class="hljs-attr">"title"</span>: <span class="hljs-string">"Regular Expression Denial of Service"</span>,
      <span class="hljs-attr">"found_by"</span>: { <span class="hljs-attr">"link"</span>: <span class="hljs-string">""</span>, <span class="hljs-attr">"name"</span>: <span class="hljs-string">"Anonymous"</span>, <span class="hljs-attr">"email"</span>: <span class="hljs-string">""</span> },
      <span class="hljs-attr">"reported_by"</span>: { <span class="hljs-attr">"link"</span>: <span class="hljs-string">""</span>, <span class="hljs-attr">"name"</span>: <span class="hljs-string">"Anonymous"</span>, <span class="hljs-attr">"email"</span>: <span class="hljs-string">""</span> },
      <span class="hljs-attr">"module_name"</span>: <span class="hljs-string">"browserslist"</span>,
      <span class="hljs-attr">"cves"</span>: [<span class="hljs-string">"CVE-2021-23364"</span>],
      <span class="hljs-attr">"vulnerable_versions"</span>: <span class="hljs-string">"&gt;=4.0.0 &lt;4.16.5"</span>,
      <span class="hljs-attr">"patched_versions"</span>: <span class="hljs-string">"&gt;=4.16.5"</span>,
      <span class="hljs-attr">"overview"</span>: <span class="hljs-string">"The package `browserslist` from 4.0.0 and before 4.16.5 are vulnerable to Regular Expression Denial of Service (ReDoS) during parsing of queries."</span>,
      <span class="hljs-attr">"recommendation"</span>: <span class="hljs-string">"Upgrade to version 4.16.5 or later"</span>,
      <span class="hljs-attr">"references"</span>: <span class="hljs-string">"- [CVE](https://nvd.nist.gov/vuln/detail/CVE-2021-23364)\n- [GitHub Advisory](https://github.com/advisories/GHSA-w8qv-6jwh-64r5)\n"</span>,
      <span class="hljs-attr">"access"</span>: <span class="hljs-string">"public"</span>,
      <span class="hljs-attr">"severity"</span>: <span class="hljs-string">"moderate"</span>,
      <span class="hljs-attr">"cwe"</span>: <span class="hljs-string">"CWE-400"</span>,
      <span class="hljs-attr">"metadata"</span>: {
        <span class="hljs-attr">"module_type"</span>: <span class="hljs-string">""</span>,
        <span class="hljs-attr">"exploitability"</span>: <span class="hljs-number">5</span>,
        <span class="hljs-attr">"affected_components"</span>: <span class="hljs-string">""</span>
      },
      <span class="hljs-attr">"url"</span>: <span class="hljs-string">"https://npmjs.com/advisories/1747"</span>
    },
    <span class="hljs-attr">"1751"</span>: {
      <span class="hljs-attr">"findings"</span>: [
        {
          <span class="hljs-attr">"version"</span>: <span class="hljs-string">"3.1.0"</span>,
          <span class="hljs-attr">"paths"</span>: [
            <span class="hljs-string">"babel-eslint&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;@typescript-eslint/eslint-plugin&gt;@typescript-eslint/experimental-utils&gt;eslint-utils&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-config-react-app&gt;@typescript-eslint/eslint-plugin&gt;@typescript-eslint/experimental-utils&gt;eslint-utils&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;@babel/eslint-parser&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;@typescript-eslint/eslint-plugin&gt;@typescript-eslint/parser&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-config-react-app&gt;@typescript-eslint/eslint-plugin&gt;@typescript-eslint/parser&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;@typescript-eslint/parser&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-config-react-app&gt;@typescript-eslint/parser&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;@typescript-eslint/eslint-plugin&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-config-react-app&gt;@typescript-eslint/eslint-plugin&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;@typescript-eslint/eslint-plugin&gt;@typescript-eslint/experimental-utils&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-config-react-app&gt;@typescript-eslint/eslint-plugin&gt;@typescript-eslint/experimental-utils&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-config-react-app&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-config-react-app&gt;eslint-plugin-flowtype&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-plugin-flowtype&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-config-react-app&gt;eslint-plugin-import&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-plugin-import&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-config-react-app&gt;eslint-plugin-jsx-a11y&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-plugin-jsx-a11y&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-config-react-app&gt;eslint-plugin-react&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-plugin-react&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-config-react-app&gt;eslint-plugin-react-hooks&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-plugin-react-hooks&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;eslint-webpack-plugin&gt;eslint&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;chokidar&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-cli&gt;gatsby-recipes&gt;chokidar&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;@pmmmwh/react-refresh-webpack-plugin&gt;webpack-dev-server&gt;chokidar&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;webpack-dev-server&gt;chokidar&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-plugin-page-creator&gt;chokidar&gt;glob-parent"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-plugin-page-creator&gt;gatsby-page-utils&gt;chokidar&gt;glob-parent"</span>
          ]
        }
      ],
      <span class="hljs-attr">"id"</span>: <span class="hljs-number">1751</span>,
      <span class="hljs-attr">"created"</span>: <span class="hljs-string">"2021-06-07T21:57:10.135Z"</span>,
      <span class="hljs-attr">"updated"</span>: <span class="hljs-string">"2021-06-07T21:58:07.745Z"</span>,
      <span class="hljs-attr">"deleted"</span>: <span class="hljs-literal">null</span>,
      <span class="hljs-attr">"title"</span>: <span class="hljs-string">"Regular expression denial of service"</span>,
      <span class="hljs-attr">"found_by"</span>: { <span class="hljs-attr">"link"</span>: <span class="hljs-string">""</span>, <span class="hljs-attr">"name"</span>: <span class="hljs-string">"Anonymous"</span>, <span class="hljs-attr">"email"</span>: <span class="hljs-string">""</span> },
      <span class="hljs-attr">"reported_by"</span>: { <span class="hljs-attr">"link"</span>: <span class="hljs-string">""</span>, <span class="hljs-attr">"name"</span>: <span class="hljs-string">"Anonymous"</span>, <span class="hljs-attr">"email"</span>: <span class="hljs-string">""</span> },
      <span class="hljs-attr">"module_name"</span>: <span class="hljs-string">"glob-parent"</span>,
      <span class="hljs-attr">"cves"</span>: [<span class="hljs-string">"CVE-2020-28469"</span>],
      <span class="hljs-attr">"vulnerable_versions"</span>: <span class="hljs-string">"&lt;5.1.2"</span>,
      <span class="hljs-attr">"patched_versions"</span>: <span class="hljs-string">"&gt;=5.1.2"</span>,
      <span class="hljs-attr">"overview"</span>: <span class="hljs-string">"`glob-parent` before 5.1.2 has a regular expression denial of service vulnerability. The enclosure regex used to check for strings ending in enclosure containing path separator."</span>,
      <span class="hljs-attr">"recommendation"</span>: <span class="hljs-string">"Upgrade to version 5.1.2 or later"</span>,
      <span class="hljs-attr">"references"</span>: <span class="hljs-string">"- [CVE](https://nvd.nist.gov/vuln/detail/CVE-2020-28469)\n- [GitHub Advisory](https://github.com/advisories/GHSA-ww39-953v-wcq6)\n"</span>,
      <span class="hljs-attr">"access"</span>: <span class="hljs-string">"public"</span>,
      <span class="hljs-attr">"severity"</span>: <span class="hljs-string">"moderate"</span>,
      <span class="hljs-attr">"cwe"</span>: <span class="hljs-string">"CWE-400"</span>,
      <span class="hljs-attr">"metadata"</span>: {
        <span class="hljs-attr">"module_type"</span>: <span class="hljs-string">""</span>,
        <span class="hljs-attr">"exploitability"</span>: <span class="hljs-number">5</span>,
        <span class="hljs-attr">"affected_components"</span>: <span class="hljs-string">""</span>
      },
      <span class="hljs-attr">"url"</span>: <span class="hljs-string">"https://npmjs.com/advisories/1751"</span>
    },
    <span class="hljs-attr">"1770"</span>: {
      <span class="hljs-attr">"findings"</span>: [
        {
          <span class="hljs-attr">"version"</span>: <span class="hljs-string">"6.1.0"</span>,
          <span class="hljs-attr">"paths"</span>: [
            <span class="hljs-string">"gatsby&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;make-fetch-happen&gt;cacache&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-cli&gt;gatsby-recipes&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;make-fetch-happen&gt;cacache&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;@pmmmwh/react-refresh-webpack-plugin&gt;webpack-dev-server&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;make-fetch-happen&gt;cacache&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;webpack-dev-server&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;make-fetch-happen&gt;cacache&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-plugin-page-creator&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;make-fetch-happen&gt;cacache&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-plugin-page-creator&gt;gatsby-page-utils&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;make-fetch-happen&gt;cacache&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-cli&gt;gatsby-recipes&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;@pmmmwh/react-refresh-webpack-plugin&gt;webpack-dev-server&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;webpack-dev-server&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-plugin-page-creator&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-plugin-page-creator&gt;gatsby-page-utils&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;tar"</span>
          ]
        }
      ],
      <span class="hljs-attr">"id"</span>: <span class="hljs-number">1770</span>,
      <span class="hljs-attr">"created"</span>: <span class="hljs-string">"2021-08-03T18:11:06.582Z"</span>,
      <span class="hljs-attr">"updated"</span>: <span class="hljs-string">"2021-08-03T19:07:08.152Z"</span>,
      <span class="hljs-attr">"deleted"</span>: <span class="hljs-literal">null</span>,
      <span class="hljs-attr">"title"</span>: <span class="hljs-string">"Arbitrary File Creation/Overwrite due to insufficient absolute path sanitization"</span>,
      <span class="hljs-attr">"found_by"</span>: { <span class="hljs-attr">"link"</span>: <span class="hljs-string">""</span>, <span class="hljs-attr">"name"</span>: <span class="hljs-string">"Anonymous"</span>, <span class="hljs-attr">"email"</span>: <span class="hljs-string">""</span> },
      <span class="hljs-attr">"reported_by"</span>: { <span class="hljs-attr">"link"</span>: <span class="hljs-string">""</span>, <span class="hljs-attr">"name"</span>: <span class="hljs-string">"Anonymous"</span>, <span class="hljs-attr">"email"</span>: <span class="hljs-string">""</span> },
      <span class="hljs-attr">"module_name"</span>: <span class="hljs-string">"tar"</span>,
      <span class="hljs-attr">"cves"</span>: [<span class="hljs-string">"CVE-2021-32804"</span>],
      <span class="hljs-attr">"vulnerable_versions"</span>: <span class="hljs-string">"&lt;3.2.2 || &gt;=4.0.0 &lt;4.4.14 || &gt;=5.0.0 &lt;5.0.6 || &gt;=6.0.0 &lt;6.1.1"</span>,
      <span class="hljs-attr">"patched_versions"</span>: <span class="hljs-string">"&gt;=3.2.2 &lt;4.0.0 || &gt;=4.4.14  &lt;5.0.0 || &gt;=5.0.6 &lt;6.0.0 || &gt;=6.1.1"</span>,
      <span class="hljs-attr">"overview"</span>: <span class="hljs-string">"The `tar` package has a high severity vulnerability before versions 3.2.2, 4.4.14, 5.0.6, and 6.1.1.\n\n### Impact\n\nArbitrary File Creation, Arbitrary File Overwrite, Arbitrary Code Execution\n\n`node-tar` aims to prevent extraction of absolute file paths by turning absolute paths into relative paths when the `preservePaths` flag is not set to `true`. This is achieved by stripping the absolute path root from any absolute file paths contained in a tar file. For example `/home/user/.bashrc` would turn into `home/user/.bashrc`. \n\nThis logic was insufficient when file paths contained repeated path roots such as `////home/user/.bashrc`. `node-tar` would only strip a single path root from such paths. When given an absolute file path with repeating path roots, the resulting path (e.g. `///home/user/.bashrc`) would still resolve to an absolute path, thus allowing arbitrary file creation and overwrite. \n\n### Workarounds\n\nUsers may work around this vulnerability without upgrading by creating a custom `onentry` method which sanitizes the `entry.path` or a `filter` method which removes entries with absolute paths.\n\n```js\nconst path = require('path')\nconst tar = require('tar')\n\ntar.x({\n  file: 'archive.tgz',\n  // either add this function...\n  onentry: (entry) =&gt; {\n    if (path.isAbsolute(entry.path)) {\n      entry.path = sanitizeAbsolutePathSomehow(entry.path)\n      entry.absolute = path.resolve(entry.path)\n    }\n  },\n\n  // or this one\n  filter: (file, entry) =&gt; {\n    if (path.isAbsolute(entry.path)) {\n      return false\n    } else {\n      return true\n    }\n  }\n})\n```\n\nUsers are encouraged to upgrade to the latest patch versions, rather than attempt to sanitize tar input themselves."</span>,
      <span class="hljs-attr">"recommendation"</span>: <span class="hljs-string">"Upgrade to version 3.2.2, 4.4.14, 5.0.6, 6.1.1 or later"</span>,
      <span class="hljs-attr">"references"</span>: <span class="hljs-string">"- [GitHub Advisory](https://github.com/npm/node-tar/security/advisories/GHSA-3jfq-g458-7qm9)\n- [CVE](https://nvd.nist.gov/vuln/detail/CVE-2021-32804)\n- [Related but distinct advisory involving symlinks](https://www.npmjs.com/advisories/1771)"</span>,
      <span class="hljs-attr">"access"</span>: <span class="hljs-string">"public"</span>,
      <span class="hljs-attr">"severity"</span>: <span class="hljs-string">"high"</span>,
      <span class="hljs-attr">"cwe"</span>: <span class="hljs-string">"CWE-22"</span>,
      <span class="hljs-attr">"metadata"</span>: {
        <span class="hljs-attr">"module_type"</span>: <span class="hljs-string">""</span>,
        <span class="hljs-attr">"exploitability"</span>: <span class="hljs-number">8</span>,
        <span class="hljs-attr">"affected_components"</span>: <span class="hljs-string">""</span>
      },
      <span class="hljs-attr">"url"</span>: <span class="hljs-string">"https://npmjs.com/advisories/1770"</span>
    },
    <span class="hljs-attr">"1771"</span>: {
      <span class="hljs-attr">"findings"</span>: [
        {
          <span class="hljs-attr">"version"</span>: <span class="hljs-string">"6.1.0"</span>,
          <span class="hljs-attr">"paths"</span>: [
            <span class="hljs-string">"gatsby&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;make-fetch-happen&gt;cacache&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-cli&gt;gatsby-recipes&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;make-fetch-happen&gt;cacache&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;@pmmmwh/react-refresh-webpack-plugin&gt;webpack-dev-server&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;make-fetch-happen&gt;cacache&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;webpack-dev-server&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;make-fetch-happen&gt;cacache&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-plugin-page-creator&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;make-fetch-happen&gt;cacache&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-plugin-page-creator&gt;gatsby-page-utils&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;make-fetch-happen&gt;cacache&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-cli&gt;gatsby-recipes&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;@pmmmwh/react-refresh-webpack-plugin&gt;webpack-dev-server&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;webpack-dev-server&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-plugin-page-creator&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;tar"</span>,
            <span class="hljs-string">"gatsby&gt;gatsby-plugin-page-creator&gt;gatsby-page-utils&gt;chokidar&gt;fsevents&gt;nan&gt;node-gyp&gt;tar"</span>
          ]
        }
      ],
      <span class="hljs-attr">"id"</span>: <span class="hljs-number">1771</span>,
      <span class="hljs-attr">"created"</span>: <span class="hljs-string">"2021-08-03T18:14:17.499Z"</span>,
      <span class="hljs-attr">"updated"</span>: <span class="hljs-string">"2021-08-03T19:01:35.564Z"</span>,
      <span class="hljs-attr">"deleted"</span>: <span class="hljs-literal">null</span>,
      <span class="hljs-attr">"title"</span>: <span class="hljs-string">"Arbitrary File Creation/Overwrite via insufficient symlink protection due to directory cache poisoning"</span>,
      <span class="hljs-attr">"found_by"</span>: { <span class="hljs-attr">"link"</span>: <span class="hljs-string">""</span>, <span class="hljs-attr">"name"</span>: <span class="hljs-string">"Anonymous"</span>, <span class="hljs-attr">"email"</span>: <span class="hljs-string">""</span> },
      <span class="hljs-attr">"reported_by"</span>: { <span class="hljs-attr">"link"</span>: <span class="hljs-string">""</span>, <span class="hljs-attr">"name"</span>: <span class="hljs-string">"Anonymous"</span>, <span class="hljs-attr">"email"</span>: <span class="hljs-string">""</span> },
      <span class="hljs-attr">"module_name"</span>: <span class="hljs-string">"tar"</span>,
      <span class="hljs-attr">"cves"</span>: [<span class="hljs-string">"CVE-2021-32803"</span>],
      <span class="hljs-attr">"vulnerable_versions"</span>: <span class="hljs-string">"&lt;3.2.3 || &gt;=4.0.0 &lt;4.4.15 || &gt;=5.0.0 &lt;5.0.7 || &gt;=6.0.0 &lt;6.1.2"</span>,
      <span class="hljs-attr">"patched_versions"</span>: <span class="hljs-string">"&gt;=3.2.3 &lt;4.0.0 || &gt;=4.4.15  &lt;5.0.0 || &gt;=5.0.7 &lt;6.0.0 || &gt;=6.1.2"</span>,
      <span class="hljs-attr">"overview"</span>: <span class="hljs-string">"The `tar` package has a high severity vulnerability before versions 3.2.3, 4.4.15, 5.0.7, and 6.1.2.\n\n### Impact\n\nArbitrary File Creation, Arbitrary File Overwrite, Arbitrary Code Execution\n\n`node-tar` aims to prevent extraction of absolute file paths by turning absolute paths into relative paths when the `preservePaths` flag is not set to `true`. This is achieved by stripping the absolute path root from any absolute file paths contained in a tar file. For example `/home/user/.bashrc` would turn into `home/user/.bashrc`. \n\nThis logic was insufficient when file paths contained repeated path roots such as `////home/user/.bashrc`. `node-tar` would only strip a single path root from such paths. When given an absolute file path with repeating path roots, the resulting path (e.g. `///home/user/.bashrc`) would still resolve to an absolute path, thus allowing arbitrary file creation and overwrite. \n\n### Workarounds\n\nUsers may work around this vulnerability without upgrading by creating a custom `onentry` method which sanitizes the `entry.path` or a `filter` method which removes entries with absolute paths.\n\n```js\nconst path = require('path')\nconst tar = require('tar')\n\ntar.x({\n  file: 'archive.tgz',\n  // either add this function...\n  onentry: (entry) =&gt; {\n    if (path.isAbsolute(entry.path)) {\n      entry.path = sanitizeAbsolutePathSomehow(entry.path)\n      entry.absolute = path.resolve(entry.path)\n    }\n  },\n\n  // or this one\n  filter: (file, entry) =&gt; {\n    if (path.isAbsolute(entry.path)) {\n      return false\n    } else {\n      return true\n    }\n  }\n})\n```\n\nUsers are encouraged to upgrade to the latest patch versions, rather than attempt to sanitize tar input themselves."</span>,
      <span class="hljs-attr">"recommendation"</span>: <span class="hljs-string">"Upgrade to version 3.2.3, 4.4.15, 5.0.7, 6.1.2 or later"</span>,
      <span class="hljs-attr">"references"</span>: <span class="hljs-string">"- [GitHub Advisory](https://github.com/npm/node-tar/security/advisories/GHSA-3jfq-g458-7qm9)\n- [CVE](https://nvd.nist.gov/vuln/detail/CVE-2021-32803)\n- [Related but distinct advisory involving absolute paths](https://www.npmjs.com/advisories/1770)"</span>,
      <span class="hljs-attr">"access"</span>: <span class="hljs-string">"public"</span>,
      <span class="hljs-attr">"severity"</span>: <span class="hljs-string">"high"</span>,
      <span class="hljs-attr">"cwe"</span>: <span class="hljs-string">"CWE-22"</span>,
      <span class="hljs-attr">"metadata"</span>: {
        <span class="hljs-attr">"module_type"</span>: <span class="hljs-string">""</span>,
        <span class="hljs-attr">"exploitability"</span>: <span class="hljs-number">8</span>,
        <span class="hljs-attr">"affected_components"</span>: <span class="hljs-string">""</span>
      },
      <span class="hljs-attr">"url"</span>: <span class="hljs-string">"https://npmjs.com/advisories/1771"</span>
    }
  },
  <span class="hljs-attr">"muted"</span>: [],
  <span class="hljs-attr">"metadata"</span>: {
    <span class="hljs-attr">"vulnerabilities"</span>: {
      <span class="hljs-attr">"info"</span>: <span class="hljs-number">0</span>,
      <span class="hljs-attr">"low"</span>: <span class="hljs-number">0</span>,
      <span class="hljs-attr">"moderate"</span>: <span class="hljs-number">79</span>,
      <span class="hljs-attr">"high"</span>: <span class="hljs-number">25</span>,
      <span class="hljs-attr">"critical"</span>: <span class="hljs-number">0</span>
    },
    <span class="hljs-attr">"dependencies"</span>: <span class="hljs-number">1236</span>,
    <span class="hljs-attr">"devDependencies"</span>: <span class="hljs-number">0</span>,
    <span class="hljs-attr">"optionalDependencies"</span>: <span class="hljs-number">0</span>,
    <span class="hljs-attr">"totalDependencies"</span>: <span class="hljs-number">1236</span>
  }
}
</code></pre><p>But what stops a malicious commit from putting these up to Team City? At this time, Team City doesn't offer a build step that runs Yarn Audit explicitly and reports the results. You could use some build log patterns in the fail conditions to fail the build, but then you'd still have to drill through potentially hundreds of lines looking for which packages were vulnerable, what used them and what needs to be done. And if you need to ignore some vulnerabilities (either because they don't affect you or there is no fix available yet) your only option is the <code>--severity</code> flag.</p>
<p> But that only lets you ignore vulnerabilities above a certain level. You certainly can't take down an enterprise application for a critical issue that should be fixed any minute. So instead you try mitigate the issue, but then you either have to manually deploy or turn off the vulnerability check to publish the mitigation or ignore everything below at or below that severity. And what if due to that you accidentally miss a vulnerability that is far more serious?</p>
<p><a target="_blank" href="https://www.jetbrains.com/help/teamcity/service-messages.html#Reporting+Build+Problems">Team City Service Messages</a> and a healthy dose of cross-platform powershell to the rescue.</p>
<p>Ok, that looks neat but what does it do?</p>
<pre><code><span class="hljs-comment">#!/usr/bin/pwsh</span>
$Audit = yarn npm audit -R -A --json | ConvertFrom-Json;
<span class="hljs-string">"##teamcity[blockOpened name='YarnAudit' description='yarn npm audit -R -A']"</span>
Foreach ($advisory <span class="hljs-keyword">in</span> $Audit.advisories.PSObject.Properties){
    $package = $($advisory.Value.findings[<span class="hljs-number">0</span>].paths[<span class="hljs-number">0</span>].Split(<span class="hljs-string">'&gt;'</span>)[<span class="hljs-number">-1</span>]).Replace(<span class="hljs-string">'|'</span>, <span class="hljs-string">'||'</span>).Replace(<span class="hljs-string">'['</span>, <span class="hljs-string">'|['</span>).Replace(<span class="hljs-string">']'</span>, <span class="hljs-string">'|]'</span>).Replace(<span class="hljs-string">'''', '|'''</span>);
    $overview = $advisory.Value.overview.Replace(<span class="hljs-string">'|'</span>, <span class="hljs-string">'||'</span>).Replace(<span class="hljs-string">'['</span>, <span class="hljs-string">'|['</span>).Replace(<span class="hljs-string">']'</span>, <span class="hljs-string">'|]'</span>).Replace(<span class="hljs-string">'''', '|'''</span>);
    <span class="hljs-string">"##teamcity[buildProblem description='Advisory for $package: $($advisory.Value.url) | $overview' identity='VulnerableDependency']"</span>
}
<span class="hljs-string">"##teamcity[blockClosed name='YarnAudit']"</span>
</code></pre><p>We start by running Yarn audit with all the bells and whistles we want. The blockOpened/blockClosed service messages help to group this in the build log.The most important thing is to output json. <a target="_blank" href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-json">ConvertFrom-Json</a> will take care of all  parsing for us. After that it's a matter of checking either the <code>advisories</code> property to report individual issues, or the <code>metadata</code> property for info on the whole run. </p>
<p>Since I wanted to be able to look at the output and see exactly what was wrong, I output a build problem per dependency with vulnerabilities. Since the <code>description</code> field is truncated after 400 symbols, we need to make sure to output the <code>url</code>. With the url anyone can follow the link for all the information that we have here even if the overview is cut short. </p>
<p>I've chosen to report individual vulnerable packages as build problems, but you could also do some script-wizardry and report them as tests for each individual test. Remember though, if you have build metric fail condition on test cases, this could cause issues you aren't expecting.</p>
<p>With a little more work, we can setup build parameters to ignore specific vulnerabilities by CVE, severity, package, etc, but that will have to wait for the next post.</p>
<p>If you liked what you read, consider supporting further work either with monetary donations, spreading the word, or contributing to any of my open source projects such as <a target="_blank" href="https://digitalcoyote.github.io/NuGetDefense/">NuGetDefense</a>.</p>
<h2 id="history">History</h2>
<ul>
<li>8/6/2021: Initial publication</li>
<li>8/9/2021: Added Escapes to TeamCity Service Messages</li>
</ul>
<iframe src="https://github.com/sponsors/digitalcoyote/button" height="35" width="116" style="border:0"></iframe>
]]></content:encoded></item><item><title><![CDATA[How to Create a NuGet Package that Runs your Console App as an MSBuild Task]]></title><description><![CDATA[Tutorial using the Build Task NuGet Package Template with GitHub or `dotnet new`]]></description><link>https://blog.codingcoyote.dev/how-to-create-a-nuget-package-that-runs-your-console-app-as-an-msbuild-task</link><guid isPermaLink="true">https://blog.codingcoyote.dev/how-to-create-a-nuget-package-that-runs-your-console-app-as-an-msbuild-task</guid><category><![CDATA[tricks]]></category><category><![CDATA[template]]></category><category><![CDATA[C#]]></category><category><![CDATA[.NET]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Mon, 02 Aug 2021 12:52:10 GMT</pubDate><content:encoded><![CDATA[<h2 id="introduction">Introduction</h2>
<p>The Build Task NuGet Package Template sets up a cross-platform msbuild task that runs at build.</p>
<h2 id="background">Background</h2>
<p>I started looking for a way to run a task at build when I started work on <a target="_blank" href="https://digitalcoyote.github.io/NuGetDefense/">NuGetDefense</a>. I needed to run a console app and emit warnings/errors to MSBuild as part of the Build. I found several articles detailing the pitfalls of trying to write a cross-platform task and settled instead on an <a target="_blank" href="https://docs.microsoft.com/en-us/visualstudio/msbuild/exec-task?view=vs-2019">MSBuild ExecTask</a> run using a nuget Targets file.</p>
<h2 id="using-the-code">Using the Code</h2>
<p>Using the repository is as simple as installing the template and then writing your app.</p>
<pre><code>dotnet <span class="hljs-built_in">new</span> <span class="hljs-comment">--install BuildTaskNuGetPackage.Template::\*</span>
dotnet <span class="hljs-built_in">new</span> nugetbuildtask -n ProjectName -A "FirstName LastName"
</code></pre><p>OR:</p>
<p>If you prefer to use GitHub, you can start with the <a target="_blank" href="https://github.com/digitalcoyote/BuildTaskNuGetPackageTemplate">template repository</a>.</p>
<ol>
<li>Click the <strong>Use This Template</strong> button</li>
<li>Fill out the page including your repository name. <strong>DO NOT</strong> check the <code>Include all branches</code> this would include the branch used to create the dotnet new template as well.</li>
</ol>
<p>Regardless of which method you use, you will need to write your console app and update the nuspec to include any dependencies that are needed for your app to run.</p>
<p><strong>This is how the files are included for NuGetDefense.</strong></p>
<pre><code class="lang-XML"> <span class="hljs-tag">&lt;<span class="hljs-name">files</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- &lt;file src="Src/bin/Release/net461/NuGetDefense.exe" 
               target="tools/net461/NuGetDefense.exe" /&gt; --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"build/nugetdefense.targets"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"build/nugetdefense.targets"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"bin/Release/netcoreapp3.1/NuGetDefense.deps.json"</span> 
          <span class="hljs-attr">target</span>=<span class="hljs-string">"tools/netcoreapp3.1/NuGetDefense.deps.json"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"bin/Release/netcoreapp3.1/NuGetDefense.dll"</span> 
          <span class="hljs-attr">target</span>=<span class="hljs-string">"tools/netcoreapp3.1/NuGetDefense.dll"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"bin/Release/netcoreapp3.1/NuGetDefense.Core.dll"</span> 
          <span class="hljs-attr">target</span>=<span class="hljs-string">"tools/netcoreapp3.1/NuGetDefense.Core.dll"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"bin/Release/netcoreapp3.1/NuGetDefense.NVD.dll"</span> 
          <span class="hljs-attr">target</span>=<span class="hljs-string">"tools/netcoreapp3.1/NuGetDefense.NVD.dll"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"bin/Release/netcoreapp3.1/NuGetDefense.OSSIndex.dll"</span> 
          <span class="hljs-attr">target</span>=<span class="hljs-string">"tools/netcoreapp3.1/NuGetDefense.OSSIndex.dll"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"bin/Release/netcoreapp3.1/NuGetDefense.runtimeconfig.json"</span> 
          <span class="hljs-attr">target</span>=<span class="hljs-string">"tools/netcoreapp3.1/NuGetDefense.runtimeconfig.json"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"bin/Release/netcoreapp3.1/MessagePack.dll"</span> 
          <span class="hljs-attr">target</span>=<span class="hljs-string">"tools/netcoreapp3.1/MessagePack.dll"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"bin/Release/netcoreapp3.1/MessagePack.Annotations.dll"</span> 
          <span class="hljs-attr">target</span>=<span class="hljs-string">"tools/netcoreapp3.1/MessagePack.Annotations.dll"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"bin/Release/netcoreapp3.1/Microsoft.Bcl.AsyncInterfaces.dll"</span> 
          <span class="hljs-attr">target</span>=<span class="hljs-string">"tools/netcoreapp3.1/Microsoft.Bcl.AsyncInterfaces.dll"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"bin/Release/netcoreapp3.1/NuGet.Versioning.dll"</span> 
          <span class="hljs-attr">target</span>=<span class="hljs-string">"tools/netcoreapp3.1/NuGet.Versioning.dll"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"bin/Release/netcoreapp3.1/System.Text.Json.dll"</span> 
          <span class="hljs-attr">target</span>=<span class="hljs-string">"tools/netcoreapp3.1/System.Text.Json.dll"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"bin/Release/netcoreapp3.1/VulnerabilityData.bin"</span> 
          <span class="hljs-attr">target</span>=<span class="hljs-string">"tools/netcoreapp3.1/VulnerabilityData.bin"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"lib/net461/\_.\_"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"lib/net461/\_.\_"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"lib/netcoreapp3.1/\_.\_"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"lib/netcoreapp3.1/\_.\_"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"lib/netstandard2.0/\_.\_"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"lib/netstandard2.0/\_.\_"</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">files</span>&gt;</span>
</code></pre>
<h2 id="the-hard-way">The Hard Way</h2>
<p>So you want to do it the hard way? Maybe you like a challenge or want a deeper understanding of the code you're using.</p>
<h3 id="get-the-console-app">Get the Console App</h3>
<p>This should be fairly basic. You can use Visual Studio or the dotnet cli to create a new console app or use one that already exists. For the sake of this tutorial, I'm going to assume it's written in .NET Core 3.1 (so it is cross-platform) but with a few tweaks, you can use anything. Make sure to build the project so you have a list of all the files and dependencies required to run the app.</p>
<h3 id="create-the-nuspec">Create the Nuspec</h3>
<p>The <em>.nuspec</em> file is the file that tells nuget how to put the package together. You can review the <a target="_blank" href="https://docs.microsoft.com/en-us/nuget/reference/nuspec#replacement-tokens">documentation</a> for this file to see more of what it can do but we are only going to look at a few of the potential fields.</p>
<p>Create the file `<em>yourProjectName.nuspec</em>` somewhere in your repository. I generally put it right beside the project file, but you could potentially have it anywhere in your project/solution. Edit the contents to match the following:</p>
<pre><code class="lang-XML"><span class="hljs-meta">&lt;?xml version="1.0"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">package</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">metadata</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">id</span>&gt;</span>mybuildtask<span class="hljs-tag">&lt;/<span class="hljs-name">id</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>MyBuildTask<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>0.0.0.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">authors</span>&gt;</span>Owner Name(s)<span class="hljs-tag">&lt;/<span class="hljs-name">authors</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">owners</span>&gt;</span>Owner Name(s)<span class="hljs-tag">&lt;/<span class="hljs-name">owners</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">requireLicenseAcceptance</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">requireLicenseAcceptance</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">description</span>&gt;</span>A Description of my Package<span class="hljs-tag">&lt;/<span class="hljs-name">description</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">license</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"expression"</span>&gt;</span>MIT<span class="hljs-tag">&lt;/<span class="hljs-name">license</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">metadata</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">package</span>&gt;</span>
</code></pre>
<p>You will want to replace the appropriate fields with values that match your project/organization. And you will want to keep the <code>id</code> as a lowercase value (<a target="_blank" href="https://github.com/NuGet/NuGetGallery/issues/2988">some NuGet sources may be case-sensitive</a>) to avoid edge-case issues.</p>
<p>Now you need to look at the <strong><em>bin/Release/</em></strong> folder and decide which files need to be included. Typically, this will be every item in that folder. Once you have a list, Add them to a files collection at the bottom of the Nuspec:</p>
<pre><code class="lang-XML"><span class="hljs-meta">&lt;?xml version="1.0"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">package</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">metadata</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">id</span>&gt;</span>mybuildtask<span class="hljs-tag">&lt;/<span class="hljs-name">id</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>MyBuildTask<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>0.0.0.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">authors</span>&gt;</span>Owner Name(s)<span class="hljs-tag">&lt;/<span class="hljs-name">authors</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">owners</span>&gt;</span>Owner Name(s)<span class="hljs-tag">&lt;/<span class="hljs-name">owners</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">requireLicenseAcceptance</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">requireLicenseAcceptance</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">description</span>&gt;</span>A Description of my Package<span class="hljs-tag">&lt;/<span class="hljs-name">description</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">license</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"expression"</span>&gt;</span>MIT<span class="hljs-tag">&lt;/<span class="hljs-name">license</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">metadata</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">files</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"build/MyBuildTask.targets"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"build/MyBuildTask.targets"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">file</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"bin/Release/netcoreapp3.1/MyBuildTask.dll"</span> 
          <span class="hljs-attr">target</span>=<span class="hljs-string">"tools/netcoreapp3.1/MyBuildTask.dll"</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">files</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">package</span>&gt;</span>
</code></pre>
<p>Each <code>&lt;file&gt;</code> element describes a file that you want to include in the package. <code>src</code> is path to the file relative to the directory that contains the project file. <code>target</code> is the location in the package that the files wil end up in. All files that are part of the console app should be placed in the <strong><em>tools</em></strong> directory to ensure they are not referenced in the project that installs this package (could cause numerous issues).</p>
<h3 id="dig-into-the-project-file">Dig into the Project File</h3>
<p>We want this to build itself, then pack itself, but only when we are not trying to debug it. As it stands, trying to run `dotnet pack` on this will fail if the project is built in any configuration other than Release. And on top of that, having to drop to a console after using a hotkey to build is absurd when we can trigger the pack automatically.</p>
<p>Add these two lines to your project file (in the same <code>propertygroup</code> that defines your target frameworks) to automatically pack the nuget package when built in Release.</p>
<pre><code class="lang-XML"><span class="hljs-tag">&lt;<span class="hljs-name">GeneratePackageOnBuild</span> <span class="hljs-attr">Condition</span>=<span class="hljs-string">"'$(Configuration)'=='RELEASE'"</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">GeneratePackageOnBuild</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">NuspecFile</span>&gt;</span>MyBuildTask.nuspec<span class="hljs-tag">&lt;/<span class="hljs-name">NuspecFile</span>&gt;</span>
</code></pre>
<p><strong>Note</strong>: <em>Many articles reference <a target="_blank" href="https://docs.microsoft.com/en-us/nuget/reference/nuspec#replacement-tokens">Replace Tokens</a> to work around the configuration issue, but I've found that .NET Core on Linux replaces $Configuration$ with an empty string. If you working with Legacy .NET applications, or if this is fixed at a later date, you may wish to utilize that feature.</em></p>
<h3 id="create-the-targets-file-and-add-to-the-nuspec">Create the Targets File and Add to the Nuspec</h3>
<p>Create a directory name `<em>build</em>` and in this directory, create a file named `<em>yourprojectname.targets</em>`. It should look something like this:</p>
<pre><code class="lang-XML"><span class="hljs-tag">&lt;<span class="hljs-name">Project</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">PropertyGroup</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">MyBuildTaskExe</span> <span class="hljs-attr">Condition</span>=<span class="hljs-string">"'$(OS)' == Unix"</span>&gt;</span>
    dotnet "$(MSBuildThisFileDirectory)../tools/netcoreapp3.1/MyBuildTask.dll"
    <span class="hljs-tag">&lt;/<span class="hljs-name">MyBuildTaskExe</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">MyBuildTaskExe</span> <span class="hljs-attr">Condition</span>=<span class="hljs-string">"'$(OS)' == 'Windows\_NT'"</span>&gt;</span>
    dotnet "$(MSBuildThisFileDirectory)..\\tools\\netcoreapp3.1\\MyBuildTask.dll"
    <span class="hljs-tag">&lt;/<span class="hljs-name">MyBuildTaskExe</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">PropertyGroup</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">Target</span> <span class="hljs-attr">Name</span>=<span class="hljs-string">"MyBuildTask"</span> <span class="hljs-attr">AfterTargets</span>=<span class="hljs-string">"Build"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Exec</span> <span class="hljs-attr">Command</span>=<span class="hljs-string">"$(MyBuildTaskExe) <span class="hljs-symbol">&amp;quot;</span>$(MSBuildProjectFullPath)<span class="hljs-symbol">&amp;quot;</span> 
     $(TargetFramework)"</span> <span class="hljs-attr">IgnoreExitCode</span>=<span class="hljs-string">"false"</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Target</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">Project</span>&gt;</span>
</code></pre>
<ul>
<li><code>MyBuildTaskExe</code> is defined based on the OS (because the path separators cause a problem otherwise). Defines command we are going to run with the <code>ExecTask</code> (without the arguments)</li>
<li><code>MSBuildThisFileDirectory</code> is the full path to the targets file.</li>
<li><code>MSBuildProjectFullPath</code> is the path to the project file and can be used to find the directory and/or files for the build. It does not wrap itself in doube-quotes (so we wrap it with <strong>"</strong>)</li>
<li><code>AfterTargets</code> could be replaced with <code>BeforeTargets</code> and is used to determine when to run the task.</li>
</ul>
<h2 id="points-of-interest">Points of Interest</h2>
<ul>
<li>This template can be used to run existing commands as well by modifying the <em>Targets</em> File to run a command that should exist on the machine (not recommended for public packages)</li>
<li><a target="_blank" href="https://docs.microsoft.com/en-us/archive/blogs/msbuild/msbuild-visual-studio-aware-error-messages-and-message-formats">Emitting Warnings/Errors to MSBuild is as simple as logging to the console with the correct pattern</a></li>
</ul>
<h2 id="additional-resources">Additional Resources</h2>
<ul>
<li><a target="_blank" href="https://digitalcoyote.github.io/NuGetDefense/">NuGetDefense</a><ul>
<li>The template was created from the source for this package.</li>
</ul>
</li>
<li><a target="_blank" href="https://docs.microsoft.com/en-us/visualstudio/msbuild/exec-task?view=vs-2019">MSBuild ExecTask</a></li>
<li>Nate McMaster's articles on the subject<ul>
<li><a target="_blank" href="https://natemcmaster.com/blog/2017/07/05/msbuild-task-in-nuget/">https://natemcmaster.com/blog/2017/07/05/msbuild-task-in-nuget/</a></li>
<li><a target="_blank" href="https://natemcmaster.com/blog/2017/11/11/msbuild-task-with-dependencies/">https://natemcmaster.com/blog/2017/11/11/msbuild-task-with-dependencies/</a></li>
</ul>
</li>
<li><a target="_blank" href="https://docs.microsoft.com/en-us/nuget/reference/nuspec#replacement-tokens">Nuspec File Reference</a></li>
<li><a target="_blank" href="https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-dot-targets-files">Targets File Documentation</a></li>
<li><a target="_blank" href="https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-reserved-and-well-known-properties">MSBuild Properties</a></li>
</ul>
<h2 id="history">History</h2>
<ul>
<li>9/7/2020 - Initial publication</li>
<li>6/2/2021 - Copied from <a target="_blank" href="https://www.codeproject.com/Tips/5273315/How-to-Create-a-NuGet-Package-that-Runs-your-Conso">CodeProject</a></li>
</ul>
<iframe src="https://github.com/sponsors/digitalcoyote/button" height="35" width="116" style="border:0"></iframe>]]></content:encoded></item><item><title><![CDATA[Creating a Wix# Installer that Includes Prerequisites Part 3]]></title><description><![CDATA[Creating Custom Actions to Simplify Tasks]]></description><link>https://blog.codingcoyote.dev/creating-a-wix-installer-that-includes-prerequisites-part-3</link><guid isPermaLink="true">https://blog.codingcoyote.dev/creating-a-wix-installer-that-includes-prerequisites-part-3</guid><category><![CDATA[C#]]></category><category><![CDATA[.NET]]></category><category><![CDATA[Tutorial]]></category><category><![CDATA[guide]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Mon, 02 Aug 2021 12:45:20 GMT</pubDate><content:encoded><![CDATA[<h2 id="introduction">Introduction</h2>
<p>In part 3, our installer gets a little more complicated. Having insured .Net 4.7 was installed in part 2, we are going to script out the install of Apache Tomcat as a Windows Service by utilizing Custom Actions.</p>
<p>The parts of this series are:</p>
<ol>
<li><a target="_blank" href="https://codingcoyote.hashnode.dev/creating-a-wix-installer-that-includes-prerequisites-part-1">Creating an MSI Installer with Wix#</a> </li>
<li><a target="_blank" href="https://codingcoyote.hashnode.dev/creating-a-wix-installer-that-includes-prerequisites-part-2">Creating an EXE Installer that Bundles Prerequisites</a></li>
<li>Creating Custom Actions to Simplify Tasks (this)</li>
</ol>
<h2 id="background">Background</h2>
<p>WiX allows MSI and EXE installers to do more than run executables and copy files. A <code>CustomAction</code> allows you to write various forms of actions that can be used for actions like backing up and restoring application settings during an upgrade, setting up prerequisites, or reporting an install to a remote server.</p>
<h2 id="assumptions">Assumptions</h2>
<p>I am going to assume a few things going forward. If these are not true, your experience may differ.</p>
<ol>
<li><a target="_blank" href="http://wixtoolset.org/releases/">WiX Toolset</a> is installed</li>
<li>You are coding in C# (VBwww.NET will have similar principals but will require some translation)</li>
<li>You have read over part 2. One of the actions we are going to use will fail if the required runtime is missing.</li>
</ol>
<h2 id="getting-started">Getting Started</h2>
<p>In this scenario we need to install a prerequisite such as Apache Tomcat for our application from the MSI and not the bootstrapper. This is accomplished differently from using a separate installer as we would from the bootstrapper.</p>
<pre><code class="lang-C#"><span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">string</span> <span class="hljs-title">BuildMsi</span>(<span class="hljs-params"></span>)</span>
{
    <span class="hljs-keyword">var</span> TestDir = <span class="hljs-keyword">new</span> Dir(<span class="hljs-string">@"%ProgramFiles%\\My Company\\My Product"</span>);

    TestDir.Files = System.IO.Directory.GetFiles(<span class="hljs-string">@"D:\\Checkouts\\WixSharp Tutorial\\WixSharp Tutorial\\SampleApp\\bin\\Release"</span>).Where(file =&gt; file.EndsWith(<span class="hljs-string">".dll"</span>) || file.EndsWith(<span class="hljs-string">".exe"</span>)).Select(file =&gt; <span class="hljs-keyword">new</span> File(file)).ToArray();

    <span class="hljs-keyword">var</span> project = <span class="hljs-keyword">new</span> Project(<span class="hljs-string">"MyProduct"</span>, TestDir);

    project.GUID = <span class="hljs-keyword">new</span> Guid(<span class="hljs-string">"6fe30b47-2577-43ad-9095-1861ba25889b"</span>);
    <span class="hljs-comment">//project.SourceBaseDir = "&lt;input dir path&gt;";</span>
    <span class="hljs-comment">//project.OutDir = "&lt;output dir path&gt;";</span>

    project.Actions = <span class="hljs-keyword">new</span> WixSharp.Action[] {          
        <span class="hljs-keyword">new</span> ElevatedManagedAction(CustomActions.CheckForApache, <span class="hljs-string">"%this%"</span>),
    };

    <span class="hljs-keyword">return</span> project.BuildMsi();
}
</code></pre>
<p>Notice above that we have added a new Section to the code. <code>project.Action</code> adds a list of actions that can be defined in anumber of ways. The WiX ways are primarily vbs or Jscript, but for the .NET developer Managed Actions will feel more familiar and be easier to upkeep. We have defined these Actions in a separate class below.</p>
<pre><code class="lang-C#"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">CustomActions</span>
{
    [<span class="hljs-meta">CustomAction</span>]
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ActionResult <span class="hljs-title">CheckForApache</span>(<span class="hljs-params">Session session</span>)</span>
    {
        <span class="hljs-comment">//check for apache</span>
        <span class="hljs-keyword">return</span> ActionResult.Success;
    }
}
</code></pre>
<p>The CustomAction Attribute marks these methods as an entry point for the installer.  This is not a feature of WiX/Wix# however so you will need to add a using statement to the top of you file.</p>
<pre><code class="lang-C#"><span class="hljs-keyword">using</span> Microsoft.Deployment.WindowsInstaller;
</code></pre>
<h2 id="implementing-the-custom-actions">Implementing the Custom Actions</h2>
<p>Below are code samples for how one might choose to implement the Custom Actions such as the one above.</p>
<h3 id="checking-for-apache">Checking for Apache</h3>
<pre><code class="lang-C#"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">CustomActions</span>
    {
        [<span class="hljs-meta">CustomAction</span>]
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ActionResult <span class="hljs-title">CheckForApache</span>(<span class="hljs-params">Session session</span>)</span>
        {
            ActionResult result = ActionResult.Success;
            <span class="hljs-keyword">try</span>
            {
                System.ServiceProcess.ServiceController apache = ServiceController.GetServices()
                    .FirstOrDefault(s =&gt; s.ServiceName == <span class="hljs-string">"Tomcat8"</span>);

                <span class="hljs-keyword">if</span> (apache == <span class="hljs-literal">null</span>)
                {
                    <span class="hljs-keyword">if</span> (System.IO.File.Exists(<span class="hljs-string">@"C:\\ProgramData\\chocolatey\\choco.exe"</span>))
                    {
                        CmdRunner(<span class="hljs-string">@"@"" % SystemRoot %\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"" - NoProfile - InputFormat None - ExecutionPolicy Bypass - Command ""iex((New - Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"" &amp;&amp; SET ""PATH =% PATH %;% ALLUSERSPROFILE %\\chocolatey\\bin"</span>);
                    }

                    CmdRunner(<span class="hljs-string">"Choco install Tomcat"</span>);
                }
            }
            <span class="hljs-keyword">catch</span>
            {
                result = ActionResult.Failure;
            }

            <span class="hljs-keyword">return</span> result;
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">CmdRunner</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> cmd</span>)</span>
        {
            System.Diagnostics.ProcessStartInfo startInfo = <span class="hljs-keyword">new</span> System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = <span class="hljs-string">"cmd.exe"</span>;
            startInfo.Arguments = <span class="hljs-string">@"/C"</span> + cmd ;
            System.Diagnostics.Process process = <span class="hljs-keyword">new</span> System.Diagnostics.Process();
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();
        }
    }
</code></pre>
<p>There are a few problems with the code above. We are forcing our user to install <a target="_blank" href="https://chocolatey.org/">Chocolatey</a> (a package manager for windows), we are checking for the default service name, and we are installing a prerequisite that is available as an installer (<a target="_blank" href="https://tomcat.apache.org/download-90.cgi">here</a>). This means we should instead bundle it in the bootstrapper. </p>
<h2 id="points-of-interest">Points of Interest</h2>
<ul>
<li>Wix# Custom Actions come in a variety of flavors as demonstrated on <a target="_blank" href="https://github.com/oleg-shilo/wixsharp/tree/master/Source/src/WixSharp.Samples/Wix%23%20Samples/CustomActions">github</a> but the <code>ManagedAction</code> and <code>ElevatedManagedAction</code> being easier to deal with for .NET developers.</li>
<li>Custom Actions are only usable in MSI's and not bootstrappers.</li>
<li>The <a target="_blank" href="http://wixtoolset.org/documentation/manual/v3/xsd/wix/customaction.html">WiX Toolset Documentation for CustomAction Elements</a> gives explicit detail on many of the options such as <code>Return</code> and <code>Execute</code>.</li>
</ul>
<h2 id="customizing-custom-actions">Customizing Custom Actions</h2>
<p>Custom Actions can be more than what we have shown above. For instance, what if you want to start the application immediately after installing it?</p>
<p>new InstalledFileAction("sample_exe", "", Return.asyncNoWait, When.After, Step.InstallFinalize, Condition.NOT_Installed),</p>
<p>This indicates that a file installed by this MSI will be run After the InstallFinalizes and it will continue to run even after the installer exits, but only if the application was not already installed.</p>
<p>Key elements that assist in deciding when and what are:</p>
<ul>
<li><code>Return</code> tells the MSI if it needs to handle the actions completion and if so, what to do with it.</li>
<li><code>Execute</code> determines when the CustomAction is scheduled to execute such as on RollBack of a failed MSI.</li>
<li><code>When</code> combined with <code>Step</code> indicate in a relation in time to when the step should be executed.</li>
</ul>
<h2 id="dealing-with-errors">Dealing With Errors</h2>
<p>Be sure to appropriately wrap you CustomActions in a <code>try/catch</code> and handle the errors appropriately. Users may need to be notified of errors and you will want to fail the action if a vital portion of the MSI does not execute due to exceptions. Your choices for notifying the user may include a popup error and/or event log messages.</p>
<h2 id="history">History</h2>
<ul>
<li>3/16/2018: Initial publication</li>
<li>8/2/2021: Copied to HashNode from <a target="_blank" href="https://www.codeproject.com/Articles/1234160/Creating-a-Wixsharp-Installer-that-Includes-Prer-2">CodeProject Article</a></li>
</ul>
<iframe src="https://github.com/sponsors/digitalcoyote/button" height="35" width="116" style="border:0"></iframe>]]></content:encoded></item><item><title><![CDATA[Creating a Wix# Installer That Includes Prerequisites Part 2]]></title><description><![CDATA[Creating an EXE Installer that Bundles Prerequisites]]></description><link>https://blog.codingcoyote.dev/creating-a-wix-installer-that-includes-prerequisites-part-2</link><guid isPermaLink="true">https://blog.codingcoyote.dev/creating-a-wix-installer-that-includes-prerequisites-part-2</guid><category><![CDATA[C#]]></category><category><![CDATA[.NET]]></category><category><![CDATA[Tutorial]]></category><category><![CDATA[guide]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Mon, 02 Aug 2021 12:33:02 GMT</pubDate><content:encoded><![CDATA[<h2 id="introduction">Introduction</h2>
<p>This tutorial picks up where Part 1 left off, but if you have a general understanding of C#, it should not be hard to follow. Today, we are going to look at adding the .NET Framework as a prerequisite of the installer.</p>
<p>The parts of this series are:</p>
<ol>
<li><a target="_blank" href="https://codingcoyote.hashnode.dev/creating-a-wix-installer-that-includes-prerequisites-part-1">Creating an MSI installer with Wix#</a></li>
<li>Creating an EXE Installer that Bundles Prerequisites (this one)</li>
<li><a target="_blank" href="https://codingcoyote.hashnode.dev/creating-a-wix-installer-that-includes-prerequisites-part-3">Creating Custom Actions to Simplify Tasks</a></li>
</ol>
<h2 id="background">Background</h2>
<p>Installers are great, but telling a user that they need to look up and install a specific framework is a lot for the average user. Even directing them to the website where they can download it can be overwhelming for some. With Wix#, it becomes easy to add in a list of packages to install that includes everything the user needs to get started.</p>
<p>The WiX Toolset relies on a tool called <a target="_blank" href="https://www.firegiant.com/wix/tutorial/net-and-net/bootstrapping/">Burn</a> (continuing with the Candle theme) to build EXE packages that install prerequisites and combine multiple MSIs. This process is known as BootStrapping.</p>
<h2 id="assumptions">Assumptions</h2>
<p>I am going to assume a few things going forward. If these are not true, your experience may differ.</p>
<ol>
<li><a target="_blank" href="https://www.visualstudio.com/vs/">Visual Studio 2017</a>  is your IDE</li>
<li><a target="_blank" href="http://wixtoolset.org/releases/">WiX Toolset</a> is installed</li>
<li>You are coding in C# (VB.NET will have similar principals but will require some translation)</li>
<li>You are using the <a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=OlegShilo.WixSharpProjectTemplates">WixSharp Project Templates Extension for Visual Studio</a></li>
<li>You have read part 1 and have a basic MSI installer project using Wix#</li>
</ol>
<h2 id="building-a-bundle-with-wix">Building a Bundle with Wix#</h2>
<p>The BootStrapper for the Installers in Wix is referred to as a Bundle and uses a WiX Toolset BurnProject. In the WixSharp Project Templates Extension, a Burn Project is referred to as a WixSharp Setup - BootStrapper. It's easiest to just start making a few changes to our starter project if you worked through Part 1.</p>
<p>Start by adding <code>WixSharp.Bootstrapper</code> as a <code>using</code> statement and then restructure your installer as follows:</p>
<pre><code class="lang-C#"><span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"></span>)</span>
{
    <span class="hljs-keyword">string</span> productMsi = BuildMsi();

    <span class="hljs-keyword">var</span> bootstrapper =
      <span class="hljs-keyword">new</span> Bundle(<span class="hljs-string">"SampleInstaller"</span>,
          <span class="hljs-keyword">new</span> MsiPackage(productMsi) { DisplayInternalUI = <span class="hljs-literal">true</span>, Compressed = <span class="hljs-literal">true</span> })
      {
          UpgradeCode = <span class="hljs-keyword">new</span> Guid(&lt;Insert Your Bundle GUID&gt;),
          Version = <span class="hljs-keyword">new</span> Version(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>),
      };
    bootstrapper.Build(<span class="hljs-string">"SampleBootStrapper.exe"</span>);
}

<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">string</span> <span class="hljs-title">BuildMsi</span>(<span class="hljs-params"></span>)</span>
{
    <span class="hljs-keyword">var</span> TestDir = <span class="hljs-keyword">new</span> Dir(<span class="hljs-string">@"%ProgramFiles%\\My Company\\My Product"</span>);
    TestDir.Files = System.IO.Directory.GetFiles
     (<span class="hljs-string">@"D:\\Checkouts\\WixSharp Tutorial\\WixSharp Tutorial\\SampleApp\\bin\\Release"</span>).Where
     (file =&gt; file.EndsWith(<span class="hljs-string">".dll"</span>) || file.EndsWith(<span class="hljs-string">".exe"</span>)).Select(file =&gt; <span class="hljs-keyword">new</span> File(file)).ToArray();

    <span class="hljs-keyword">var</span> project = <span class="hljs-keyword">new</span> Project(<span class="hljs-string">"MyProduct"</span>, TestDir);

    project.GUID = <span class="hljs-keyword">new</span> Guid(&lt;Insert Your MSI GUID&gt;);

    <span class="hljs-keyword">return</span> project.BuildMsi();
}
</code></pre>
<p>As you notice, we are still building the MSI for the installer in essentially the same way, but then we return the path to the freshly built MSI and build a bundle wrapped around it.</p>
<h2 id="including-prerequisites-with-bundle">Including Prerequisites with Bundle</h2>
<p>Now that we have a basic bundle put together, we realize that our application requires .NET 4.7 to be installed and the user may not have it installed. With the <code>PackageRefGroups</code> available through the Wix Toolkit, we can check for and have installed a number of runtimes.</p>
<pre><code class="lang-C#"><span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"></span>)</span>
{
    <span class="hljs-keyword">string</span> productMsi = BuildMsi();

    <span class="hljs-keyword">var</span> bootstrapper =
     <span class="hljs-keyword">new</span> Bundle(<span class="hljs-string">"SampleInstaller"</span>,
       <span class="hljs-keyword">new</span> PackageGroupRef(<span class="hljs-string">"NetFx462Web"</span>),
       <span class="hljs-keyword">new</span> MsiPackage(productMsi) { DisplayInternalUI = <span class="hljs-literal">true</span>, Compressed = <span class="hljs-literal">true</span> })
     {
         UpgradeCode = <span class="hljs-keyword">new</span> Guid(&lt; Insert Your Bundle GUID &gt;),
         Version = <span class="hljs-keyword">new</span> Version(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>),
     };

    bootstrapper.IncludeWixExtension(WixExtension.NetFx);
    bootstrapper.Build(<span class="hljs-string">"SampleBootStrapper.exe"</span>);
}
</code></pre>
<p>The key lines above are <code>new PackageGroupRef("NetFx462Web")</code> and <code>bootstrapper.IncludeWixExtension(WixExtension.NetFx);</code></p>
<p><code>NetFx462Web</code> tells WiX to install .NET 4.62 by bundling in the .NET Web Installer. This keeps your installer size small but requires an internet connection with access to the URL.  You could also use <code>NetFx462Redist</code> which tells WiX to bundle the standalone installer from. WiX also has variables that can be used to check if .NET 4.62 is already installed instead of dumping the user into an installer needlessly.</p>
<h2 id="packaging-net-47-or-any-exe">Packaging .NET 4.7 (or any .exe)</h2>
<p>If your application needs 4.7+ versions of the .NET Framework, there is at the time of this writing no support in the NetFx extension from WiX. However, the base WiX tools contain everything needed to accomplish this. As noted under Points of Interest, Microsoft has an in depth guide on how to detect what versions of the framework are installed. But to do this in WiX, we need to add a fragment. This fragment combined with another extension (Util) essentially allows us to store the result of a registry search.</p>
<pre><code class="lang-C#">bootstrapper.IncludeWixExtension(WixExtension.Util);

bootstrapper.AddWixFragment(<span class="hljs-string">"Wix/Bundle"</span>,
    <span class="hljs-keyword">new</span> UtilRegistrySearch
    {
        Root = Microsoft.Win32.RegistryHive.LocalMachine,
        Key = <span class="hljs-string">@"SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full"</span>,
        Value = <span class="hljs-string">"Release"</span>,
        Variable = <span class="hljs-string">"NetVersion"</span>
    });

The <span class="hljs-keyword">value</span> found <span class="hljs-keyword">by</span> <span class="hljs-keyword">this</span> result <span class="hljs-keyword">is</span> stored <span class="hljs-keyword">as</span> `NetVersion` and <span class="hljs-keyword">is</span> used <span class="hljs-keyword">in</span> conditions to determine <span class="hljs-keyword">if</span> we need to install the framework or not. This <span class="hljs-keyword">is</span> a good first step and difficult to translate <span class="hljs-keyword">from</span> direct WiX examples.

<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ExePackage <span class="hljs-title">Net471</span>(<span class="hljs-params"></span>)</span>
        {
            <span class="hljs-keyword">string</span> currentN2t471webinstaller = 
              <span class="hljs-string">@"https://download.microsoft.com/download/8/E/2/8E2BDDE7-F06E-44CC-A145-56C6B9BBE5DD/
              NDP471-KB4033344-Web.exe"</span>;
            <span class="hljs-keyword">string</span> Net471Installer = <span class="hljs-string">"Net471-web.exe"</span>;
            <span class="hljs-keyword">using</span> (<span class="hljs-keyword">var</span> client = <span class="hljs-keyword">new</span> WebClient())
            {
                client.DownloadFile(currentN2t471webinstaller, Net471Installer);
            }
            ExePackage Net471exe = <span class="hljs-keyword">new</span> ExePackage(Net471Installer)
            {
                Compressed = <span class="hljs-literal">true</span>,
                Vital = <span class="hljs-literal">true</span>,
                Name = <span class="hljs-string">"Net471-web.exe"</span>,
                DetectCondition = <span class="hljs-string">"NetVersion &gt;= 460798"</span>,
                PerMachine = <span class="hljs-literal">true</span>,
                Permanent = <span class="hljs-literal">true</span>,
            };

            <span class="hljs-keyword">return</span> Net471exe;
        }
</code></pre>
<p>Now we have declared a <code>static</code> method that generates an <code>ExePackage</code> for our installer to use. There are a few key things to obtain from this code sample:</p>
<ul>
<li><code>WebClient</code> is from the <code>System.Net</code> namespace and is used here to download the file at build time. If the link you are using may not always be obtainable, you will want to store it locally and replace this with logic to copy the file into the working directory.</li>
<li><code>Compressed</code> if <code>true</code> causes the executable to be compressed and stored in the resulting installers instead of distributed as a separate file.</li>
<li><code>Vital</code> causes the install to fail and roll back if this EXE fails.</li>
<li><code>DetectCondition</code> is where we use the variable from above. Per Microsoft, 460798 is the min version (depends on OS) that can be .NET 4.7. In this case, my app is built in 4.7 but we are installing 4.71 with the installer to prevent needless framework installs if we decide to upgrade in the near future.</li>
</ul>
<h2 id="putting-it-all-together-complete-code">Putting It All Together - Complete Code</h2>
<pre><code class="lang-C#"><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Linq;
<span class="hljs-keyword">using</span> System.Net;
<span class="hljs-keyword">using</span> WixSharp;
<span class="hljs-keyword">using</span> WixSharp.Bootstrapper;

<span class="hljs-keyword">namespace</span> <span class="hljs-title">WixSharp</span>\_<span class="hljs-title">Setup2</span>
{
    <span class="hljs-keyword">class</span> <span class="hljs-title">Program</span>
    {
        <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"></span>)</span>
        {

            <span class="hljs-keyword">string</span> productMsi = BuildMsi();


            <span class="hljs-keyword">var</span> bootstrapper =
              <span class="hljs-keyword">new</span> Bundle(<span class="hljs-string">"SampleInstaller"</span>,
              Net471(),
                  <span class="hljs-keyword">new</span> MsiPackage(productMsi) { DisplayInternalUI = <span class="hljs-literal">true</span>, Compressed = <span class="hljs-literal">true</span> })
              <span class="hljs-comment">//new PackageGroupRef("NetFx47Web"))</span>
              {
                  UpgradeCode = <span class="hljs-keyword">new</span> Guid(&lt;Insert Your Bundle GUID&gt;),
                  Version = <span class="hljs-keyword">new</span> Version(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0</span>),
              };

            bootstrapper.IncludeWixExtension(WixExtension.Util);

            bootstrapper.AddWixFragment(<span class="hljs-string">"Wix/Bundle"</span>,
                <span class="hljs-keyword">new</span> UtilRegistrySearch
                {
                    Root = Microsoft.Win32.RegistryHive.LocalMachine,
                    Key = <span class="hljs-string">@"SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full"</span>,
                    Value = <span class="hljs-string">"Release"</span>,
                    Variable = <span class="hljs-string">"NetVersion"</span>

                });
            bootstrapper.PreserveTempFiles = <span class="hljs-literal">true</span>;
            bootstrapper.Application.LicensePath = <span class="hljs-literal">null</span>;
            bootstrapper.Build(<span class="hljs-string">"SampleBootStrapper.exe"</span>);
        }

        <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">string</span> <span class="hljs-title">BuildMsi</span>(<span class="hljs-params"></span>)</span>
        {
            <span class="hljs-keyword">var</span> TestDir = <span class="hljs-keyword">new</span> Dir(<span class="hljs-string">@"%ProgramFiles%\\My Company\\My Product"</span>);

            TestDir.Files = System.IO.Directory.GetFiles
            (<span class="hljs-string">@"D:\\Checkouts\\WixSharp Tutorial\\WixSharp Tutorial\\SampleApp\\bin\\Release"</span>).Where
            (file =&gt; file.EndsWith(<span class="hljs-string">".dll"</span>) || file.EndsWith(<span class="hljs-string">".exe"</span>)).Select
            (file =&gt; <span class="hljs-keyword">new</span> File(file)).ToArray();

            <span class="hljs-keyword">var</span> project = <span class="hljs-keyword">new</span> Project(<span class="hljs-string">"MyProduct"</span>, TestDir);

            project.GUID = <span class="hljs-keyword">new</span> Guid(&lt;Insert Your MSI GUID&gt;);
            <span class="hljs-comment">//project.SourceBaseDir = "&lt;input dir path&gt;";</span>
            <span class="hljs-comment">//project.OutDir = "&lt;output dir path&gt;";</span>

            <span class="hljs-keyword">return</span> project.BuildMsi();
        }

        <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ExePackage <span class="hljs-title">Net471</span>(<span class="hljs-params"></span>)</span>
        {
            <span class="hljs-keyword">string</span> currentN2t471webinstaller = 
               <span class="hljs-string">@"https://download.microsoft.com/download/8/E/2/8E2BDDE7-F06E-44CC-A145-56C6B9BBE5DD/
               NDP471-KB4033344-Web.exe"</span>;
            <span class="hljs-keyword">string</span> Net471Installer = <span class="hljs-string">"Net471-web.exe"</span>;
            <span class="hljs-keyword">using</span> (<span class="hljs-keyword">var</span> client = <span class="hljs-keyword">new</span> WebClient())
            {
                client.DownloadFile(currentN2t471webinstaller, Net471Installer);
            }
            ExePackage Net471exe = <span class="hljs-keyword">new</span> ExePackage(Net471Installer)
            {
                Compressed = <span class="hljs-literal">true</span>,
                Vital = <span class="hljs-literal">true</span>,
                Name = <span class="hljs-string">"Net471-web.exe"</span>,
                DetectCondition = <span class="hljs-string">"NetVersion &gt;= 460798"</span>,
                PerMachine = <span class="hljs-literal">true</span>,
                Permanent = <span class="hljs-literal">true</span>,
            };

            <span class="hljs-keyword">return</span> Net471exe;
        }
    }
}
</code></pre>
<h2 id="points-of-interest">Points of Interest</h2>
<ul>
<li>Microsoft has an excellent guide to using registry keys to detect which version of the .NET Framework is installed (<a target="_blank" href="https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed">https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed</a>)</li>
<li>WiX's .NetFx extension contains a lot of prebuilt methods of detecting and installing .NET (<a target="_blank" href="http://wixtoolset.org/documentation/manual/v3/customactions/wixnetfxextension.html">http://wixtoolset.org/documentation/manual/v3/customactions/wixnetfxextension.html</a>)</li>
<li>At the time of this writing, WiX does not include a package for .NET 4.7. However, this can be bundled with your installer by including the installer as an <code>ExePackage</code> as detailed above.</li>
<li>WiX <code>ExePackage</code> optoins will map directly to those used in Wix# (<a target="_blank" href="http://wixtoolset.org/documentation/manual/v3/xsd/wix/exepackage.html">http://wixtoolset.org/documentation/manual/v3/xsd/wix/exepackage.html</a>)</li>
</ul>
<h2 id="history">History</h2>
<ul>
<li>3/10/2018: Initial publication</li>
<li>8/2/2021: Copied to HashNode from <a target="_blank" href="https://www.codeproject.com/Articles/1232233/Creating-a-Wixsharp-Installer-That-Includes-Prer">CodeProject Article</a></li>
</ul>
<iframe src="https://github.com/sponsors/digitalcoyote/button" height="35" width="116" style="border:0"></iframe>]]></content:encoded></item><item><title><![CDATA[Creating a Wix# Installer That Includes Prerequisites Part 1]]></title><description><![CDATA[A beginners guide to creating Windows Installers with Wix#.]]></description><link>https://blog.codingcoyote.dev/creating-a-wix-installer-that-includes-prerequisites-part-1</link><guid isPermaLink="true">https://blog.codingcoyote.dev/creating-a-wix-installer-that-includes-prerequisites-part-1</guid><category><![CDATA[C#]]></category><category><![CDATA[.NET]]></category><category><![CDATA[Tutorial]]></category><category><![CDATA[guide]]></category><dc:creator><![CDATA[Curtis Carter]]></dc:creator><pubDate>Mon, 02 Aug 2021 12:15:43 GMT</pubDate><content:encoded><![CDATA[<h2 id="introduction">Introduction</h2>
<p>In part 1, we are going to look at going from Console App to MSI using Wix# and getting the program installed and capable of being upgraded/uninstalled.</p>
<p>The parts of this series are:</p>
<ol>
<li>Creating an MSI Installer with Wix# (this one)</li>
<li><a target="_blank" href="https://codingcoyote.hashnode.dev/creating-a-wix-installer-that-includes-prerequisites-part-2">Creating an EXE Installer that Bundles Prerequisites</a></li>
<li><a target="_blank" href="https://codingcoyote.hashnode.dev/creating-a-wix-installer-that-includes-prerequisites-part-3">Creating Custom Actions to Simplify Tasks</a></li>
</ol>
<h2 id="background">Background</h2>
<p>Wix# (<a target="_blank" href="https://github.com/oleg-shilo/wixsharp">GitHub</a>) is a programmatic way to create Windows installers using the power and flexibility of the <a target="_blank" href="http://wixtoolset.org/">WiX Toolset</a>. I found this to be the easiest way to make the installer editable and maintainable by a group of developers with very little knowledge of the WiX Toolset itself. The main benefit of Wix# over raw WiX is the approachability. You do not have to be a WiX Toolset expert to use Wix# effectively.</p>
<h2 id="assumptions">Assumptions</h2>
<p>I am going to assume a few things going forward. If these are not true, your experience may differ.</p>
<ol>
<li><a target="_blank" href="https://www.visualstudio.com/vs/">Visual Studio 2017</a> is your IDE</li>
<li><a target="_blank" href="http://wixtoolset.org/releases/">WiX Toolset</a> is installed</li>
<li>You are coding in C# (VB.NET or another CLR language will have similar principals but will require some translation)</li>
<li>You are using the <a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=OlegShilo.WixSharpProjectTemplates">WixSharp Project Templates Extension for Visual Studio</a></li>
</ol>
<h2 id="getting-started">Getting Started</h2>
<pre><code class="lang-C#">
<span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> WixSharp;

<span class="hljs-keyword">class</span> <span class="hljs-title">Script</span>
{
    <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>)</span>
    {
        <span class="hljs-keyword">var</span> project = <span class="hljs-keyword">new</span> Project(<span class="hljs-string">"MyProduct"</span>,
                          <span class="hljs-keyword">new</span> Dir(<span class="hljs-string">@"%ProgramFiles%\\My Company\\My Product"</span>,
                              <span class="hljs-keyword">new</span> File(<span class="hljs-string">@"Files\\Docs\\Manual.txt"</span>),
                              <span class="hljs-keyword">new</span> File(<span class="hljs-string">@"Files\\Bin\\MyApp.exe"</span>)));

        project.GUID = <span class="hljs-keyword">new</span> Guid(<span class="hljs-string">"6f330b47-2577-43ad-9095-1861ba25889b"</span>);

        Compiler.BuildMsi(project);
    }
}
</code></pre>
<p>As you can see, the <code>Project</code> gets a name and then a Directory is defined and which files should be copied to it. Then a GUID is added (you need to replace this). And lastly, it builds the MSI. Building this project now adds a "<em>wix</em>" folder to our project and dumps the resulting WiX file into it.</p>
<pre><code class="lang-XML"><span class="hljs-meta">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span>
<span class="hljs-comment">&lt;!--
&lt;auto-generated&gt;
    This code was generated by WixSharp.
    Changes to this file will be lost if the code is regenerated.
&lt;/auto-generated&gt;
--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">Wix</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://schemas.microsoft.com/wix/2006/wi"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">Product</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-1861ca25889c"</span> <span class="hljs-attr">Name</span>=<span class="hljs-string">"MyProduct"</span> 
   <span class="hljs-attr">Language</span>=<span class="hljs-string">"1033"</span> <span class="hljs-attr">Codepage</span>=<span class="hljs-string">"Windows-1252"</span> <span class="hljs-attr">Version</span>=<span class="hljs-string">"1.0.0.0"</span> 
   <span class="hljs-attr">UpgradeCode</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-1861ba25889b"</span> <span class="hljs-attr">Manufacturer</span>=<span class="hljs-string">"ccarter"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Package</span> <span class="hljs-attr">InstallerVersion</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">Compressed</span>=<span class="hljs-string">"yes"</span> 
    <span class="hljs-attr">SummaryCodepage</span>=<span class="hljs-string">"Windows-1252"</span> <span class="hljs-attr">Languages</span>=<span class="hljs-string">"1033"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Media</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"1"</span> <span class="hljs-attr">Cabinet</span>=<span class="hljs-string">"MyProduct.cab"</span> <span class="hljs-attr">EmbedCab</span>=<span class="hljs-string">"yes"</span> /&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">Directory</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"TARGETDIR"</span> <span class="hljs-attr">Name</span>=<span class="hljs-string">"SourceDir"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Directory</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"ProgramFilesFolder"</span> <span class="hljs-attr">Name</span>=<span class="hljs-string">"ProgramFilesFolder"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Directory</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"ProgramFilesFolder.My\_Company"</span> <span class="hljs-attr">Name</span>=<span class="hljs-string">"My Company"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Directory</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"INSTALLDIR"</span> <span class="hljs-attr">Name</span>=<span class="hljs-string">"My Product"</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.Program.cs\_2096188551"</span> 
            <span class="hljs-attr">Guid</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-1861e8d77bb1"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">File</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Program.cs\_2096188551"</span> <span class="hljs-attr">Source</span>=<span class="hljs-string">"Program.cs"</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Component</span>&gt;</span>

          <span class="hljs-tag">&lt;/<span class="hljs-name">Directory</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Directory</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Directory</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Directory</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">UIRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"WixUI\_Minimal"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">UIRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"WixUI\_ErrorProgressText"</span> /&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">Feature</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Complete"</span> <span class="hljs-attr">Title</span>=<span class="hljs-string">"Complete"</span> <span class="hljs-attr">Absent</span>=<span class="hljs-string">"allow"</span> <span class="hljs-attr">Level</span>=<span class="hljs-string">"1"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ComponentRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.Program.cs\_2096188551"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Feature</span>&gt;</span>

  <span class="hljs-tag">&lt;/<span class="hljs-name">Product</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">Wix</span>&gt;</span>
</code></pre>
<p>And we also find a "<em>MyProduct.msi</em>" is output to the project directory.</p>
<p>So besides the Wix# sample being more succinct, why else would we prefer Wix# over raw Wix. Well..</p>
<pre><code class="lang-C#"><span class="hljs-keyword">var</span> TestDir = <span class="hljs-keyword">new</span> Dir(<span class="hljs-string">@"%ProgramFiles%\\My Company\\My Product"</span>);

TestDir.Files = System.IO.Directory.GetFiles
(<span class="hljs-string">@"\\path\\to\\solution\\WixSharp Tutorial\\WixSharp Tutorial\\SampleApp\\bin\\Release"</span>).Where
(file =&gt; file.EndsWith(<span class="hljs-string">".dll"</span>) || 
file.EndsWith(<span class="hljs-string">".exe"</span>)).Select(file =&gt; <span class="hljs-keyword">new</span> File(file)).ToArray();

<span class="hljs-keyword">var</span> project = <span class="hljs-keyword">new</span> Project(<span class="hljs-string">"MyProduct"</span>, TestDir);
</code></pre>
<p>which produces this:</p>
<pre><code class="lang-XML"><span class="hljs-meta">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span>
<span class="hljs-comment">&lt;!--
&lt;auto-generated&gt;
    This code was generated by WixSharp.
    Changes to this file will be lost if the code is regenerated.
&lt;/auto-generated&gt;
--&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">Wix</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://schemas.microsoft.com/wix/2006/wi"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">Product</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-1861ca25889c"</span> <span class="hljs-attr">Name</span>=<span class="hljs-string">"MyProduct"</span> 
   <span class="hljs-attr">Language</span>=<span class="hljs-string">"1033"</span> <span class="hljs-attr">Codepage</span>=<span class="hljs-string">"Windows-1252"</span> <span class="hljs-attr">Version</span>=<span class="hljs-string">"1.0.0.0"</span> 
   <span class="hljs-attr">UpgradeCode</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-1861ba25889b"</span> <span class="hljs-attr">Manufacturer</span>=<span class="hljs-string">"ccarter"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Package</span> <span class="hljs-attr">InstallerVersion</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">Compressed</span>=<span class="hljs-string">"yes"</span> 
    <span class="hljs-attr">SummaryCodepage</span>=<span class="hljs-string">"Windows-1252"</span> <span class="hljs-attr">Languages</span>=<span class="hljs-string">"1033"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Media</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"1"</span> <span class="hljs-attr">Cabinet</span>=<span class="hljs-string">"MyProduct.cab"</span> <span class="hljs-attr">EmbedCab</span>=<span class="hljs-string">"yes"</span> /&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">Directory</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"TARGETDIR"</span> <span class="hljs-attr">Name</span>=<span class="hljs-string">"SourceDir"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Directory</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"INSTALLDIR"</span> <span class="hljs-attr">Name</span>=<span class="hljs-string">"ProgramFilesFolder"</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.Editor.dll\_374746071"</span> 
        <span class="hljs-attr">Guid</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-18613d90149a"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">File</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Editor.dll\_374746071"</span> 
          <span class="hljs-attr">Source</span>=<span class="hljs-string">"..\\SampleApp\\bin\\Release\\Editor.dll"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Component</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.SampleApp.dll\_1981177689"</span> 
        <span class="hljs-attr">Guid</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-1861c6f85d24"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">File</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"SampleApp.dll\_1981177689"</span> 
          <span class="hljs-attr">Source</span>=<span class="hljs-string">"..\\SampleApp\\bin\\Release\\SampleApp.dll"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Component</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.SampleApp.exe\_60174109"</span> 
        <span class="hljs-attr">Guid</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-1861a74d0c84"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">File</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"SampleApp.exe\_60174109"</span> 
          <span class="hljs-attr">Source</span>=<span class="hljs-string">"..\\SampleApp\\bin\\Release\\SampleApp.exe"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Component</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.SampleAppEditor.exe\_729600358"</span> 
         <span class="hljs-attr">Guid</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-18610c59b6e9"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">File</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"SampleAppEditor.exe\_729600358"</span> 
           <span class="hljs-attr">Source</span>=<span class="hljs-string">"..\\SampleApp\\bin\\Release\\SampleAppEditor.exe"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Component</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.Sugar.dll\_2062009301"</span> 
        <span class="hljs-attr">Guid</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-18616d4e4353"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">File</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Sugar.dll\_2062009301"</span> 
          <span class="hljs-attr">Source</span>=<span class="hljs-string">"..\\SampleApp\\bin\\Release\\Sugar.dll"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Component</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.SugarShaker.exe\_1177907971"</span> 
         <span class="hljs-attr">Guid</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-1861746701f0"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">File</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"SugarShaker.exe\_1177907971"</span> 
          <span class="hljs-attr">Source</span>=<span class="hljs-string">"..\\SampleApp\\bin\\Release\\SugarShaker.exe"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Component</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Directory</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"INSTALLDIR.My\_Company"</span> <span class="hljs-attr">Name</span>=<span class="hljs-string">"My Company"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Directory</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"INSTALLDIR.My\_Company.My\_Product"</span> <span class="hljs-attr">Name</span>=<span class="hljs-string">"My Product"</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"My\_Product.EmptyDirectory"</span> 
             <span class="hljs-attr">Guid</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-18611cb1741a"</span> <span class="hljs-attr">KeyPath</span>=<span class="hljs-string">"yes"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">CreateFolder</span> /&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">RemoveFolder</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"INSTALLDIR.My\_Company.My\_Product"</span> <span class="hljs-attr">On</span>=<span class="hljs-string">"uninstall"</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Component</span>&gt;</span>

          <span class="hljs-tag">&lt;/<span class="hljs-name">Directory</span>&gt;</span>

          <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"INSTALLDIR.My\_Company"</span> 
           <span class="hljs-attr">Guid</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-18618b356f3d"</span> <span class="hljs-attr">KeyPath</span>=<span class="hljs-string">"yes"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">CreateFolder</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">RemoveFolder</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"INSTALLDIR.My\_Company"</span> <span class="hljs-attr">On</span>=<span class="hljs-string">"uninstall"</span> /&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">Component</span>&gt;</span>

        <span class="hljs-tag">&lt;/<span class="hljs-name">Directory</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Directory</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"TARGETDIR"</span> 
      <span class="hljs-attr">Guid</span>=<span class="hljs-string">"6fe30b47-2577-43ad-9095-18612df5f80e"</span> <span class="hljs-attr">KeyPath</span>=<span class="hljs-string">"yes"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">CreateFolder</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">RemoveFolder</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"TARGETDIR"</span> <span class="hljs-attr">On</span>=<span class="hljs-string">"uninstall"</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Component</span>&gt;</span>

    <span class="hljs-tag">&lt;/<span class="hljs-name">Directory</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">UIRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"WixUI\_Minimal"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">UIRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"WixUI\_ErrorProgressText"</span> /&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">Feature</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Complete"</span> <span class="hljs-attr">Title</span>=<span class="hljs-string">"Complete"</span> <span class="hljs-attr">Absent</span>=<span class="hljs-string">"allow"</span> <span class="hljs-attr">Level</span>=<span class="hljs-string">"1"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ComponentRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.Editor.dll\_374746071"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ComponentRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.SampleApp.dll\_1981177689"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ComponentRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.SampleApp.exe\_60174109"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ComponentRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.SampleAppEditor.exe\_729600358"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ComponentRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.Sugar.dll\_2062009301"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ComponentRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"Component.SugarShaker.exe\_1177907971"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ComponentRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"My\_Product.EmptyDirectory"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ComponentRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"INSTALLDIR.My\_Company"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ComponentRef</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"TARGETDIR"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Feature</span>&gt;</span>

  <span class="hljs-tag">&lt;/<span class="hljs-name">Product</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">Wix</span>&gt;</span>
</code></pre>
<p>Using linq, we can make complex rulesets that are more understandable to someone familiar with C# and Linq. The Wix way of doing that does not require the need for individual file entries in the Wix file. but also loses some of the power as we can evaluate the files however we like.</p>
<pre><code class="lang-XML"><span class="hljs-tag">&lt;<span class="hljs-name">Wix</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://schemas.microsoft.com/wix/2006/wi"</span> 
 <span class="hljs-attr">xmlns:fg</span>=<span class="hljs-string">"http://www.firegiant.com/schemas/v3/wxs/fgwep.xsd"</span>&gt;</span> 
 <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span> 
  <span class="hljs-tag">&lt;<span class="hljs-name">ComponentGroup</span> <span class="hljs-attr">Id</span>=<span class="hljs-string">"ContentComponents"</span>&gt;</span> 
   <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> <span class="hljs-attr">Directory</span>=<span class="hljs-string">"ContentFolder"</span>&gt;</span> 
    <span class="hljs-tag">&lt;<span class="hljs-name">fg:HarvestFolder</span> <span class="hljs-attr">Source</span>=<span class="hljs-string">"path\\to\\content\\\*.dll"</span> /&gt;</span> 
   <span class="hljs-tag">&lt;/<span class="hljs-name">Component</span>&gt;</span> 
   <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> <span class="hljs-attr">Directory</span>=<span class="hljs-string">"ContentFolder"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">fg:HarvestFolder</span> <span class="hljs-attr">Source</span>=<span class="hljs-string">"path\\to\\content\\\*.exe"</span> /&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">Component</span>&gt;</span> 
  <span class="hljs-tag">&lt;/<span class="hljs-name">ComponentGroup</span>&gt;</span> 
 <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span> 
<span class="hljs-tag">&lt;/<span class="hljs-name">Wix</span>&gt;</span>
</code></pre>
<p>In Part 2 of this series, we are going to look at installing multiple Projects as separate MSI files, feature sets, and installing the pre-requisite .NET Framework.</p>
<h2 id="points-of-interest">Points of Interest</h2>
<ul>
<li>Exploring more complicated setups with Wix# often requires referencing Wix tutorials and examples, then looking for an equivalent in Wix#.</li>
<li>The Wix# Github repository has a large selection of sample projects that are very useful.</li>
</ul>
<h2 id="dealing-with-errors">Dealing With Errors</h2>
<p>If you get an error on build, like the following:</p>
<p>Severity    Code    Description    Project    File    Line    Suppression State
Error        The command ""Path\to\Project\WixSharp Setup\bin\Debug\WixSharp Setup.exe" 
"/MSBUILD:WixSharp Setup"" exited with code -532459699.</p>
<p>Debug "<em>Path\to\Project\WixSharp Setup\bin\Debug\WixSharp Setup.exe</em>" "/MSBUILD:WixSharp Setup". In my case, it ended up being that it was looking for a directory that did not exist to pull files from for the MSI.</p>
<h2 id="history">History</h2>
<ul>
<li>2/24/2018: Initial publication</li>
<li>8/2/2021: Copied to HashNode from <a target="_blank" href="https://www.codeproject.com/Articles/1231849/Creating-a-Wixsharp-Installer-that-Includes-Prereq">CodeProject Article</a></li>
</ul>
<iframe src="https://github.com/sponsors/digitalcoyote/button" height="35" width="116" style="border:0"></iframe>]]></content:encoded></item></channel></rss>