Friday, September 30, 2022

Visual Studio: Disable Map Mode

In the blog post, Visual Studio Code: Disabling the Minimap, it was demonstrated how to disable the minimap feature in Visual; Studio Code. Minimap consumes screen real estate that could be used for viewing code. Microsoft chose to add the minimap feature to Visual Studio (starting with Visual Studio 2019) and called it Map Mode. This post demonstrates how to disable Map Mode in Visual Studio.

Below is an example of the traditional, Visual Studio, vertical scrollbar being replaced with a Map Mode vertical scrollbar:



To disable Map Mode, open Visual Studio and navigate to: 
    Tools | Options | Text Editor | All Languages | Scroll Bars

Navigating as described above will display the following:


Note above that the radio button, "Use map mode for vertical scroll bar" is selected, and "Use bar mode for vertical scroll bar" is not selected. To disable Map Mode, simply select "Use bar mode for vertical scroll bar" (see below) which deselects "Use map mode for vertical scroll bar":


Click on Ok in the previous dialog to finalize the disabling of Map Mode in Visual Studio.

Once Map Mode is disabled, the standard coding windows in Visual Studio appear as follows with a traditional scroll bar:


Microsoft's Justification for Map Mode

Microsoft's Visual Studio documentation, How to: Customize the scroll bar, includes a justification for the use of Map Mode. The following snippet is part of the aforementioned documentation and demonstrates a use of Map Mode namely how to identify find and replace locations throughout an entire source file:


Eight percent of males are color blind (1 in 12) and Microsoft has added a useful feature, Map Mode, the relies on the colors red and green. Map Mode reduces the amount of code visible in Visual Studio and is biased against a large number of developers with a rather well-documented disability.

The other blog posts that are addressed being color blind include:
I would much rather spend time developing code than working around "features" that demonstrate a bias against those who are color blind.


Sunday, September 11, 2022

C#: xUnit Testing Constructor Exceptions

xUnit's Assert class implements the following methods that test whether or not a method throws an exception:


 

The Throws method signatures that take a parameter of type Action are used to test methods that return no value (void). The Throws method signatures that take a parameter of type Func are used to test methods that return a value. In order to test a constructor that throws an exception, a lambda expression is required such as:

Assert.Throws<ArgumentException>(
    () => new AnyClass());

Assert.Throws<ArgumentException>(
    () => new AnyClass("some paramter", 123, DateTime.Now));

The lambda expressions above return no values, so the Throws method that takes an Action parameter is invoked by the above code (public static T Throws<T>(Action testCode) where T : Exception).






 




Friday, September 9, 2022

Git: Get Submodules from an already Cloned Repo

A git repo that contains a submodule or modules is cloned using the --recurse-submodules command-line option such as the following:

git clone --recurse-submodules -j8 git://github.com/foo/bar.git

At one time or another, every developer has performed a git clone and forgotten the --recurse-submodules command-line option. The following git command will remedy this problem but updating the submodules for a git repo ever in the case where the git clone was performed without specifying --recurse-submodules:

git submodule update --init --recursive


Sunday, September 4, 2022

cmake: ExternalProject_Add Ignore the INSTALL_COMMAND

The cmake function ExternalProject_Add  is documented at ExternalProject. There are circumstances when ExternalProject_Add's install command needs to be disabled.  The simplest way to disable the install command is to set the parameter to an empty string as follows: 

ExternalProject_Add(example_package
  SOURCE_DIR ${example_package_CMAKE}
  BUILD_COMMAND "cmake"
  INSTALL_COMMAND ""
)

A better solution is to write a message to the build output indicating that the install command has been disabled:

ExternalProject_Add(example_package
  SOURCE_DIR ${example_package_CMAKE}
  BUILD_COMMAND "cmake"
  INSTALL_COMMAND cmake -E echo "example_package: temporarily skipping install step."
)

Docker/Ubuntu: Installing the latest cmake on Docker Image

This post demonstrates how to install the latest version of cmake during docker build targetting an Ubuntu 20.04 image. The Dockerfile for such an image starts with:

FROM ubuntu:20.04

A previous post, Ubuntu: Upgrade to the latest cmake, demonstrated the bash commands that could be used to install the latest version of cmake on an Ubuntu 20.04.4 LTS image. Fundamentally these are the same commands that could be used to install cmake on a Docker image during build. From the previously demonstrated script, the sudo commands were removed a few additional apt-get commands are required.

A sample Dockerfile that installs the latest cmake (currently 3.24.1) on an Ubuntu 20.04 image is follows:

FROM ubuntu:20.04


# Put your own Dockfile commands here

RUN apt-get update \
  && apt-get -y install build-essential \
  && apt-get install -y wget \
  && rm -rf /var/lib/apt/lists/* \
  && wget https://github.com/Kitware/CMake/releases/download/v3.24.1/cmake-3.24.1-Linux-x86_64.sh \
      -q -O /tmp/cmake-install.sh \
      && chmod u+x /tmp/cmake-install.sh \
      && mkdir /opt/cmake-3.24.1 \
      && /tmp/cmake-install.sh --skip-license --prefix=/opt/cmake-3.24.1 \
      && rm /tmp/cmake-install.sh \
      && ln -s /opt/cmake-3.24.1/bin/* /usr/local/bin


# Put your own Dockfile commands here

Ubuntu/Azure: Upgrade an Ubuntu 20.04.4 LTS VM to the latest cmake

This post demonstrates how to install the latest version of cmake on an Azure Ubuntu 20.04.4 LTS virtual machine (VM).

In the post, Ubuntu: Upgrade to the latest cmake, the steps required to install the latest version of cmake on Ubuntu 20.04.4 LTS were presented. These aforementioned steps apply to environments like running Ubuntu under WSL where the native Ubuntu image was used to create the virtual machine or image. Multiple bash commands were required to install cmake and similarly, the uninstall required multiple bash commands.

Azures Ubuntu 20.04.4 LTS VMs contain additional packages to that of a stock Ubuntu image. These packages include Snap, a popular package manager. Using Snap the latest version of cmake can be installed as follows:

sudo snap install cmake --classic

The output from the above install steps is shown below where the version of cmake (3.24.1) is displayed during installation:


Be warned:

Using apt-get to install cmake will get Ubuntu's version of cmake which is not the latest version of cmake. This means that features like DOWNLOAD_NO_EXTRACT will not be available to cmake.

The steps to uninstall cmake using the snap command are as follows:

sudo snap remove cmake

Appendix A: Snap

For those new to snap (see Snap (software)):







Friday, September 2, 2022

Ubuntu: Upgrade to the latest cmake

Ubuntu 20.04.4 LTS allows cmake to be installed as follows using -y to suppress the prompt:

sudo apt-get update && sudo apt-get install -y cmake

The cmake installed is not the latest version as can be seen using cmake --version:


To install the latest version of cmake, first uninstall cmake (if it is already installed and that it was installed using apt-get):

sudo apt-get remove -y cmake


Latest cmake Install

The cmake releases can be found at:

https://github.com/Kitware/CMake/releases

The following script downloads and installs a specific version of cmake (version 3.24.1):

wget https://github.com/Kitware/CMake/releases/download/v3.24.1/cmake-3.24.1-Linux-x86_64.sh \
  -q -O /tmp/cmake-install.sh \
&& chmod u+x /tmp/cmake-install.sh \
&& sudo mkdir /opt/cmake-3.24.1 \
&& sudo /tmp/cmake-install.sh --skip-license --prefix=/opt/cmake-3.24.1 \
&& rm /tmp/cmake-install.sh \
&& sudo ln -s /opt/cmake-3.24.1/bin/* /usr/local/bin

To verify the correct version of cmake is installed invoke cmake --version:


cmake Uninstall

Obviously "sudo apt-get remove -y cmake" cannot be used to uninstall cmake installed via cmake-install.sh.

The following steps uninstall cmake, be versing the steps used to install cmake above which used cmake-install.sh to install cmake:

pushd . \
&& cd /usr/local/bin \
&& ls /opt/cmake-3.24.1/bin | \
  sudo xargs rm \
&& sudo rm -rf /opt/cmake-3.24.1 \
&& popd


Saturday, July 2, 2022

ASP.NET Core: Improving the Online Documentation (Open Source Contribution)


While working on Window Services for .NET 6 I found multiple issues in :ASP.NET Core fundamentals overview:



The changes committed in my name are as follows (three commits to main):



Actually, I tossed two edits to Marty (mcasey329) and Jayson so there were five commits. I wanted us all to show up as contributors:



XmlDocument/C#: Improving the Online Documentation (Open Source Contribution)

The documentation for .NET's Xml Document class can be found at XmlDocument Class:


This documentation dates back to 2002 and was in need of some changes to make it more contemporary including an update from .NET 1.0 circa 2002 code to .NET 1.1 circa 2003:


And cleaning up the sample code:


One more change merged:


Visual Studio/Debug Local Docker Containers: Improving Online Documentation (Open Source Contribution)

Microsoft's documentation Debug apps in a local Docker container demonstrates how to debug applications in Docker containers:


Three changes I made to this page were committed by Microsoft:



Thursday, May 12, 2022

Xml Schemas/C#: Improving Online Documentation (Open Source Contribution)

While reading a code snippet in Microsoft's documentation, Reading and Writing XML Schemas I noticed the following example code:


The variable of type XmlSchema is named myschema. This is a C# example and prefixing variable names with the term "my" is a coding style associated with VB.NET. 

I edited the page which brought me to Github. Github makes a fork and allowed me to suggest the change by creating a pull request. Here is updated documentation reflecting my accepted change:


Here is the notification that the pull request was merged into the main branch:





Saturday, April 9, 2022

Ubuntu/Linux/Hyper-V: Creating an Ubuntu 20.04 VM can be accessed from Hyper-V's Host Computer

I was assigned a project where a shell script had to be run remotely on an Ubunto 20.04 Virtual Machine (VM). The remote script failed on an Azure VM so I fell back on Hyper-V running on Windows 11. The way I traditionally configured an Ubuntu 20.04 VM did not permit SSH access from the host computer. These are the steps required to set up an Ubuntu 20.04 that can be configured to accept SSH connections from the host computer.

Create VM (Basic Ubuntu Install)

Create VM pre-installing Ubuntu from an Image

1) Download the Ubuntu 20.04 LTS from Download Ubuntu Desktop on the host computer. The format of the image download will be ISO.

2) Launch Hyper-V Manager on the host computer and make sure that the host computer name is selected in the left panel:

3) Select Action | New| Virtual Machine:


4)  Navigate click Next in the New Virtual Machine Wizard under the Specify Name and Location panel is displayed:

5) Enter the name of the virtual machine to be created (the name above is devops02-ubuntu20.04) and click on Next:


By default, Generation 2 VMs cannot use an ISO operating system image for installation. This will be addressed at a later step in the installation process.

6) From the Specify Generation panel select the Generation 2 radio button and click Next:

7) From the Configure Networking panel select Bridge (which allows external access to say the internet and provides access to the internal network, a.k.a. the LAN) which will enable access to the VM from the host computer (the VM will be assigned a local IP address) and click Next:

8) Configure the hard drives and click Next:

9) Specify the location of the Ubuntu install media and click on next:

10) Click Finish on the final setup screen:

Configure Ubuntu to Install from ISO

1) From Hyper-V Manager highlight the VM being setup and from the Actions panel on the right click on Settings:

2) From Settings dialog select Hardware | Security from the pane on the left:

3) Click on OK.

Installing Ubuntu on the VM

1) From Hyper-V Manager, double click on the VM to be set up in order to establish a connection to the VM (opens the connection dialog):

2) From the per-VM connection dialog click on Start:

3) Clicking on Start displays the standard Ubuntu install wizard.


4) Click on Continue and perform the steps required to complete the installation of Ubuntu.

Verify Static IP Address

The previous steps ensured that a VM was assigned a static IP address. In order to verify that this static IP address was assigned, run ifconfig from a terminal on the VM. The installation of ifconfig is presented in the post, Ubuntu/Linux: Retrieving a Computer's IP Address using ifconfig). 

Below is a screenshot showing the output from ifconfig when run from the VM:



The output from running ifconfig (above) shows a static IP address 192.168.1.214 has been assigned to the VM.

Saturday, April 2, 2022

Ubuntu/Linux: Configuring a host to support ssh Connections (Installing OpenSSH Server)

By default, an Ubuntu 20.04 instance does not contain an SSH server and hence there is no ssh access to the machine. To remedy this, install OpenSSH Server by:

1) Install the OpenSSH Server package:

sudo apt-get install -y openssh-server

2) Enable the service using systemctl enable:

sudo systemctl enable ssh

3) Start the service using the systemctrl start command:

sudo systemctl start ssh

4) On a different host, verify that the port for ssh (port 22) is open using the technique described in Ubuntu/Linux: How to tell if a Port is Open on a Remote Host:

echo > /dev/tcp/10.0.0.37/22 && echo "Port 22 (SSH) is open!"

When the above command is invoked and OpenSSH has been installed successfully the following is displayed:


Remember the above command is run on the client and not the host (remote computer) where OpenSSH server will run.

Ubuntu/Linux: How to tell if a Port is Open on a Remote Host

While trying to get ssh working to a remote host, I encountered the "Connection refused" error message on Ubuntu.  The first step in debugging is to make sure the port is open on the remote host which can be done as follows:

echo > /dev/tcp/<remote host>/<port> && echo "The port is open"

Checking to see if ssh (port 20) is open on a host (10.0.0.37) is simply:



To verify script snippet works I did tested it against HTTPS (port 443) on a well know host (google.com):




Ubuntu/Linux: Retrieving a Computer's IP Address using ifconfig

Consider the case where a developer wants to ssh from a host computer to a Ubuntu client running in Hyper-V. The most fundamental way to access the client machine via ssh is using its IP address. Linux's ifconfig utility is short for interface config.

The ifconfig utility is part of the net-tools package. To install this package on Ubutu use apt-get as follows:

sudo apt-get update -y
sudo apt-get install -y net-tools

An example of ifconfig being invoked is as follows where the computer's IP is shown to be 10.0.37:





Ubuntu/Linux: Disable sudo Password Prompt

When running scripts as sudo, it is convenient not to be prompted for a user's password. Password prompts defeat automation so this post will demonstrate how to disable the password prompt associated with the sudo command.

The command sudo date is often used to determine if invoking sudo will prompt the user to enter their password (as follows):


Above, it can clearly be seen how the sudo before the date command causes a password prompt for the password of user devops. 

The sudo command allows commands to be elevated and run as root. In order to use the sudo command, a user must be part of the sudo group. The following post demonstrates how to add a user to the sudo group: Ubuntu: Adding a user to the sudo group.

To disable the sudo caused password prompt invoke the command

sudo visudo

Running the above command does require a user to enter their password in response to sudo (hopefully for the last time):


The visudo command is an editor for the /etc/sudoers file. visudo validates the sudoers file's format on save. The man page for the sodoers file can be found at Sudoers Manual.

visudo brings the sudoers file up in an editor:


To suppress the password for a specif member of the sudo group, add the following text under the line %sudo  ALL=(ALL:ALL) ALL:

<user name here> ALL=(ALL) NOPASSWD: ALL

For the case of a user named, devops, the following line would be added in the editor:

devops ALL=(ALL) NOPASSWD: ALL

The reason for putting the new line after the line containing %sudo  ALL=(ALL:ALL) ALL is explained in the sodoer man page (referenced above):



The sudoer file being edited by visudo will look as follows:


Use ctrl-s to save the file and cttrl-x to exit the editor.

Once the sudoer file has been edited correctly and saved, a user should no longer be prompted to enter a password when invoking the sudo command.



Ubuntu/Linux: Adding a user to the sudo group

Members of the sudo group can execute any command as root user using the sudo command:

sudo date

The command above invokes the date command with root privileges.

In order to add a user to the sudo group, invoke the following from a terminal:

sudo usermod -aG sudo <username>

The leading sudo runs the usermod command as root. Depending on how a user is configured, the sudo command may prompt the user to enter a password.

To add a user named, devops, to the sudo group invoke the following command:

sudo usermod -aG sudo devops



Thursday, March 31, 2022

Google Sheets: Pasting Delimited Data

The National Emergency Medicine Services Information System (NEMSIS) is a data standard for patient care information corresponding to a 911 call or other emergency care situations(such as being treated for a medical condition at a NASCAR event). The schemas associated with NEMSIS are delineated as federal (national) and state. The elements of this schema are defined at Required National and Recommended State Elements (V3.5.0) where the definitions are a value separated list where the separator is the vertical bar (|) character.

An example of this data is as follows:


Rather than save the data to a file and then import, the data can simply be cut and pasted into a clean Google Sheet:


Notice the past icon (the clipboard) with a down triangle indicating a menu. Clicking on the down triangle displays a menu from which Split text to columns should be selected:



Clicking on Split text to columns displays the following:


Clicking on Detect automatically displays the following:


In the Custom separator textbox enter a vertical bar:


Click on enter and the delimited data will populate the columns using the vertical bar character as a separator:


As an alternative, the Data menu contains Split text into columns which could have been used to separate the data using the vertical bar.

The delimited data values each contain a leading and trailing single quote. To remove these click the top left of the sheet (to the left of A and above 1) to select all data elements:


From the Edit menu select Find and replace:


Using Find and replace the single quotes can be removed: