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

task :default => [:wip]

9
SOURCE_FILES = FileList['livro/livro.asc', 'livro/*']
10 11
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`.strip
@RELEASE_DIR = "releases/#{CURRENT_BRANCH}"
12 13 14 15 16 17 18 19 20 21
@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
22 23 24

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 -P preface.tocdepth=1' -a livro-pdf"
A2X_EPUB_COMMAND="-v -k -f epub --icons -a docinfo1 -a edition=`git describe` -a lang=pt-BR -d book "
25 26 27 28
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','')

29 30 31 32 33 34
# release
REPOSITORIO_PATH=`git rev-parse --show-toplevel`.strip
#@RELEASE_DIR = ''
@tag = ''
@SEJDA_BIN = '/home/santana/ambiente/sejda/bin/sejda-console'

35 36 37 38 39 40 41
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"]
42
task :epub
43 44 45

namespace "wip" do

46 47
  file @BOOK_SOURCE

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

53
  file WIP_ADOC => [@BOOK_SOURCE] do
54 55 56
    Rake::Task["wip:new"].invoke
  end

57 58 59
  desc "build book from #{@RELEASE_DIR}"
  task :build => [WIP_ADOC, :sync] do
    DRAFT_COMMAND = "--dblatex-opts '-P draft.mode=yes'"
60
    prefacio_code_att = ""
61
    PREFACIO_CODE_DIR = "#{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/code/prefacio"
62 63 64 65 66 67 68 69 70
    if Dir.exist?(PREFACIO_CODE_DIR) then
      Dir.chdir(PREFACIO_CODE_DIR) do
        prefacio_code_file = Dir.glob("*").first
        if (prefacio_code_file) then
          prefacio_code_att = "-a prefacio-code=#{prefacio_code_file}"
        end
      end
    end
    system "#{@A2X_BIN} #{A2X_COMMAND} #{DRAFT_COMMAND} #{prefacio_code_att} #{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/wip.adoc"
71 72 73 74 75 76 77 78 79
  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"
80
  task "xml" => ["#{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/wip.xml"] do
81 82 83 84 85 86 87 88
    system "#{OPEN_PDF_CMD} #{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/wip.xml"
  end

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

89 90 91 92

  desc "build wip epub book"
  task :epub do
    system "#{@A2X_BIN} #{A2X_EPUB_COMMAND} #{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/wip.adoc"
93 94
  end

95

96 97 98 99 100
end




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
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
171
    sh "git push origin"
172 173 174 175 176 177 178 179 180 181 182 183 184 185
    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
186 187 188 189 190
<revision>
  <revnumber>#{edition}</revnumber>
  <date>#{date}</date>
  <authorinitials>#{authors}</authorinitials>
  <revremark>
191
#{history}      </revremark>
192
</revision>\n\n"
193 194 195 196 197 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 228 229 230 231 232 233 234 235
    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 "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

namespace "github" do
236
  desc "List issues from github milestone. Default milestone state is closed, can also be all."
237 238 239 240 241 242
  task :issues, [:milestone] do |t,args|
    puts "Acessing: #{GITHUB_REPO} milestone=#{args.milestone}"
    require 'octokit'
#    require 'highline/import'
    client = Octokit::Client.new
    milestone = nil
243
    milestones = client.list_milestones(GITHUB_REPO, state: 'all', sort: 'created', direction: 'desc')
244 245 246 247 248 249 250 251 252 253
    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
254
      milestone = milestones[0]
255
    end
256
    puts "Milestone: #{milestone[:title]} #{milestone[:state].upcase}"
257 258 259

    puts ""
    puts "Para adicionar ao docinfo.xml:\n"
260 261 262 263 264 265 266 267 268 269 270
    issues = client.list_issues(GITHUB_REPO, milestone:milestone[:number], sort: 'created', direction: 'asc', state:'all')
    open_issues = []
    issues.each do |i|
      if (i[:state] == 'open') then
        open_issues << i[:number]
      end
    end
    if (open_issues.size > 0) then
      puts "Open issues: #{open_issues}"
      puts ""
    end
271 272 273 274 275 276 277 278 279
    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

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    puts ""
    puts "Para commits:"
    issues.each do |i|
      puts "#{i[:title]} closes ##{i[:number]}"
    end


  end
end


namespace "release" do

  desc "Archive files from git tag. If not tag is passed, the last tag applied will be used."
  task :archive, [:tag] do |t, args|
    last_tag = `git describe --abbrev=0`.strip
    args.with_defaults(:tag => last_tag)
    @tag = args.tag
    @RELEASE_DIR = "releases/#{args.tag}"
299
    system "git archive --format=tar --prefix=#{@RELEASE_DIR}/ #{@tag} | (tar xf -) "
300
  end
301 302 303 304 305 306 307 308

  desc "Build book release. If not tag is passed, the last tag applied will be used."
  task :build, [:tag] do |t, args|
    last_tag = `git describe --abbrev=0`.strip
    args.with_defaults(:tag => last_tag)
    @tag = args.tag
    @RELEASE_DIR = "releases/#{args.tag}"
    release_dir = "releases/#{args.tag}"
309
    target_file = "releases/#{PROJECT_NAME}-#{@tag}.pdf"
310 311 312 313 314 315 316
    ficha_file = "#{release_dir}/livro/editora/ficha-#{args.tag}.pdf"
    if File.exist?(ficha_file) then
      editora_file = ficha_file
    else
      puts "Using 'editora.pdf' instead of ficha-#{args.tag}.pdf"
      editora_file = "#{release_dir}/livro/editora/editora.pdf"
    end
317 318
    livro_source = "#{release_dir}/livro/livro.asc"
    livro_pdf = "#{release_dir}/livro/livro.pdf"
319 320 321 322 323 324 325 326 327 328 329
    
    prefacio_code_att = ""
    PREFACIO_CODE_DIR = "#{@RELEASE_DIR}/#{@BOOK_SOURCE_DIR}/code/prefacio"
    if Dir.exist?(PREFACIO_CODE_DIR) then
      Dir.chdir(PREFACIO_CODE_DIR) do
        prefacio_code_file = Dir.glob("*").first
        if (prefacio_code_file) then
          prefacio_code_att = "-a prefacio-code=#{prefacio_code_file}"
        end
      end
    end
330 331 332 333 334

    directory release_dir
    file livro_source => [release_dir]
    file livro_pdf => [livro_source] do
      Dir.chdir(@RELEASE_DIR) do
335
        @A2X_COMMAND="-v -k -f pdf --icons -a docinfo1 -a edition=#{@tag} -a lang=pt-BR -d book --dblatex-opts '-T computacao -P latex.babel.language=brazilian' -a livro-pdf #{prefacio_code_att}"
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
        system "#{@A2X_BIN} #{@A2X_COMMAND} livro/livro.asc"
      end
    end
    file target_file => [livro_pdf] do
      if File.exist? editora_file then
        system "#{@SEJDA_BIN} merge -f #{editora_file} #{livro_pdf} -o #{target_file}"
      else
        mv livro_pdf, target_file
      end
    end

    Rake::Task[target_file].invoke()

  end # build

351
end