One way of suppressing 'Defined but not used' warning (-Wunused-matches)

Haskell compiler will complain when it detects unused identifiers, like so:

1
2
3
4
5
file.hs:272:51: warning: [-Wunused-matches]
Defined but not used: ‘myVar’
|
272 | doStuff responses expectedFl ‘myVar’ = do
|

I found an quick and easy way of suppressing such warning by prefixing identifiers with underscore, like _myVar

Of course, ideally we shoul address such warnings, or rely on compilation flags, so use it wisely.

Run MeasureThat.net application on Linux: building and running the application

In this post I will show how to run MeasureThat.net application on Linux (Ubuntu 16.04 in particular)
MeasureThat is the open source application that allows users to write and run JavaScript microbenchmarks.

It is written with .Net Core, the new cross-platform framework from Microsoft.
Actual MeasureThat.net application is hosted in the Microsoft Azure. It uses SQL Server as a database.

But it is possible to run the exact same application on the Linux machine using PostgreSQL as a database.
This post shows how it can be done.

Prerequisites

To be able to follow the steps described here you will need to have machine with Linux installed. I used Virtual Machine with Ubuntu 16.04:

1
2
3
4
5
6
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.1 LTS
Release: 16.04
Codename: xenial

Here is how I created the VM

Also some additional software is needed, like: .net core, git, nodejs, bower, npm, gulp, PostgreSQL.
Follow the directions described in the other post if you need to install any of those: installing prerequisites (node, .net code, PostgreSQL, git, npm, gulp, bower)

Lets get started.

Check out the application’s source code:

Open the terminal window. Create folder source for the application and check out the code:

1
2
3
4
5
6
7
8
9
10
11
$ mkdir source
$ cd source/
$ git clone https://github.com/thecoderok/MeasureThat.net.git
Cloning into 'MeasureThat.net'...
remote: Counting objects: 2324, done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 2324 (delta 2), reused 0 (delta 0), pack-reused 2305
Receiving objects: 100% (2324/2324), 1.16 MiB | 650.00 KiB/s, done.
Resolving deltas: 100% (1478/1478), done.
Checking connectivity... done.
vitalii@vitalii-vm:~/source$

Build the application

Now everything is ready to build the application. Step into the folder with source code (MeasureThat.net/src/BenchmarkLab$) and run restore dotnet, npm and bower packages (it will take couple of minutes):

1
2
3
dotnet restore
npm install
bower install

Build frontend:

1
gulp

Build the application:

1
dotnet build

Build should succeed:

build_the_application

Prepare the configuration file

Open appsettings.json file in the text editor and:

  • Disable External authentication: set UseFacebookAuthentication / UseGoogleAuthentication / UseTwitterAuthentication / UseMicrosoftAuthenticaiton to false
    (Please let me know in the commens if you want to be able to use External authentication, I can explain how it can be done )
  • Set RequireEmailConfirmation to false
  • Disable reCaptcha: set ReCaptchaEnabled to false.
  • Set database type to PostgreSQL.

Result should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
"ApplicationInsights": {
"InstrumentationKey": "6fbb4f00-bf94-4fe8-a0e3-a5b4e1283fc2"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"UseFacebookAuthentication": false,
"UseGoogleAuthentication": false,
"UseTwitterAuthentication": false,
"UseMicrosoftAuthenticaiton": false,
"ResultsConfig": {
"UploadResultsToDb": true,
"UploadGuestUserResultsToDb": true
},
"GoogleAnalytics": {
"Enabled": true,
"Identifier": "UA-83528903-1"
},
"AllowGuestUsersToCreateBenchmarks": true,
"SenderEmail": "MeasureThat@outlook.com",
"SenderName": "MeasureThat Admin",
"DatabaseType": "PostgreSQL",
"RequireEmailConfirmation": false,
"ReCaptchaEnabled": false
}

Prepare the database

Now we need to prepare the database.
Switch to postgres user and run the postgres client:

1
2
$ sudo -i -u postgres
$ psql

If you need to set the passowrd for the postgres user, enter command the password to do so. This may be needed right after PostgreSQL was installed.

1
2
3
postgres=# \password postgres
Enter new password:
Enter it again:

Create the database MeasureThat:

1
2
postgres=# create database MeasureThat;
CREATE DATABASE

You can exit from postgres cleint and switch back to the original user:

1
2
3
4
postgres=# \q
postgres@vitalii-vm:~$ exit
logout
vitalii@vitalii-vm:~/source/MeasureThat.net/src/BenchmarkLab$

Set the connection string (using Secret Manager tool):

1
2
$ dotnet user-secrets set ConnectionStrings:DefaultConnection 'User ID=postgres;Password=root;Host=localhost;Port=5432;Database=MeasureThat;
Pooling=true;'

Create the database schema:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ dotnet ef migrations add testPg
Project BenchmarkLab (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Done. To undo this action, use 'dotnet ef migrations remove'
$ dotnet ef database update
Project BenchmarkLab (.NETCoreApp,Version=v1.0) will be compiled because Input items added from last build
Compiling BenchmarkLab for .NETCoreApp,Version=v1.0
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:04.6049020
Done.

Run the application

Set the ASPNETCORE_ENVIRONMENT variable to Development

1
$ export ASPNETCORE_ENVIRONMENT=Development

And (drum roll) run the application:

1
dotnet run

Run MeasureThat.net application on Linux: installing prerequisites (node, .net code, PostgreSQL, git, npm, gulp, bower)

In this topic I will tell which dependencies are need to be able to run http://MeasureThat.Net application on Ubuntu 16.04.
Although there are quite a few dependencies, none of them are unique and should be known to the developers.
So, here is what is needed:

  • .Net core
  • Visual Studio Code (optional)
  • PostgreSQL (it will be used as Database)
  • Git (to checkout the code)
  • Node.js
  • gulp
  • bower
  • npm

.net core

Information about how to install .net core on Linux can be found on Microsoft’s website here: https://www.microsoft.com/net/core#ubuntu
I will also provide it here (just in case):

Configure apt-get feed:

1
2
3
sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
sudo apt-get update

Install .NET Core SDK

1
sudo apt-get install dotnet-dev-1.0.0-preview2-003131

Visual Studio Code (optional)

Information about how to isntall Visual Studio Code on Linux can be found in my previous post: http://the-coderok.azurewebsites.net/2016/09/30/How-to-install-Visual-Studio-Code-on-Ubuntu-using-Debian-package-manager/
Or on their website: https://code.visualstudio.com/#alt-downloads

PostgreSQL

Open the terminal Window and type following commands:

1
2
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

git

1
2
3
4
5
6
7
8
9
sudo apt install git
```
# Install nodejs & npm
Due to the issues with running `bower` using regular `nodejs` package, we will need to install `nodejs-legacy` package.
```bash
sudo apt-get install nodejs-legacy
sudo apt-get install npm

Some additional Information: http://askubuntu.com/questions/507855/bower-install-does-nothing-not-even-throw-an-error

Install bower & gulp

1
2
sudo npm install bower -g
sudo npm install gulp -g

We just installed all dependencies and now we can proceed (check out Run MeasureThat.net application on Linux: building and running the application).

Run MeasureThat.net application on Linux: Create Virtual Machine with Ubuntu 16.04 using Virtual Box

This is the first post related to the topic of running http://measurethat.net application on the linux machine.
In this post I will show how to create a virtual machine with Linux (Ubuntu 16.04) to be able to follow the next steps (actuall building and running the application).
If you already have machine with Linux you probably don’t need to follow this steps.
So here it goes, there are the steps I followed to create VM:

1 create_new_virtual_machine

  • Give the name to the virtual machine and make sure that OS type and version are properly populated:

2 new_os_linux

  • Set the RAM memory size:

3 memory_size

  • Select “Create a virtual disk now”:

4 create_disk_now

  • Type: VDI

5 vdi

  • Select Dynamically allocated:

6 select_dynamically_allocated

  • Enter maximum disk size. 20 GB should be enough.

7 virtual_disk_file

  • Once the machine created, press Settings button, go to Storage section and select the iso file that was previously downloaded:

    8 select_iso_file

  • Start the machine and follow the promps of the installation wizard.
  • Restart machine once the installation completes:

9 installation complete

  • Your machine is ready to be used:

10 machine_is_ready

  • Install Guest Additions
    I also recommend to isntall Guest Additions to be able to share clipboard with host OS:
    (shared clipboard can be enabled by selecting Devices->Shared clipboard->Bidirectional)

11_install_guest_additions

  • Restart the Virtual Machine

How to install Visual Studio Code on Ubuntu using Debian package manager

Visual Studio Code can be installed from terminal using Debian package manager.

1. Download latest version

Latest version can be downloaded from this URL: https://code.visualstudio.com/docs/?dv=linux64_deb

Or, if you know direct download URL, you can use wget to obtain it:

1
2
3
4
5
6
7
8
9
10
$ wget https://az764295.vo.msecnd.net/stable/5be4091987a98e3870d89d630eb87be6d9bafd27/code_1.5.3-1474533365_amd64.deb
--2016-09-30 09:37:18-- https://az764295.vo.msecnd.net/stable/5be4091987a98e3870d89d630eb87be6d9bafd27/code_1.5.3-1474533365_amd64.deb
Resolving az764295.vo.msecnd.net (az764295.vo.msecnd.net)... 72.21.81.200, 2606:2800:11f:17a5:191a:18d5:537:22f9
Connecting to az764295.vo.msecnd.net (az764295.vo.msecnd.net)|72.21.81.200|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 32914062 (31M) [application/x-debian-package]
Saving to: ‘code_1.5.3-1474533365_amd64.deb’
code_1.5.3-14745333 100%[===================>] 31.39M 59.9MB/s in 0.5s

1

2. Install it

Run the command (don’t forget to change the name of the file according to the downloaded version):

1
$ sudo dpkg -i code_1.5.3-1474533365_amd64.deb

3. Run

Type code command:

screenshot

How to print a stack trace on breakpoint in Chrome

Sometimes debugging can be tricky, even in JavaScript world, especially if it is some kind of timing issue and you can’t modify the source code. There is no silver bullet for such cases. But there is one useful technique that I use from time to time. Occasionally it is useful to print stack trace in some particular line of code. It is very easy to use. The trick is to run this line of code on breakpoint:

1
console.log((new Error()).stack)

Or even better (Chrome supports it):

1
console.trace()

Lets say I want to debug something on the https://www.measurethat.net/ website. Go to https://www.measurethat.net/Benchmarks/Show/451/1/array-loops and open developer tools. Switch to sources tab, open benchmarklab.js file, find the function onCycleHandler and put a breakpoint there:

Breakpoint

Right click on the breakpoint and select Edit breakpoint and type the expression as condition there:

Expression to print call stack

Voila! Now when you run the test (click Run tests button you will see call stack in console):
Results

Result of the console.trace()

Of cource, this can be used to do other stuff as well, for example printing functio arguments, etc:

1
console.log(JSON.stringify(arguments))

ASCII transliterations of Unicode text for .net core

Whenever I needed SEO friendly urls in asp.net apps, I have been using UnidecodeSharpFork library to do just that. It is a good tool that transliterates Unicode to ASCII.
Unfortunately, published binaries on NuGet are not compatible with .net core and author of the tool was no longer working with .net.
Since the library is good and useful I decided to revitalize it: I republished it to GitHub: https://github.com/thecoderok/Unidecode.NET

In addition I created NuGet module and published it as well: https://www.nuget.org/packages/Unidecode.NET/

Feel free to use it and contribure.
Report issues on GitHub.

Scaffold-DbContext fails with error: The expression after '&' in a pipeline element produced a object that was not valid.

I’m countinuing my bumpy ride with .net core (which I like a lot so far btw)
Recently I tried to run Scaffold-DbContext command which I used multiple times but I’ve got the error this time:

1
2
The expression after '&' in a pipeline element produced a object that was not valid.
It must result in a command name, script block or CommandInfo object.

That was a bug in entity framework when parameters were not escaped which was fixed

But there is also a workaround: use dotnet toot to scaffold:

1
dotnet ef dbcontext scaffold [connectionstring] [providername]

More info on dotnet ef dbcontext scaffold: dotnet-ef-dbcontext-scaffold

How to see environment variables that Visual Studio process sees

Sometimes there is a need to find out what are the environment variables that are visible to Visual Studio process. While there are ways to see them via Macro window or Build configuration windo, there is a universal way of doing that.
The good old friend, Process Explorer tool from Mark Russinovich & Co. Among many other things it can show list of environment variables for any process.

Just start the procexp.exe application, point to the process you are interested, double click and open Environment tab:

envirnoment_tab

As simple as that.