Skip to content

Commit

Permalink
Add upload_latest rake task
Browse files Browse the repository at this point in the history
  • Loading branch information
pangratz committed Jun 9, 2012
1 parent 7d2f845 commit bceb024
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -7,4 +7,6 @@ dist/
.DS_Store
.project

.github-upload-token

tests/ember-data-tests.js
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -6,3 +6,5 @@ before_script:
- "sh -e /etc/init.d/xvfb start"
- "rake clean"
script: "rake test[all]"
notifications:
webhooks: http://emberjs-master-builds.herokuapp.com/upload/data
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -7,7 +7,8 @@ gem "uglifier", "~> 1.0.3"

group :development do
gem "rack"
gem "github-upload"
gem "rest-client"
gem "github_api"
gem "ember-docs", :git => "https://github.com/emberjs/docs-generator.git"
gem "kicker"
end
52 changes: 29 additions & 23 deletions Gemfile.lock
@@ -1,22 +1,22 @@
GIT
remote: https://github.com/emberjs/docs-generator.git
revision: cfbb82496c1c342ff6da49fc46559dd8fddf8b55
revision: cfbc2a3aabc08de305224b290ad8f37f3ef5f847
specs:
ember-docs (0.1)
rack
thor

GIT
remote: https://github.com/livingsocial/rake-pipeline.git
revision: b70ca6cad7655e58d13031f3e24df7dfc74f9030
revision: 543f4322fe70facee9572d29ddabf7f090dad68a
specs:
rake-pipeline (0.6.0)
rake (~> 0.9.0)
thor

GIT
remote: https://github.com/wycats/rake-pipeline-web-filters.git
revision: ba0b8a00356b4c854930a8e849b5629d51ffd70f
revision: 81a22fb0808dfdeab8ed92d5d8c898ad198b9938
specs:
rake-pipeline-web-filters (0.6.0)
rack
Expand All @@ -26,30 +26,35 @@ GEM
remote: http://rubygems.org/
specs:
colored (1.2)
confparser (0.0.2.1)
execjs (1.3.0)
execjs (1.4.0)
multi_json (~> 1.0)
faster_xml_simple (0.5.0)
libxml-ruby (>= 0.3.8.4)
github-upload (0.0.2)
confparser
net-github-upload (>= 0.0.6)
httpclient (2.2.4)
json (1.6.6)
faraday (0.8.1)
multipart-post (~> 1.1)
github_api (0.5.4)
faraday (~> 0.8.0)
hashie (~> 1.2.0)
multi_json (~> 1.3)
nokogiri (~> 1.5.2)
oauth2 (~> 0.7)
hashie (1.2.0)
httpauth (0.1)
kicker (2.5.0)
rb-fsevent
libxml-ruby (2.3.2)
multi_json (1.2.0)
net-github-upload (0.0.8)
faster_xml_simple
httpclient
json
nokogiri (>= 1.4.0)
nokogiri (1.5.2)
mime-types (1.18)
multi_json (1.3.6)
multipart-post (1.1.5)
nokogiri (1.5.3)
oauth2 (0.7.1)
faraday (~> 0.8)
httpauth (~> 0.1)
multi_json (~> 1.0)
rack (~> 1.4)
rack (1.4.1)
rake (0.9.2.2)
rb-fsevent (0.9.0)
thor (0.14.6)
rb-fsevent (0.9.1)
rest-client (1.6.7)
mime-types (>= 1.16)
thor (0.15.2)
uglifier (1.0.4)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
Expand All @@ -60,9 +65,10 @@ PLATFORMS
DEPENDENCIES
colored
ember-docs!
github-upload
github_api
kicker
rack
rake-pipeline!
rake-pipeline-web-filters!
rest-client
uglifier (~> 1.0.3)
43 changes: 43 additions & 0 deletions Rakefile
Expand Up @@ -10,6 +10,40 @@ def pipeline
Rake::Pipeline::Project.new("Assetfile")
end

def setup_uploader
require './lib/github_uploader'

# get the github user name
login = `git config github.user`.chomp

# get repo from git config's origin url
origin = `git config remote.origin.url`.chomp # url to origin
# extract USERNAME/REPO_NAME
# sample urls: https://github.com/emberjs/ember.js.git
# git://github.com/emberjs/ember.js.git
# git@github.com:emberjs/ember.js.git
# git@github.com:emberjs/ember.js

repoUrl = origin.match(/github\.com[\/:]((.+?)\/(.+?))(\.git)?$/)
username = repoUrl[2] # username part of origin url
repo = repoUrl[3] # repository name part of origin url

token = ENV["GH_OAUTH_TOKEN"]
uploader = GithubUploader.new(login, username, repo, token)
uploader.authorize

uploader
end

def upload_file(uploader, filename, description, file)
print "Uploading #{filename}..."
if uploader.upload_file(filename, description, file)
puts "Success"
else
puts "Failure"
end
end

desc "Strip trailing whitespace for JavaScript files in packages"
task :strip_whitespace do
Dir["packages/**/*.js"].each do |name|
Expand All @@ -34,6 +68,15 @@ task :clean do
puts "Done"
end

desc "Upload latest Ember Data build to GitHub repository"
task :upload_latest => :dist do
uploader = setup_uploader

# Upload minified first, so non-minified shows up on top
upload_file(uploader, 'ember-data-latest.min.js', "Ember Data Master (minified)", "dist/ember-data.min.js")
upload_file(uploader, 'ember-data-latest.js', "Ember Data Master", "dist/ember-data.js")
end

desc "Run tests with phantomjs"
task :test, [:suite] => :dist do |t, args|
unless system("which phantomjs > /dev/null 2>&1")
Expand Down
86 changes: 86 additions & 0 deletions lib/github_uploader.rb
@@ -0,0 +1,86 @@
require "rest-client"
require "github_api"

class GithubUploader

def initialize(login, username, repo, token=nil, root=Dir.pwd)
@login = login
@username = username
@repo = repo
@root = root
@token = token || check_token
end

def authorized?
!!@token
end

def token_path
File.expand_path(".github-upload-token", @root)
end

def check_token
File.exist?(token_path) ? File.open(token_path, "rb").read : nil
end

def authorize
return if authorized?

puts "There is no file named .github-upload-token in this folder. This file holds the OAuth token needed to communicate with GitHub."
puts "You will be asked to enter your GitHub password so a new OAuth token will be created."
print "GitHub Password: "
system "stty -echo" # disable echoing of entered chars so password is not shown on console
pw = STDIN.gets.chomp
system "stty echo" # enable echoing of entered chars
puts ""

# check if the user already granted access for Ember.js Uploader by checking the available authorizations
response = RestClient.get "https://#{@login}:#{pw}@api.github.com/authorizations"
JSON.parse(response.to_str).each do |auth|
if auth["note"] == "Ember.js Uploader"
# user already granted access, so we reuse the existing token
@token = auth["token"]
end
end

## we need to create a new token
unless @token
payload = {
:scopes => ["public_repo"],
:note => "Ember.js Uploader",
:note_url => "https://github.com/#{@username}/#{@repo}"
}
response = RestClient.post "https://#{@login}:#{pw}@api.github.com/authorizations", payload.to_json, :content_type => :json
@token = JSON.parse(response.to_str)["token"]
end

# finally save the token into .github-upload-token
File.open(".github-upload-token", 'w') {|f| f.write(@token)}
end

def upload_file(filename, description, file)
return false unless authorized?

gh = Github.new :user => @username, :repo => @repo, :oauth_token => @token

# remvove previous download with the same name
gh.repos.downloads.list @username, @repo do |download|
if filename == download.name
gh.repos.downloads.delete @username, @repo, download.id
break
end
end

# step 1
hash = gh.repos.downloads.create @username, @repo,
"name" => filename,
"size" => File.size(file),
"description" => description

# step 2
gh.repos.downloads.upload hash, file

return true
end

end

0 comments on commit bceb024

Please sign in to comment.