Skip to content

packager

napt.build.packager

.intunewin package generation for NAPT.

This module handles creating .intunewin packages from built PSADT directories using Microsoft's IntuneWinAppUtil.exe tool.

Design Principles
  • IntuneWinAppUtil.exe is cached per release under the cache root (directories.cache), not per build
  • Package output is named by IntuneWinAppUtil.exe: Invoke-AppDeployToolkit.intunewin
  • Tool is downloaded from Microsoft's official GitHub repository
Verification

The build manifest is the link between steps. Before packaging, the installer inside the build is re-hashed against the manifest's installer_sha256, and that hash is checked against the release deployment state records when the caller supplies it. After packaging, the manifest copied into the package folder gains the .intunewin's filename and hash, which napt upload re-checks before sending. Two runs of IntuneWinAppUtil never produce the same bytes (it generates a fresh encryption key each time), so the .intunewin hash only ever ties one package to one upload.

Filing

The package folder for the version being packaged is cleared and rebuilt, so it holds exactly what this run produced. Other versions' package folders are never touched.

PackageResult dataclass

Result from creating a .intunewin package.

Attributes:

Name Type Description
build_dir Path

Path to the build directory.

package_path Path

Path to the created .intunewin file.

app_id str

Unique application identifier.

version str

Application version.

Source code in napt/build/packager.py
@dataclass(frozen=True)
class PackageResult:
    """Result from creating a .intunewin package.

    Attributes:
        build_dir: Path to the build directory.
        package_path: Path to the created .intunewin file.
        app_id: Unique application identifier.
        version: Application version.
    """

    build_dir: Path
    package_path: Path
    app_id: str
    version: str

create_intunewin

create_intunewin(
    build_dir: Path,
    cache_dir: Path,
    output_dir: Path | None = None,
    tool_release: str = "latest",
    expected_sha256: str | None = None,
) -> PackageResult

Creates a .intunewin package from a PSADT build version directory.

Uses Microsoft's IntuneWinAppUtil.exe tool to package the PSADT build into a .intunewin file for Intune deployment.

The output directory is versioned: packages/{app_id}/{version}/. That folder is cleared and rebuilt so it holds exactly what this run produced; other versions' package folders are never touched. Detection and requirements scripts are copied into the output directory so that 'napt upload' is self-contained and does not need the builds directory.

Parameters:

Name Type Description Default
build_dir Path

Path to the version directory produced by 'napt build' (e.g., builds/google-chrome/144.0.7559.110/). Must contain a packagefiles/ subdirectory with a valid PSADT structure.

required
cache_dir Path

The cache root (directories.cache). IntuneWinAppUtil.exe releases live under its intunewin/ folder, one per version.

required
output_dir Path | None

Parent directory for package output. Default: packages/ (configurable via directories.package in org.yaml).

None
tool_release str

IntuneWinAppUtil.exe release to use, "latest" or a specific version (e.g., "1.8.6"). Default is "latest".

'latest'
expected_sha256 str | None

Hash of the release deployment state records, or None when no release is recorded. The build's installer must match it.

None

Returns:

Type Description
PackageResult

Package metadata including .intunewin path, app ID, and version.

Raises:

Type Description
ConfigError

If the build directory structure is invalid, or its manifest is missing or unusable.

PackagingError

If the build's installer does not match its manifest or the recorded release, a script the manifest names is missing, packaging fails, or build_dir is missing.

NetworkError

If IntuneWinAppUtil.exe download fails.

Example

Basic packaging:

result = create_intunewin(
    Path("builds/google-chrome/144.0.7559.110"), Path("cache")
)
print(result.package_path)
# packages/google-chrome/144.0.7559.110/Invoke-AppDeployToolkit.intunewin

Note

Requires build directory from 'napt build' command. IntuneWinAppUtil.exe is downloaded and cached on first use. Setup file is always "Invoke-AppDeployToolkit.exe". Output file is named by IntuneWinAppUtil.exe: packages/{app_id}/{version}/Invoke-AppDeployToolkit.intunewin

Source code in napt/build/packager.py
def create_intunewin(
    build_dir: Path,
    cache_dir: Path,
    output_dir: Path | None = None,
    tool_release: str = "latest",
    expected_sha256: str | None = None,
) -> PackageResult:
    """Creates a .intunewin package from a PSADT build version directory.

    Uses Microsoft's IntuneWinAppUtil.exe tool to package the PSADT build
    into a .intunewin file for Intune deployment.

    The output directory is versioned: packages/{app_id}/{version}/. That
    folder is cleared and rebuilt so it holds exactly what this run
    produced; other versions' package folders are never touched.
    Detection and requirements scripts are copied into the output directory
    so that 'napt upload' is self-contained and does not need the builds
    directory.

    Args:
        build_dir: Path to the version directory produced by 'napt build'
            (e.g., builds/google-chrome/144.0.7559.110/). Must contain a
            packagefiles/ subdirectory with a valid PSADT structure.
        cache_dir: The cache root (directories.cache). IntuneWinAppUtil.exe
            releases live under its intunewin/ folder, one per version.
        output_dir: Parent directory for package output.
            Default: packages/ (configurable via directories.package in
            org.yaml).
        tool_release: IntuneWinAppUtil.exe release to use, "latest" or a
            specific version (e.g., "1.8.6"). Default is "latest".
        expected_sha256: Hash of the release deployment state records, or
            None when no release is recorded. The build's installer must
            match it.

    Returns:
        Package metadata including .intunewin path, app ID, and version.

    Raises:
        ConfigError: If the build directory structure is invalid, or its
            manifest is missing or unusable.
        PackagingError: If the build's installer does not match its
            manifest or the recorded release, a script the manifest names
            is missing, packaging fails, or build_dir is missing.
        NetworkError: If IntuneWinAppUtil.exe download fails.

    Example:
        Basic packaging:
            ```python
            result = create_intunewin(
                Path("builds/google-chrome/144.0.7559.110"), Path("cache")
            )
            print(result.package_path)
            # packages/google-chrome/144.0.7559.110/Invoke-AppDeployToolkit.intunewin
            ```

    Note:
        Requires build directory from 'napt build' command. IntuneWinAppUtil.exe
        is downloaded and cached on first use. Setup file is always
        "Invoke-AppDeployToolkit.exe". Output file is named by IntuneWinAppUtil.exe:
        packages/{app_id}/{version}/Invoke-AppDeployToolkit.intunewin
    """
    logger = get_global_logger()

    build_dir = build_dir.resolve()

    if not build_dir.exists():
        raise PackagingError(f"Build directory not found: {build_dir}")

    # build_dir is the version directory: builds/{app_id}/{version}/
    version = build_dir.name
    app_id = build_dir.parent.name
    packagefiles_dir = build_dir / "packagefiles"

    logger.verbose("PACKAGE", f"Packaging {app_id} v{version}")

    # Verify PSADT structure inside packagefiles/ and the build against
    # its manifest and the recorded release, before anything is written.
    logger.step(1, 5, "Verifying build...")
    _verify_build_structure(packagefiles_dir)
    manifest = read_build_manifest(
        build_dir,
        ("installer_sha256", "detection_script_path"),
        "Run 'napt build' to create the build again.",
    )
    installer_sha256: str = manifest["installer_sha256"]
    if expected_sha256 is not None and installer_sha256 != expected_sha256:
        raise PackagingError(
            f"The build in {build_dir} was made from a different installer "
            f"(sha256 {installer_sha256}) than the recorded release "
            f"(sha256 {expected_sha256}). Run 'napt build' so the build "
            "matches the recorded release."
        )
    _verify_installer(packagefiles_dir, installer_sha256)
    logger.verbose("PACKAGE", f"Installer sha256 {installer_sha256} verified")

    # Get IntuneWinAppUtil tool
    logger.step(2, 5, "Getting IntuneWinAppUtil tool...")
    tool_path = _get_intunewin_tool(cache_dir / "intunewin", tool_release)

    # Versioned output directory: packages/{app_id}/{version}/. This run
    # replaces it wholesale; other versions' folders are never touched.
    packages_root = output_dir.resolve() if output_dir else Path("packages").resolve()
    version_output_dir = packages_root / app_id / version
    if version_output_dir.exists():
        logger.info("PACKAGE", f"Replacing the earlier package of {version}")
        shutil.rmtree(version_output_dir)
    version_output_dir.mkdir(parents=True)

    # Create .intunewin package
    logger.step(3, 5, "Creating .intunewin package...")
    package_path = _execute_packaging(
        tool_path,
        packagefiles_dir,
        "Invoke-AppDeployToolkit.exe",
        version_output_dir,
    )

    # Copy the scripts the manifest names into the package folder so napt
    # upload is self-contained, then write the manifest with the package
    # named in it, for upload to verify what it sends.
    logger.step(4, 5, "Copying detection scripts...")
    _copy_named_script(build_dir, manifest["detection_script_path"], version_output_dir)
    requirements_name = manifest.get("requirements_script_path")
    if isinstance(requirements_name, str) and requirements_name:
        _copy_named_script(build_dir, requirements_name, version_output_dir)
    manifest["intunewin_filename"] = package_path.name
    manifest["intunewin_sha256"] = sha256_file(package_path)
    write_text_atomic(
        version_output_dir / MANIFEST_NAME,
        json.dumps(manifest, indent=2) + "\n",
    )
    logger.verbose("PACKAGE", f"Wrote: {MANIFEST_NAME}")

    logger.step(5, 5, "Package complete")
    logger.verbose("PACKAGE", f"[OK] Package created: {package_path}")

    return PackageResult(
        build_dir=build_dir,
        package_path=package_path,
        app_id=app_id,
        version=version,
    )