Cherry pick the banglets and such from wip-l1summercoat, this will be the basis for...
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / general / DeveloperNoteTaskAction.java
blob59311c5eee05dc86aea3b3c5fa50584426a6dd89
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.plugin.general;
12 import cc.squirreljme.plugin.util.FossilExe;
13 import cc.squirreljme.plugin.util.NoteCalendarGenerator;
14 import cc.squirreljme.plugin.util.SimpleHTTPProtocolException;
15 import cc.squirreljme.plugin.util.SimpleHTTPServer;
16 import java.io.IOException;
17 import java.time.LocalDateTime;
18 import org.gradle.api.Action;
19 import org.gradle.api.Task;
21 /**
22 * Task action for {@link DeveloperNoteTask}.
24 * @since 2022/07/10
26 class DeveloperNoteTaskAction
27 implements Action<Task>
29 /**
30 * {@inheritDoc}
32 * @since 2022/07/10
34 @SuppressWarnings("HttpUrlsUsage")
35 @Override
36 public void execute(Task __task)
38 // Setup session
39 LocalDateTime now = LocalDateTime.now();
40 String filePath = DeveloperNoteTask.__blogFilePath(now.toLocalDate());
41 __DeveloperNoteSession__ session = new __DeveloperNoteSession__(
42 filePath);
44 // Load pre-existing blog
45 FossilExe exe = FossilExe.instance();
46 byte[] content = exe.unversionCatBytes(filePath);
47 boolean doCreate = (content == null);
48 if (doCreate)
49 content = DeveloperNoteTask.__template(now);
50 session._content = content;
52 // Open server
53 try (SimpleHTTPServer<__DeveloperNoteSession__> server =
54 new SimpleHTTPServer<>(
55 session))
57 // Note on where to get it
58 String url = String.format("http://%s:%d/", server.hostname,
59 server.port);
60 __task.getLogger().lifecycle("Editing " + filePath);
61 __task.getLogger().lifecycle("Server opened at " + url);
63 // Launch a web browser
64 DeveloperNoteTask.__launchBrowser(__task, url);
66 // Continuous handling loop
67 for (;;)
68 try
70 // Stop the loop if we were interrupted
71 if (Thread.interrupted())
72 break;
74 // Otherwise, wait for another packet
75 if (!server.next(DeveloperNoteTask::__httpHandler))
76 break;
78 catch (SimpleHTTPProtocolException e)
80 e.printStackTrace();
84 // Problem with the server
85 catch (IOException e)
87 e.printStackTrace();
88 throw new RuntimeException("Server read/write error.", e);
91 // Store the note in the un-versioned space, but only if saved
92 if (session._saveCount > 0)
93 exe.unversionStoreBytes(filePath, session._content);
95 // Recreate the calendar
96 if (doCreate)
97 try
99 NoteCalendarGenerator.generateAndStore(exe);
101 catch (IOException e)
103 throw new RuntimeException("Could not generate calendar.", e);