From f80fca8eead42d377c8b0a02483fe265f24be453 Mon Sep 17 00:00:00 2001 From: tonyhcj Date: Thu, 5 Nov 2026 17:58:34 -0300 Subject: [PATCH] =?UTF-8?q?Item=206:=20atualiza=C3=A7=C3=A3o=20autom=C3=A1?= =?UTF-8?q?tica=20do=20Pitiupi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shell/ApplicationRestarter.java | 2 +- .../presentation/shell/SelfUpdater.java | 92 +++++++++++++++++++ .../presentation/shell/ShellMenuBar.java | 3 + 3 files changed, 96 insertions(+), 1 deletion(-) diff --git a/pitiupi/src/pitiupi/presentation/shell/ApplicationRestarter.java b/pitiupi/src/pitiupi/presentation/shell/ApplicationRestarter.java index b4ec6e1..20ebf8d 100644 --- a/pitiupi/src/pitiupi/presentation/shell/ApplicationRestarter.java +++ b/pitiupi/src/pitiupi/presentation/shell/ApplicationRestarter.java @@ -57,7 +57,7 @@ public final class ApplicationRestarter { } private static String locateRunningJar() { - return pitiupi.Main.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + return pitiupi.platform.AppVersion.runningJar().map(java.nio.file.Path::toString).orElse(null); } } } diff --git a/pitiupi/src/pitiupi/presentation/shell/SelfUpdater.java b/pitiupi/src/pitiupi/presentation/shell/SelfUpdater.java index 3accaac..a613f87 100644 --- a/pitiupi/src/pitiupi/presentation/shell/SelfUpdater.java +++ b/pitiupi/src/pitiupi/presentation/shell/SelfUpdater.java @@ -16,3 +16,95 @@ import pitiupi.platform.AppVersion; import pitiupi.platform.PitiupiHome; import pitiupi.plugins.remote.*; + + +public final class SelfUpdater { + private SelfUpdater() { } + + public static void checkAndPrompt(Component parent) { + Optional jar = AppVersion.runningJar(); + if (jar.isEmpty()) { + JOptionPane.showMessageDialog(parent, + "A atualização automática só funciona quando o Pitiupi roda pelo JAR (java -jar)."); + return; + } + final URI base; + try { + base = PluginRepositoryClient.normalize( + PitiupiHome.get().config().getString("plugins.serverUrl", "")); + } catch (Exception ex) { + JOptionPane.showMessageDialog(parent, + "Cadastre o servidor em Plugins > Plugins online… antes de verificar."); + return; + } + PluginRepositoryClient client = new PluginRepositoryClient(); + new SwingWorker, Void>() { + @Override protected Optional doInBackground() throws Exception { + return client.fetchList(base, PluginListFormat.CORE_FILE_NAME).stream().findFirst(); + } + @Override protected void done() { + try { + Optional remote = get(); + String current = AppVersion.current(); + if (remote.isEmpty() || VersionCompare.compare(remote.get().version(), current) <= 0) { + JOptionPane.showMessageDialog(parent, "Você já está na versão mais recente (" + current + ")."); + return; + } + int ok = JOptionPane.showConfirmDialog(parent, + "Versão atual: " + current + "\nDisponível: " + remote.get().version() + + "\nBaixar, substituir e reiniciar agora?", + "Atualização do Pitiupi", JOptionPane.YES_NO_OPTION); + if (ok == JOptionPane.YES_OPTION) { + apply(parent, client, base, remote.get(), jar.get()); + } + } catch (Exception ex) { + JOptionPane.showMessageDialog(parent, "Não foi possível verificar: " + messageOf(ex)); + } + } + }.execute(); + } + + private static void apply(Component parent, PluginRepositoryClient client, URI base, + RemotePluginEntry entry, Path jar) { + new SwingWorker() { + @Override protected Void doInBackground() throws Exception { + Path tmp = client.download(base, entry, PitiupiHome.get().cacheDirectory()); + try { + verifyIsPitiupi(tmp); + Path next = jar.resolveSibling(jar.getFileName() + ".new"); + Files.copy(tmp, next, StandardCopyOption.REPLACE_EXISTING); + Files.copy(jar, jar.resolveSibling(jar.getFileName() + ".bak"), + StandardCopyOption.REPLACE_EXISTING); + Files.move(next, jar, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); + } finally { + Files.deleteIfExists(tmp); + } + return null; + } + @Override protected void done() { + try { + get(); + ApplicationRestarter.restart(); + } catch (Exception ex) { + JOptionPane.showMessageDialog(parent, "Falha ao atualizar: " + messageOf(ex) + + "\nA versão antiga foi mantida (backup: " + jar.getFileName() + ".bak)."); + } + } + }.execute(); + } + + private static void verifyIsPitiupi(Path jar) throws Exception { + try (JarFile jf = new JarFile(jar.toFile())) { + Manifest m = jf.getManifest(); + String main = m == null ? null : m.getMainAttributes().getValue("Main-Class"); + if (!"pitiupi.Main".equals(main)) { + throw new PitiupiException(ErrorCode.UPDATE_FAILED, "O arquivo baixado não é um Pitiupi."); + } + } + } + + private static String messageOf(Exception ex) { + Throwable t = ex.getCause() != null ? ex.getCause() : ex; + return t instanceof PitiupiException pe ? pe.getUserMessage() : String.valueOf(t.getMessage()); + } +} diff --git a/pitiupi/src/pitiupi/presentation/shell/ShellMenuBar.java b/pitiupi/src/pitiupi/presentation/shell/ShellMenuBar.java index 9b49baa..5a442c9 100644 --- a/pitiupi/src/pitiupi/presentation/shell/ShellMenuBar.java +++ b/pitiupi/src/pitiupi/presentation/shell/ShellMenuBar.java @@ -23,6 +23,9 @@ public final class ShellMenuBar extends JMenuBar { JMenuItem about = new JMenuItem("Sobre"); about.addActionListener(e -> new AboutDialog().setVisible(true)); help.add(about); + JMenuItem update = new JMenuItem("Verificar atualização…"); + update.addActionListener(e -> SelfUpdater.checkAndPrompt(owner)); + help.add(update); add(help); } }