Types/OpenPackage.View/org.poshweb.op.ps1

<#
.SYNOPSIS
    Views a package as an `org.poshweb.op`
.DESCRIPTION
    Views a package as an `org.poshweb.op`.
    
    This provides some summary metadata about an open package,
    and contains basic information about files in the package.
.INPUTS
    System.IO.Packaging.Package
#>

[Reflection.AssemblyMetadata('schema', @'
{
    "$schema": "https://json-schema.org/schema",
    "$id": "https://op.poshweb.org/schemas/org.poshweb.op",
    "type": "object",
    "required": ["id"],
    "properties": {
        "id": {
            "type": "string",
            "description": "The package id"
        },
        "creator": {
            "type": "string",
            "description": "The package creator"
        },
        "tags": {
            "type": "array",
            "items": {
                "type": "string"
            }
        },
        "files": {
            "type": "array",
            "items": {
                "type": "object",
                "required": ["algorithm", "contentType", "hash", "path"],
                "properties": {
                    "algorithm": {
                        "type": "string",
                        "description": "The hash algorithm"
                    },
                    "hash": {
                        "type": "string",
                        "description": "The hash value"
                    },
                    "contentType": {
                        "type": "string",
                        "description": "The content type"
                    },
                    "path": {
                        "type": "string",
                        "description": "The path of the hashed content"
                    }
                }
            }
        },
        "version": {
            "type": "string",
            "description": "The package version"
        },
        "url": {
            "type": "string",
            "format": "url",
            "description": "The package url"
        }
    }
}
'@

)]
param()

$allInputAndArgs = @($input) + @($args)

$mySchema = foreach ($attribute in $MyInvocation.MyCommand.ScriptBlock.Attributes) {
    if ($attribute.Key -in 'schema', 'jsonschema','json-schema') {
        $attribute.Value | ConvertFrom-Json
        break
    }
}

foreach ($in in $allInputAndArgs) {
    if ($in -isnot [IO.Packaging.Package]) {
        continue
    }

    [PSCustomObject]@{
        PSTypeName = 'org.poshweb.op'
        '$schema' = "$($mySchema.'$schema')"
        '$id' = "$($mySchema.'$id')"
        '$type' = 'org.poshweb.op'
        id = "$($in.Identifier)"
        description = "$($in.description)"
        version = "$($in.Version)"
        files = @(
            foreach ($part in $in.GetParts()) {
                $part.GetHash() |
                    Select-Object @{
                        name='path';expression={$_.path}
                    }, @{
                        name='contentType';Expression={$part.ContentType}
                    }, @{                        
                        name='hash';expression={$_.hash.ToLower()}
                    }, @{
                        name='algorithm';expression={$_.algorithm.ToLower()}
                    }
            }            
        )
        tags = @($in.Keywords -split '\s+')               
    }    
}