OSDN Git Service

Add autobuild script
authoreagletmt <eagletmt@gmail.com>
Fri, 9 Nov 2012 07:28:00 +0000 (16:28 +0900)
committereagletmt <eagletmt@gmail.com>
Fri, 9 Nov 2012 07:28:00 +0000 (16:28 +0900)
CutenServer/nginx/cuten.conf
tools/autobuild/.gitignore [new file with mode: 0644]
tools/autobuild/Gemfile [new file with mode: 0644]
tools/autobuild/Gemfile.lock [new file with mode: 0644]
tools/autobuild/app.rb [new file with mode: 0644]
tools/autobuild/config.ru [new file with mode: 0644]
tools/autobuild/views/build.haml [new file with mode: 0644]
tools/autobuild/views/index.haml [new file with mode: 0644]
tools/autobuild/views/layout.haml [new file with mode: 0644]
tools/autobuild/views/remote.haml [new file with mode: 0644]

index 4ef02bc..d89c23a 100644 (file)
@@ -2,6 +2,10 @@ upstream unicorn-cuten {
     server 127.0.0.1:3010;
 }
 
+upstream autobuild {
+    server 127.0.0.1:3020;
+}
+
 server {
     listen 3001;
     server_name _;
@@ -32,6 +36,12 @@ server {
         break;
     }
 
+    location ~ ^/autobuild {
+        root ../tools/autobuild;
+        proxy_pass http://autobuild;
+        break;
+    }
+
     location / {
         proxy_pass http://unicorn-cuten;
     }
diff --git a/tools/autobuild/.gitignore b/tools/autobuild/.gitignore
new file mode 100644 (file)
index 0000000..c7927f1
--- /dev/null
@@ -0,0 +1,8 @@
+/.bundle
+/vendor/bundle
+
+# Ignore cache
+/cache
+
+# Ignore logs
+/*.log
diff --git a/tools/autobuild/Gemfile b/tools/autobuild/Gemfile
new file mode 100644 (file)
index 0000000..a3d5321
--- /dev/null
@@ -0,0 +1,14 @@
+source :rubygems
+
+gem 'sinatra'
+gem 'haml'
+gem 'grit'
+
+group :development do
+  gem 'sinatra-contrib'
+
+  gem 'pry'
+  #gem 'pry-doc'
+  gem 'pry-debugger'
+  gem 'pry-stack_explorer'
+end
diff --git a/tools/autobuild/Gemfile.lock b/tools/autobuild/Gemfile.lock
new file mode 100644 (file)
index 0000000..88eca13
--- /dev/null
@@ -0,0 +1,63 @@
+GEM
+  remote: http://rubygems.org/
+  specs:
+    backports (2.6.5)
+    binding_of_caller (0.6.8)
+    coderay (1.0.8)
+    columnize (0.3.6)
+    debugger (1.1.4)
+      columnize (>= 0.3.1)
+      debugger-linecache (~> 1.1.1)
+      debugger-ruby_core_source (~> 1.1.3)
+    debugger-linecache (1.1.2)
+      debugger-ruby_core_source (>= 1.1.1)
+    debugger-ruby_core_source (1.1.4)
+    diff-lcs (1.1.3)
+    eventmachine (1.0.0)
+    grit (2.5.0)
+      diff-lcs (~> 1.1)
+      mime-types (~> 1.15)
+      posix-spawn (~> 0.3.6)
+    haml (3.1.7)
+    method_source (0.8.1)
+    mime-types (1.19)
+    posix-spawn (0.3.6)
+    pry (0.9.10)
+      coderay (~> 1.0.5)
+      method_source (~> 0.8)
+      slop (~> 3.3.1)
+    pry-debugger (0.2.0)
+      debugger (~> 1.1.3)
+      pry (~> 0.9.9)
+    pry-stack_explorer (0.4.7)
+      binding_of_caller (~> 0.6.8)
+    rack (1.4.1)
+    rack-protection (1.2.0)
+      rack
+    rack-test (0.6.2)
+      rack (>= 1.0)
+    sinatra (1.3.3)
+      rack (~> 1.3, >= 1.3.6)
+      rack-protection (~> 1.2)
+      tilt (~> 1.3, >= 1.3.3)
+    sinatra-contrib (1.3.2)
+      backports (>= 2.0)
+      eventmachine
+      rack-protection
+      rack-test
+      sinatra (~> 1.3.0)
+      tilt (~> 1.3)
+    slop (3.3.3)
+    tilt (1.3.3)
+
+PLATFORMS
+  ruby
+
+DEPENDENCIES
+  grit
+  haml
+  pry
+  pry-debugger
+  pry-stack_explorer
+  sinatra
+  sinatra-contrib
diff --git a/tools/autobuild/app.rb b/tools/autobuild/app.rb
new file mode 100644 (file)
index 0000000..7978b10
--- /dev/null
@@ -0,0 +1,119 @@
+require 'sinatra/base'
+require 'haml'
+require 'grit'
+require 'tmpdir'
+require 'fileutils'
+require 'pathname'
+require 'logger'
+
+class Grit::Tree
+  def project?
+    blobs.map(&:name).include? 'MainClass.txt'
+  end
+end
+
+class Grit::Blob
+  def project?
+    false
+  end
+end
+
+class Logger
+  attr_reader :logdev
+end
+
+$stdout.reopen open('stdout.log', 'a')
+$stdout.sync = true
+$stderr.reopen open('stderr.log', 'a')
+$stderr.sync = true
+
+class App < Sinatra::Base
+  if development?
+    require 'sinatra/reloader'
+    register Sinatra::Reloader
+  end
+
+  enable :logging
+
+  def initialize
+    super
+    @repo = Grit::Repo.new(ENV['GIT_DIR'] || File.expand_path('../../..', __FILE__))
+    @logger = Logger.new "autobuild.log"
+    @logdev = @logger.logdev.dev
+  end
+
+  def fetch
+    @repo.remote_fetch 'origin'
+  end
+
+  get '/' do
+    fetch
+    @remotes = @repo.remotes
+    haml :index
+  end
+
+  get %r!^/remotes/(.+)! do
+    remote_name = params[:captures].first
+    @remote = @repo.remotes.find { |r| r.name == remote_name }
+    return 404 if @remote.nil?
+    @objects = @remote.commit.tree.contents
+    haml :remote
+  end
+
+  get '/build/:sha/:project' do
+    @commit = @repo.commit params[:sha]
+    return 404 if @commit.nil?
+    @project = params[:project]
+    cache_dir = cache_dir @commit, @project
+    name_path = cache_dir.join 'MainClass.txt'
+
+    if not name_path.exist?
+      Dir.mktmpdir do |tmpdir|
+        Dir.chdir tmpdir do
+          fname = "archive.tar"
+          @repo.archive_to_file @commit, nil, fname, nil, 'cat'
+          system 'tar', 'xf', fname
+          Dir.chdir @project do
+            build_and_copy @commit, @project, cache_dir, name_path
+          end
+        end
+      end
+    end
+    @files = Dir.glob("#{cache_dir.to_s}/*.apk").map { |file| File.basename file }.sort
+    haml :build
+  end
+
+  get '/apk/:sha/:project/:file' do
+    commit = @repo.commit params[:sha]
+    return 404 if commit.nil?
+    project = params[:project]
+    cache_dir = cache_dir commit, project
+    name_path = cache_dir.join 'MainClass.txt'
+    file_path = cache_dir.join params[:file]
+    return 404 if not file_path.exist?
+
+    fname = name_path.read.chomp + ".apk"
+    send_file file_path.to_s, :filename => fname
+  end
+
+  def build_and_copy(commit, project, cache_dir, name_path)
+    @logger.info "Building #{commit} #{project}"
+    system 'rake', [:out, :err] => @logdev
+    FileUtils.cp 'MainClass.txt', name_path.to_s
+    Dir.glob('bin/*-debug.apk') do |file|
+      FileUtils.cp file, cache_dir.to_s
+    end
+  end
+
+  def cache_dir(commit, project)
+    dir = Pathname.new 'cache'
+    if not dir.exist?
+      dir.mkpath
+    end
+    dir = dir.join "#{commit}-#{project}"
+    if not dir.exist?
+      dir.mkpath
+    end
+    dir.realpath
+  end
+end
diff --git a/tools/autobuild/config.ru b/tools/autobuild/config.ru
new file mode 100644 (file)
index 0000000..093bfcf
--- /dev/null
@@ -0,0 +1,5 @@
+$LOAD_PATH.unshift File.dirname(__FILE__)
+require 'app'
+map '/autobuild' do
+  run App
+end
diff --git a/tools/autobuild/views/build.haml b/tools/autobuild/views/build.haml
new file mode 100644 (file)
index 0000000..413ef0e
--- /dev/null
@@ -0,0 +1,5 @@
+%ul
+  - @files.each do |file|
+    %li
+      download
+      %a{:href => "#{env['SCRIPT_NAME']}/apk/#{@commit}/#{@project}/#{file}"}= file
diff --git a/tools/autobuild/views/index.haml b/tools/autobuild/views/index.haml
new file mode 100644 (file)
index 0000000..62adfc7
--- /dev/null
@@ -0,0 +1,5 @@
+%div
+  %ul
+    - @remotes.each do |remote|
+      %li
+        %a{:href => "#{env['SCRIPT_NAME']}/remotes/#{remote.name}"}= remote.name
diff --git a/tools/autobuild/views/layout.haml b/tools/autobuild/views/layout.haml
new file mode 100644 (file)
index 0000000..5db723c
--- /dev/null
@@ -0,0 +1,8 @@
+!!! 5
+%html{:lang => 'ja'}
+  %head
+    %meta{:charset => 'utf-8'}
+    %meta{:name => 'viewport', :content => 'width=device-width'}
+    %title AutoBuild
+  %body
+    = yield
diff --git a/tools/autobuild/views/remote.haml b/tools/autobuild/views/remote.haml
new file mode 100644 (file)
index 0000000..a3e8511
--- /dev/null
@@ -0,0 +1,8 @@
+%div
+  %ul
+    - @objects.each do |obj|
+      - if obj.project?
+        %li
+          %a{:href => "#{env['SCRIPT_NAME']}/build/#{@remote.commit.sha}/#{obj.name}"}= obj.name
+      - else
+        %li= obj.name