Class: Docwu::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/docwu/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWorker

Returns a new instance of Worker.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
# File 'lib/docwu/worker.rb', line 8

def initialize
  @deploy_path = ::Docwu.config.deploy_path   # 部署路径
  @tmp_path = ::Docwu.config.tmp_path

  @data = {
    'worker' => {
      'copyright' => {
        'year'    => ::Date.today.year,
        'content' => 'document world util',
        'name'    => 'Doc WU'
      }
    }
  }

  ::Docwu::Utils.hash_deep_merge!(@data['worker'], ::Docwu.config.worker)

  self.data['page'] ||= {}

  self.data['reader'] ||= {}

  # 关于目录
  @folders             = []
  @layouts             = {}
  @topics              = []

  # 布局模板
  ::Docwu.config.routes['layouts'].each do |name, path|
    _path = "#{::Docwu.config.layouts_path}/#{path}"

    if File.exists?(_path) && File.file?(_path)
      @layouts[name] = File.read(_path)
    end
  end

  #                                      原文件, 目标路径
  ::Docwu.config.routes['topics'].each do |src, path|
    _src_path = "#{::Docwu.config.topics_path}/#{src}"

    if File.exists?(_src_path) && File.file?(_src_path)
      @topics << ::Docwu::Topic.new(:src => _src_path, :path => path, :worker => self)
    end
  end

  # 计算出当前所有的 folders                源,   目标
  ::Docwu.config.routes['folders'].each do |_src, _path|
    _folder_src = "#{plain_path("/#{_src}")}"

    if File.exists?(_folder_src) && File.directory?(_folder_src)
      @folders << ::Docwu::Folder.new(:src => _folder_src, :worker => self, :path => _path)
    end
  end

  # TODO: add 更多的全局数据

  self.data['reader'].merge!(
    'global'  => {
      'folders' => self.folders_data
    }
  )

end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#deploy_pathObject (readonly)

Returns the value of attribute deploy_path.



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

def deploy_path
  @deploy_path
end

#foldersObject (readonly)

Returns the value of attribute folders.



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

def folders
  @folders
end

#layoutsObject (readonly)

Returns the value of attribute layouts.



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

def layouts
  @layouts
end

#tmp_pathObject (readonly)

Returns the value of attribute tmp_path.



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

def tmp_path
  @tmp_path
end

#topicsObject (readonly)

Returns the value of attribute topics.



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

def topics
  @topics
end

Instance Method Details

#folders_dataObject



108
109
110
# File 'lib/docwu/worker.rb', line 108

def folders_data
  self.folders.map(&:to_data)
end

#generateObject

输出:

TODO: 先生成临时目录, 然后 -> deploy


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
# File 'lib/docwu/worker.rb', line 72

def generate
  begin
    # 删除要输出的路径
    FileUtils.mkdir_p(self.tmp_deploy_path)

    ::Docwu::Utils.cp_r("#{plain_path('/assets')}", "#{self.tmp_deploy_path}/assets")

    # 复制静态文件里去
    ::Docwu::Utils.cp_r("#{plain_path('/static')}", "#{self.tmp_deploy_path}/static")

    self.folders.each do |folder|
      folder.generate
    end

    self.topics.each do |topic|
      topic.generate
    end

  rescue Exception => exception
    FileUtils.rm_rf(self.tmp_deploy_path)
    raise "#{exception}"
  else
    FileUtils.rm_rf(self.deploy_path)
    FileUtils.mv(self.tmp_deploy_path, self.deploy_path)
  ensure
  end
end

#plain_path(path) ⇒ Object



104
105
106
# File 'lib/docwu/worker.rb', line 104

def plain_path(path)
  "#{::Docwu.config.workspace}#{path}"
end

#tmp_deploy_pathObject



100
101
102
# File 'lib/docwu/worker.rb', line 100

def tmp_deploy_path
  @tmp_deploy_path ||= "#{self.tmp_path}/_deploy/#{Time.now.to_i}"
end