|
1 | 1 | require 'filewatcher' |
2 | | -require 'date' # for use by templates |
| 2 | +require 'tty-markdown' |
3 | 3 |
|
4 | 4 | module Bashly |
5 | 5 | module Commands |
6 | 6 | class Render < Base |
7 | 7 | help 'Render the bashly data structure using cutsom templates' |
8 | 8 |
|
9 | 9 | usage 'bashly render SOURCE TARGET [options]' |
| 10 | + usage 'bashly render SOURCE --about' |
| 11 | + usage 'bashly render --list' |
10 | 12 | usage 'bashly render (-h|--help)' |
11 | 13 |
|
12 | 14 | param 'SOURCE', <<~HELP |
13 | 15 | An ID to an internal templates source, or a path to a custom templates directory. |
14 | 16 |
|
15 | | - A leading colon (:) denotes an internal ID. |
16 | | -
|
17 | | - Available IDs: |
18 | | - - :markdown - render markdown documents for each command. |
19 | | - - :mandoc - render man pages for each command. |
| 17 | + A leading colon (:) denotes an internal ID (see `--list`). |
20 | 18 | HELP |
21 | 19 |
|
22 | 20 | param 'TARGET', 'Output directory' |
23 | 21 |
|
24 | 22 | option '-w --watch', 'Watch bashly.yml and the templates source for changes and render on change' |
| 23 | + option '-l --list', 'Show list of built-in templates' |
| 24 | + option '-a --about', 'Show information about a given templates source' |
25 | 25 |
|
| 26 | + example 'bashly render --list' |
| 27 | + example 'bashly render :markdown --about' |
26 | 28 | example 'bashly render :markdown docs --watch' |
27 | 29 | example 'bashly render /path/to/templates ./out_path' |
28 | 30 |
|
29 | | - attr_reader :watching, :source, :target |
| 31 | + attr_reader :watching, :target, :source |
30 | 32 |
|
31 | 33 | def run |
32 | | - @source = source_path |
33 | | - @target = args['TARGET'] |
34 | | - @watching = args['--watch'] |
35 | | - |
36 | | - render |
37 | | - watch if watching |
| 34 | + if args['--list'] then show_list |
| 35 | + elsif args['--about'] then show_about |
| 36 | + else |
| 37 | + start_render |
| 38 | + end |
38 | 39 | end |
39 | 40 |
|
40 | 41 | private |
41 | 42 |
|
42 | | - # This method is the single DSL method for the render script |
43 | | - def save(filename, content) |
44 | | - File.deep_write filename, content |
45 | | - say "g`saved` #{filename}" |
| 43 | + def show_list |
| 44 | + RenderSource.internal.each_value do |source| |
| 45 | + say "g`:#{source.selector.to_s.ljust 10}` #{source.summary}" |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + def show_about |
| 50 | + puts TTY::Markdown.parse(render_source.readme) |
46 | 51 | end |
47 | 52 |
|
48 | | - def watch |
49 | | - say "g`watching`\n" |
| 53 | + def render_source |
| 54 | + @render_source ||= begin |
| 55 | + source = RenderSource.new selector |
| 56 | + raise "Invalid render source: #{args['SOURCE']}" unless source.exist? |
50 | 57 |
|
51 | | - Filewatcher.new(watchables).watch do |
52 | | - reset |
53 | | - render |
54 | | - rescue Bashly::ConfigurationError => e |
55 | | - say! "rib` #{e.class} `\n#{e.message}" |
56 | | - ensure |
57 | | - say "g`waiting`\n" |
| 58 | + source |
58 | 59 | end |
59 | 60 | end |
60 | 61 |
|
61 | | - def render |
62 | | - with_valid_config { instance_eval render_script } |
63 | | - end |
| 62 | + def selector |
| 63 | + return args['SOURCE'] unless args['SOURCE'].start_with? ':' |
64 | 64 |
|
65 | | - def reset |
66 | | - @config = nil |
67 | | - @config_validator = nil |
68 | | - @command = nil |
| 65 | + args['SOURCE'][1..].to_sym |
69 | 66 | end |
70 | 67 |
|
71 | | - def command |
72 | | - @command ||= Script::Command.new config |
| 68 | + def start_render |
| 69 | + @target = args['TARGET'] |
| 70 | + @watching = args['--watch'] |
| 71 | + |
| 72 | + render |
| 73 | + watch if watching |
73 | 74 | end |
74 | 75 |
|
75 | | - def watchables |
76 | | - @watchables ||= [Settings.config_path, source] |
| 76 | + def render |
| 77 | + render_source.render target |
77 | 78 | end |
78 | 79 |
|
79 | | - def source_path |
80 | | - result = args['SOURCE'] |
| 80 | + def watch |
| 81 | + say "g`watching`\n" |
81 | 82 |
|
82 | | - if result.start_with? ':' |
83 | | - id = result[1..] |
84 | | - result = asset "libraries/render/#{id}" |
| 83 | + Filewatcher.new(watchables).watch do |
| 84 | + render |
| 85 | + say "g`waiting`\n" |
85 | 86 | end |
86 | | - |
87 | | - return result if Dir.exist? result |
88 | | - |
89 | | - raise "Invalid source.\nDirectory not found: #{result}" |
90 | 87 | end |
91 | 88 |
|
92 | | - def render_script |
93 | | - @render_script ||= File.read "#{source}/render.rb" |
| 89 | + def watchables |
| 90 | + @watchables ||= [Settings.config_path, args['SOURCE']] |
94 | 91 | end |
95 | 92 | end |
96 | 93 | end |
|
0 commit comments