Unverified Commit f0d98ab8 authored by Sandro Jäckel's avatar Sandro Jäckel Committed by GitHub
Browse files

musicfree-desktop: init at 0.0.8 (#427080)

parents 027b87d5 ed573515
Loading
Loading
Loading
Loading
+2179 −0

File added.

Preview size limit exceeded, changes collapsed.

+12 −0
Original line number Diff line number Diff line
diff --git a/forge.config.ts b/forge.config.ts
index 0b0b282..d732b68 100644
--- a/forge.config.ts
+++ b/forge.config.ts
@@ -14,6 +14,7 @@ const config: ForgeConfig = {
     icon: path.resolve(__dirname, "res/logo"),
     executableName: "MusicFree",
     extraResource: [path.resolve(__dirname, "res")],
+    electronZipDir: path.resolve(__dirname, ".electron-packager/electron-zips"),
     protocols: [
       {
         name: "MusicFree",
+18 −0
Original line number Diff line number Diff line
diff --git a/src/main/window-manager/index.ts b/src/main/window-manager/index.ts
index 42fd81c..45f93ab 100644
--- a/src/main/window-manager/index.ts
+++ b/src/main/window-manager/index.ts
@@ -129,13 +129,6 @@ class WindowManager implements IWindowManager {
         initUrl.hash = `/main/musicsheet/${localPluginName}/favorite`;
         mainWindow.loadURL(initUrl.toString()).then(voidCallback);
 
-        // 3. 开发者工具
-        if (!app.isPackaged) {
-            mainWindow.on("ready-to-show", () => {
-                mainWindow.webContents.openDevTools();
-            });
-        }
-
         // 4. 主窗口http hack逻辑
         mainWindow.webContents.session.webRequest.onBeforeSendHeaders(
             (details, callback) => {
+168 −0
Original line number Diff line number Diff line
{
  lib,
  stdenv,
  buildNpmPackage,
  fetchFromGitHub,
  makeDesktopItem,
  copyDesktopItems,
  makeWrapper,
  nix-update-script,
  electron,
  python3,
  nodejs,
  vips,
  gitMinimal,
  removeReferencesTo,
  xcodebuild,
  zip,
}:

buildNpmPackage (finalAttrs: {
  pname = "musicfree-desktop";
  version = "0.0.8";

  inherit nodejs;

  src = fetchFromGitHub {
    owner = "maotoumao";
    repo = "MusicFreeDesktop";
    tag = "v${finalAttrs.version}";
    hash = "sha256-gGGlWzd6LRrB5EbkXZWqg2cwMyZ5OMQm6otpnZFUbvo=";
  };

  patches = [
    # update sharp to recognize SHARP_FORCE_GLOBAL_LIBVIPS
    # update node-abi to support newer Electron
    # update nan to fix build with newer Electron (https://github.com/nodejs/nan/issues/921)
    # see update.sh for how this patch was generated
    ./bump-deps.patch

    # tell electron-packager to look for Electron zips in a local dir
    # instead of downloading them from the internet
    ./electron-zip-dir.patch

    # Do not open devtools on startup.
    # ELECTRON_FORCE_IS_PACKAGED=1 should also do the trick,
    # but it causes weird problem of being unable to look up the resource dir
    ./no-startup-dev-tools.patch
  ];

  nativeBuildInputs = [
    copyDesktopItems
    makeWrapper
    (python3.withPackages (ps: [ ps.setuptools ])) # Used by node-gyp
    gitMinimal # Used by electron-forge
    zip
    removeReferencesTo
  ]
  ++ lib.optionals stdenv.hostPlatform.isDarwin [ xcodebuild ]; # Used by better-sqlite3

  npmDepsHash = "sha256-lxDEbf++QH1/DaIEgXMnmqMD9d5Ak7IJMc5pQjysKBA=";

  postConfigure = ''
    # use Electron's headers to make node-gyp compile against the Electron ABI
    export npm_config_nodedir="${electron.headers}"

    # override the detected electron version
    substituteInPlace node_modules/@electron-forge/core-utils/dist/electron-version.js \
      --replace-fail "return version" "return '${electron.version}'"

    # create the electron archive to be used by electron-packager
    # the contents do not matter, but some files must be present, such as a file named "electron" intended to be the executable
    electron_zip=$(pwd)/.electron-packager/electron-zips/electron-v${electron.version}-${stdenv.hostPlatform.node.platform}-${stdenv.hostPlatform.node.arch}.zip
    mkdir -p $(dirname $electron_zip)
    ${
      if stdenv.hostPlatform.isDarwin then
        ''
          for app in Electron.app{,/Contents/Frameworks/Electron\ Helper.app}; do
            mkdir -p "$app/Contents/MacOS"
            cp "${electron.dist}/$app/Contents/Info.plist" "$app/Contents/Info.plist"
            chmod +w "$app/Contents/Info.plist"
            echo dummy > "$app/Contents/MacOS/$(basename "$app" .app)"
          done
          zip -r $electron_zip Electron.app
          rm -r Electron.app
        ''
      else
        ''
          echo dummy > electron
          zip $electron_zip electron
          rm electron
        ''
    }
  '';

  buildInputs = [
    vips # for sharp
  ];

  # sharp tries to write to npm-deps/_libvips when compiling
  makeCacheWritable = true;

  npmBuildScript = "package";

  env = {
    SHARP_FORCE_GLOBAL_LIBVIPS = "1";
    ELECTRON_SKIP_BINARY_DOWNLOAD = "1";
    # Have more info in build logs and also prevent the fancy UI of electron-forge spamming the build logs
    DEBUG = "electron-forge:*,electron-packager,@electron/get:*";
  };

  desktopItems = [
    (makeDesktopItem {
      name = "musicfree-desktop";
      desktopName = "MusicFree";
      exec = "MusicFree %U";
      icon = "musicfree-desktop";
      type = "Application";
      comment = finalAttrs.meta.description;
      categories = [ "Utility" ];
      mimeTypes = [ "x-scheme-handler/musicfree" ];
    })
  ];

  installPhase = ''
    runHook preInstall

    # the output of electron-forge is here
    build_dir=$(find out -type d -maxdepth 1 -mindepth 1 | head -n1)
    build_dir=$build_dir/${
      if stdenv.hostPlatform.isDarwin then "MusicFree.app/Contents/Resources" else "resources"
    }

    phome=$out/lib/node_modules/musicfree-desktop
    mkdir -p $(dirname $phome)
    cp -ar $build_dir/app $phome
    cp -ar $build_dir/res $phome
    find $phome/node_modules -type f -executable -exec remove-references-to -t ${nodejs} '{}' \;

    makeWrapper ${lib.getExe electron} $out/bin/MusicFree \
      --add-flags $phome \
      --add-flags "\''${NIXOS_OZONE_WL:+\''${WAYLAND_DISPLAY:+--ozone-platform-hint=auto --enable-features=WaylandWindowDecorations --enable-wayland-ime=true}}" \
      --inherit-argv0

    mkdir -p $out/share/pixmaps
    ln -s $phome/res/logo.png $out/share/pixmaps/musicfree-desktop.png

    ${lib.optionalString stdenv.hostPlatform.isDarwin ''
      mkdir -p $out/Applications/MusicFree.app/Contents/{MacOS,Resources}
      cp $build_dir/../Info.plist $out/Applications/MusicFree.app/Contents
      ln -s $out/bin/MusicFree $out/Applications/MusicFree.app/Contents/MacOS/MusicFree
      ln -s $phome/res/logo.icns $out/Applications/MusicFree.app/Contents/Resources/electron.icns
    ''}

    runHook postInstall
  '';

  passthru.updateScript = ./update.sh;

  meta = {
    description = "Customizable music player with plugin support";
    homepage = "https://musicfree.catcat.work";
    changelog = "https://github.com/maotoumao/MusicFreeDesktop/blob/${finalAttrs.src.tag}/changelog.md";
    license = lib.licenses.agpl3Only;
    maintainers = with lib.maintainers; [ ulysseszhan ];
    mainProgram = "MusicFree";
    platforms = electron.meta.platforms;
  };
})
+34 −0
Original line number Diff line number Diff line
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p nodejs nix-update jq curl patch diffutils

set -euo pipefail

old_version=$(nix-instantiate --eval -A musicfree-desktop.version | tr -d '"')
new_version=$(curl -sL ${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} https://api.github.com/repos/maotoumao/MusicFreeDesktop/releases/latest | jq -r .tag_name | tr -d v)
if [ "$old_version" == "$new_version" ]; then
  echo "Already up to date" >&2
  exit
fi

patch=$(dirname $(nix-instantiate --eval -A musicfree-desktop.meta.position | tr -d '"' | cut -d : -f 1))/bump-deps.patch

export HOME=$(mktemp -d)
pushd $HOME

curl -L https://github.com/maotoumao/MusicFreeDesktop/raw/v$new_version/package.json -o package.json
curl -L https://github.com/maotoumao/MusicFreeDesktop/raw/v$new_version/package-lock.json -o package-lock.json

if patch --batch --dry-run -p1 -i $patch; then
  echo "Old patch works, no need to update" >&2
else
  mv package.json package.json.old
  jq '.dependencies.sharp |= (if . != null and . < "^0.33.4" then "^0.33.4" else . end)' package.json.old > package.json
  cp package-lock.json package-lock.json.old
  npm update sharp node-abi nan --package-lock-only
  diff -u --label a/package.json package.json.old --label b/package.json package.json > $patch || true
  diff -u --label a/package-lock.json package-lock.json.old --label b/package-lock.json package-lock.json >> $patch || true
fi

popd

nix-update musicfree-desktop --version $new_version