Module: Docwu

Defined in:
lib/docwu/render.rb,
lib/docwu.rb,
lib/docwu/post.rb,
lib/docwu/route.rb,
lib/docwu/topic.rb,
lib/docwu/utils.rb,
lib/docwu/config.rb,
lib/docwu/folder.rb,
lib/docwu/server.rb,
lib/docwu/worker.rb,
lib/docwu/version.rb

Overview

Defined Under Namespace

Classes: Config, Folder, Post, Render, Route, Server, Topic, Utils, Worker

Constant Summary collapse

VERSION =
"0.0.16"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject



6
7
8
# File 'lib/docwu/config.rb', line 6

def config
  @config ||= Config.new
end

Class Method Details

.configure {|self.config ||= Config.new| ... } ⇒ Object

Yields:



10
11
12
# File 'lib/docwu/config.rb', line 10

def configure
  yield self.config ||= Config.new
end

.start(workspace) ⇒ Object

程序开始



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
112
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
138
139
140
141
142
143
144
145
# File 'lib/docwu.rb', line 22

def self.start(workspace)
  _start_time = Time.now

  args = ARGV

  _command = args.shift

  _useful_cmds = ['g', 'generate', 's', 'server', '-h', '--help', 'new', '-v', '--version']

  unless _useful_cmds.include?(_command)
    puts "command #{_command} is not available, not in (#{_useful_cmds.join('|')})"
    exit
  end

  if ['-v', '--version'].include?(_command)
    puts "docwu: #{::Docwu::VERSION}"

    exit
  end

  if ['new'].include?(_command)
    _project_name = args.shift

    if _project_name.nil?
      puts 'error: You need input a project name! '
      exit
    end

    _new_dest = "#{workspace}/#{_project_name}"

    _template_src = "#{File.dirname(__FILE__)}/template_files"

    if File.exists?(_new_dest) && File.directory?(_new_dest)
      puts "error: #{_new_dest} already exists! Please check !"
    else
      FileUtils.cp_r("#{_template_src}", "#{_new_dest}")

      puts "cd #{_new_dest} && bundle install && bundle exec docwu generate"
      system "cd #{_new_dest} && bundle install && bundle exec docwu generate"

      puts "[#{_new_dest}] already success created!"
    end

    exit
  end

  _default_params = {
    '-p'        => [5656],
    '-a'        => ['0.0.0.0'],
    '-c'        => ["#{workspace}/config.yml"]
  }

  if ['--help', '-h'].include?(_command)
    puts "docwu:"

    puts "   * docwu new [project_name]  ; eg.: docwu new project"
    puts ""
    puts "   * docwu g, docwu generate, docwu s, docwu server"
    _default_params.each do |_cmd, _cfg|
      puts "    #{_cmd}, #{_cfg[1]} (default: #{_cfg[0]})"
    end

    exit
  end

  # 获取参数
  params = Hash[args.each_slice(2).to_a]

  _default_params.each do |_k, _v|
    params[_k] = params[_k] || _v.first
  end

  params['-p'] = params['-p'].to_i

  # 需要生成
  _need_generate = ['g', 'generate', 's', 'server'].include?(_command)

  # 需要开启server
  _need_server = ['s', 'server'].include?(_command)

  _config_file = params['-c']

  _config = {}

  if File.exists?(_config_file) && File.file?(_config_file)
    _yml = YAML.load(File.read(_config_file))

    if _yml.is_a?(Hash)
      _config.merge!(_yml['docwu'] || {})
    end
  else
    raise "#{_config_file} not exists!"
  end

  _logger = ::Logger.new(STDOUT)
  _logger.level = ::Logger::INFO

  # docwu 的配置
  ::Docwu.configure do |config|
    config.logger      = _logger
    config.params      = params.freeze
    config.routes      = (_config['routes'] || {}).freeze
    config.worker      = (_config['worker'] || {}).freeze
    config.workspace   = "#{workspace}".freeze
  end

  ::MustacheRender.configure do |config|
    config.file_template_root_path = ::Docwu.config.layouts_path
    config.logger                  = ::Docwu.config.logger
  end

  if _need_generate
    ::Docwu::Worker.new.generate 

    _logger.info("generate: success, #{Time.now - _start_time}")
  end

  if _need_server
    ::Docwu::Server.process(
      :Port => ::Docwu.config.params['-p'],
      :Host => ::Docwu.config.params['-a']
    )
  end
end