Rakefile 7 KB
Newer Older
1
2
3
4
5
6
7
8
9
require 'net/http'
require 'rake/clean'
require 'find'
require 'date'
require 'open3'

task :default => [:wip]

SOURCE_FILES = FileList['livro/livro.asc', 'livro/capitulos/*']
10
@RELEASE_DIR = 'releases/current'
11
12
13
14
15
16
17
18
19
20
@BOOK_SOURCE_DIR = 'livro'
@BOOK_SOURCE = 'livro/livro.asc'
@BOOK_TARGET = 'livro/livro.pdf'
@A2X_BIN = '~/ambiente/asciidoc/a2x.py'
WIP_ADOC = "#{@BOOK_SOURCE_DIR}/wip.adoc"
RELEASE_BOOK_SOURCE = "#{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/livro.asc"
RELEASE_BOOK  = "#{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/livro.pdf"
RELEASE_WIP_ADOC =  "#{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/wip.adoc"
RELEASE_WIP_PDF  =  "#{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/wip.pdf"
OPEN_PDF_CMD=`git config --get producao.pdfviewer`.strip
21
A2X_COMMAND="-v -k -f pdf --icons -a docinfo1 -a edition=`git describe` -a lang=pt-BR -d book --dblatex-opts '-T computacao -P latex.babel.language=brazilian' -a livro-pdf"
22
23
24
25
26
27
28
29
30
31
32
PROJECT_NAME = File.basename(Dir.getwd)
LIVRO_URL = `git config --get livro.url`.strip
GITHUB_REPO = `git config remote.origin.url`.strip.gsub('git@github.com:','').gsub('.git','')

directory @RELEASE_DIR

CLEAN.include('releases')

desc "Sync, build and open wip file"
task :wip => [WIP_ADOC, "sync", "wip:build", "wip:open"]
task :edit => ["wip:edit"]
33

34
35
36
37
38
39
40
41
42
43
44
45

namespace "wip" do

  desc "Create new wip file from book source"
  task "new" do
    cp "#{@BOOK_SOURCE}", "#{@BOOK_SOURCE_DIR}/wip.adoc"
  end

  file WIP_ADOC do
    Rake::Task["wip:new"].invoke
  end

46
47
  file RELEASE_WIP_PDF do
    system "#{@A2X_BIN} #{A2X_COMMAND} #{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/wip.adoc"
48
49
50
51
52
53
54
55
56
  end

  desc "Open wip pdf"
  task :open => RELEASE_WIP_PDF do |t|
      puts "#{OPEN_PDF_CMD} #{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/wip.pdf"
      system "#{OPEN_PDF_CMD} #{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/wip.pdf"
  end

  desc "Open docbook xml from wip build"
57
  task "xml" do
58
59
60
61
62
63
64
65
    system "#{OPEN_PDF_CMD} #{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/wip.xml"
  end

  desc "Edit wip source"
  task "edit" do
    system "gvim #{WIP_ADOC}"
  end

66
67
68
  desc "build book from #{@RELEASE_DIR}"
  task :build => [WIP_ADOC, :sync] do
    system "#{@A2X_BIN} #{A2X_COMMAND} #{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/wip.adoc"
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  end

end




desc "Archive, build and open book file"
task :book => [:clean, :archive, "book:build", "book:open"]

namespace "book" do

  desc "Build book"
  task :build => ['sync'] do
    system "#{@A2X_BIN} #{A2X_COMMAND} #{@RELEASE_DIR}/#{@BOOK_SOURCE}"
  end

  desc "Open pdf book"
  task "open" do
    system "#{OPEN_PDF_CMD} #{@RELEASE_DIR}/#{@BOOK_TARGET}"
  end

  desc "Open docbook xml from book build"
  task "xml" do
    system "#{OPEN_PDF_CMD} #{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/livro.xml"
  end

  desc "Edit book source"
  task "edit" do
    system "gvim #{@BOOK_SOURCE}"
  end

  desc "Release new edition book"
  task :release, [:tag] do |t, args|
    #PROJECT = sh "`git config --get remote.origin.url | cut -f 2 -d / | cut -f 1 -d .`"
    puts "PROJECT_NAME='#{PROJECT_NAME}' LIVRO_URL='#{LIVRO_URL}'"
    mkdir_p "~/releases/#{PROJECT_NAME}"
    cd "~/releases/#{PROJECT_NAME}"
    `wget #{LIVRO_URL}`
    puts "Salvando arquivo em #{Dir.getwd}"
    mv "livro.pdf", "#{PROJECT_NAME}-#{args.tag}.pdf"
    #Dir.mkdir(File.join(Dir.home, ".foo"), 0700)
  end

end

desc "Extract files from repository (git archive)"
task :archive => :clean do
  system "git archive --format=tar --prefix=#{@RELEASE_DIR}/ HEAD | (tar xf -) "
end

desc "Local sync of the files"
task :sync => @RELEASE_DIR do |t|
  system "rsync -r --delete #{@BOOK_SOURCE_DIR}/ #{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}"
end

namespace "tag" do

  desc "List project tags"
  task :list do
    sh "git tag --list"
  end

  desc "Aplly a tag to the project. The tag can be used as the edition."
  task :apply, [:tag] do |t, args|
    sh "git status"
    sh "git tag -a #{args.tag} -m 'Gerando versão #{args.tag}'"
  end

  desc "Delete a tag applied."
  task :delete, [:tag] do |t,args|
    sh "git tag -d #{args.tag}"
  end

  desc "Push tags"
  task "push" do
    sh "git push origin --tags"
  end

  desc "Generate revision history, compare HEAD and tag.
  The tag is optional, if not specified it will use the last tag applied."
  task :revision, [:tag] do |t, args|
    last_tag = `git describe --abbrev=0`.strip
    args.with_defaults(:tag => last_tag)
    tag = args.tag
    edition = ENV.fetch('edition', "#{tag} FIXME!")
    authors = ENV.fetch('authors', " FIXME! ")
    date = Date.today.strftime "%d/%m/%Y"
    history, s = Open3.capture2("git log --reverse --format='- %s. ' #{args.tag}..HEAD")
    revision = "\n
    <revision>
      <revnumber>#{edition}</revnumber>
      <date>#{date}</date>
      <authorinitials>#{authors}</authorinitials>
      <revremark>
#{history}      </revremark>
    </revision>\n\n"
    puts revision
  end

  desc "Open docinfo for edition.
  Before apply tag you should edit docinfo and add the revision history."
  task :docinfo do
    puts "#{OPEN_PDF_CMD} #{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/wip.pdf"
    system "xdg-open #{@BOOK_SOURCE_DIR}/docinfo.xml"
  end

end


desc "Open orginal pdf to work"
task :original do
    sh "#{OPEN_PDF_CMD} original/original.pdf"
end


namespace "config" do

#  desc "Configure open command. `xdg-open` for ubuntu and open for `osx`"
  task :pdfviewer, [:app] do |t,args|
    sh "git config --global producao.pdfviewer #{args.app}"
  end

end


desc "Download new Rakefile"
task :uprake do
197
  `wget --output-document=Rakefile https://raw.githubusercontent.com/edusantana/novo-livro/master/Rakefile`
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
end


desc "Build images from R files"
task :r
task :sync => :r

FileList['livro/images/**/*.R'].each do |source|
  rpdf = source.ext('pdf')
  file rpdf => source do |t|
    rm_rf "Rplots.pdf"
    sh "R --no-save < #{t.source}"
    mv "Rplots.pdf","#{t.name}"
  end
  task :r => rpdf
end

desc "Build images from dot files"
task :dot
task :sync => :dot

FileList['livro/images/**/*.dot'].each do |source|
  epsfile = source.ext('eps')
  file epsfile => source do |t|
    sh "dot -Teps -o #{t.name} #{t.source}"
  end
  task :dot => epsfile
end

namespace "github" do
228
  desc "List issues from github milestone"
229
230
231
232
233
234
  task :issues, [:milestone] do |t,args|
    puts "Acessing: #{GITHUB_REPO} milestone=#{args.milestone}"
    require 'octokit'
#    require 'highline/import'
    client = Octokit::Client.new
    milestone = nil
235
    milestones = client.list_milestones(GITHUB_REPO, state: "all")
236
237
238
239
240
241
242
243
244
245
    opcoes = milestones.map {|m| m[:title]}

    if (args.milestone) then
      #puts "milestones: #{milestones}"
      milestones.each do |m|
        if m[:title] == args.milestone then
          milestone = m
        end
      end
    else
246
      milestone = milestones[-1]
247
    end
248
    puts "Milestone: #{milestone[:title]}"
249
250
251

    puts ""
    puts "Para adicionar ao docinfo.xml:\n"
252
    issues = client.list_issues(GITHUB_REPO, state:'Closed', milestone:milestone[:number], direction:'asc')
253
254
255
256
257
258
259
260
261
262
263
    issues.each do |i|
      puts "<ulink url=\"{gitrepo}/issues/#{i[:number]}\">#{i[:title]};</ulink>"
    end
    puts ""
    puts "Para adicionar ao release notes no github:"
    issues.each do |i|
      puts "- #{i[:title]} (##{i[:number]});"
    end

  end
end