From 8395d7a4d390dad584ebc3f31c14c67f71e9c591 Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Tue, 24 May 2022 13:06:19 +0200 Subject: libreoffice: add update-script for darwin --- pkgs/applications/office/libreoffice/darwin.nix | 51 --------------- .../office/libreoffice/darwin/darwin.nix | 73 ++++++++++++++++++++++ .../office/libreoffice/darwin/update-test.nix | 41 ++++++++++++ .../office/libreoffice/darwin/update-utils.nix | 45 +++++++++++++ pkgs/top-level/all-packages.nix | 2 +- 5 files changed, 160 insertions(+), 52 deletions(-) delete mode 100644 pkgs/applications/office/libreoffice/darwin.nix create mode 100644 pkgs/applications/office/libreoffice/darwin/darwin.nix create mode 100644 pkgs/applications/office/libreoffice/darwin/update-test.nix create mode 100644 pkgs/applications/office/libreoffice/darwin/update-utils.nix diff --git a/pkgs/applications/office/libreoffice/darwin.nix b/pkgs/applications/office/libreoffice/darwin.nix deleted file mode 100644 index 765cbcd4302..00000000000 --- a/pkgs/applications/office/libreoffice/darwin.nix +++ /dev/null @@ -1,51 +0,0 @@ -{ stdenv -, lib -, fetchurl -, undmg -}: - -let - appName = "LibreOffice.app"; - version = "7.2.5"; - dist = { - aarch64-darwin = { - arch = "aarch64"; - sha256 = "bdbcb9a98211f866ca089d440aebcd1d313aa99e8ab4104aae4e65ea3cee74ca"; - }; - - x86_64-darwin = { - arch = "x86_64"; - sha256 = "0b7ef18ed08341ac6c15339fe9a161ad17f6b469009d987cfc7d50c628d12a4e"; - }; - }."${stdenv.hostPlatform.system}"; -in -stdenv.mkDerivation { - inherit version; - pname = "libreoffice"; - src = fetchurl { - url = "https://download.documentfoundation.org/libreoffice/stable/${version}/mac/${dist.arch}/LibreOffice_${version}_MacOS_${dist.arch}.dmg"; - inherit (dist) sha256; - }; - - nativeBuildInputs = [ undmg ]; - sourceRoot = "${appName}"; - dontPatch = true; - dontConfigure = true; - dontBuild = true; - - installPhase = '' - runHook preInstallPhase - mkdir -p $out/{Applications/${appName},bin} - cp -R . $out/Applications/${appName} - ln -s $out/Applications/${appName}/Contents/MacOS/soffice $out/bin - runHook postInstallPhase - ''; - - meta = with lib; { - description = "Comprehensive, professional-quality productivity suite, a variant of openoffice.org"; - homepage = "https://libreoffice.org/"; - license = licenses.lgpl3; - maintainers = with maintainers; [ tricktron ]; - platforms = [ "aarch64-darwin" "x86_64-darwin" ]; - }; -} diff --git a/pkgs/applications/office/libreoffice/darwin/darwin.nix b/pkgs/applications/office/libreoffice/darwin/darwin.nix new file mode 100644 index 00000000000..7b2f89cf0a5 --- /dev/null +++ b/pkgs/applications/office/libreoffice/darwin/darwin.nix @@ -0,0 +1,73 @@ +{ stdenv +, lib +, fetchurl +, undmg +, writeScript +}: + +let + appName = "LibreOffice.app"; + version = "7.2.5"; + baseUrl = "https://download.documentfoundation.org/libreoffice/stable"; + dist = { + aarch64-darwin = rec { + arch = "aarch64"; + archSuffix = arch; + url = "${baseUrl}/${version}/mac/${arch}/LibreOffice_${version}_MacOS_${archSuffix}.dmg"; + sha256 = "bdbcb9a98211f866ca089d440aebcd1d313aa99e8ab4104aae4e65ea3cee74ca"; + }; + + x86_64-darwin = rec { + arch = "x86_64"; + archSuffix = "x86-64"; + url = "${baseUrl}/${version}/mac/${arch}/LibreOffice_${version}_MacOS_${archSuffix}.dmg"; + sha256 = "0b7ef18ed08341ac6c15339fe9a161ad17f6b469009d987cfc7d50c628d12a4e"; + }; + }; +in +stdenv.mkDerivation rec { + inherit version; + pname = "libreoffice"; + src = fetchurl { + inherit (dist."${stdenv.hostPlatform.system}") url sha256; + }; + + nativeBuildInputs = [ undmg ]; + sourceRoot = "${appName}"; + dontPatch = true; + dontConfigure = true; + dontBuild = true; + + installPhase = '' + runHook preInstallPhase + mkdir -p $out/{Applications/${appName},bin} + cp -R . $out/Applications/${appName} + ln -s $out/Applications/${appName}/Contents/MacOS/soffice $out/bin + runHook postInstallPhase + ''; + + passthru.updateScript = + let + inherit (import ./update-utils.nix { inherit lib; }) getLatestStableVersion getSha256 nullHash; + newVersion = getLatestStableVersion; + in + writeScript "update-libreoffice.sh" + '' + #!/usr/bin/env nix-shell + #!nix-shell -i bash -p common-updater-scripts + set -o errexit + set -o nounset + set -o pipefail + update-source-version libreoffice ${newVersion} ${getSha256 dist."aarch64-darwin".url version newVersion} --system=aarch64-darwin + update-source-version libreoffice 0 ${nullHash} --system=x86_64-darwin + update-source-version libreoffice ${newVersion} ${getSha256 dist."x86_64-darwin".url version newVersion} --system=x86_64-darwin + ''; + + meta = with lib; { + description = "Comprehensive, professional-quality productivity suite, a variant of openoffice.org"; + homepage = "https://libreoffice.org/"; + license = licenses.lgpl3; + maintainers = with maintainers; [ tricktron ]; + platforms = [ "aarch64-darwin" "x86_64-darwin" ]; + }; +} diff --git a/pkgs/applications/office/libreoffice/darwin/update-test.nix b/pkgs/applications/office/libreoffice/darwin/update-test.nix new file mode 100644 index 00000000000..eeffec39830 --- /dev/null +++ b/pkgs/applications/office/libreoffice/darwin/update-test.nix @@ -0,0 +1,41 @@ +{ pkgs ? import {}, nixt }: +let + inherit (import ./update-utils.nix { inherit (pkgs) lib; }) + extractLatestVersionFromHtml + extractSha256FromHtml; +in +nixt.mkSuite "LibreOffice Updater" +{ + "should extract latest stable version from html" = let + latestVersionHtmlMock = + '' + + + + Index of /libreoffice/stable + + + +

Index of /libreoffice/stable

+ + + + + + +
 NameLast modifiedSizeMetadata

 Parent Directory  -  
 7.2.7/10-Mar-2022 11:12 -  
 7.3.3/12-May-2022 10:06 -  
 7.2.6/05-May-2022 07:57 -  

+
Apache Server at download.documentfoundation.org Port 80
+
MirrorBrain powered by Apache
+ + ''; + + actual = extractLatestVersionFromHtml latestVersionHtmlMock; + + in "7.3.3" == actual; + + "should extract sha256 from html" = let + sha256Html = "50ed3deb8d9c987516e2687ebb865bca15486c69da79f1b6d74381e43f2ec863 LibreOffice_7.3.3_MacOS_aarch64.dmg\n"; + actual = extractSha256FromHtml sha256Html; + in "50ed3deb8d9c987516e2687ebb865bca15486c69da79f1b6d74381e43f2ec863" == actual; +} diff --git a/pkgs/applications/office/libreoffice/darwin/update-utils.nix b/pkgs/applications/office/libreoffice/darwin/update-utils.nix new file mode 100644 index 00000000000..fb4a7b0e7b2 --- /dev/null +++ b/pkgs/applications/office/libreoffice/darwin/update-utils.nix @@ -0,0 +1,45 @@ +{ lib }: +let +# extractLatestVersionFromHtml :: String -> String + extractLatestVersionFromHtml = htmlString: + let + majorMinorPatchGroup = "([0-9]+\\.[0-9]+\\.[0-9]+)"; + splittedVersions = builtins.split "href=\"${majorMinorPatchGroup}" htmlString; + stableVersions = builtins.concatLists (builtins.filter (e: builtins.isList e) splittedVersions); + in + if stableVersions == [] + then abort "Failed to extract versions from html." + else lib.last (builtins.sort builtins.lessThan stableVersions); + +# getHtml :: String -> String + getHtml = url: + builtins.readFile (builtins.fetchurl url); + +# getLatestStableVersion :: String + getLatestStableVersion = + extractLatestVersionFromHtml (getHtml "https://download.documentfoundation.org/libreoffice/stable/"); + +# extractSha256FromHtml :: String -> String + extractSha256FromHtml = htmlString: + let + sha256 = (builtins.match ".*([0-9a-fA-F]{64}).*" htmlString); + in + if sha256 == [] + then abort "Failed to extract sha256 from html." + else builtins.head sha256; + +# getSha256 :: String -> String + getSha256 = dmgUrl: oldVersion: newVersion: + extractSha256FromHtml (getHtml (getSha256Url dmgUrl oldVersion newVersion)); + +# getSha256Url :: String -> String -> String -> String + getSha256Url = dmgUrl: oldVersion: newVersion: + (builtins.replaceStrings [ oldVersion ] [ newVersion ] dmgUrl) + ".sha256"; + +# nullHash :: String + nullHash = "0000000000000000000000000000000000000000000000000000000000000000"; +in +{ + inherit extractLatestVersionFromHtml getHtml getLatestStableVersion + extractSha256FromHtml getSha256 getSha256Url nullHash; +} diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 9d16d3298d1..194229f944c 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -27773,7 +27773,7 @@ with pkgs; }; libreoffice = if stdenv.isDarwin - then callPackage ../applications/office/libreoffice/darwin.nix {} + then callPackage ../applications/office/libreoffice/darwin/darwin.nix {} else hiPrio libreoffice-still; libreoffice-unwrapped = (hiPrio libreoffice-still).libreoffice; -- cgit 1.4.1