diff options
Diffstat (limited to 'lib/bundler/vendor/thor/actions')
-rw-r--r--[-rwxr-xr-x] | lib/bundler/vendor/thor/actions/create_file.rb | 4 | ||||
-rw-r--r-- | lib/bundler/vendor/thor/actions/create_link.rb | 57 | ||||
-rw-r--r--[-rwxr-xr-x] | lib/bundler/vendor/thor/actions/directory.rb | 4 | ||||
-rw-r--r--[-rwxr-xr-x] | lib/bundler/vendor/thor/actions/empty_directory.rb | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | lib/bundler/vendor/thor/actions/file_manipulation.rb | 71 | ||||
-rw-r--r--[-rwxr-xr-x] | lib/bundler/vendor/thor/actions/inject_into_file.rb | 25 |
6 files changed, 132 insertions, 29 deletions
diff --git a/lib/bundler/vendor/thor/actions/create_file.rb b/lib/bundler/vendor/thor/actions/create_file.rb index 5541ad5f..ed5973a4 100755..100644 --- a/lib/bundler/vendor/thor/actions/create_file.rb +++ b/lib/bundler/vendor/thor/actions/create_file.rb @@ -18,7 +18,7 @@ class Thor # "vhost.name = #{hostname}" # end # - # create_file "config/apach.conf", "your apache config" + # create_file "config/apache.conf", "your apache config" # def create_file(destination, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} @@ -27,7 +27,7 @@ class Thor end alias :add_file :create_file - # AddFile is a subset of Template, which instead of rendering a file with + # CreateFile is a subset of Template, which instead of rendering a file with # ERB, it gets the content from the user. # class CreateFile < EmptyDirectory #:nodoc: diff --git a/lib/bundler/vendor/thor/actions/create_link.rb b/lib/bundler/vendor/thor/actions/create_link.rb new file mode 100644 index 00000000..1975644a --- /dev/null +++ b/lib/bundler/vendor/thor/actions/create_link.rb @@ -0,0 +1,57 @@ +require 'thor/actions/create_file' + +class Thor + module Actions + + # Create a new file relative to the destination root from the given source. + # + # ==== Parameters + # destination<String>:: the relative path to the destination root. + # source<String|NilClass>:: the relative path to the source root. + # config<Hash>:: give :verbose => false to not log the status. + # :: give :symbolic => false for hard link. + # + # ==== Examples + # + # create_link "config/apache.conf", "/etc/apache.conf" + # + def create_link(destination, *args, &block) + config = args.last.is_a?(Hash) ? args.pop : {} + source = args.first + action CreateLink.new(self, destination, source, config) + end + alias :add_link :create_link + + # CreateLink is a subset of CreateFile, which instead of taking a block of + # data, just takes a source string from the user. + # + class CreateLink < CreateFile #:nodoc: + attr_reader :data + + # Checks if the content of the file at the destination is identical to the rendered result. + # + # ==== Returns + # Boolean:: true if it is identical, false otherwise. + # + def identical? + exists? && File.identical?(render, destination) + end + + def invoke! + invoke_with_conflict_check do + FileUtils.mkdir_p(File.dirname(destination)) + # Create a symlink by default + config[:symbolic] ||= true + File.unlink(destination) if exists? + if config[:symbolic] + File.symlink(render, destination) + else + File.link(render, destination) + end + end + given_destination + end + + end + end +end diff --git a/lib/bundler/vendor/thor/actions/directory.rb b/lib/bundler/vendor/thor/actions/directory.rb index 717508eb..dc238939 100755..100644 --- a/lib/bundler/vendor/thor/actions/directory.rb +++ b/lib/bundler/vendor/thor/actions/directory.rb @@ -21,7 +21,7 @@ class Thor # directory "doc" # # It will create a doc directory in the destination with the following - # files (assuming that the app_name is "blog"): + # files (assuming that the `app_name` method returns the value "blog"): # # doc/ # components/ @@ -70,7 +70,7 @@ class Thor lookup = config[:recursive] ? File.join(source, '**') : source lookup = File.join(lookup, '{*,.[a-z]*}') - Dir[lookup].each do |file_source| + Dir[lookup].sort.each do |file_source| next if File.directory?(file_source) file_destination = File.join(given_destination, file_source.gsub(source, '.')) file_destination.gsub!('/./', '/') diff --git a/lib/bundler/vendor/thor/actions/empty_directory.rb b/lib/bundler/vendor/thor/actions/empty_directory.rb index 484cb820..484cb820 100755..100644 --- a/lib/bundler/vendor/thor/actions/empty_directory.rb +++ b/lib/bundler/vendor/thor/actions/empty_directory.rb diff --git a/lib/bundler/vendor/thor/actions/file_manipulation.rb b/lib/bundler/vendor/thor/actions/file_manipulation.rb index e9494626..ad049b3c 100755..100644 --- a/lib/bundler/vendor/thor/actions/file_manipulation.rb +++ b/lib/bundler/vendor/thor/actions/file_manipulation.rb @@ -30,6 +30,28 @@ class Thor end end + # Links the file from the relative source to the relative destination. If + # the destination is not given it's assumed to be equal to the source. + # + # ==== Parameters + # source<String>:: the relative path to the source root. + # destination<String>:: the relative path to the destination root. + # config<Hash>:: give :verbose => false to not log the status. + # + # ==== Examples + # + # link_file "README", "doc/README" + # + # link_file "doc/README" + # + def link_file(source, *args, &block) + config = args.last.is_a?(Hash) ? args.pop : {} + destination = args.first || source + source = File.expand_path(find_in_source_paths(source.to_s)) + + create_link destination, source, config + end + # Gets the content at the given address and places it at the given relative # destination. If a block is given instead of destination, the content of # the url is yielded and used as location. @@ -51,7 +73,7 @@ class Thor config = args.last.is_a?(Hash) ? args.pop : {} destination = args.first - source = File.expand_path(find_in_source_paths(source.to_s)) unless source =~ /^http\:\/\// + source = File.expand_path(find_in_source_paths(source.to_s)) unless source =~ /^https?\:\/\// render = open(source) {|input| input.binmode.read } destination ||= if block_given? @@ -80,13 +102,13 @@ class Thor # def template(source, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} - destination = args.first || source + destination = args.first || source.sub(/\.tt$/, '') source = File.expand_path(find_in_source_paths(source.to_s)) context = instance_eval('binding') create_file destination, nil, config do - content = ERB.new(::File.binread(source), nil, '-').result(context) + content = ERB.new(::File.binread(source), nil, '-', '@output_buffer').result(context) content = block.call(content) if block content end @@ -110,7 +132,7 @@ class Thor FileUtils.chmod_R(mode, path) unless options[:pretend] end - # Prepend text to a file. Since it depends on inject_into_file, it's reversible. + # Prepend text to a file. Since it depends on insert_into_file, it's reversible. # # ==== Parameters # path<String>:: path of the file to be changed @@ -119,19 +141,20 @@ class Thor # # ==== Example # - # prepend_file 'config/environments/test.rb', 'config.gem "rspec"' + # prepend_to_file 'config/environments/test.rb', 'config.gem "rspec"' # - # prepend_file 'config/environments/test.rb' do + # prepend_to_file 'config/environments/test.rb' do # 'config.gem "rspec"' # end # - def prepend_file(path, *args, &block) + def prepend_to_file(path, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} config.merge!(:after => /\A/) - inject_into_file(path, *(args << config), &block) + insert_into_file(path, *(args << config), &block) end + alias_method :prepend_file, :prepend_to_file - # Append text to a file. Since it depends on inject_into_file, it's reversible. + # Append text to a file. Since it depends on insert_into_file, it's reversible. # # ==== Parameters # path<String>:: path of the file to be changed @@ -140,20 +163,21 @@ class Thor # # ==== Example # - # append_file 'config/environments/test.rb', 'config.gem "rspec"' + # append_to_file 'config/environments/test.rb', 'config.gem "rspec"' # - # append_file 'config/environments/test.rb' do + # append_to_file 'config/environments/test.rb' do # 'config.gem "rspec"' # end # - def append_file(path, *args, &block) + def append_to_file(path, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} config.merge!(:before => /\z/) - inject_into_file(path, *(args << config), &block) + insert_into_file(path, *(args << config), &block) end + alias_method :append_file, :append_to_file # Injects text right after the class definition. Since it depends on - # inject_into_file, it's reversible. + # insert_into_file, it's reversible. # # ==== Parameters # path<String>:: path of the file to be changed @@ -172,7 +196,7 @@ class Thor def inject_into_class(path, klass, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} config.merge!(:after => /class #{klass}\n|class #{klass} .*\n/) - inject_into_file(path, *(args << config), &block) + insert_into_file(path, *(args << config), &block) end # Run a regular expression replacement on a file. @@ -225,5 +249,22 @@ class Thor end alias :remove_dir :remove_file + private + attr_accessor :output_buffer + def concat(string) + @output_buffer.concat(string) + end + + def capture(*args, &block) + with_output_buffer { block.call(*args) } + end + + def with_output_buffer(buf = '') #:nodoc: + self.output_buffer, old_buffer = buf, output_buffer + yield + output_buffer + ensure + self.output_buffer = old_buffer + end end end diff --git a/lib/bundler/vendor/thor/actions/inject_into_file.rb b/lib/bundler/vendor/thor/actions/inject_into_file.rb index 812c2e7d..c48cfab5 100755..100644 --- a/lib/bundler/vendor/thor/actions/inject_into_file.rb +++ b/lib/bundler/vendor/thor/actions/inject_into_file.rb @@ -10,19 +10,19 @@ class Thor # destination<String>:: Relative path to the destination root # data<String>:: Data to add to the file. Can be given as a block. # config<Hash>:: give :verbose => false to not log the status and the flag - # for injection (:after or :before) or :force => true for + # for injection (:after or :before) or :force => true for # insert two or more times the same content. - # + # # ==== Examples # - # inject_into_file "config/environment.rb", "config.gem :thor", :after => "Rails::Initializer.run do |config|\n" + # insert_into_file "config/environment.rb", "config.gem :thor", :after => "Rails::Initializer.run do |config|\n" # - # inject_into_file "config/environment.rb", :after => "Rails::Initializer.run do |config|\n" do + # insert_into_file "config/environment.rb", :after => "Rails::Initializer.run do |config|\n" do # gems = ask "Which gems would you like to add?" # gems.split(" ").map{ |gem| " config.gem :#{gem}" }.join("\n") # end # - def inject_into_file(destination, *args, &block) + def insert_into_file(destination, *args, &block) if block_given? data, config = block, args.shift else @@ -30,6 +30,7 @@ class Thor end action InjectIntoFile.new(self, destination, data, config) end + alias_method :inject_into_file, :insert_into_file class InjectIntoFile < EmptyDirectory #:nodoc: attr_reader :replacement, :flag, :behavior @@ -76,12 +77,16 @@ class Thor protected def say_status(behavior) - status = if flag == /\A/ - behavior == :invoke ? :prepend : :unprepend - elsif flag == /\z/ - behavior == :invoke ? :append : :unappend + status = if behavior == :invoke + if flag == /\A/ + :prepend + elsif flag == /\z/ + :append + else + :insert + end else - behavior == :invoke ? :inject : :deinject + :subtract end super(status, config[:verbose]) |