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
;
22 * Task action for {@link DeveloperNoteTask}.
26 class DeveloperNoteTaskAction
27 implements Action
<Task
>
34 @SuppressWarnings("HttpUrlsUsage")
36 public void execute(Task __task
)
39 LocalDateTime now
= LocalDateTime
.now();
40 String filePath
= DeveloperNoteTask
.__blogFilePath(now
.toLocalDate());
41 __DeveloperNoteSession__ session
= new __DeveloperNoteSession__(
44 // Load pre-existing blog
45 FossilExe exe
= FossilExe
.instance();
46 byte[] content
= exe
.unversionCatBytes(filePath
);
47 boolean doCreate
= (content
== null);
49 content
= DeveloperNoteTask
.__template(now
);
50 session
._content
= content
;
53 try (SimpleHTTPServer
<__DeveloperNoteSession__
> server
=
54 new SimpleHTTPServer
<>(
57 // Note on where to get it
58 String url
= String
.format("http://%s:%d/", server
.hostname
,
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
70 // Stop the loop if we were interrupted
71 if (Thread
.interrupted())
74 // Otherwise, wait for another packet
75 if (!server
.next(DeveloperNoteTask
::__httpHandler
))
78 catch (SimpleHTTPProtocolException e
)
84 // Problem with the server
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
99 NoteCalendarGenerator
.generateAndStore(exe
);
101 catch (IOException e
)
103 throw new RuntimeException("Could not generate calendar.", e
);