ghpages_deploy 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e73a6dfd2d85194171c33b2efe0fac59a9454ae9
4
+ data.tar.gz: 996ebf3a6adf3d7b547478875b12757938b7b7b0
5
+ SHA512:
6
+ metadata.gz: 5f7e5e07bdef4625689b6a6550036409fada7c1fe7cf60e7dcf8bd2640638283a530c0ee25d78c56c55d13cacc6c7940ca48824533d11b54b2e4647ce943f7f0
7
+ data.tar.gz: 13fb1a221a429d4f97a36befd7e6e2b4a1c819928f56d4d6f31192dd88c2faec5c03ea5863dc92a0d379fc34d073c673f0e9008ec079db1fc7a9a6b70e026c8d
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) 2016 Nathan Currier
3
+
4
+ source 'https://rubygems.org'
5
+
6
+ gemspec
7
+
8
+ group :dev do
9
+ gem 'rake', require: false
10
+
11
+ # gem 'rubocop', '>= 0.36.0', require: false
12
+ end
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2013-2016, Nathan Currier
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ * Neither the name of the <organization> nor the
12
+ names of its contributors may be used to endorse or promote products
13
+ derived from this software without specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
19
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) 2016 Nathan Currier
3
+
4
+ require 'rubygems'
5
+ require 'bundler'
6
+
7
+ Bundler.setup(:default, :development)
8
+
9
+ require 'bundler/gem_tasks'
10
+
11
+ require 'rubocop/rake_task'
12
+ RuboCop::RakeTask.new(:rubocop) do |t|
13
+ t.fail_on_error = false
14
+ end
15
+ task('rubocop:auto_correct').clear
16
+
17
+ task default: %i(rubocop)
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) 2016 Nathan Currier
3
+
4
+ require './lib/ghpages_deploy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ghpages_deploy'
8
+ spec.version = GithubPages::VERSION
9
+ spec.authors = ['Nathan Currier']
10
+ spec.email = ['nathan.currier@gmail.com']
11
+ spec.license = 'BSD-3-Clause'
12
+
13
+ spec.description = 'Deploy to Github Pages'
14
+ spec.summary = 'Deploy your site to Github Pages'
15
+ spec.homepage = 'https://github.com/rideliner/ghpages_deploy'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.bindir = 'bin'
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'bundler', '>= 1.11.2'
24
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) 2016 Nathan Currier
3
+
4
+ require 'zlib'
5
+
6
+ require 'ghpages_deploy/util'
7
+
8
+ module GithubPages
9
+ class SiteCompressor
10
+ def initialize(directory)
11
+ @directory = directory
12
+ @level = Zlib::BEST_COMPRESSION
13
+ end
14
+
15
+ attr_accessor :level
16
+
17
+ def compress
18
+ GithubPages.all_nested_files(@directory).each do |item|
19
+ compress_file(item) if can_compress?(item) && !compressed?(item)
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def compress_file(filename)
26
+ Zlib::GzipWriter.open("#{filename}.gz", @level) do |gz|
27
+ gz.mtime = File.mtime(filename)
28
+ gz.orig_name = filename
29
+ gz.write File.binread(filename)
30
+ end
31
+ File.rename("#{filename}.gz", filename)
32
+ end
33
+
34
+ def can_compress?(filename)
35
+ ext = File.extname(filename)
36
+ return false if ext.empty?
37
+ %i(css js html).include?(ext[1..-1].to_sym)
38
+ end
39
+
40
+ GZIP_MAGIC = ['1F8B'].pack('H*')
41
+
42
+ def compressed?(filename)
43
+ File.open(filename) do |f|
44
+ f.readpartial(2) == GZIP_MAGIC
45
+ end
46
+ rescue EOFError
47
+ false
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) 2016 Nathan Currier
3
+
4
+ require 'ghpages_deploy/compress'
5
+ require 'ghpages_deploy/git_manager'
6
+
7
+ module GithubPages
8
+ class Deployer
9
+ def initialize(git, source, destinations)
10
+ @git = git
11
+ @source = source
12
+ @destinations = destinations
13
+ end
14
+
15
+ def deploy
16
+ SiteCompressor.new(@source).compress
17
+
18
+ @destinations.keep_if { |dest| deploy_site_to(dest) }
19
+
20
+ if @destinations.empty?
21
+ puts 'No changes detected, not commiting.'
22
+ else
23
+ @git.commit_and_push message
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ # @return [Boolean] true if there were changes to the destination
30
+ def deploy_site_to(dest)
31
+ # create the full path to the destination
32
+ FileUtils.mkdir_p(dest)
33
+
34
+ # remove files that are already cached in the destination directory
35
+ @git.ls_files(dest).each { |file| File.delete(file) }
36
+
37
+ # recursively copy all files from @source into dest
38
+ FileUtils.cp_r("#{@source}/.", dest)
39
+
40
+ stage_destination_files(dest)
41
+
42
+ # check if any changes were made to the destination
43
+ !@git.staged_modifications(dest).empty?
44
+ end
45
+
46
+ def stage_destination_files(dest)
47
+ files = GithubPages.all_nested_files(@source)
48
+ files.keep_if { |file| File.file?(file) }
49
+
50
+ files.map! do |file|
51
+ simple = file.sub(%r{^#{@source}/?}, '')
52
+ File.join(dest, simple)
53
+ end
54
+
55
+ @git.stage files
56
+ end
57
+
58
+ def message
59
+ # English join
60
+ msg =
61
+ if @destinations.length == 1
62
+ @destinations.first
63
+ elsif @destinations.length == 2
64
+ @destinations.join ' and '
65
+ else
66
+ @destinations[0..-2].join(', ') + ", and #{@destinations.last}"
67
+ end
68
+
69
+ "Deployed to #{msg}."
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,107 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) 2016 Nathan Currier
3
+
4
+ require 'git'
5
+
6
+ module GithubPages
7
+ class GitManager
8
+ def initialize(remote)
9
+ @remote = remote
10
+ @repo = `git config remote.#{remote}.url`.gsub(/^git:/, 'https:')
11
+ @branch =
12
+ if @repo =~ /\.github\.(?:com|io)\.git$/
13
+ 'master'
14
+ else
15
+ 'gh-pages'
16
+ end
17
+
18
+ @git = Git.open(Dir.pwd)
19
+ @original_branch = @git.lib.branch_current
20
+
21
+ setup
22
+ end
23
+
24
+ def self.open(remote)
25
+ git = new(remote)
26
+ yield git
27
+ git.cleanup
28
+ end
29
+
30
+ def cleanup
31
+ cleanup_credentials
32
+ restore_state
33
+ end
34
+
35
+ def stage(files)
36
+ @git.add files
37
+ end
38
+
39
+ def staged_modifications(dir)
40
+ cached_files('diff --name-only', dir)
41
+ end
42
+
43
+ def ls_files(dir)
44
+ cached_files('ls-files', dir)
45
+ end
46
+
47
+ def commit_and_push(msg)
48
+ @git.commit(msg)
49
+ @git.push(@git.remote(remote))
50
+ end
51
+
52
+ private
53
+
54
+ attr_reader :remote, :branch, :repo
55
+
56
+ def cached_files(cmd, dir)
57
+ git("#{cmd} --cached -z #{dir}").split("\0")
58
+ end
59
+
60
+ CREDENTIALS_FILE = '.git/credentials'.freeze
61
+
62
+ def git(command)
63
+ `git #{command}`
64
+ end
65
+
66
+ def setup
67
+ setup_repo
68
+ setup_user
69
+ setup_credentials
70
+ setup_branch
71
+ end
72
+
73
+ def setup_repo
74
+ @git.add_remote(remote, repo) unless @git.remote(remote)
75
+ @git.remote(remote).fetch
76
+ end
77
+
78
+ def setup_user
79
+ @git.config('user.name', ENV['GIT_NAME']) if ENV['GIT_NAME']
80
+ @git.config('user.email', ENV['GIT_EMAIL']) if ENV['GIT_EMAIL']
81
+ end
82
+
83
+ def setup_branch
84
+ if @git.is_local_branch?(branch) || @git.is_remote_branch?(branch)
85
+ @git.branch(branch).checkout
86
+ else
87
+ git "checkout --orphan #{branch}"
88
+ end
89
+ end
90
+
91
+ def setup_credentials
92
+ @git.config('credential.helper', "store --file=#{CREDENTIALS_FILE}")
93
+ File.open(CREDENTIALS_FILE, 'w') do |file|
94
+ token = ENV['GITHUB_OAUTH_TOKEN']
95
+ file.write "https://#{token}:x-oauth-basic@github.com"
96
+ end
97
+ end
98
+
99
+ def cleanup_credentials
100
+ # TODO
101
+ end
102
+
103
+ def restore_state
104
+ @git.checkout(@original_branch)
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) 2016 Nathan Currier
3
+
4
+ require 'rake'
5
+ require 'rake/tasklib'
6
+
7
+ require 'ghpages_deploy/deployer'
8
+ require 'ghpages_deploy/git_manager'
9
+
10
+ module GithubPages
11
+ class DeployTask < ::Rake::TaskLib
12
+ def initialize(name = :deploy)
13
+ @name = name
14
+ @destinations = []
15
+
16
+ yield self if block_given?
17
+
18
+ @source ||= '.'
19
+ @remote ||= 'origin'
20
+
21
+ @destinations << '.' if @destinations.empty?
22
+
23
+ define
24
+ end
25
+
26
+ attr_accessor :name
27
+
28
+ attr_accessor :remote, :source
29
+
30
+ def register(destination)
31
+ @destinations << destination
32
+ end
33
+
34
+ private
35
+
36
+ def define
37
+ task @name do
38
+ GitManager.open(@remote) do |git|
39
+ deployer = Deployer.new(git, @source, @destinations)
40
+ deployer.deploy
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) 2016 Nathan Currier
3
+
4
+ module GithubPages
5
+ def all_nested_files(dir)
6
+ files = Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH)
7
+ files.delete_if { |file| file =~ %r{(^|/)?\.?\.$} }
8
+ end
9
+ module_function :all_nested_files
10
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) 2016 Nathan Currier
3
+
4
+ module GithubPages
5
+ VERSION = '1.0.0'.freeze
6
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) 2016 Nathan Currier
3
+
4
+ require 'ghpages_deploy/compress'
5
+ require 'ghpages_deploy/deployer'
6
+ require 'ghpages_deploy/git_manager'
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ghpages_deploy
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Currier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ type: :runtime
15
+ name: bundler
16
+ prerelease: false
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: 1.11.2
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.11.2
27
+ description: Deploy to Github Pages
28
+ email:
29
+ - nathan.currier@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - LICENSE
36
+ - Rakefile
37
+ - ghpages_deploy.gemspec
38
+ - lib/ghpages_deploy.rb
39
+ - lib/ghpages_deploy/compress.rb
40
+ - lib/ghpages_deploy/deployer.rb
41
+ - lib/ghpages_deploy/git_manager.rb
42
+ - lib/ghpages_deploy/rake_task.rb
43
+ - lib/ghpages_deploy/util.rb
44
+ - lib/ghpages_deploy/version.rb
45
+ homepage: https://github.com/rideliner/ghpages_deploy
46
+ licenses:
47
+ - BSD-3-Clause
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.4.8
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Deploy your site to Github Pages
69
+ test_files: []