diff --git a/USAGE b/USAGE index c2f49ab..ae6b3db 100644 --- a/USAGE +++ b/USAGE @@ -30,6 +30,10 @@ activate :deploy do |deploy| # strategy is optional (default is :force_push) deploy.strategy = :submodule + + # Git credentials helper (default is :none_helper) + deploy.credentials = :cache_helper + deploy.credentials_timeout = 3600 # (default is 12h) end # To deploy the build directory to a remote host via ftp: diff --git a/lib/middleman-deploy/commands.rb b/lib/middleman-deploy/commands.rb index 4f274a3..cbf54d3 100644 --- a/lib/middleman-deploy/commands.rb +++ b/lib/middleman-deploy/commands.rb @@ -3,6 +3,7 @@ require "middleman-deploy/extension" require "middleman-deploy/methods" require "middleman-deploy/strategies" +require "middleman-deploy/credentials" require "middleman-deploy/pkg-info" module Middleman diff --git a/lib/middleman-deploy/credentials.rb b/lib/middleman-deploy/credentials.rb new file mode 100644 index 0000000..e84b9d6 --- /dev/null +++ b/lib/middleman-deploy/credentials.rb @@ -0,0 +1,2 @@ +require 'middleman-deploy/credentials/git/cache' +require 'middleman-deploy/credentials/git/none' diff --git a/lib/middleman-deploy/credentials/git/cache.rb b/lib/middleman-deploy/credentials/git/cache.rb new file mode 100644 index 0000000..bf4b568 --- /dev/null +++ b/lib/middleman-deploy/credentials/git/cache.rb @@ -0,0 +1,27 @@ + +module Middleman + module Deploy + module Credentials + module Git + class CacheHelper + attr_accessor :timeout + def initialize(timeout) + self.timeout = timeout + end + def save_credentials + unless credentials_active? + `git config --global credential.helper 'cache --timeout=#{timeout}' ` + puts "Git credentials-helper has cached your data for #{timeout} seconds" + else + puts "Git credentials already active" + end + end + + def credentials_active? + `git config --global credential.helper` != '' + end + end + end + end + end +end diff --git a/lib/middleman-deploy/credentials/git/none.rb b/lib/middleman-deploy/credentials/git/none.rb new file mode 100644 index 0000000..8399273 --- /dev/null +++ b/lib/middleman-deploy/credentials/git/none.rb @@ -0,0 +1,15 @@ + +module Middleman + module Deploy + module Credentials + module Git + class NoneHelper + def initialize(*args); end + def save_credentials + # No implementation for none helper + end + end + end + end + end +end diff --git a/lib/middleman-deploy/extension.rb b/lib/middleman-deploy/extension.rb index 58eaf3b..c98cc3e 100644 --- a/lib/middleman-deploy/extension.rb +++ b/lib/middleman-deploy/extension.rb @@ -5,7 +5,7 @@ module Middleman module Deploy - class Options < Struct.new(:whatisthis, :method, :host, :port, :user, :password, :path, :clean, :remote, :branch, :strategy, :build_before, :flags, :commit_message); end + class Options < Struct.new(:whatisthis, :method, :host, :port, :user, :password, :path, :clean, :remote, :branch, :strategy, :build_before, :flags, :commit_message, :credentials, :credentials_timeout); end class << self @@ -26,7 +26,9 @@ def registered(app, options_hash={}, &block) options.branch ||= "gh-pages" options.strategy ||= :force_push options.commit_message ||= nil - + options.credentials ||= :none_helper + options.credentials_timeout ||= 3600 * 12 + options.build_before ||= false @@options = options diff --git a/lib/middleman-deploy/methods/git.rb b/lib/middleman-deploy/methods/git.rb index d772013..7286846 100644 --- a/lib/middleman-deploy/methods/git.rb +++ b/lib/middleman-deploy/methods/git.rb @@ -11,6 +11,13 @@ def process strategy_instance = strategy_class_name.constantize.new(self.server_instance.build_dir, self.options.remote, self.options.branch, self.options.commit_message) strategy_instance.process + + camelized_helper = self.options.credentials.to_s.split('_').map { |word| word.capitalize}.join + helper_class_name = "Middleman::Deploy::Credentials::Git::#{camelized_helper}" + helper_instance = helper_class_name.constantize.new(self.options.credentials_timeout) + + helper_instance.save_credentials + end end