### Install LibMan from alternative source Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Installs LibMan from an alternative location using a specific version and source path. The example installs from C:\Temp\Microsoft.Web.LibraryManager.CLI.1.0.94-g606058a278.nupkg. ```shell > dotnet tool install -g Microsoft.Web.LibraryManager.CLI --version 1.0.94-g606058a278 --add-source C:\Temp\ ``` -------------------------------- ### Install library from filesystem provider with multiple files Source: https://github.com/aspnet/librarymanager/wiki/LibMan-CLI-Reference Installs calendar.js and calendar.css from a filesystem provider using the --files option. ```text libman install myCalendar --provider filesystem --files calendar.js --files calendar.css ``` -------------------------------- ### fileMapping with root and destination Source: https://github.com/aspnet/librarymanager/wiki/libman.json-reference Example fileMapping that specifies root as 'Folder/' and destination as 'lib/Sample'. The files pattern **/*.js is relative to the root, so the installed files are lib/Sample/folderFile.js and lib/Sample/SubFolder/subFolder.js. ```json { "root": "Folder/", "destination": "lib/Sample", "files": [ **/*.js ] } ``` -------------------------------- ### libman.json after install Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Example libman.json file created or updated after running 'libman install jquery'. Shows the structure with version, defaultProvider, and libraries array. ```json { "version": "1.0", "defaultProvider": "cdnjs", "libraries": [ { "library": "jquery@3.3.1", "destination": "lib\\jquery" } ] } ``` -------------------------------- ### libman.json fileMappings example with jsdelivr provider Source: https://github.com/aspnet/librarymanager/wiki/libman.json-reference Advanced libman.json example using fileMappings to install dist/js assets to lib/js and dist/css assets to lib/css, omitting the dist/ folder in both destinations. Requires version 3.0 or higher. ```json { "version": "3.0", "libraries": [ { "library": "bootstrap@5.3.5", "provider": "jsdelivr", "fileMappings": [ { "root": "dist/", "destination": "lib/js", "files": [ "js/**" ] }, { "root": "dist/", "destination": "lib/css", "files": [ "css/**" ] } ] } ] } ``` -------------------------------- ### Install specific library version Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Installs a specific version of the library from the default provider. Syntax is [LibraryName]@[LibraryVersion] for all providers except File System. ```shell > libman install jquery@2.2.0 ``` -------------------------------- ### Install library by ID with libman install Source: https://github.com/aspnet/librarymanager/wiki/LibMan-CLI-Reference Installs jquery@3.2.1 using the default provider and destination, adding the library definition to libman.json and downloading files. ```text libman install jquery@3.2.1 ``` -------------------------------- ### Install library to custom destination Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Installs the library to a specified destination folder using the --destination flag. ```shell > libman install twitter-bootstrap --destination wwwroot/lib/bootstrap ``` -------------------------------- ### Example repo structure with solution and project files Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Illustrates the expected layout of a solution and project folders in the repo root, with src and test folders matching solution folders. ```text /Fruit.sln /src/Microsoft.AspNet.Banana/Banana.csproj /src/Microsoft.AspNet.Banana/Banana.cs /src/Microsoft.AspNet.Banana/Util/BananaUtil.cs /src/Microsoft.AspNet.Lychee/Lychee.csproj /src/Microsoft.AspNet.Lychee/Lychee.cs /src/Microsoft.AspNet.Lychee/Util/LycheeUtil.cs /test/Microsoft.AspNet.Banana.Tests/BananaTest.csproj /test/Microsoft.AspNet.Banana.Tests/BananaTest.cs /test/Microsoft.AspNet.Banana.Tests/Util/BananaUtilTest.cs ``` -------------------------------- ### Install specific files from provider with destination Source: https://github.com/aspnet/librarymanager/wiki/LibMan-CLI-Reference Installs jquery.min.js from the cdnjs provider into wwwroot\scripts\jquery, using explicit provider and destination options. Without these, the command requires defaults. ```text libman install jquery --provider cdnjs --destination wwwroot\scripts\jquery --files jquery.min.js ``` -------------------------------- ### libman.json example with cdnjs and filesystem providers Source: https://github.com/aspnet/librarymanager/wiki/libman.json-reference Example libman.json manifest with two libraries: one from the cdnjs provider and one from the file-system provider. Shows defaultProvider, defaultDestination, and per-library provider/destination overrides. ```json { "version": "1.0", "defaultProvider": "cdnjs", "defaultDestination": "js/lib", "libraries": [ { "library": "jquery@3.2.1", "files": [ "jquery.min.js" ] }, { "provider": "filesystem", "library": "../../path/to/file.js", "destination": "js/files", "files": ["file.js"] } ] } ``` -------------------------------- ### Install LibMan global tool Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Installs the LibMan global tool via the dotnet CLI. Required before using any LibMan commands. ```shell > dotnet tool install -g Microsoft.Web.LibraryManager.CLI ``` -------------------------------- ### Install latest library version Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Installs the latest version of the library 'jquery' from the default provider. Prompts for default provider if no defaultProvider is set in libman.json, and prompts for destination if no defaultDestination is set. ```shell > libman install jquery ``` -------------------------------- ### GitHub Actions: Install LibMan CLI and restore libraries Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-in-a-CI-Build GitHub Actions workflow that installs the LibMan CLI tool globally and then runs 'libman restore' in the specified working directory. Use this as a step before building your .NET project. ```yaml # Install LibMan CLI globally - name: Install LibMan CLI run: dotnet tool install -g Microsoft.Web.LibraryManager.Cli # Restore libraries using LibMan - name: Run LibMan Restore run: libman restore working-directory: '' ``` -------------------------------- ### Correct unit test method naming examples Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Examples of correct unit test method names that describe what is being tested, under what conditions, and what the expectations are. Pascal casing and underscores are allowed. ```text PublicApiArgumentsShouldHaveNotNullAnnotation Public_api_arguments_should_have_not_null_annotation ``` -------------------------------- ### Incorrect var usage examples Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Shows incorrect usage of the var keyword, with comments explaining why each is wrong. ```csharp var fruit = "Lychee"; // don't use var for built-in types List fruits = new List(); // when the type is apparent, var can be used FruitFlavor flavor = fruit.GetFlavor(); ``` -------------------------------- ### Correct var usage examples Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Shows correct usage of the var keyword when the return type is apparent. Includes a comment noting that var cannot be used with const. ```csharp string fruit = "Lychee"; var fruits = new List(); bool tasty = fruit.IsTasty(); string fruit = null; // can't use "var" because the type isn't known (though you could do (string)null, don't!) const string expectedName = "name"; // can't use "var" with const ``` -------------------------------- ### Print LibMan version with --version flag Source: https://github.com/aspnet/librarymanager/wiki/LibMan-CLI-Reference Use the --version flag to print the current LibMan executable version. This is a simple non-interactive command that quickly verifies the installation. ```bash libman --version ``` -------------------------------- ### Correct public API naming examples Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Correct public API names that use complete words or common/standard abbreviations. ```csharp public void AddReference(AssemblyReference reference); public EcmaScriptObject SomeObject { get; } ``` -------------------------------- ### Azure Pipelines: Install LibMan CLI and restore libraries Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-in-a-CI-Build Azure Pipelines YAML pipeline that installs the LibMan CLI tool globally and then runs 'libman restore' in the directory containing libman.json. Use this as a build step before building your .NET project. ```yaml # Install the CLI tool - task: DotNetCoreCLI@2 displayName: 'Install LibMan CLI' inputs: command: custom custom: tool arguments: 'install -g Microsoft.Web.LibraryManager.Cli' # Restore - task: CmdLine@2 displayName: 'Run LibMan Restore' inputs: script: 'libman restore' workingDirectory: '' ``` -------------------------------- ### Restore libraries from libman.json Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Downloads and installs all files defined in libman.json. Fails if there is a conflict with libraries (e.g., more than one library with the same name). ```shell > libman restore ``` -------------------------------- ### Incorrect unit test method naming examples Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Examples of incorrect unit test method names that are not descriptive enough about the test's purpose, conditions, or expectations. ```text Test1 Constructor FormatString GetData ``` -------------------------------- ### Incorrect public API naming examples Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Incorrect public API names that use unclear abbreviations or reserved keywords. ```csharp public void AddRef(AssemblyReference ref); public EcmaScriptObject SomeObj { get; } ``` -------------------------------- ### MATPrerequisite Target added to csproj Source: https://github.com/aspnet/librarymanager/wiki/Using-MAT-for-RESX-to-XLIFF-conversion This Target is added to the csproj file to warn if the Multilingual App Toolkit is not installed during build. It checks for the presence of the toolkit's targets file and emits a warning if missing. ```xml ``` -------------------------------- ### Mark API as obsolete with [Obsolete] attribute Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Example of marking an API as obsolete with the [Obsolete] attribute and XML doc comments. Use this pattern when introducing a breaking change in a major version, providing the recommended alternative in both the attribute message and the documentation. ```csharp /// /// /// This method/property/type is obsolete and will be removed in a future version. /// The recommended alternative is Microsoft.SomethingCore.SomeType.SomeNewMethod. /// /// /// ... old docs... /// /// [Obsolete("This method/property/type is obsolete and will be removed in a future version. The recommended alternative is Microsoft.SomethingCore.SomeType.SomeNewMethod.")] public void SomeOldMethod(...) { ... } ``` -------------------------------- ### Extension method sponsor type naming Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Illustrates the naming pattern for extension method sponsor types: `Extensions`, `Extensions`, or `Extensions`. The namespace should generally represent the functionality of the extension method, not the target type. When writing extension methods for an interface, the sponsor type name must not start with an `I`. ```csharp namespace Food { class Fruit { ... } } namespace Fruit.Eating { class FruitExtensions { public static void Eat(this Fruit fruit); } OR class FruitEatingExtensions { public static void Eat(this Fruit fruit); } OR class EatingFruitExtensions { public static void Eat(this Fruit fruit); } } ``` -------------------------------- ### Initialize LibMan with full configuration flags Source: https://github.com/aspnet/librarymanager/wiki/LibMan-CLI-Reference Creates libman.json with version 1.0, default provider cdnjs, and default destination script\libman in one command. ```text libman init --version 1.0 --default-provider cdnjs --default-destination script\libman ``` -------------------------------- ### Initialize LibMan interactively with libman init Source: https://github.com/aspnet/librarymanager/wiki/LibMan-CLI-Reference Run libman init without options to create libman.json interactively. The command will prompt for a default destination if not provided. ```text libman init ``` -------------------------------- ### Initialize LibMan with version option Source: https://github.com/aspnet/librarymanager/wiki/LibMan-CLI-Reference Creates libman.json with a specific version (1.0). Note: the --version flag for init is distinct from the global --version flag that prints the executable version. ```text libman init --version "1.0" ``` -------------------------------- ### SampleLibrary@1.0.0 directory structure Source: https://github.com/aspnet/librarymanager/wiki/libman.json-reference Shows the directory structure of a library named SampleLibrary@1.0.0. The root property in a fileMapping can be set to 'Folder' to omit that portion from the destination path. ```text SampleLibrary@1.0.0 - Folder - SubFolder * subFolderFile.js * folderFile.js * rootFile.js ``` -------------------------------- ### Initialize LibMan with default provider Source: https://github.com/aspnet/librarymanager/wiki/LibMan-CLI-Reference Initializes libman.json with the specified default provider (cdnjs) without prompting for a provider. ```text libman init --default-provider "cdnjs" ``` -------------------------------- ### List cached libraries with files Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Prints a list of all libraries in the cache, listing all files in the library cache. ```shell > libman cache list --files ``` -------------------------------- ### Fix broken build with git clean and build Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Run these commands to recover from a broken build state. The first removes all non-source-controlled files, and the second runs the build, which pulls in NuGet packages and other dependencies. ```bash git clean -xdf (clean all non-source controlled files) build (this will run the build and will pull in NuGet packages, etc.) ``` -------------------------------- ### List cached libraries Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Prints a list of all libraries that exist in the cache, by library name only. ```shell > libman cache list ``` -------------------------------- ### Cross-platform environment variable retrieval Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Retrieves the user's home directory by checking USERPROFILE on Windows and HOME on Linux, using null-coalescing. ```csharp var homeDir = Environment.GetEnvironmentVariable("USERPROFILE") ?? Environment.GetEnvironmentVariable("HOME"); ``` -------------------------------- ### MAT success output Source: https://github.com/aspnet/librarymanager/wiki/Using-MAT-for-RESX-to-XLIFF-conversion Expected output in the Multilingual App Toolkit output window when enabling MAT succeeds. It confirms the project was enabled and the source culture is 'en'. ```text 1> Project 'Microsoft.Web.LibraryInstaller.Contracts' was enabled. The project's source culture is 'en' [English]. ``` -------------------------------- ### Copyright header for source files Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Required header for all source code files in the repository. Do not modify it. It may be omitted on generated files such as *.designer.cs. ```csharp // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. ``` -------------------------------- ### Conditional compilation for target frameworks in C# Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Shows using #if with #elif and #error to handle framework-specific code in the Desktop context. ```csharp #if NET46 Console.WriteLine("Hi .NET 4.6"); #elif NETCOREAPP2_0 Console.WriteLine("Hi .NET Standard 2.0"); #else #error Target framework needs to be updated #endif ``` -------------------------------- ### Unit test structure comments (Arrange/Act/Assert) Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Optional comments that split a unit test into three distinct stages: Arrange, Act, and Assert. The Act stage must be exactly one statement that calls the method under test. ```csharp // Arrange // Act // Assert ``` -------------------------------- ### Ideal unit test structure with Arrange/Act/Assert Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Ideal unit test pattern that moves complex parameter building into the Arrange section, leaving the Act stage as a single call to the method under test. This ensures the only reason the Act line can fail is if the method itself throws. ```csharp // Arrange P1 p1 = GetComplexParam1(); P2 p2 = GetComplexParam2(); P3 p3 = GetComplexParam3(); // Act int result = myObj.CallSomeMethod(p1, p2, p3); // Assert Assert.AreEqual(1234, result); ``` -------------------------------- ### Update library to specific version Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Updates the library to a specific version using the --to flag. ```shell > libman update jquery --to 2.2.0 ``` -------------------------------- ### Add NeutralResourcesLanguage attribute to AssemblyInfo.cs Source: https://github.com/aspnet/librarymanager/wiki/Using-MAT-for-RESX-to-XLIFF-conversion Add this attribute to AssemblyInfo.cs to specify the neutral language as English before enabling MAT. Without it, enabling MAT fails with the error: "Project 'Microsoft.Web.LibraryInstaller.Contracts' was not enabled - the project's source culture could not be determined." ```csharp [assembly: NeutralResourcesLanguage("en")] ``` -------------------------------- ### Update library to latest version Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Updates the library to the latest version. ```shell > libman update jquery ``` -------------------------------- ### Correct C# type keyword usage Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Shows correct usage of C# type keywords (string, Int32) in favor of .NET type names. Includes a comment that C# type keywords cannot be used with nameof. ```csharp public string TrimString(string s) { return string.IsNullOrEmpty(s) ? null : s.Trim(); } var intTypeName = nameof(Int32); // can't use C# type keywords with nameof ``` -------------------------------- ### Testing exception messages with Assert.Throws and Assert.Equal Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Verifies the exact exception message by comparing it to the RESX resource (or a string literal). This ensures the desired exception is tested rather than a different exception of the same type. ```csharp var ex = Assert.Throws( () => fruitBasket.GetBananaById(1234)); Assert.Equal( Strings.FormatInvalidBananaID(1234), ex.Message); ``` -------------------------------- ### libman restore Source: https://github.com/aspnet/librarymanager/wiki/LibMan-CLI-Reference Restores all libraries defined in libman.json by downloading files from their providers and saving them to the configured destinations. ```APIDOC ## libman restore ### Description Downloads all files from provider and saves them to configured destination. ### Usage `libman restore [options]` ### Options - **--help|-h** - Show help information - **--verbosity** - Set the verbosity of output (eg. "normal", "detailed", "quiet") ### Remarks - Will throw error if no libman.json in current folder - If a library specifies a provider, it will override the defaultProvider - If a library specifies a destination, it will override the defaultDestination ``` -------------------------------- ### Uninstall library Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Removes jquery files from the folder they were restored to and removes the library definition from libman.json. ```shell > libman uninstall jquery ``` -------------------------------- ### Clean entire cache Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Removes all files in the local machine's cache. ```shell > libman cache clean ``` -------------------------------- ### Not recommended unit test Act statement Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Not recommended because the Act stage contains multiple complex parameter calls that can throw for reasons unrelated to the test, making failures unclear. ```csharp int result = myObj.CallSomeMethod(GetComplexParam1(), GetComplexParam2(), GetComplexParam3()); ``` -------------------------------- ### Async method with cancellation token Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Shows the required async method pattern: the method name ends with 'Async' and a cancellation token is passed as an optional parameter with a default value of `default(CancellationToken)`. The main exception is in web scenarios where an `HttpContext` is already available, in which case its own cancellation token is used. ```csharp public Task GetDataAsync( QueryParams query, int maxData, CancellationToken cancellationToken = default(CancellationToken)) { ... } ``` -------------------------------- ### Manual merges to master Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Commands to manually merge changes from a release/x.y branch back to master, including updating master, creating a merge branch, resolving conflicts, and opening a PR for review. ```sh # Make your changes to release/x.y git checkout release/x.y git merge --ff-only your-name/my-approved-PR-branch git push # Make sure master is up to date first git checkout master git pull # Prepare your merge on a separate branch git checkout --branch your-name/merge-release-x-y-to-master git merge --no-commit release/x.y # This is where you need to manually check your changes are good git commit -m "Merge branch release/x.y into master" git push -u origin your-name/merge-release-x-y-to-master # Open a PR to review the changes # Once approved git checkout master git merge --ff-only your-name/merge-release-x-y-to-master git push ``` -------------------------------- ### libman uninstall Source: https://github.com/aspnet/librarymanager/wiki/LibMan-CLI-Reference Uninstalls a specified library by deleting its files from the destination and removing its configuration from libman.json. ```APIDOC ## libman uninstall ### Description Deletes the library file/s from the specified destination, then removes the specified library config from libman.json. ### Usage `libman uninstall [arguments] [options]` ### Arguments - **libraryId** - Library to uninstall ### Options - **--help|-h** - Show help information - **--verbosity** - Set the verbosity of output (eg. "normal", "detailed", "quiet") ### Remarks - Will throw error if no libman.json in current folder - Will throw error if specified library doesn't exist - If there's more than one library with the same libraryId, you'll be prompted to choose. ### Examples - `libman uninstall jquery` - `libman uninstall jquery@3.3.1` ``` -------------------------------- ### Incorrect C# type keyword usage Source: https://github.com/aspnet/librarymanager/wiki/Engineering-Guidelines Shows incorrect usage of .NET type names (String) instead of C# type keywords. ```csharp public String TrimString(String s) { return String.IsNullOrEmpty(s) ? null : s.Trim(); } ``` -------------------------------- ### Clean restored files Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Removes from the project all files that were restored by LibMan. ```shell > libman clean ``` -------------------------------- ### Clean cache for specific provider Source: https://github.com/aspnet/librarymanager/wiki/Using-LibMan-CLI Removes all files in the local machine's cache for the specified provider (cdnjs). ```shell > libman cache clean --provider cdnjs ``` -------------------------------- ### libman update Source: https://github.com/aspnet/librarymanager/wiki/LibMan-CLI-Reference Updates a specified library to the latest version, optionally to a specific version or pre-release. ```APIDOC ## libman update ### Description Updates the specified library to the latest version. ### Usage `libman update [arguments] [options]` ### Arguments - **libraryId** - Library to update ### Options - **--help|-h** - Show help information - **--verbosity** - Set the verbosity of output (eg. "normal", "detailed", "quiet") - **-pre** - If specified, the latest pre-release version of the library will be downloaded (where applicable) - **--to** - The version to update the library to (needs complete libraryid for the provider) ### Remarks - Error if no libman.json in current folder - Error if specified library doesn't exist - If there's more than one library with the same libraryId, you'll be prompted to choose. ### Examples - `libman update jquery` - `libman update jquery --to 3.3.1` - `libman update jquery -pre` ``` -------------------------------- ### Add XliffResource ItemGroup to csproj for MAT resx regeneration Source: https://github.com/aspnet/librarymanager/wiki/Using-MAT-for-RESX-to-XLIFF-conversion Add this ItemGroup to your csproj file to work around a MAT bug where localized resx files are not regenerated after initial generation. Without it, newly added strings in the English resx file update the XLF files but leave the translated resx files unchanged. ```xml ``` -------------------------------- ### MAT PropertyGroup added to csproj Source: https://github.com/aspnet/librarymanager/wiki/Using-MAT-for-RESX-to-XLIFF-conversion This PropertyGroup is added to the csproj file when MAT is enabled. It sets the toolkit version, fallback language, and conditional properties for translation reports and pseudo warnings. ```xml 4.0 en true true ```