Permalink
Browse files

Add upload task

  • Loading branch information...
1 parent 897dcb3 commit 4f33cf41bec4b1324bf44032e699d9ff700eaeee @josepjaume josepjaume committed Jun 12, 2012
Showing with 165 additions and 20 deletions.
  1. +1 −0 .gitignore
  2. +0 −14 Assetfile
  3. +2 −0 Gemfile
  4. +28 −6 Gemfile.lock
  5. +47 −0 Rakefile
  6. +87 −0 lib/github_uploader.rb
View
@@ -7,3 +7,4 @@ tests/source/
.project
tests/ember-forms-tests.js
+.github-upload-token
View
@@ -59,20 +59,6 @@ input "packages" do
end
input "packages" do
- match "*/lib/**/*.js" do
- minispade :rewrite_requires => true, :string => true, :module_id_generator => proc { |input|
- id = input.path.dup
- id.sub!('/lib/', '/')
- id.sub!(/\.js$/, '')
- id.sub!(/\/main$/, '')
- id
- }
-
- concat "ember-forms-spade.js"
- end
-end
-
-input "packages" do
match "*/lib/**/main.js" do
neuter(
:additional_dependencies => proc { |input|
View
@@ -8,4 +8,6 @@ gem "uglifier", "~> 1.0.3"
group :development do
gem "rack"
gem "kicker"
+ gem 'rest-client'
+ gem 'github_api'
end
View
@@ -1,14 +1,14 @@
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
@@ -18,15 +18,35 @@ GEM
remote: http://rubygems.org/
specs:
colored (1.2)
- execjs (1.3.0)
+ execjs (1.4.0)
multi_json (~> 1.0)
+ 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
- multi_json (1.2.0)
+ mime-types (1.18)
+ multi_json (1.3.6)
+ multipart-post (1.1.5)
+ nokogiri (1.5.4)
+ 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)
@@ -36,8 +56,10 @@ PLATFORMS
DEPENDENCIES
colored
+ github_api
kicker
rack
rake-pipeline!
rake-pipeline-web-filters!
+ rest-client
uglifier (~> 1.0.3)
View
@@ -80,4 +80,51 @@ task :autotest do
system("kicker -e 'rake test' packages")
end
+def setup_uploader(root=Dir.pwd)
+ require './lib/github_uploader'
+
+ login = origin = nil
+
+ Dir.chdir(root) do
+ # 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
+ end
+
+ repoUrl = origin.match(/github\.com[\/:]((.+?)\/(.+?))(\.git)?$/)
+ username = ENV['GH_USERNAME'] || repoUrl[2] # username part of origin url
+ repo = ENV['GH_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 "Upload the files to github"
+task :upload do
+ uploader = setup_uploader
+ upload_file(uploader, 'ember-forms.js', "" , 'dist/ember-forms.js')
+ upload_file(uploader, 'ember-forms.min.js', "" , 'dist/ember-forms.min.js')
+ upload_file(uploader, 'ember-forms.prod.js', "" , 'dist/ember-forms.prod.js')
+end
+
task :default => :test
@@ -0,0 +1,87 @@
+require "rest-client"
+require "github_api"
+require 'json'
+
+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 4f33cf4

Please sign in to comment.