ModuleFast

repository·main·Indexed 41 days ago

https://github.com/justingrote/modulefast

A high-performance PowerShell module installer that uses parallel installation and a custom NuGet v3 mirror (pwsh.gallery) for speed. It features a declarative, IaC-style approach to dependency management, supporting SemVer 2, requirement files (.psd1, .json, .ps1), and CI/CD lockfiles via the -CI parameter. Key commands include Install-ModuleFast (alias imf) and Clear-ModuleFastCache.

Tokens
2.7K
Snippets
9
Records
15
Agent score
100%

What's inside ModuleFast

  1. How dependency resolution works

    main

    ModuleFast uses a custom dependency resolution algorithm designed to select the newest available modules that satisfy all criteria.

    Key Behaviors:

    • Incremental Approach: It evaluates locally installed modules first. If an existing module satisfies the criteria, it is used instead of fetching the newest version from the gallery.
    • Override: Use the -Update switch to force ModuleFast to recheck the remote gallery regardless of local state.
    • Dependency Tree: It builds a declarative dependency tree using a custom HTTPClient and async tasks to optimize performance via batch queries and HTTP/2.
  2. Create a CI Lockfile

    main
    To ensure reproducible builds in CI/CD, use the -CI parameter. This creates a lockfile in the current directory that pins specific version specifications. When others run Install-ModuleFast -CI, they will receive the exact versions used during your initial installation, even if newer versions meeting the original specs are available.
    Install-ModuleFast -CI
  3. Test the latest unstable main commit

    main

    You can test the latest unstable commit of ModuleFast by running a script that configures an instance of ModuleFast to use preview.pwsh.gallery as the default repository. This script supports all standard ModuleFast methods and parameters, though you can override the default repository using the -Source parameter.

    & ([scriptblock]::Create((iwr 'bit.ly/modulefastmain'))) -UseMain
  4. Bootstrap ModuleFast for a session

    main

    You can quickly load ModuleFast into your current PowerShell session without using the built-in PowerShellGet package manager. Once loaded, you can use the Install-ModuleFast command or its alias imf.

    To avoid breaking changes in CI/CD environments, it is recommended to pin to a specific release instead of using the default bit.ly link which points to the latest version.

    # Load and install a module
    iwr bit.ly/modulefast | iex
    Install-ModuleFast ImportExcel
    
    # Using the alias
    imf ImportExcel
  5. Build and test ModuleFast from source

    main

    To develop ModuleFast, use the provided PowerShell build scripts:

    1. Initial Build: Run .\build.ps1. This script installs all necessary prerequisites (using ModuleFast itself) and then compiles and tests the package.
    2. Subsequent Builds: After the initial setup, you can use Invoke-Build to start new builds more quickly.

    By default, the compiled module is output into the Build directory.

    .\build.ps1
    # Then use:
    Invoke-Build
  6. Single Line Installation for CI/CD

    main

    For CI/CD pipelines (like GitHub Actions), you can load and invoke Install-ModuleFast in a single command. This syntax automatically unloads the module upon completion. Any arguments passed to the script block are passed directly to Install-ModuleFast.

    To ensure stability, pin to a specific GitHub release using the -Release parameter.

    # Install the latest version in one line
    & ([scriptblock]::Create((iwr 'bit.ly/modulefast'))) -Specification ImportExcel
    
    # Install a specific version for stability
    & ([scriptblock]::Create((iwr 'bit.ly/modulefast'))) -Release 'v0.2.0' -Specification ImportExcel
  7. Use Install-ModuleFast in CI/CD environments

    main

    When running in a Continuous Integration (CI) environment, use the -CI switch. This enables two specific behaviors:

    1. Lockfile Usage: The command looks for a lockfile at the path specified by -CILockFilePath (defaults to requires.lock.json). If found, it uses this file to evaluate the required module specifications.
    2. Lockfile Generation: After a successful installation, the command writes a new lockfile to the -CILockFilePath location, containing the names and exact versions of the installed modules.

    Example for CI:

    Install-ModuleFast -CI -CILockFilePath "./ci/requires.lock.json"
  8. Install modules from requirement files

    main

    ModuleFast can automatically resolve and install dependencies by reading specification files.

    Supported file formats:

    • .ps1 or .psm1 files containing #Requires comments.
    • .psd1 Module Manifests with a RequiredModules property.
    • .psd1 files in PSDepend format.
    • .psd1 files with module/version pairs.
    • .json or .jsonc files with module/version pairs or arrays of specification strings.

    Usage:

    • Use the -Path parameter to specify a file.
    • If no arguments are provided, Install-ModuleFast searches the current directory for *.requires.psd1|psm1|json|jsonc files.
  9. Module Specification Syntax

    main

    ModuleFast uses a shorthand string syntax for defining module requirements in the form <ModuleName><Operator><Version>. The version supports SemVer 2 and prerelease tags.

    Available Operators:

    • =: Exact version match (e.g., ImportExcel=7.1.0)
    • >: Greater than (e.g., ImportExcel>7.1.0)
    • >=: Greater than or equal to (e.g., ImportExcel>=7.1.0)
    • <: Less than (e.g., ImportExcel<7.1.0)
    • <=: Less than or equal to (e.g., ImportExcel<=7.1.0)
    • !: Prerelease operator. Can be at the start or end (e.g., ImportExcel! or !ImportExcel). Can be combined: ImportExcel!>7.1.0.
    • :: NuGet version range (e.g., ImportExcel:(7.0.0, 7.2.1-preview]). Supports wildcards like ImportExcel:3.2.*.

    Note on SemVer: By default, ModuleFast is not strictly SemVer-compliant for ergonomics. For example, Module<2.0 will exclude 2.0-alpha1 even though it is technically less than 2.0 by SemVer standards. Use the -StrictSemVer parameter to enable strict compliance.

    # Examples of specification syntax
    Install-ModuleFast 'ImportExcel>7.1.0','Az.Resources','IPNetwork=0.1.0'
  10. Troubleshoot with Verbose and Debug logging

    main

    ModuleFast provides detailed logging to help understand the module selection and installation process:

    • -Verbose: Provides a high-level "what" view of the module selection process.
    • -Debug: Provides a detailed "Why" view, explaining the logic behind module selection and installation. This is recommended for troubleshooting issues.
  11. Use Install-ModuleFast to install modules

    main

    The Install-ModuleFast cmdlet is the primary command for installing PowerShell modules. It supports multiple ways to specify what to install: via direct module specifications, by pointing to a path containing specification files, or by passing existing ModuleFastInfo objects. It can automatically detect specification files in the current directory if no arguments are provided.

    # Install specific modules using specification objects
    Install-ModuleFast -Specification $mySpecs
    
    # Install modules defined in a specific file or directory
    Install-ModuleFast -Path "./modules/"
    
    # Install modules and output the results
    Install-ModuleFast -Specification $mySpecs -PassThru
  12. Configure Install-ModuleFast installation behavior

    main

    Use the following flags to control how Install-ModuleFast executes:

    • -Destination <string>: Specify a custom directory for module installation. If a custom destination is used, the command automatically sets -NoProfileUpdate to true.
    • -Source <string>: The repository URL to fetch modules from. Defaults to https://pwsh.gallery/index.json. If a non-HTTP(S) URI is provided, it is automatically prefixed with https:// and suffixed with /index.json.
    • -Update: Clears the local cache and attempts to install newer versions of modules.
    • -Prerelease: Allows installation of pre-release module versions.
    • -StrictSemVer: Enforces strict Semantic Versioning rules.
    • -Scope <InstallScope>: Determines the installation scope (e.g., CurrentUser, AllUsers).
    • -ThrottleLimit <int>: Limits the number of concurrent installations. Defaults to the processor count.
    • -Timeout <int>: Sets the overall timeout in seconds (multiplied by 10 internally). Defaults to 30.
    • -Plan: Instead of installing, the command returns the calculated installation plan (an array of ModuleFastInfo).
    • -PassThru: Returns the list of successfully installed ModuleFastInfo objects.