okf 1.10.0 → 1.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +294 -0
- data/README.md +310 -445
- data/lib/okf/bundle/folder.rb +24 -5
- data/lib/okf/bundle/linter.rb +1 -1
- data/lib/okf/bundle/search/index.rb +13 -3
- data/lib/okf/bundle/search.rb +93 -13
- data/lib/okf/bundle/skeleton.rb +241 -0
- data/lib/okf/bundle.rb +19 -14
- data/lib/okf/cli/catalog.rb +6 -6
- data/lib/okf/cli/command.rb +241 -14
- data/lib/okf/cli/dirs.rb +118 -0
- data/lib/okf/cli/files.rb +2 -2
- data/lib/okf/cli/graph.rb +115 -9
- data/lib/okf/cli/index.rb +67 -25
- data/lib/okf/cli/loose.rb +2 -2
- data/lib/okf/cli/registry.rb +152 -15
- data/lib/okf/cli/render.rb +3 -2
- data/lib/okf/cli/search.rb +33 -2
- data/lib/okf/cli/server.rb +16 -5
- data/lib/okf/cli/stats.rb +36 -11
- data/lib/okf/cli/tags.rb +29 -7
- data/lib/okf/cli/types.rb +1 -1
- data/lib/okf/cli.rb +10 -6
- data/lib/okf/registry.rb +351 -20
- data/lib/okf/render/graph/template.html.erb +504 -103
- data/lib/okf/render/graph.rb +27 -3
- data/lib/okf/server/app.rb +74 -3
- data/lib/okf/server/hub.rb +40 -30
- data/lib/okf/skill/SKILL.md +17 -10
- data/lib/okf/skill/playbooks/consume.md +3 -3
- data/lib/okf/skill/playbooks/maintain.md +4 -3
- data/lib/okf/skill/playbooks/menu.md +3 -2
- data/lib/okf/skill/playbooks/refine.md +30 -3
- data/lib/okf/skill/playbooks/search.md +7 -7
- data/lib/okf/skill/reference/cli.md +171 -32
- data/lib/okf/version.rb +1 -1
- data/lib/okf.rb +10 -0
- metadata +21 -8
data/lib/okf/cli/command.rb
CHANGED
|
@@ -118,13 +118,15 @@ module OKF
|
|
|
118
118
|
# reproduced on the CLI so an agent can read the same knowledge without one.
|
|
119
119
|
# Each prints a scannable human view by default and machine JSON with --json;
|
|
120
120
|
# all are advisory reads (exit 0). They share OKF::Bundle#catalog for their data,
|
|
121
|
-
# and (with `types`) narrow through the same --type/--
|
|
121
|
+
# and (with `types`) narrow through the same --type/--dir/--tag filters the
|
|
122
122
|
# server UI offers, so browser and CLI can answer the same questions.
|
|
123
123
|
#
|
|
124
|
-
# ── their shared --type/--
|
|
124
|
+
# ── their shared --type/--dir/--tag narrowing ──
|
|
125
125
|
# Each view takes the filters orthogonal to it (tags can't filter by tag).
|
|
126
|
-
# Matching is case-insensitive and
|
|
127
|
-
# the
|
|
126
|
+
# Matching is case-insensitive; --type and --tag are exact, --dir is a prefix
|
|
127
|
+
# over the whole path (see #under_dir?). The bundle root is `.`, spellable
|
|
128
|
+
# `root` so no shell quoting is needed. --area is --dir's deprecated
|
|
129
|
+
# predecessor and keeps its old first-segment-only behavior.
|
|
128
130
|
|
|
129
131
|
# The shared back half of `tags` and `types`: load, narrow, print.
|
|
130
132
|
def print_inverted_index(dir, label, key, plural, options)
|
|
@@ -177,27 +179,187 @@ module OKF
|
|
|
177
179
|
|
|
178
180
|
def filter_flags(parser, options, *keys)
|
|
179
181
|
parser.on("--type TYPE", "only concepts of this type") { |v| options[:type] = v } if keys.include?(:type)
|
|
180
|
-
|
|
182
|
+
if keys.include?(:area)
|
|
183
|
+
parser.on("--dir PATH", "only concepts in this directory or below it",
|
|
184
|
+
"(`root` — or `.` — for the bundle root)") { |v| options[:dir] = v }
|
|
185
|
+
parser.on("--area AREA", "deprecated: use --dir (matches the first path segment only)") do |v|
|
|
186
|
+
options[:area] = v
|
|
187
|
+
deprecated("--area", "--dir")
|
|
188
|
+
end
|
|
189
|
+
end
|
|
181
190
|
parser.on("--tag TAG", "only concepts carrying this tag") { |v| options[:tag] = v } if keys.include?(:tag)
|
|
182
191
|
end
|
|
183
192
|
|
|
184
193
|
def filter_entries(entries, options)
|
|
185
194
|
entries.select do |entry|
|
|
186
195
|
(options[:type].nil? || fold(entry[:type]) == fold(options[:type])) &&
|
|
187
|
-
(options[:area].nil? || fold(entry[:
|
|
196
|
+
(options[:area].nil? || fold(entry[:top_dir]) == fold_area(options[:area])) &&
|
|
197
|
+
(options[:dir].nil? || under_dir?(entry[:dir], options[:dir])) &&
|
|
188
198
|
(options[:tag].nil? || entry[:tags].any? { |tag| fold(tag) == fold(options[:tag]) })
|
|
189
199
|
end
|
|
190
200
|
end
|
|
191
201
|
|
|
202
|
+
# The one rule --dir is built on: a dir names itself and everything beneath
|
|
203
|
+
# it. `--dir foo` reaches foo/bar, `--dir foo/bar` narrows, and `--dir .`
|
|
204
|
+
# needs no special case at all — nothing starts with "./", so the root
|
|
205
|
+
# selects only what lives directly in it.
|
|
206
|
+
def under_dir?(entry_dir, wanted)
|
|
207
|
+
entry = fold(entry_dir)
|
|
208
|
+
path = fold_dir(wanted)
|
|
209
|
+
entry == path || entry.start_with?("#{path}/")
|
|
210
|
+
end
|
|
211
|
+
|
|
192
212
|
def fold(value)
|
|
193
213
|
value.to_s.downcase
|
|
194
214
|
end
|
|
195
215
|
|
|
196
216
|
def fold_area(value)
|
|
197
|
-
folded = fold(value)
|
|
217
|
+
folded = trim_slash(fold(value))
|
|
198
218
|
folded == "root" ? "(root)" : folded
|
|
199
219
|
end
|
|
200
220
|
|
|
221
|
+
# `.` is the stored spelling of the root everywhere; `root` is the one a
|
|
222
|
+
# shell needs no quoting for, and the only reason the two exist.
|
|
223
|
+
def fold_dir(value)
|
|
224
|
+
folded = trim_slash(fold(value))
|
|
225
|
+
folded.empty? || folded == "root" ? "." : folded
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# The human views print a directory with the slash that says it is one —
|
|
229
|
+
# `tables/`, `docs/api/` — so the flag has to accept the label the CLI
|
|
230
|
+
# itself just printed. Without this, pasting a row back into --dir matched
|
|
231
|
+
# nothing and exited 0: an empty answer that reads like a real one.
|
|
232
|
+
def trim_slash(value)
|
|
233
|
+
value.sub(%r{/+\z}, "")
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# The inverse of fold_dir: `.` is the stored spelling of the root and
|
|
237
|
+
# "(root)" the human one, and every grouped view keeps that split so a
|
|
238
|
+
# table and its --json never disagree about which spelling is the data.
|
|
239
|
+
# `slash:` adds the trailing slash the listing views use to say "directory"
|
|
240
|
+
# — the same one fold_dir now accepts back.
|
|
241
|
+
def dir_label(dir, slash: false)
|
|
242
|
+
return "(root)" if [ ".", "(root)" ].include?(dir)
|
|
243
|
+
|
|
244
|
+
slash ? "#{dir}/" : dir
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# How many path segments deep a directory sits. The root is 0.
|
|
248
|
+
def dir_depth(dir)
|
|
249
|
+
dir == "." ? 0 : dir.count("/") + 1
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# --depth N: how many directory levels below the *starting point* to keep,
|
|
253
|
+
# where the starting point is each --dir when one is given and the bundle
|
|
254
|
+
# root otherwise. Relative rather than absolute on purpose: `--dir a/b
|
|
255
|
+
# --depth 1` reads "a/b and one level under it" without the caller first
|
|
256
|
+
# working out how deep a/b already is — and the two flags then compose the
|
|
257
|
+
# way a reader descending a tree actually moves.
|
|
258
|
+
def depth_flag(parser, options)
|
|
259
|
+
parser.on("--depth N", "keep only this many directory levels below the",
|
|
260
|
+
"starting point (--dir when given, else the bundle root)") { |v| options[:depth] = v }
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# Checked here rather than with OptionParser's Integer coercion, which
|
|
264
|
+
# accepts "-1" and "0x2" and reports in its own words. Returns the exit
|
|
265
|
+
# status to hand back, or nil when the value is fine.
|
|
266
|
+
def depth_error(options)
|
|
267
|
+
raw = options[:depth]
|
|
268
|
+
return nil if raw.nil? || raw.to_s =~ /\A\d+\z/
|
|
269
|
+
|
|
270
|
+
usage_error("--depth takes a whole number of levels (got #{raw.inspect})")
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# The chain from the bundle root down to each --dir, so a branch is never
|
|
274
|
+
# shown adrift. On by default in the *directory* views (`index`, `dirs`):
|
|
275
|
+
# the map's job there is orientation, and a subtree printed with nothing
|
|
276
|
+
# above it has dropped the authored context that says what it is — the root
|
|
277
|
+
# index.md's prose first among it. Off with --no-ancestors, which restores
|
|
278
|
+
# the subtree alone.
|
|
279
|
+
#
|
|
280
|
+
# Deliberately not offered on the concept filters (search/catalog/files/…):
|
|
281
|
+
# there --dir narrows *concepts*, and a concept in `a/` is simply not in
|
|
282
|
+
# `a/b`. Same flag, one meaning, because it is asked about two different
|
|
283
|
+
# kinds of row.
|
|
284
|
+
def ancestors_flag(parser, options)
|
|
285
|
+
parser.on("--[no-]ancestors", "with --dir, also show the chain up to the root",
|
|
286
|
+
"so the branch is placed (default: yes)") { |v| options[:ancestors] = v }
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# Every proper ancestor of each --dir, root included. Empty unless --dir
|
|
290
|
+
# named something below the root: with no --dir the whole bundle is already
|
|
291
|
+
# the starting point, and `--dir .` has nothing above it.
|
|
292
|
+
#
|
|
293
|
+
# `known` is the map's own directory list, and a base outside it contributes
|
|
294
|
+
# no chain. Without that check `--dir typo` came back with the root — a
|
|
295
|
+
# chain to a place that does not exist, which reads as a partial answer to
|
|
296
|
+
# a query that in fact matched nothing.
|
|
297
|
+
#
|
|
298
|
+
# The deprecated --area gains no chain either: it is exact, and a deprecated
|
|
299
|
+
# flag that quietly answers with more than it used to is worse than one that
|
|
300
|
+
# is merely old.
|
|
301
|
+
# Matching folds case, but a row is found by its *stored* spelling, so the
|
|
302
|
+
# chain is walked folded and handed back in the map's own words. Returning
|
|
303
|
+
# the folded string instead dropped every ancestor a bundle spelled with a
|
|
304
|
+
# capital — the rows are selected with `include?`, which does not fold.
|
|
305
|
+
def ancestor_dirs(options, known)
|
|
306
|
+
return [] unless options[:ancestors]
|
|
307
|
+
|
|
308
|
+
stored = known.each_with_object({}) { |dir, out| out[fold(dir)] = dir }
|
|
309
|
+
Array(options[:dirs]).each_with_object([]) do |path, out|
|
|
310
|
+
base = fold_dir(path)
|
|
311
|
+
next unless stored.key?(base)
|
|
312
|
+
|
|
313
|
+
current = dir_parent(base)
|
|
314
|
+
while current
|
|
315
|
+
out << stored.fetch(current, current)
|
|
316
|
+
current = dir_parent(current)
|
|
317
|
+
end
|
|
318
|
+
end.uniq
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# nil above the root, so the walk above terminates on it rather than on ".".
|
|
322
|
+
def dir_parent(dir)
|
|
323
|
+
return nil if dir == "."
|
|
324
|
+
|
|
325
|
+
slash = dir.rindex("/")
|
|
326
|
+
slash ? dir[0, slash] : "."
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
# The directories a --dir/--depth pair selects, out of the map's own
|
|
330
|
+
# ordered list. Neither flag given keeps everything — these narrow a view,
|
|
331
|
+
# they do not define one. The ancestor chain is unioned on top by the
|
|
332
|
+
# caller, which is also what tells a row apart from context.
|
|
333
|
+
def select_dirs(dirs, options)
|
|
334
|
+
bases = Array(options[:dirs]).map { |path| fold_dir(path) }
|
|
335
|
+
depth = options[:depth]&.to_i
|
|
336
|
+
return dirs if bases.empty? && depth.nil?
|
|
337
|
+
# No --dir means the whole bundle is the starting point, which is *not*
|
|
338
|
+
# `--dir .`: that one selects the root alone, by the same prefix rule
|
|
339
|
+
# everything else here uses.
|
|
340
|
+
return dirs.select { |dir| dir_depth(dir) <= depth } if bases.empty?
|
|
341
|
+
|
|
342
|
+
dirs.select do |dir|
|
|
343
|
+
bases.any? do |base|
|
|
344
|
+
# --depth bounds the *descent*; the chain above is the ascent, and the
|
|
345
|
+
# two are separate axes. That is what keeps `--depth 0` meaning "the
|
|
346
|
+
# named directory alone" even while its chain is printed with it.
|
|
347
|
+
under_dir?(dir, base) && (depth.nil? || dir_depth(dir) - dir_depth(base) <= depth)
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
# A deprecated spelling still does what it always did — never silently
|
|
353
|
+
# something else — and says so once per run, on stderr so a --json
|
|
354
|
+
# consumer's stdout stays a clean machine substrate.
|
|
355
|
+
def deprecated(what, instead)
|
|
356
|
+
@deprecated ||= {}
|
|
357
|
+
return if @deprecated[what]
|
|
358
|
+
|
|
359
|
+
@deprecated[what] = true
|
|
360
|
+
@err.puts "warning: #{what} is deprecated, use #{instead}"
|
|
361
|
+
end
|
|
362
|
+
|
|
201
363
|
# Turn an inverted index ({ value => [id, …] }) into display rows ordered by
|
|
202
364
|
# count, narrowed to the concepts the active filters select; rows the narrowing
|
|
203
365
|
# empties drop. With no filters the index passes through whole.
|
|
@@ -212,7 +374,7 @@ module OKF
|
|
|
212
374
|
# The ids the filters select, resolved through the catalog metadata — or nil
|
|
213
375
|
# when no filter is active, meaning keep everything.
|
|
214
376
|
def filter_ids(folder, options)
|
|
215
|
-
return nil if options[:type].nil? && options[:area].nil? && options[:tag].nil?
|
|
377
|
+
return nil if options[:type].nil? && options[:area].nil? && options[:dir].nil? && options[:tag].nil?
|
|
216
378
|
|
|
217
379
|
filter_entries(folder.catalog, options).map { |entry| entry[:id] }
|
|
218
380
|
end
|
|
@@ -259,15 +421,58 @@ module OKF
|
|
|
259
421
|
# Parse options, then take zero or more bundle positionals (the multi-bundle
|
|
260
422
|
# server) — directories or @refs. Returns the resolved array (possibly
|
|
261
423
|
# empty), or nil (after reporting) so the caller returns 2.
|
|
262
|
-
def positional_dirs(parser, argv)
|
|
424
|
+
def positional_dirs(parser, argv, expand_groups: false)
|
|
263
425
|
parser.parse!(argv)
|
|
264
|
-
dirs =
|
|
426
|
+
dirs = if expand_groups
|
|
427
|
+
argv.flat_map { |arg| resolve_ref_expanding(arg) }
|
|
428
|
+
else
|
|
429
|
+
argv.map { |dir| resolve_ref(dir) }
|
|
430
|
+
end
|
|
265
431
|
dirs.include?(nil) ? nil : dirs
|
|
266
432
|
rescue OptionParser::ParseError => e
|
|
267
433
|
@err.puts e.message
|
|
268
434
|
nil
|
|
269
435
|
end
|
|
270
436
|
|
|
437
|
+
# Like #resolve_ref, but a group @ref fans out to its member directories —
|
|
438
|
+
# the multi-bundle expansion only `server` wants (single-bundle verbs reject
|
|
439
|
+
# a group in #resolve_registered). Always returns an array of dirs so the
|
|
440
|
+
# caller can flat_map, or nil (reported) to fail the run.
|
|
441
|
+
def resolve_ref_expanding(arg)
|
|
442
|
+
return [ resolve_ref(arg) ] unless arg.start_with?("@")
|
|
443
|
+
|
|
444
|
+
registry = load_registry
|
|
445
|
+
return [ nil ] unless registry
|
|
446
|
+
|
|
447
|
+
slug = OKF::Registry.normalize(arg[1..-1])
|
|
448
|
+
return [ resolve_ref(arg) ] if slug.empty? || registry.group?(slug).nil?
|
|
449
|
+
|
|
450
|
+
group_member_dirs(registry, slug)
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
# A group's member directories, in order, skipping ones whose directory has
|
|
454
|
+
# vanished with the same note `@all` gives — and populating +ref_slugs+ so the
|
|
455
|
+
# hub mounts each under its registered slug. nil (reported) when nothing
|
|
456
|
+
# readable is left, or on a hand-edited cycle.
|
|
457
|
+
def group_member_dirs(registry, slug)
|
|
458
|
+
dirs = []
|
|
459
|
+
registry.expand(slug).each do |entry|
|
|
460
|
+
if File.directory?(entry.path)
|
|
461
|
+
ref_slugs[entry.path] = entry.slug
|
|
462
|
+
dirs << entry.path
|
|
463
|
+
else
|
|
464
|
+
skip_registered(entry)
|
|
465
|
+
end
|
|
466
|
+
end
|
|
467
|
+
return dirs unless dirs.empty?
|
|
468
|
+
|
|
469
|
+
@err.puts "error: @#{slug} resolves to no readable bundle (okf registry list)"
|
|
470
|
+
nil
|
|
471
|
+
rescue OKF::Error => e
|
|
472
|
+
@err.puts "error: #{e.message}"
|
|
473
|
+
nil
|
|
474
|
+
end
|
|
475
|
+
|
|
271
476
|
# "@slug" — or bare "@", the registry's default — names a registered bundle
|
|
272
477
|
# wherever a <dir> goes; anything else must be a directory on disk. A
|
|
273
478
|
# leading @ always means the registry (a directory literally named that way
|
|
@@ -290,15 +495,24 @@ module OKF
|
|
|
290
495
|
# only `server` and the `registry` verbs rescue one. Returns nil after
|
|
291
496
|
# reporting, so every caller returns 2.
|
|
292
497
|
def load_registry
|
|
293
|
-
|
|
294
|
-
OKF::Registry.load
|
|
498
|
+
open_registry
|
|
295
499
|
rescue OKF::Error => e
|
|
296
500
|
@err.puts "error: #{e.message}"
|
|
297
501
|
nil
|
|
298
502
|
end
|
|
299
503
|
|
|
300
|
-
#
|
|
301
|
-
#
|
|
504
|
+
# The registry a verb resolves against — the single seam that opts the CLI
|
|
505
|
+
# into discovery. `cwd: Dir.pwd` is what makes OKF::Registry.load walk up for
|
|
506
|
+
# a project-local .okf-registry.json; a library caller passing no cwd stays
|
|
507
|
+
# global-only. The registry subcommands and `server` open through here too,
|
|
508
|
+
# so a bare `okf server` inside a repo serves that repo's bundles.
|
|
509
|
+
def open_registry
|
|
510
|
+
require "okf/registry"
|
|
511
|
+
OKF::Registry.load(cwd: Dir.pwd)
|
|
512
|
+
end
|
|
513
|
+
|
|
514
|
+
# Resolve one @ref through the active registry — a discovered project-local
|
|
515
|
+
# one, else the global $OKF_HOME (default ~/.okf). The slug part is normalized
|
|
302
516
|
# exactly as registration normalized it, so @One finds the bundle
|
|
303
517
|
# registered from dir One — but never through #slugify's mint-a-name
|
|
304
518
|
# placeholder, so "@***" is a bad ref rather than whatever is slugged
|
|
@@ -324,6 +538,19 @@ module OKF
|
|
|
324
538
|
|
|
325
539
|
asked = ref[1..-1]
|
|
326
540
|
slug = OKF::Registry.normalize(asked)
|
|
541
|
+
|
|
542
|
+
# A group is a set, and this verb takes one bundle. Reading its first member
|
|
543
|
+
# would answer confidently about a bundle the user never singled out — the
|
|
544
|
+
# silent-wrong-answer shape the second-bundle rule already forbids — so it is
|
|
545
|
+
# exit 2, with the two verbs that *can* take a group named.
|
|
546
|
+
group = slug.empty? ? nil : registry.group?(slug)
|
|
547
|
+
if group
|
|
548
|
+
count = group.members.size
|
|
549
|
+
@err.puts "error: @#{slug} names a group of #{count} #{count == 1 ? "member" : "members"}; " \
|
|
550
|
+
"only `okf search` and `okf server` take a group"
|
|
551
|
+
return nil
|
|
552
|
+
end
|
|
553
|
+
|
|
327
554
|
entry = if asked.empty?
|
|
328
555
|
registry.default # bare "@"
|
|
329
556
|
elsif slug.empty?
|
data/lib/okf/cli/dirs.rb
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OKF
|
|
4
|
+
class CLI
|
|
5
|
+
# The bundle's directories — its clusters — and how many concepts live
|
|
6
|
+
# directly in each. The shape view: `index` reads a directory's contents,
|
|
7
|
+
# `dirs` reads the layout they hang off, which is the question `--dir` is
|
|
8
|
+
# answered against.
|
|
9
|
+
#
|
|
10
|
+
# `count` is *direct*, never cumulative: a dir's number is what lives in it,
|
|
11
|
+
# so the column sums to the bundle's concept count and an empty intermediate
|
|
12
|
+
# dir reads as the zero it is. `subtree` is the other half of that honesty —
|
|
13
|
+
# what `--dir <that row>` would return — because a direct count alone cannot
|
|
14
|
+
# say where the mass is once `--depth` truncates the listing: on a deep
|
|
15
|
+
# bundle the top-level rows are then all zeroes.
|
|
16
|
+
#
|
|
17
|
+
# Presentation only — every number comes off Bundle#directory_index, the same
|
|
18
|
+
# source `okf index` and the server's Index panel read. Advisory: exit 0.
|
|
19
|
+
class Dirs < Command
|
|
20
|
+
def self.id
|
|
21
|
+
:dirs
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.group
|
|
25
|
+
:read
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.help_rows
|
|
29
|
+
[
|
|
30
|
+
[ "dirs <dir|@slug> [--json] [--dir D] [--depth N]", "list the bundle's dirs (clusters) and their concept counts" ]
|
|
31
|
+
]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def call(argv)
|
|
35
|
+
options = { json: false, dirs: nil, depth: nil, ancestors: true }
|
|
36
|
+
parser = OptionParser.new do |o|
|
|
37
|
+
o.banner = "Usage: okf dirs <dir|@slug> [--dir PATH] [--depth N] [--json]"
|
|
38
|
+
json_flags(o, options, "emit the dirs as JSON")
|
|
39
|
+
projection_flags(o, options)
|
|
40
|
+
o.on("--dir PATH", "only this directory and the ones below it",
|
|
41
|
+
"(repeatable; `root` for the bundle root)") { |v| (options[:dirs] ||= []) << v }
|
|
42
|
+
depth_flag(o, options)
|
|
43
|
+
ancestors_flag(o, options)
|
|
44
|
+
help_flag(o)
|
|
45
|
+
end
|
|
46
|
+
dir = positional_dir(parser, argv) or return 2
|
|
47
|
+
bad_depth = depth_error(options)
|
|
48
|
+
return bad_depth if bad_depth
|
|
49
|
+
|
|
50
|
+
folder = OKF::Bundle::Folder.load(dir)
|
|
51
|
+
report_skipped(folder)
|
|
52
|
+
rows = select_rows(folder.directory_index, options)
|
|
53
|
+
return emit_list_json(dir, "dirs", rows, options, "total" => total(rows)) if options[:json]
|
|
54
|
+
|
|
55
|
+
print_dirs(dir, rows)
|
|
56
|
+
0
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
# The subtree counts come off the *whole* map, before any narrowing — a
|
|
62
|
+
# truncated view still has to report the real weight hanging below a row,
|
|
63
|
+
# which is the only reason the column exists.
|
|
64
|
+
def select_rows(entries, options)
|
|
65
|
+
subtree = subtree_counts(entries)
|
|
66
|
+
all_dirs = entries.map { |entry| entry[:dir] }
|
|
67
|
+
wanted = select_dirs(all_dirs, options)
|
|
68
|
+
chain = ancestor_dirs(options, all_dirs) - wanted
|
|
69
|
+
entries.select { |entry| wanted.include?(entry[:dir]) || chain.include?(entry[:dir]) }.map do |entry|
|
|
70
|
+
{ "dir" => entry[:dir], "ancestor" => chain.include?(entry[:dir]), "count" => entry[:count],
|
|
71
|
+
"subtree" => subtree[entry[:dir]], "subdirs" => entry[:subdirs] }
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Per dir, the concepts at or below it — defined as exactly what `--dir` on
|
|
76
|
+
# that row selects, so the number on the row and the flag can never
|
|
77
|
+
# disagree. Which is also why the root's subtree is its own direct count:
|
|
78
|
+
# `.` is a prefix of nothing, the same rule `--dir .` is built on.
|
|
79
|
+
def subtree_counts(entries)
|
|
80
|
+
entries.each_with_object({}) do |entry, out|
|
|
81
|
+
out[entry[:dir]] = entries.reduce(0) do |sum, other|
|
|
82
|
+
under_dir?(other[:dir], entry[:dir]) ? sum + other[:count] : sum
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The chain is context, not the answer, so it stays out of the total —
|
|
88
|
+
# which is what keeps a row's `subtree` equal to the total `--dir` on that
|
|
89
|
+
# row returns. `count` in the envelope is rows printed, chain included,
|
|
90
|
+
# because that is what it has always meant: how many rows came back.
|
|
91
|
+
def total(rows)
|
|
92
|
+
rows.reject { |row| row["ancestor"] }.map { |row| row["count"] }.reduce(0, :+)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def print_dirs(dir, rows)
|
|
96
|
+
@out.puts "Dirs — #{bundle_label(dir)}"
|
|
97
|
+
@out.puts
|
|
98
|
+
labels = rows.map { |row| "#{"↑ " if row["ancestor"]}#{dir_label(row["dir"])}" }
|
|
99
|
+
# The second column earns its place only where a dir actually nests. On a
|
|
100
|
+
# flat bundle it would repeat the first one down the page.
|
|
101
|
+
nested = rows.any? { |row| row["subtree"] != row["count"] }
|
|
102
|
+
unless rows.empty?
|
|
103
|
+
width = [ 3, *labels.map(&:length) ].max
|
|
104
|
+
@out.puts " #{"Dir".ljust(width)} Concepts#{" Subtree" if nested}"
|
|
105
|
+
rows.each_with_index do |row, i|
|
|
106
|
+
line = " #{labels[i].ljust(width)} #{row["count"].to_s.rjust(8)}"
|
|
107
|
+
line += " #{row["subtree"].to_s.rjust(7)}" if nested
|
|
108
|
+
@out.puts line
|
|
109
|
+
end
|
|
110
|
+
@out.puts
|
|
111
|
+
end
|
|
112
|
+
@out.puts " #{rows.size} #{pluralize(rows.size, "dir")} · #{total(rows)} #{pluralize(total(rows), "concept")}"
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
register(Dirs)
|
|
117
|
+
end
|
|
118
|
+
end
|
data/lib/okf/cli/files.rb
CHANGED
|
@@ -22,7 +22,7 @@ module OKF
|
|
|
22
22
|
def call(argv)
|
|
23
23
|
options = { json: false }
|
|
24
24
|
parser = OptionParser.new do |o|
|
|
25
|
-
o.banner = "Usage: okf files <dir|@slug> [--type T] [--
|
|
25
|
+
o.banner = "Usage: okf files <dir|@slug> [--type T] [--dir D] [--tag T] [--json]"
|
|
26
26
|
json_flags(o, options, "emit the file tree as JSON")
|
|
27
27
|
projection_flags(o, options)
|
|
28
28
|
filter_flags(o, options, :type, :area, :tag)
|
|
@@ -47,7 +47,7 @@ module OKF
|
|
|
47
47
|
entries.group_by { |entry| entry[:dir] }.sort_by(&:first).each do |folder, group|
|
|
48
48
|
width = group.map { |entry| File.basename("#{entry[:id]}.md").length }.max
|
|
49
49
|
@out.puts
|
|
50
|
-
@out.puts " #{folder
|
|
50
|
+
@out.puts " #{dir_label(folder, slash: true)}"
|
|
51
51
|
group.each do |entry|
|
|
52
52
|
@out.puts " #{File.basename("#{entry[:id]}.md").ljust(width)} #{entry[:title]}"
|
|
53
53
|
end
|
data/lib/okf/cli/graph.rb
CHANGED
|
@@ -15,22 +15,26 @@ module OKF
|
|
|
15
15
|
|
|
16
16
|
def self.help_rows
|
|
17
17
|
[
|
|
18
|
-
[ "graph <dir|@slug> [--json] [--minimal] [--hubs]", "print the knowledge graph" ]
|
|
18
|
+
[ "graph <dir|@slug> [--json] [--minimal] [--hubs]", "print the knowledge graph" ],
|
|
19
|
+
[ "graph <dir|@slug> --traffic [--cut N]", "directories and the link traffic between them" ]
|
|
19
20
|
]
|
|
20
21
|
end
|
|
21
22
|
|
|
22
23
|
def call(argv)
|
|
23
|
-
options = { json: false, minimal: false, body: true, hubs: false }
|
|
24
|
+
options = { json: false, minimal: false, body: true, hubs: false, traffic: false, cut: nil }
|
|
24
25
|
parser = OptionParser.new do |o|
|
|
25
|
-
o.banner = "Usage: okf graph <dir|@slug> [--json] [--minimal] [--no-body] [--hubs]"
|
|
26
|
+
o.banner = "Usage: okf graph <dir|@slug> [--json] [--minimal] [--no-body] [--hubs] [--traffic]"
|
|
26
27
|
json_flags(o, options, "emit nodes and edges as JSON")
|
|
27
28
|
o.on("--minimal", "leanest nodes (id + title); adds type/tag indexes") { options[:minimal] = true }
|
|
28
29
|
o.on("--[no-]body", "include each concept's body (default: yes)") { |v| options[:body] = v }
|
|
29
|
-
o.on("--hubs", "rank concepts by inbound links, with the source
|
|
30
|
+
o.on("--hubs", "rank concepts by inbound links, with the source top-level dirs") { options[:hubs] = true }
|
|
31
|
+
o.on("--traffic", "collapse concepts into their dirs; count the links between them") { options[:traffic] = true }
|
|
32
|
+
o.on("--cut N", Integer, "least arc weight to draw (default: fitted to the bundle)") { |v| options[:cut] = v }
|
|
30
33
|
help_flag(o)
|
|
31
34
|
end
|
|
32
35
|
dir = positional_dir(parser, argv) or return 2
|
|
33
36
|
|
|
37
|
+
return print_traffic(dir, options) if options[:traffic]
|
|
34
38
|
return print_hubs(dir, options) if options[:hubs]
|
|
35
39
|
|
|
36
40
|
folder = OKF::Bundle::Folder.load(dir)
|
|
@@ -52,16 +56,118 @@ module OKF
|
|
|
52
56
|
|
|
53
57
|
private
|
|
54
58
|
|
|
59
|
+
# `graph --traffic`: concepts collapse into the directory they live in, and
|
|
60
|
+
# the links between two directories collapse into one weighted arc. It is
|
|
61
|
+
# the reduction that pays, and the measurement says why: on a typical
|
|
62
|
+
# bundle three quarters of all links cross a directory boundary, and those
|
|
63
|
+
# aggregate roughly ten-to-one — 227 links became 14 arcs on the bundle
|
|
64
|
+
# this was built for.
|
|
65
|
+
#
|
|
66
|
+
# The other half of the view is the cohesion column, which is the reason
|
|
67
|
+
# `refine` wants this. `--hubs` measures concepts; refine's step-3
|
|
68
|
+
# judgements ("does this directory prune?", "concern or container?") are
|
|
69
|
+
# about *directories*, and nothing measured those. Cohesion is a
|
|
70
|
+
# directory's internal traffic over its total, so it reads directly:
|
|
71
|
+
# near-zero with heavy inbound is a shared primitive, heavy outbound with
|
|
72
|
+
# nothing coming back is an index behaving like a container, and high is a
|
|
73
|
+
# directory that genuinely holds together.
|
|
74
|
+
def print_traffic(dir, options)
|
|
75
|
+
folder = OKF::Bundle::Folder.load(dir)
|
|
76
|
+
skeleton = folder.skeleton
|
|
77
|
+
cut = options[:cut] || skeleton.suggested_cut
|
|
78
|
+
return usage_error("--cut must be 1 or more, got #{cut}") if cut < 1
|
|
79
|
+
|
|
80
|
+
report_skipped(folder)
|
|
81
|
+
arcs = OKF::Bundle::Skeleton.arcs_above(skeleton.arcs, cut)
|
|
82
|
+
rows = dir_traffic(skeleton)
|
|
83
|
+
if options[:json]
|
|
84
|
+
emit_json(bundle_head(dir).merge(
|
|
85
|
+
"cut" => cut, "fitted" => options[:cut].nil?, "dirs" => stringify_rows(rows),
|
|
86
|
+
"arcs" => stringify_rows(arcs), "total_arcs" => skeleton.arcs.size
|
|
87
|
+
))
|
|
88
|
+
else
|
|
89
|
+
@out.puts "Traffic — #{bundle_label(dir)} (#{skeleton.dirs.size} #{pluralize(skeleton.dirs.size, "dir")}, " \
|
|
90
|
+
"#{counted(arcs.size, skeleton.arcs.size, "arc")} at weight #{cut} or more)"
|
|
91
|
+
print_dir_rows(rows)
|
|
92
|
+
print_arc_rows(arcs)
|
|
93
|
+
end
|
|
94
|
+
0
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Each directory's link traffic, split three ways. Counted over *every*
|
|
98
|
+
# arc, never the cut ones: the cut decides what is drawn, and a measurement
|
|
99
|
+
# that moved when the picture was tidied would be worthless as evidence.
|
|
100
|
+
def dir_traffic(skeleton)
|
|
101
|
+
out = Hash.new(0)
|
|
102
|
+
into = Hash.new(0)
|
|
103
|
+
skeleton.arcs.each do |arc|
|
|
104
|
+
out[arc[:source]] += arc[:weight]
|
|
105
|
+
into[arc[:target]] += arc[:weight]
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
skeleton.dirs.map do |row|
|
|
109
|
+
total = row[:internal] + out[row[:dir]] + into[row[:dir]]
|
|
110
|
+
row.merge(out: out[row[:dir]], in: into[row[:dir]],
|
|
111
|
+
cohesion: total.zero? ? nil : (100.0 * row[:internal] / total).round)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# An empty bundle gets no table at all — a column header over nothing is a
|
|
116
|
+
# heading that promises rows. The arc list is the opposite case and prints
|
|
117
|
+
# regardless (see print_arc_rows); the difference is that an empty arc list
|
|
118
|
+
# is a fact about the *cut*, and an empty dir table is a fact about the
|
|
119
|
+
# bundle the count line has already stated.
|
|
120
|
+
#
|
|
121
|
+
# Sorted by cohesion ascending, so the directories with a case to answer
|
|
122
|
+
# come first — a table that leads with the healthy ones buries its finding
|
|
123
|
+
# under the rows nobody needed to read. A directory with no traffic at all
|
|
124
|
+
# has no ratio to report and prints `—` rather than a 0% it did not earn.
|
|
125
|
+
def print_dir_rows(rows)
|
|
126
|
+
return if rows.empty?
|
|
127
|
+
|
|
128
|
+
ordered = rows.sort_by { |row| [ row[:cohesion] || 999, row[:dir] ] }
|
|
129
|
+
@out.puts
|
|
130
|
+
labels = ordered.map { |row| dir_label(row[:dir]) }
|
|
131
|
+
width = [ 3, *labels.map(&:length) ].max
|
|
132
|
+
@out.puts " #{"Dir".ljust(width)} Concepts Internal Out In Cohesion"
|
|
133
|
+
ordered.each_with_index do |row, i|
|
|
134
|
+
@out.puts " #{labels[i].ljust(width)} #{row[:count].to_s.rjust(8)} #{row[:internal].to_s.rjust(8)} " \
|
|
135
|
+
"#{row[:out].to_s.rjust(4)} #{row[:in].to_s.rjust(4)} #{(row[:cohesion] ? "#{row[:cohesion]}%" : "—").rjust(8)}"
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# The arc list is the answer this mode exists for, so it prints even when
|
|
140
|
+
# the cut emptied it — a heading over nothing says "the cut was too tight",
|
|
141
|
+
# where silence reads as "the bundle has no cross-links".
|
|
142
|
+
def print_arc_rows(arcs)
|
|
143
|
+
@out.puts
|
|
144
|
+
@out.puts " Arcs"
|
|
145
|
+
return @out.puts " (none at this cut)" if arcs.empty?
|
|
146
|
+
|
|
147
|
+
width = arcs.map { |arc| dir_label(arc[:source]).length }.max
|
|
148
|
+
twidth = arcs.map { |arc| dir_label(arc[:target]).length }.max
|
|
149
|
+
arcs.each do |arc|
|
|
150
|
+
@out.puts " #{dir_label(arc[:source]).ljust(width)} → #{dir_label(arc[:target]).ljust(twidth)} ×#{arc[:weight]}"
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# The skeleton is symbol-keyed (it is a pure model, not a payload), and every
|
|
155
|
+
# other --json view in this CLI answers in strings. Converted at the edge, so
|
|
156
|
+
# the model stays the model.
|
|
157
|
+
def stringify_rows(rows)
|
|
158
|
+
rows.map { |row| stringify(row) }
|
|
159
|
+
end
|
|
160
|
+
|
|
55
161
|
# `graph --hubs`: the inbound ranking with each hub's links grouped by
|
|
56
|
-
# source
|
|
57
|
-
# inbound majority comes from outside its own
|
|
58
|
-
# --minimal/--no-body shape node payloads and change nothing here.
|
|
162
|
+
# source top-level dir — the "is this hub well-homed?" evidence. A hub whose
|
|
163
|
+
# inbound majority comes from outside its own top-level dir is a move
|
|
164
|
+
# candidate; --minimal/--no-body shape node payloads and change nothing here.
|
|
59
165
|
def print_hubs(dir, options)
|
|
60
166
|
folder = OKF::Bundle::Folder.load(dir)
|
|
61
167
|
hubs = folder.hubs
|
|
62
168
|
report_skipped(folder)
|
|
63
169
|
if options[:json]
|
|
64
|
-
rows = hubs.map { |row| { "id" => row[:id], "
|
|
170
|
+
rows = hubs.map { |row| { "id" => row[:id], "top_dir" => row[:top_dir], "inbound" => row[:inbound], "by_top_dir" => row[:by_top_dir] } }
|
|
65
171
|
emit_json(bundle_head(dir).merge("count" => hubs.size, "hubs" => rows))
|
|
66
172
|
else
|
|
67
173
|
@out.puts "Hubs — #{bundle_label(dir)} (#{counted(hubs.size, folder.concepts.size, "concept")} with inbound links)"
|
|
@@ -69,7 +175,7 @@ module OKF
|
|
|
69
175
|
width = hubs.map { |row| row[:id].length }.max || 0
|
|
70
176
|
dwidth = hubs.map { |row| row[:inbound].to_s.length }.max || 0
|
|
71
177
|
hubs.each do |row|
|
|
72
|
-
sources = row[:
|
|
178
|
+
sources = row[:by_top_dir].map { |top_dir, count| "#{top_dir} #{count}" }.join(", ")
|
|
73
179
|
@out.puts " #{row[:id].ljust(width)} ×#{row[:inbound].to_s.rjust(dwidth)} #{sources}"
|
|
74
180
|
end
|
|
75
181
|
end
|