-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathgeneratesocialqueue.java
More file actions
245 lines (216 loc) · 9.11 KB
/
Copy pathgeneratesocialqueue.java
File metadata and controls
245 lines (216 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25
//DEPS com.fasterxml.jackson.core:jackson-databind:2.18.3
//DEPS com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.18.3
//DEPS org.yaml:snakeyaml:2.4
import module java.base;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
/**
* Reconcile the pending social queue or generate a tweet draft for one pattern.
*
* Default behavior preserves pending order, prunes deleted patterns, and appends
* new patterns. Use --file content/category/slug.yaml to write one tweet draft,
* or --reshuffle to begin a fresh cycle containing every pattern.
*/
static final String CONTENT_DIR = "content";
static final String SOCIAL_DIR = "social";
static final String QUEUE_FILE = SOCIAL_DIR + "/queue.txt";
static final String TWEETS_DIR = SOCIAL_DIR + "/tweets";
static final String STATE_FILE = SOCIAL_DIR + "/state.yaml";
static final String BASE_URL = "https://javaevolved.dev";
static final int MAX_TWEET_LENGTH = 280;
static final ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory());
static final ObjectMapper JSON_MAPPER = new ObjectMapper();
static final ObjectMapper YAML_WRITER = new ObjectMapper(
new YAMLFactory()
.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
.enable(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE)
);
record PatternInfo(String category, String slug, String title, String summary,
String oldApproach, String modernApproach, String jdkVersion) {
String key() { return category + "/" + slug; }
}
void main(String... args) throws Exception {
var allPatterns = scanContentFiles();
System.out.println("Found " + allPatterns.size() + " patterns in content/");
if (args.length == 2 && args[0].equals("--file")) {
var file = Path.of(args[1]).normalize();
var pattern = readPattern(file);
if (!allPatterns.containsKey(pattern.key())) {
throw new IllegalArgumentException("Not a registered content pattern: " + file);
}
writeTweetDraft(pattern);
return;
}
if (args.length > 1 || (args.length == 1 && !args[0].equals("--reshuffle"))) {
throw new IllegalArgumentException(
"Usage: generatesocialqueue.java [--reshuffle | --file content/category/slug.yaml]");
}
var state = loadState();
var posted = loadPostedKeys(state).stream()
.filter(allPatterns::containsKey)
.collect(Collectors.toCollection(LinkedHashSet::new));
var pending = loadExistingQueue().stream()
.filter(allPatterns::containsKey)
.filter(key -> !posted.contains(key))
.collect(Collectors.toCollection(LinkedHashSet::new));
if (args.length == 1) {
pending.clear();
posted.clear();
var keys = new ArrayList<>(allPatterns.keySet());
Collections.shuffle(keys);
pending.addAll(keys);
System.out.println("Started a fresh shuffled cycle");
} else if (pending.isEmpty() && posted.containsAll(allPatterns.keySet())) {
posted.clear();
var keys = new ArrayList<>(allPatterns.keySet());
Collections.shuffle(keys);
pending.addAll(keys);
System.out.println("Completed cycle; started a fresh shuffled cycle");
} else {
var known = new LinkedHashSet<>(posted);
known.addAll(pending);
var newKeys = allPatterns.keySet().stream()
.filter(key -> !known.contains(key))
.collect(Collectors.toCollection(ArrayList::new));
Collections.shuffle(newKeys);
pending.addAll(newKeys);
if (!newKeys.isEmpty()) {
System.out.println("Appended " + newKeys.size() + " new patterns: " + newKeys);
}
}
Files.createDirectories(Path.of(SOCIAL_DIR));
Files.writeString(Path.of(QUEUE_FILE), String.join("\n", pending) + "\n");
state.put("postedKeys", new ArrayList<>(posted));
YAML_WRITER.writerWithDefaultPrettyPrinter().writeValue(Path.of(STATE_FILE).toFile(), state);
System.out.println("Queue reconciled: " + pending.size() + " pending, " + posted.size() + " posted");
}
Map<String, PatternInfo> scanContentFiles() throws Exception {
var patterns = new LinkedHashMap<String, PatternInfo>();
var contentDir = Path.of(CONTENT_DIR);
try (var categories = Files.list(contentDir)) {
for (var catDir : categories.filter(Files::isDirectory).sorted().toList()) {
var category = catDir.getFileName().toString();
try (var files = Files.list(catDir)) {
for (var file : files.filter(f -> isContentFile(f)).sorted().toList()) {
var info = readPattern(file);
if (!info.category().equals(category)) {
throw new IllegalArgumentException(
file + " category must match its parent directory");
}
patterns.put(info.key(), info);
}
}
}
}
return patterns;
}
PatternInfo readPattern(Path file) throws Exception {
if (!Files.isRegularFile(file) || !isContentFile(file)) {
throw new IllegalArgumentException("Pattern file does not exist: " + file);
}
var node = file.getFileName().toString().endsWith(".json")
? JSON_MAPPER.readTree(file.toFile())
: YAML_MAPPER.readTree(file.toFile());
return new PatternInfo(
node.path("category").asText(),
node.path("slug").asText(),
node.path("title").asText(),
node.path("summary").asText(),
node.path("oldApproach").asText(),
node.path("modernApproach").asText(),
node.path("jdkVersion").asText()
);
}
boolean isContentFile(Path p) {
var name = p.getFileName().toString();
return name.endsWith(".yaml") || name.endsWith(".yml") || name.endsWith(".json");
}
List<String> loadExistingQueue() throws Exception {
var path = Path.of(QUEUE_FILE);
if (!Files.exists(path)) return List.of();
return Files.readAllLines(path).stream()
.map(String::strip)
.filter(s -> !s.isEmpty())
.toList();
}
@SuppressWarnings("unchecked")
Map<String, Object> loadState() throws Exception {
var path = Path.of(STATE_FILE);
if (Files.exists(path)) {
Map<String, Object> state = YAML_MAPPER.readValue(path.toFile(), LinkedHashMap.class);
if (!state.containsKey("postedKeys")) {
throw new IllegalArgumentException(
"Existing social/state.yaml must define postedKeys; migrate it before reconciliation");
}
return state;
}
var state = new LinkedHashMap<String, Object>();
state.put("lastPostedKey", null);
state.put("lastTweetId", null);
state.put("lastPostedAt", null);
state.put("postedKeys", new ArrayList<String>());
return state;
}
List<String> loadPostedKeys(Map<String, Object> state) {
var value = state.get("postedKeys");
if (!(value instanceof List<?> values)
|| values.stream().anyMatch(item -> !(item instanceof String))) {
throw new IllegalArgumentException("social/state.yaml postedKeys must be a string list");
}
return values.stream().map(String.class::cast).toList();
}
void writeTweetDraft(PatternInfo pattern) throws Exception {
var tweet = buildTweet(pattern);
if (tweet.length() > MAX_TWEET_LENGTH) {
tweet = buildTweetTruncated(pattern);
}
if (tweet.length() > MAX_TWEET_LENGTH) {
throw new IllegalArgumentException(
pattern.key() + " tweet is " + tweet.length() + " characters");
}
var path = Path.of(TWEETS_DIR, pattern.category(), pattern.slug() + ".yaml");
Files.createDirectories(path.getParent());
YAML_WRITER.writerWithDefaultPrettyPrinter()
.writeValue(path.toFile(), Map.of("text", tweet));
System.out.println("Wrote " + path + " (" + tweet.length() + " characters)");
}
String buildTweet(PatternInfo p) {
return """
☕ %s
%s
%s → %s (JDK %s+)
🔗 %s/%s/%s.html
#Java #JavaEvolved""".formatted(
p.title(), p.summary(),
p.oldApproach(), p.modernApproach(), p.jdkVersion(),
BASE_URL, p.category(), p.slug()
).stripIndent().strip();
}
String buildTweetTruncated(PatternInfo p) {
// Calculate budget: total minus everything except summary
var template = """
☕ %s
%s
%s → %s (JDK %s+)
🔗 %s/%s/%s.html
#Java #JavaEvolved""".stripIndent().strip();
var withoutSummary = template.formatted(
p.title(), "",
p.oldApproach(), p.modernApproach(), p.jdkVersion(),
BASE_URL, p.category(), p.slug()
);
int budget = MAX_TWEET_LENGTH - withoutSummary.length();
var summary = p.summary();
if (summary.length() > budget && budget > 3) {
summary = summary.substring(0, budget - 1) + "…";
}
return template.formatted(
p.title(), summary,
p.oldApproach(), p.modernApproach(), p.jdkVersion(),
BASE_URL, p.category(), p.slug()
);
}