Class: Daimyo::Publish

Inherits:
Client
  • Object
show all
Includes:
Helper
Defined in:
lib/daimyo/publish.rb

Instance Method Summary collapse

Methods included from Helper

#diff_print_header

Methods inherited from Client

#export, #list, #publish

Methods included from Config

#read_daimyo_yaml

Constructor Details

#initialize(params = nil) ⇒ Publish

Returns a new instance of Publish.



7
8
9
10
# File 'lib/daimyo/publish.rb', line 7

def initialize(params = nil)
  @wiki ||= Daimyo::Client.new
  @is_local = params[:local] unless params.nil?
end

Instance Method Details

#read_file(path) ⇒ Object



34
35
36
37
38
# File 'lib/daimyo/publish.rb', line 34

def read_file(path)
  File.open(path, 'r') do |file|
    file.read
  end
end

#run(project_id, dry_run) ⇒ Object



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
# File 'lib/daimyo/publish.rb', line 40

def run(project_id, dry_run)
  files = search_files(project_id)

  diff_files = []
  files.each do |file|
    if file[0].include?('.md')
      original = read_file(file[0]) 
      latest = read_file(file[1])
      diff_file = []
      if original != latest
        diff_file << file[1]
        diff_file << original
        diff_file << latest
        diff_files << diff_file
      end
    end
  end

  diff_files.each do |diff_file|
    puts diff_print_header(diff_file[0], @is_local)
    path_array = diff_file[0].split('/')
    wiki_id = path_array[-1].split('_')[0]

    if @is_local
      puts Diffy::Diff.new(diff_file[1], diff_file[2],
                           :include_diff_info => false, :context => 1).to_s(:color)
  
    else
      wiki_content = @wiki.export(wiki_id).body.content.gsub("\r\n", "\n")
      puts Diffy::Diff.new(wiki_content, diff_file[2],
                           :include_diff_info => false, :context => 1).to_s(:color)
    end

    # Todo: このへん直す!
    path_array.shift
    path_array.shift
    wiki_name = path_array[-1].split('_')[1].gsub(/.md/, '')
    # Todo: このへん直す!
    path_array.pop
    if path_array.length > 0
      wiki_name = path_array.join('/') + '/' + wiki_name
    else
      wiki_name
    end
    @wiki.publish(wiki_id, wiki_name, diff_file[2]) if dry_run.nil?
  end
end

#search_files(project_id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/daimyo/publish.rb', line 12

def search_files(project_id)
  space = @wiki.instance_variable_get(:@client).instance_variable_get(:@space_id)
  paths = Dir.glob(space + '/' + project_id  + '/**/*')

  diffy_paths = []
  paths.each do |path|
    diffy_path = []  
    if path.include?('.md')
      path_array = path.split('/')
      original_file_path = '.' + path_array[-1]
      path_array.pop
      original_path = path_array.join('/') + '/' + original_file_path
      diffy_path << original_path
    else
      diffy_path << path
    end
    diffy_path << path
    diffy_paths << diffy_path
  end
  diffy_paths
end