# Defines a manifest data structure for `package.nu`. # A manifest is a declarative description of some software package, that `package.nu` # can install. # A manifest can describe exactly one package, or be a meta manifest grouping several # other manifests together. use ./folders.nu use ./lib.nu * export def new [ org: string name: string source?: string version?: string bin?: path home?: string description?: string linux_x64_url?: string linux_aarch64_url?: string win32_x64_url?: string fedora_name?: string arch_name?: string termux_name?: string win32_name?: string ] { if (package-exists $org $name) { print $"A manifest for ($org)/($name) already exists!" return } let source = match $source { null => { [ github codeberg custom ] | input list --fuzzy "Pick a source, where the package is hosted." } "github" | "codeberg" | "custom" => $source _ => { error make { msg: "unkown source" label: { text: "source must be one of github, codeberg or custom" span: (metadata $source).span } } } } mut manifest = {} if $source == "github" { print "Since the source of this package is github releases, the rest of the manifest values will be guessed on a best effort basis. Please review the manifest file afterwards." $manifest = (new-from-github $org $name) } if $source == 'codeberg' { print "Codeberg is not yet supported. Proceeding as if direct mode was selected." $manifest = (new-from-custom-input $org $name) } if ($source == 'custom') { $manifest = (new-from-custom-input $org $name) } if ($manifest | is-empty) { print --stderr $"New source type detected: ($source). Add handling for that!" return } let manifest_path = save-manifest $manifest let open_with_editor = (input --default y "Open the manifest in editor? (y/n)") if ($open_with_editor == 'y') { run-external $env.EDITOR $manifest_path } } def save-manifest [ manifest: record ] { cd (folders manifests) let org = $manifest.organisation let name = $manifest.name let version = $manifest.version let file = (make-manifest-file-name $org $name) mut manifest = $manifest $manifest = $manifest | update 'linux x64 url' { templatize $version } $manifest = $manifest | update 'linux aarch64 url' { templatize $version } $manifest = $manifest | update 'win32 x64 url' { templatize $version } print "Generated manifest: " print $manifest $manifest | to nuon --indent 2 | save $file print $"Manifest for ($org)/($name) saved under ($file)." return ($file | path expand) } def templatize [ version: string ]: any -> string { if ($in | is-not-empty) { $in | type-check 'string' $in | str replace --all $version "{ version }" } } def new-from-custom-input [ org: string name: string ] { let version = (input $"Give version of ($org)/($name): ") let binary_path = (input --default $name "Give relative binary path: ") let homepage = (input "Give homepage: ") let description = (input "Give description: ") let linux_x64_url = (input "Give linux x64 url: ") let linux_aarch64_url = (input "Give linux aarch64 url: ") let win32_x64_url = (input "Give win32 x64 url: ") let fedora_name = (input --default $name "Give fedora package name: ") let arch_name = (input --default $name "Give arch package name: ") let termux_name = (input --default $name "Give termux package name: ") let win32_name = (input --default ($name ++ '.' ++ $name) "Give win32 package name: ") return (make-manifest $org $name 'custom' $version $binary_path $homepage $description '' $linux_x64_url $linux_aarch64_url $win32_x64_url $fedora_name $arch_name $termux_name $win32_name) } def new-from-github [ org: string name: string ] { use ./github.nu use ./native-package-manager mut version = github fetch-latest-release-version $org $name let version_extractor = (input --default "v?(.*)" $"Github tag version is ($version). Choose version extractor regex: ") $version = ($version | parse --regex $version_extractor | get capture0.0) print $"Extracted version is ($version)" let binary_path = $name let homepage = github fetch-homepage $org $name let description = github fetch-description $org $name let release_artifacts = github fetch-release-artifacts $org $name let linux_x64_url = if ($release_artifacts | is-empty) { null } else select-linux-x64-artifact $release_artifacts let linux_aarch64_url = if ($release_artifacts | is-empty) { null } else select-linux-aarch64-artifact $release_artifacts let win32_x64_url = if ($release_artifacts | is-empty) { null } else select-win32-x64-artifact $release_artifacts let arch_names = native-package-manager arch query-packages $name let arch_name = if ($arch_names | is-empty) { null } else select-native-package-name $arch_names $name $"Pick the arch package name for ($name)" let fedora_names = native-package-manager fedora query-packages $name let fedora_name = if ($fedora_names | is-empty) { null } else select-native-package-name $fedora_names $name $"Pick the fedora package name for ($name)" let termux_names = native-package-manager termux query-packages $name let termux_name = if ($termux_names | is-empty) { null } else select-native-package-name $termux_names $name $"Pick the termux package name for ($name)" let win32_names = native-package-manager win32 query-packages $name let win32_name = if ($win32_names | is-empty) { null } else select-native-package-name $win32_names $name $"Pick the win32 package name for ($name)" return (make-manifest $org $name 'github' $version $binary_path $homepage $description $version_extractor $linux_x64_url $linux_aarch64_url $win32_x64_url $fedora_name $arch_name $termux_name $win32_name) } def make-manifest [ org: string name: string source: string version: string binary_path: string homepage: string description: string version_extractor?: string linux_x64_url?: string linux_aarch64_url?: string win32_x64_url?: string fedora_name?: string arch_name?: string termux_name?: string win32_name?: string ] { { 'organisation': $org 'name': $name 'source': $source 'version': $version 'version extractor': $version_extractor 'relative binary path': $binary_path 'homepage': $homepage 'description': $description 'linux x64 url': $linux_x64_url 'linux aarch64 url': $linux_aarch64_url 'win32 x64 url': $win32_x64_url 'fedora package name': $fedora_name 'arch package name': $arch_name 'termux package name': $termux_name 'win32 package name': $win32_name } } def select-artifact [ all_artifacts: table filtered_artifacts: table target: string ] { let filtered_length = $filtered_artifacts | length if ($filtered_length == 0) { return null } if ($filtered_length == 1) { return ($filtered_artifacts | get browser_download_url.0) } let pick = ($filtered_artifacts | input list --display name --fuzzy $"Select the artifact for ($target):") if ($pick | is-empty) { return null } return ( $pick | get browser_download_url) } def select-linux-x64-artifact [ artifacts: table ] { let filtered_artifacts = $artifacts | filter-by-linux | filter-by-x64 select-artifact $artifacts $filtered_artifacts "linux x64" } def select-linux-aarch64-artifact [ artifacts: table ] { let filtered_artifacts = $artifacts | filter-by-linux | filter-by-aarch64 select-artifact $artifacts $filtered_artifacts "linux aarch64" } def select-win32-x64-artifact [ artifacts: table ] { let filtered_artifacts = $artifacts | filter-by-win32 | filter-by-x64 select-artifact $artifacts $filtered_artifacts "win32 x64" } def filter-by-linux []: table -> table { where name =~ '(?i)[\.\-_]linux' | where name !~ '.sig$' | where name !~ '.sha.*$' | where name !~ '.apk$' | where name !~ '.deb$' | where name !~ '.rpm$' } def filter-by-win32 []: table -> table { where name =~ '(?i)[\.\-_]win' | where name !~ '.sig$' | where name !~ '.sha.*$' | where name !~ '.msi$' } def filter-by-x64 []: table -> table { where name =~ '(?i)x64' or name =~ '(?i)x86_64' or name =~ '(?i)amd64' or name =~ '(?i)64bit' } def filter-by-aarch64 []: table -> table { where name =~ '(?i)aarch64' or name =~ '(?i)arm64' } # Selects one package from `native_packages`. # If `native_packages` is empty, returns `null`. # If one exact match with `name` is found, returns that. # Otherwise starts interactive picker to select native package name. def select-native-package-name [ native_packages: table # List of native packages to select from name: string # The canonical name of this package. If an exact match in `packages` is found, it will be picked directly. prompt: string # Prompt to render, if interacive selection process is necessary ] { if ($native_packages | is-empty) { return null } let exact_matches = $native_packages | where name == $name if ($exact_matches != 0) and ($exact_matches | length) == 1 { return ($exact_matches | get name.0) } let pick = $native_packages | input list --fuzzy $prompt if ($pick | is-not-empty) { return ($pick | get name) } null } def package-exists [ org: string name: string ] { cd (folders manifests) ls | any { |manifest| $manifest.name == (make-manifest-file-name $org $name) } } def make-unique-package-name [ org: string name: string ] { $"($org).($name)" } def make-manifest-file-name [ org: string name: string ] { (make-unique-package-name $org $name) ++ '.nuon' }