rstrip 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ tmp/*
6
+ .rstrip
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem 'rake'
5
+ end
6
+ # Specify your gem's dependencies in rstrip.gemspec
7
+ gemspec
@@ -0,0 +1,33 @@
1
+ == rstrip
2
+
3
+ rstrip provides a ruby executable that removes the trailing white space in the current project directory.
4
+
5
+ It also remove the last empty line if there is one.
6
+
7
+ It's configurable on the file types it will operate on.
8
+
9
+ == Usage
10
+ * Install the gem: gem install rstrip
11
+ * In your project folder run `rstrip config generate`, or run `rstrip config generate --global true` to generate the config file in your home directory
12
+ * Modify the list of file extensions generated in the .rstrip config file
13
+ * In your project folder run `rstrip process`
14
+ * Example the change and commit the code
15
+
16
+ == Alternatives:
17
+ There are a lot of ways to do the similar thing:
18
+
19
+ * textmate bundle
20
+ https://github.com/mocoso/code-beautifier.tmbundle
21
+
22
+ * textmate marco
23
+ http://mjijackson.com/2009/01/textmate-macro-remove-trailing-space-and-save
24
+
25
+ * bash
26
+ http://stackoverflow.com/questions/149057/how-to-removing-trailing-whitespace-of-all-files-recursively
27
+
28
+ * Use a git prehook
29
+ http://stackoverflow.com/questions/591923/make-git-automatically-remove-trailing-whitespace-before-committing
30
+
31
+ I used the textmate marco before. The problem with textmate bundle or marco is they only modify the file if you edit and save it, which makes the commits harder to read as you will remove white spaces gradually. rstrip gets it done all in once in the folder level. Then you can use the bundle or marco with a fresh start.
32
+
33
+ rstrip also tries to make it as easy as possible so you don't need to remember the cryptic bash or perl commands.
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rstrip/cli'
4
+ Rstrip::CLI.start
@@ -0,0 +1,60 @@
1
+ Feature: Config
2
+ In order to use rstrip
3
+ I have to have a .rstrip in the current directory or my home directory
4
+
5
+ Scenario: Generate the .rstrip in the current directory
6
+ When I run `rstrip config generate`
7
+ Then a file named ".rstrip" should exist
8
+ And the file ".rstrip" should contain "rb"
9
+ And the file ".rstrip" should contain "erb"
10
+
11
+ Scenario: Prompt to run the generator
12
+ When I run `rstrip config list`
13
+ Then the output should contain "Please run 'rstrip config generate'"
14
+
15
+ Scenario: Show the list of files that will be operated on
16
+ Given a file named ".rstrip" with:
17
+ """
18
+ rb
19
+ erb
20
+ """
21
+ Given an empty file named "lib/dummy.rb"
22
+ Given an empty file named "dummy.erb"
23
+ When I run `rstrip config list`
24
+ Then the output should contain "in the current directory"
25
+ And the stdout should contain "lib/dummy.rb"
26
+ And the stdout should contain "dummy.erb"
27
+
28
+ Scenario: Don't show the files that's not in .rstrip
29
+ Given a file named ".rstrip" with:
30
+ """
31
+ rb
32
+ """
33
+ Given an empty file named "lib/dummy.rb"
34
+ Given an empty file named "dummy.erb"
35
+ When I run `rstrip config list`
36
+ And the stdout should contain "lib/dummy.rb"
37
+ And the stdout should not contain "dummy.erb"
38
+
39
+ Scenario: Run rstrip should remove the last new lines
40
+ Given a file named ".rstrip" with:
41
+ """
42
+ rb
43
+ """
44
+ Given a file named "lib/dummy.rb" with:
45
+ """
46
+ def a
47
+ puts 'hello'
48
+ end
49
+
50
+
51
+ """
52
+ When I run `rstrip process`
53
+ Then the stdout should contain "process lib/dummy.rb"
54
+ And a file named "lib/dummy.rb" should exist
55
+ And the file "lib/dummy.rb" should contain exactly:
56
+ """
57
+ def a
58
+ puts 'hello'
59
+ end
60
+ """
@@ -0,0 +1 @@
1
+ require 'aruba/cucumber'
@@ -0,0 +1,3 @@
1
+ module Rstrip
2
+ # Your code goes here...
3
+ end
@@ -0,0 +1,41 @@
1
+ require 'thor'
2
+ require 'rstrip/rstrip'
3
+ module Rstrip
4
+ class CLI < Thor
5
+
6
+ include Thor::Actions
7
+
8
+ desc "config", "generate: to generate a .rstrip file. list: to show the files that will be operated on"
9
+ long_desc <<-D
10
+ config can receive two arguments.
11
+
12
+ generate: Generate a .rstrip config file containing file extensions rstrip will operate on.Defaults to the current
13
+ directory.Use --global true to generate .rstrip to the user's home directory.\n
14
+ list: Show the location of the .rstrip config file that will be used. And show a list of files that will be
15
+ operated on based on the .rstrip config file
16
+ D
17
+ method_option :global, :type => :boolean, :aliases => "-g", :default => false
18
+ def config(task)
19
+ case task.downcase
20
+ when 'generate'
21
+ if options[:global]
22
+ template("rstrip.tt.txt", "#{Dir.home}/.rstrip")
23
+ else
24
+ template("rstrip.tt.txt", "#{Dir.pwd}/.rstrip")
25
+ end
26
+ when 'list'
27
+ Rstrip.list
28
+ end
29
+ end
30
+
31
+ desc "process", "run rstrip in the current folder recursively"
32
+ def process
33
+ Rstrip.list.collect{ |f| Rstrip.process(f) }
34
+ end
35
+
36
+ def self.source_root
37
+ File.dirname(__FILE__) + "/templates"
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,77 @@
1
+ module Rstrip
2
+
3
+ class Rstrip
4
+
5
+ class << self
6
+
7
+ def process(file_path)
8
+ if File.symlink?(file_path)
9
+ puts "skip symlink #{file_path}" and return
10
+ elsif !File.writable?(file_path)
11
+ puts "skip read only #{file_path}" and return
12
+ else
13
+ puts "process #{file_path}"
14
+ end
15
+ content = ""
16
+ File.open(file_path, 'r') do |file|
17
+ file.each_line do |line|
18
+ content << rstrip(line)
19
+ end
20
+ end
21
+ File.open(file_path, 'w') do |file|
22
+ file.write(content.rstrip)
23
+ end
24
+ end
25
+
26
+ def rstrip(line)
27
+ line.rstrip + "\n"
28
+ end
29
+
30
+ def list
31
+ if File.exists?("#{Dir.pwd}/.rstrip")
32
+ path = "#{Dir.pwd}/.rstrip"
33
+ puts "Using #{Dir.pwd}/.rstrip in the current directory"
34
+ elsif File.exists?("#{Dir.home}/.rstrip")
35
+ path ="#{Dir.home}/.rstrip"
36
+ puts "Using #{Dir.home}/.rstrip in your home directory"
37
+ else
38
+ puts %{ Can't find any .rstrip config file\n
39
+ Please run 'rstrip config generate'\n
40
+ }
41
+ exit
42
+ end
43
+ extensions = get_extensions(path)
44
+ get_files(extensions)
45
+ end
46
+
47
+ private
48
+
49
+ def get_extensions(path)
50
+ extensions = []
51
+ file = File.open(path, "r")
52
+ file.each_line do |line|
53
+ line.strip!
54
+ unless line.empty?
55
+ if line.start_with?(".")
56
+ line = line[1..-1]
57
+ end
58
+ extensions << line
59
+ end
60
+ end
61
+ puts "Using the following extensions:\n #{extensions.join(', ') }\n"
62
+ extensions
63
+ end
64
+
65
+ def get_files(extensions)
66
+ pattern = "**/*.{#{extensions.join(",")}}"
67
+ files = Dir.glob(pattern)
68
+ puts "Files that will be operated on:"
69
+ files.collect{|f| puts f}
70
+ files
71
+ end
72
+
73
+ end # end class method
74
+
75
+ end
76
+
77
+ end
@@ -0,0 +1,10 @@
1
+ rb
2
+ erb
3
+ haml
4
+ js
5
+ coffee
6
+ css
7
+ scss
8
+ rdoc
9
+ feature
10
+ yml
@@ -0,0 +1,3 @@
1
+ module Rstrip
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rstrip/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rstrip"
7
+ s.version = Rstrip::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["zhengjia"]
10
+ s.email = ["jiazheng@live.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{A ruby executable that removes the trailing white space in the current project directory}
13
+ s.description = %q{A ruby executable that removes the trailing white space in the current project directory. It also remove the last empty line if there is one}
14
+
15
+ s.rubyforge_project = "rstrip"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency "rspec", ">= 2.0.0"
23
+ s.add_development_dependency "aruba", "~> 0.4.3"
24
+ s.add_dependency "thor"
25
+ end
@@ -0,0 +1,31 @@
1
+ require 'rstrip/rstrip'
2
+
3
+ describe Rstrip::Rstrip do
4
+
5
+ it "should remove right whitespace" do
6
+ Rstrip::Rstrip.rstrip("Lorem ipsum \n").should == "Lorem ipsum\n"
7
+ end
8
+
9
+ it "should remove right tab" do
10
+ Rstrip::Rstrip.rstrip("Lorem ipsum\t\n").should == "Lorem ipsum\n"
11
+ end
12
+
13
+ it "should remove right form" do
14
+ Rstrip::Rstrip.rstrip("Lorem ipsum\f\n").should == "Lorem ipsum\n"
15
+ end
16
+
17
+ it "should remove right carriage return" do
18
+ Rstrip::Rstrip.rstrip("Lorem ipsum\r\n").should == "Lorem ipsum\n"
19
+ end
20
+
21
+ it "should remove multiple whitespace" do
22
+ Rstrip::Rstrip.rstrip("Lorem ipsum \f\t \n").should == "Lorem ipsum\n"
23
+ end
24
+
25
+ it "should not remove right newline\n" do
26
+ Rstrip::Rstrip.rstrip("Lorem ipsum\n").should == "Lorem ipsum\n"
27
+ Rstrip::Rstrip.rstrip("Lorem ipsum\n\n").should == "Lorem ipsum\n"
28
+ Rstrip::Rstrip.rstrip("Lorem ipsum\r\n").should == "Lorem ipsum\n"
29
+ end
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rstrip
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.2
6
+ platform: ruby
7
+ authors:
8
+ - zhengjia
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-26 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.0.0
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: aruba
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 0.4.3
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: thor
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id003
49
+ description: A ruby executable that removes the trailing white space in the current project directory. It also remove the last empty line if there is one
50
+ email:
51
+ - jiazheng@live.com
52
+ executables:
53
+ - rstrip
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - Gemfile
61
+ - README.rdoc
62
+ - Rakefile
63
+ - bin/rstrip
64
+ - feature/config.feature
65
+ - feature/support/env.rb
66
+ - lib/rstrip.rb
67
+ - lib/rstrip/cli.rb
68
+ - lib/rstrip/rstrip.rb
69
+ - lib/rstrip/templates/rstrip.tt.txt
70
+ - lib/rstrip/version.rb
71
+ - rstrip.gemspec
72
+ - spec/rstrip_spec.rb
73
+ has_rdoc: true
74
+ homepage: ""
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options: []
79
+
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project: rstrip
97
+ rubygems_version: 1.6.2
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: A ruby executable that removes the trailing white space in the current project directory
101
+ test_files:
102
+ - spec/rstrip_spec.rb