Class: Command::Init

Inherits:
CommandBase show all
Defined in:
lib/command/init.rb

Instance Method Summary collapse

Methods inherited from CommandBase

execute_and_rescue_exit, #load_local_settings

Constructor Details

#initializeInit

Returns a new instance of Init.



19
20
21
22
23
24
25
26
# File 'lib/command/init.rb', line 19

def initialize
  super("")
  if Narou.already_init?
    initialize_already_init
  else
    initialize_init_yet
  end
end

Instance Method Details

#ask_aozoraepub3_pathObject



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
# File 'lib/command/init.rb', line 113

def ask_aozoraepub3_path
  puts
  print "<bold><green>AozoraEpub3のあるフォルダを入力して下さい:</green></bold>\n(未入力でスキップ".termcolor
  if @global_setting["aozoraepub3dir"]
    print "、:keep で前回と同じ設定"
  end
  print ")\n>"
  while input = $stdin.gets.rstrip.gsub(/"/, "")
    path = File.expand_path(input)
    case
    when input == ":keep"
      aozora_dir = @global_setting["aozoraepub3dir"]
      if aozora_dir && Narou.aozoraepub3_directory?(aozora_dir)
        return aozora_dir
      end
    when Narou.aozoraepub3_directory?(path)
      return path
    when input == ""
      break
    end
    print ("\n<bold><green>入力されたフォルダにAozoraEpub3がありません。" +
           "もう一度入力して下さい:</green></bold>\n&gt;").termcolor
  end
  nil
end

#execute(argv) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/command/init.rb', line 49

def execute(argv)
  super
  if Narou.already_init?
    init_aozoraepub3(true)
  else
    Narou.init
    puts "-" * 30
    init_aozoraepub3
    puts "初期化が完了しました!"
    puts "現在のフォルダ下で各種コマンドが使用出来るようになりました。"
    puts "まずは narou help で簡単な説明を御覧ください。"
  end
end

#init_aozoraepub3(force = false) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/command/init.rb', line 63

def init_aozoraepub3(force = false)
  @global_setting = GlobalSetting.get["global_setting"]
  if !force && @global_setting["aozoraepub3dir"]
    return
  end
  puts "<bold><green>AozoraEpub3の設定を行います</green></bold>".termcolor
  unless @global_setting["aozoraepub3dir"]
    puts ("<red>" + "!!!WARNING!!!".center(70) + "</red>").termcolor
    puts "AozoraEpub3の構成ファイルを書き換えます。narouコマンド用に別途新規インストールしておくことをオススメします"
  end
  aozora_path = ask_aozoraepub3_path
  unless aozora_path
    puts "設定をスキップしました。あとで " + "<bold><yellow>narou init</yellow></bold>".termcolor + " で再度設定出来ます"
    return
  end
  puts
  rewrite_aozoraepub3_files(aozora_path)
  @global_setting["aozoraepub3dir"] = aozora_path
  GlobalSetting.get.save_settings("global_setting")
  puts "<bold><green>AozoraEpub3の設定を終了しました</green></bold>".termcolor
end

#initialize_already_initObject



39
40
41
42
43
44
45
46
47
# File 'lib/command/init.rb', line 39

def initialize_already_init
  @opt.separator <<-EOS

  ・AozoraEpub3 の再設定を行います。

  Example:
narou init
  EOS
end

#initialize_init_yetObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/command/init.rb', line 28

def initialize_init_yet
  @opt.separator <<-EOS

  ・現在のフォルダを小説格納用フォルダとして初期化します。
  ・初期化されるまでは他のコマンドは使えません。

  Example:
narou init
  EOS
end

#oneline_helpObject



11
12
13
14
15
16
17
# File 'lib/command/init.rb', line 11

def oneline_help
  if Narou.already_init?
    "AozoraEpub3 の再設定を行います"
  else
    "現在のフォルダを小説用に初期化します"
  end
end

#rewrite_aozoraepub3_files(aozora_path) ⇒ Object



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
# File 'lib/command/init.rb', line 85

def rewrite_aozoraepub3_files(aozora_path)
  # chuki_tag.txt の書き換え
  custom_chuki_tag_path = File.join(Narou.get_preset_dir, "custom_chuki_tag.txt")
  chuki_tag_path = File.join(aozora_path, "chuki_tag.txt")
  custom_chuki_tag = open(custom_chuki_tag_path, "r:BOM|UTF-8") { |fp| fp.read }
  chuki_tag = open(chuki_tag_path, "r:BOM|UTF-8") { |fp| fp.read }
  embedded_mark = "### Narou.rb embedded custom chuki ###"
  if chuki_tag =~ /#{embedded_mark}/
    chuki_tag.gsub!(/#{embedded_mark}.+#{embedded_mark}/m, custom_chuki_tag)
  else
    chuki_tag << "\n" + custom_chuki_tag
  end
  File.write(chuki_tag_path, chuki_tag)
  puts "(次のファイルを書き換えました)"
  puts chuki_tag_path
  puts
  # ファイルコピー
  src = ["AozoraEpub3.ini", "vertical_font.css", "DMincho.ttf"]
  dst = ["AozoraEpub3.ini", "template/OPS/css_custom/vertical_font.css", "template/OPS/fonts/DMincho.ttf"]
  puts "(次のファイルをコピーor上書きしました)"
  src.count.times do |i|
    src_full_path = File.join(Narou.get_preset_dir, src[i])
    dst_full_path = File.join(aozora_path, dst[i])
    FileUtils.install(src_full_path, dst_full_path)
    puts dst_full_path
  end
end