Debian Codegraph @

core/boot/shell/spec.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # specファイル自動生成
3
4 require "boot/delayer"
5 require "userconfig"
6
7 require "fileutils"
8 require 'ripper'
9
10 # イカサマ依存関係自動解決クラス。
11 # あまり頼りにしないでくれ、Rubyのパース面倒なんだよ
12 class Depend < Ripper::Filter
13 attr_reader :spec
14 def initialize(*args)
15 @last_const = ""
16 super end
17
18 def set_spec(spec)
19 @spec = spec
20 self end
21
22 # シンボル開始の:。次のidentをシンボルと見る
23 def on_symbeg(tok, f)
24 :symbol end
25
26 def on_ident(tok, f)
27 if f == :symbol
28 on_ex_symbol(tok)
29 else
30 on_ex_ident(tok) end end
31
32 # 定数。Gtk::TimeLine みたいなのが出てきた場合、on_const Gtk, on_op ::, on_const NestedQuote
33 # の順番でイベントが発生するみたい…。
34 def on_const(tok, f)
35 if f == :op
36 @last_const += '::' + tok
37 else
38 @last_const = tok end
39 case @last_const
40 when /\AG[td]k\Z/ # GtkクラスとかGdkクラス使ってたらgtkプラグインに依存してるだろう
41 depend :gtk3
42 when /\APlugin::(\w+)/ # Plugin::なんとか は、プラグインスラッグのキャメルケース名なので、使ってたら依存してる
43 depend $1.gsub(/([a-z])([A-Z])/, '\1_\2').downcase.to_sym end end
44
45 def on_op(tok, f)
46 case tok
47 when "::"
48 :op end end
49
50 # シンボル
51 def on_ex_symbol(tok)
52 end
53
54 # 変数とかメソッド的なやつ。
55 # command DSLを検出してもcommandプラグインには依存させない。
56 # commandプラグインは基本的なmikutterコマンドを定義するだけで、mikutterコマンドの仕組み自体には関係ないから。
57 # commandプラグインで使われている条件クラスを使っていたら、on_constで依存してると判断される。
58 def on_ex_ident(tok)
59 case tok
60 when 'defactivity'
61 depend :activity
62 when 'tab', 'timeline', 'nativewidget' # UIっぽい単語があったらguiに依存してそう
63 depend :gui
64 when 'profiletab' # profiletabはプロフィールにも依存する
65 depend :gui
66 depend :profile
67 when 'settings' # 設定DSLの開始。settingsプラグイン。
68 depend :settings
69 end end
70
71 # slug に依存していることを登録する
72 # ==== Args
73 # [slug] 依存しているプラグイン (Symbol)
74 def depend(slug)
75 if spec['slug'].to_sym != slug and not spec["depends"]["plugin"].include?(slug.to_s)
76 spec["depends"]["plugin"] << slug.to_s end end
77 end
78
79 def spec_generate(dir)
80 dir = File.expand_path(dir)
81 specfile = File.join(dir, ".mikutter.yml")
82 legacy_specfile = File.join(dir, "spec")
83 spec = if FileTest.exist?(specfile)
84 YAML.load_file(specfile)
85 elsif FileTest.exist?(legacy_specfile)
86 YAML.load_file(legacy_specfile)
87 else
88 user = UserConfig[:verify_credentials] || {}
89 idname = user[:idname]
90 {"slug" => File.basename(dir).to_sym, "depends" => {"mikutter" => Environment::VERSION.to_s}, "version" => "1.0", "author" => idname} end
91 slug = spec["slug"].to_sym
92 basefile = File.join(dir, "#{slug}.rb")
93 unless FileTest.exist? basefile
94 puts "file #{basefile} notfound. select plugin slug."
95 expects = Dir.glob(File.join(dir, "*.rb")).map{ |filename| File.basename(filename, '.rb') }
96 if expects.empty?
97 puts "please create #{basefile}."
98 end
99 expects.each_with_index{ |filename, index|
100 puts "[#{index}] #{filename}"
101 }
102 print "input number or slug [q:quit, s:skip]> "
103 number = STDIN.gets(chomp: true)
104 case number
105 when /q/i
106 abort
107 when /s/i
108 return
109 when /\A[0-9]+\Z/
110 slug = expects[number.to_i].to_sym
111 else
112 slug = number.to_sym end
113 spec["slug"] = slug
114 basefile = File.join(dir, "#{slug}.rb") end
115 source = File.open(basefile){ |io| io.read }
116
117 if not spec.has_key?("name")
118 print "#{slug}: name> "
119 spec["name"] = STDIN.gets(chomp: true)
120 end
121 if not spec.has_key?("description")
122 print "#{slug}: description> "
123 spec["description"] = STDIN.gets(chomp: true)
124 end
125 spec["depends"] = {"version" => "1.0", "plugin" => []} if not spec.has_key?("depends")
126 spec["depends"]["plugin"] = [] if not spec["depends"].has_key?("plugin")
127 depend = Depend.new(source).set_spec(spec)
128 depend.parse
129 content = YAML.dump(depend.spec)
130 File.open(specfile, "w"){ |io| io.write content }
131 puts content
132 end
133
134 target = ARGV[1]
135 if target == "all"
136 unless ARGV[2]
137 puts "directory is not specified."
138 puts "usage: mikutter.rb spec all directory"
139 exit end
140 Dir.glob(File.join(ARGV[2], "*/")).each{ |dir|
141 spec_generate(dir) }
142 else
143 unless ARGV[1]
144 puts "directory is not specified."
145 puts "usage: mikutter.rb spec directory"
146 exit end
147 spec_generate(target)
148 end