Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

Tuesday, 28 April 2020

Prepend to every line in Visual Studio Code

Problem:

I want to add a string to the beginning of every line in a selection of code or text.

For example, I am reformatting what used to be a liquid templating list to a YAML list, e.g.

From:

item1
item2
item3

To:

  - item1
  - item2
  - item3


One solution - overview:

There are many ways to prepend to code quickly in bulk. One of the more versatile ways is by using the replace tool in Visual Studio Code combined with regex (i.e. regular expressions).

The "find" field will have the caret character ^ which indicates the beginning of a line. The "replace" field will have whatever you want to add at the beginning of lines.

Example:

For example, if I have the code in the screenshot below that I want to prepend the string " - " to:

First, select the code to prepend to:

Then invoke the "replace" tool:

Make sure you select the option to replace in your selection only. Then use a regex search for "^" as the beginning of a line, and replace with what you want to prepend. Do this for each line one by one until you have the results you desire.

After you are done, the lines should now be prepended with the desired string.



Notes:

There are many other ways that you can prepend lines in Visual Studio Code. The how-to above shows the regex replace method because it's a powerful tool that can handle many scenarios, not just prepending to lines.

Other methods may include installing plugins, using Visual Studio's multi-line cursor/select tools, etc.

References:

Friday, 20 January 2017

Restore my Xamarin project's NuGet and Xamarin-Component dependencies using CLI

Problem:

How do I restore all of my Xamarin project dependencies when neither Visual Studio's "extensions and updates", nor NuGet package manager GUI have the versions my project requires?

Similarly, how do I restore these packages in the command line so Jenkins can automatically build my Xamarin project?

My questions apply to fresh clones of my team's Xamarin project from Git, where the NuGet nor Xamarin component 3rd party dependencies aren't included in the project's repository.

Before you start:

The solution below assumes that you have knowledge of Xamarin, Xamarin components, NuGet, (Jenkins), the command line, and have a valid account for Xamarin. (It was written as a quick reminder. Please refer to the referenced links if more detail is needed.)

One solution:

One solution is to use the nuget.exe and xamarin-component.exe command line tools to restore NuGet and Xamarin component dependencies.

You can get the Xamarin component CLI executable from here: https://components.xamarin.com/submit/ (you'll need to sign in with your Xamarin account, and follow the instructions Microsoft provided)

You can get the NuGet CLI executable from here: https://dist.nuget.org/index.html

When you have these CLI tools installed (or copied to your project's root - not ideal, but helps to troubleshoot), you can then use them or include them in your script as follows:

  • navigate to the directory where your solution file is (e.g. YourSolutionName.sln)
  • 
    nuget.exe restore
    xamarin-component.exe restore YourSolutionName.sln
    
    
  • (first time) enter your Xamarin account credentials (needed for xamarin-component)

At this point, the output should give an indication of all 3rd party dependencies that were successfully restored.

Notes:

This solution was tested to work on a project that has dependencies about a year old (and are no longer available via the Visual Studio GUI). The versions of the tools tested were Visual Studio 2015 (14.0.25431.01 Update 3), Xamarin 4.2.2.11 (with corresponding Xamarin.Android 7.0.2.42 and Xamarin.iOS 10.3.1.8). Additionally, all 3rd party libraries were originally installed using Visual Studio's "Extensions and Updates..." GUI and NuGet package manager's GUI. Your mileage may vary, depending on versions and how your team has setup your Xamarin project.

This solution also depends on Microsoft/Xamarin still providing the CLI tools, as well as the 3rd party dependency versions specified in your project still existing for download.

Nonetheless, hopefully this has at least come in handy or given some new ideas :)

References: