|
| 1 | +require 'rubygems' |
| 2 | +require 'bundler/setup' |
| 3 | +Bundler.require(:default) |
| 4 | + |
| 5 | +require 'pp' |
| 6 | + |
| 7 | +# ------------------------------------------------------------------------------------------------------------ |
| 8 | +# This script scans all patterns in /patterns/1-initial and /patterns/2-structured. |
| 9 | +# Based on the number of Known Instances in the pattern, it suggests which patterns that might be ready to be leveled-up. |
| 10 | +# |
| 11 | +# The number of Known Instances are only one of the [requirement](https://github.com/InnerSourceCommons/InnerSourcePatterns/blob/main/meta/contributor-handbook.md#requirements-level-2---structured) |
| 12 | +# for our patterns to reach the next level. Therefore reading the pattern and the level requirements in detail is still required |
| 13 | +# to decide whether or not a pattern can be pushed to the next level. |
| 14 | + |
| 15 | +# NOTE: This script and `/book/scripts/generate_toc.rb` have some overlap in how they are parsing markdown. |
| 16 | +# However the overlap seemed minimal, so I opted not to do any deduplication of code. |
| 17 | +# ------------------------------------------------------------------------------------------------------------ |
| 18 | + |
| 19 | +# Count Known Instances in a pattern |
| 20 | +# - return 0 if the section does not exist, or does not contain the expected list structure |
| 21 | +# - return <count of Known Instances> |
| 22 | +def count_known_instances(file) |
| 23 | + section_nodes = collect_section_nodes(file, "Known Instances") |
| 24 | + list_nodes = [] |
| 25 | + # pick the first list in the "Known Instances" section, and return the number of elements in that list. |
| 26 | + # CAUTION: this assumes a certain structure across all patterns. Therefore fairly brittle. |
| 27 | + list_nodes = section_nodes.select {|n| n.type == :list} |
| 28 | + |
| 29 | + known_instances_count = 0 |
| 30 | + known_instances_count = list_nodes.first.count if !list_nodes.first.nil? |
| 31 | + |
| 32 | + return known_instances_count |
| 33 | +end |
| 34 | + |
| 35 | +def collect_section_nodes(file, section_title) |
| 36 | + markdown = open(file).readlines().join |
| 37 | + doc = CommonMarker.render_doc(markdown) |
| 38 | + |
| 39 | + title_found = false |
| 40 | + section_nodes = [] |
| 41 | + |
| 42 | + doc.walk do |node| |
| 43 | + if node.type == :header |
| 44 | + if title_found == false |
| 45 | + node.each do |subnode| |
| 46 | + if subnode.type == :text and subnode.string_content == section_title |
| 47 | + title_found = true |
| 48 | + end |
| 49 | + end |
| 50 | + # stop the recursion once the next header is reached |
| 51 | + # TODO: is this correct, or should we check if this is another `##` header, rather than any header? |
| 52 | + else |
| 53 | + break |
| 54 | + end |
| 55 | + # once the title has been found, collect all nodes up to the next header |
| 56 | + elsif title_found == true |
| 57 | + section_nodes << node |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + return section_nodes |
| 62 | +end |
| 63 | + |
| 64 | + |
| 65 | +# Main block |
| 66 | + |
| 67 | +puts "## Initial => Structured" |
| 68 | +puts "## 1-Initial patterns primed for upgrade to 2-Structured (based on Known Instances only)" |
| 69 | +l1_patterns = Dir["../../patterns/1-initial/*.md"] |
| 70 | + |
| 71 | +l1_patterns.each do |file| |
| 72 | + known_instances_count = count_known_instances(file) |
| 73 | + puts "#{known_instances_count} | #{file}" if known_instances_count >= 1 |
| 74 | +end |
| 75 | + |
| 76 | +puts "\n" |
| 77 | +puts "## Structured => Validated" |
| 78 | +puts "## 2-Structured patterns primed for upgrade to 3-Validated (based on Known Instances only)" |
| 79 | +l2_patterns = Dir["../../patterns/2-structured/*.md", "../../patterns/2-structured/project-setup/*.md"] |
| 80 | + |
| 81 | +l2_patterns.each do |file| |
| 82 | + known_instances_count = count_known_instances(file) |
| 83 | + puts "#{known_instances_count} | #{file}" if known_instances_count >= 3 |
| 84 | +end |
0 commit comments