jstyler 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module Jstyler
5
+ VERSION = '0.0.4'
6
+
7
+ GEM_DIRECTORY = File.expand_path(File.dirname(__FILE__)+'/../')
8
+
9
+ FORMATTER_LIB = 'formatter-0.0.2.jar'
10
+
11
+ JAVA_LIBS = File.expand_path(File.dirname(__FILE__)+'/../javalibs')
12
+
13
+ end
14
+
15
+ require 'jstyler/beautify'
16
+ require 'jstyler/settings'
@@ -0,0 +1,151 @@
1
+ # beautify.rb
2
+
3
+ require 'buildr'
4
+ require 'session'
5
+
6
+ module Jstyler
7
+
8
+ module Beautify
9
+
10
+ include Extension
11
+
12
+ class BeautifyRunner
13
+
14
+ include Jstyler
15
+
16
+ def initialize
17
+ @conventions = ['java', 'eclipse']
18
+ @allowed_options = ['config', 'convention', 'verbose']
19
+ end
20
+
21
+ # :call-seq:
22
+ # run = config_file, src/ src2
23
+ # run = hash, src (hash - {:config=>config_file, :verbose=>true })
24
+ def run(config_options, *srcs)
25
+ @options = { :config=>config_options } unless Hash === config_options
26
+ @options = config_options if @options == nil
27
+ current_dir = Dir.pwd
28
+
29
+ #validation
30
+ result = validate_config_path @options
31
+ result = validate_source_directory srcs if result
32
+ result = validate_env if result
33
+
34
+ #prepare call
35
+ execution_string = ''
36
+ if result
37
+ execution_string = flatten_sources srcs
38
+ execution_string = flatten_options(@options) + execution_string
39
+
40
+ Dir.chdir(JAVA_LIBS)
41
+ java = File.expand_path ENV['JAVA_HOME']+'/bin/java'
42
+ cmd = "#{java} -jar #{FORMATTER_LIB} #{execution_string} "
43
+ shell = Session::Shell.new
44
+ stdout, stderr = shell.execute cmd
45
+ status = shell.status
46
+ puts stdout
47
+ puts stderr if !status
48
+
49
+ #change directory to previous one
50
+ Dir.chdir(current_dir)
51
+ result = execution_string
52
+ end
53
+
54
+ result
55
+ end
56
+
57
+ def flatten_options(options = {})
58
+ options_string = ''
59
+ options.each {|key,val|
60
+ val = File.expand_path val if key.to_s.include? 'config'
61
+ options_string += " -#{key} #{val}"
62
+ }
63
+
64
+ options_string
65
+ end
66
+
67
+ def flatten_sources(srcs = [])
68
+ src_string = ''
69
+ srcs.uniq.flatten.each {|src| src_string += " " + File.expand_path(src)}
70
+
71
+ src_string
72
+ end
73
+
74
+ def validate_env
75
+ if !ENV.has_key?('JAVA_HOME')
76
+ puts "There is no JAVA_HOME in your environment"
77
+ return false
78
+ end
79
+ return true
80
+ end
81
+ private :validate_env
82
+
83
+ def validate_source_directory sources
84
+ if ! sources.empty? && sources.uniq.flatten.each { |item| return ! item.nil? && File.exist?(item) }
85
+ puts "Source(s) directory(ies) cannot be accessed"
86
+ return false
87
+ end
88
+ return true
89
+ end
90
+ private :validate_source_directory
91
+
92
+ def validate_config_path config_options
93
+ #validation
94
+ # use default convention (Java)
95
+ if ! config_options.has_key?(:config) && ! config_options.has_key?(:convention)
96
+ puts "Config path and convention were not defined, will use Java convention."
97
+ @options[:config] = File.expand_path GEM_DIRECTORY+'/conventions/java.prefs'
98
+ end
99
+ # use specified built-in convention
100
+ if ! config_options.has_key?(:config) && config_options.has_key?(:convention)
101
+ convention = "#{config_options.fetch(:convention)}".downcase
102
+ puts "Convention '#{convention}' was defined."
103
+ @options[:config] = ''
104
+ @options[:config] = File.expand_path(GEM_DIRECTORY+"/conventions/#{convention}.prefs") if @conventions.include?(convention)
105
+ @options.delete :convention
106
+ end
107
+ if !File.exist? @options.fetch(:config).to_s
108
+ puts "Config path does not exist"
109
+ return false
110
+ end
111
+ options_to_check = [].concat @options.keys
112
+ options_to_check.each{|key|
113
+ @options.delete key if !(@allowed_options.include? key.to_s)
114
+ @options[key] = '' if key.to_s == 'verbose' #verbose value should be empty
115
+ }
116
+ return true
117
+ end
118
+ private :validate_config_path
119
+
120
+ end
121
+
122
+ # task not specific to any project
123
+ first_time do
124
+ Project.local_task('format')
125
+ end
126
+
127
+ before_define do |project|
128
+
129
+ end
130
+
131
+ after_define do |project|
132
+
133
+ task 'format' do |task|
134
+ puts "Perform formatting...."
135
+ JAVA_LIBS = File.expand_path project.path_to :target
136
+
137
+ runner = BeautifyRunner.new
138
+ sources_to_format = [].concat project.compile.sources
139
+ sources_to_format.concat project.test.compile.sources if ! project.test.compile.sources.empty?
140
+ runner.run(project.jstyler.options, sources_to_format.flatten)
141
+ end
142
+ end
143
+
144
+ end
145
+
146
+ end
147
+
148
+ #inject beautifier into buildr project
149
+ class Buildr::Project
150
+ include Jstyler::Beautify
151
+ end
@@ -0,0 +1,46 @@
1
+ # settings.rb
2
+
3
+ module Jstyler
4
+
5
+ class Settings
6
+
7
+ include Singleton
8
+ include Buildr
9
+
10
+ # :call-seq:
11
+ # jstyler => Hash
12
+ #
13
+ # Returns an hash of all settings attributes for Jstyler.
14
+ #
15
+ # Within application build file you can specify settings for Jstyler in following way:
16
+ # jstyler.options = {:config=>'path_to_config'}
17
+ #
18
+ # You can also specify configuration options in the build.yaml (within project source directory)
19
+ # or in the .buildr/settings.yaml in your HOME directory.
20
+ #
21
+ # For example:
22
+ # jstyler:
23
+ # options:
24
+ # config: path_to_config
25
+ # verbose: true
26
+ def options
27
+ unless @options_holder
28
+ @options_holder = Hash.new(Buildr.settings.build).merge({}) { |repos, hash|
29
+ repos | Hash.new(hash['jstyler'] && hash['jstyler']['options'])
30
+ }
31
+ end
32
+
33
+ @options_holder
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+
40
+ module Buildr
41
+
42
+ def jstyler
43
+ return Jstyler::Settings.instance
44
+ end
45
+
46
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/jstyler.rb'}"
9
+ puts "Loading jstyler gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,78 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ include Jstyler
4
+ include Jstyler::Beautify
5
+ include Buildr
6
+
7
+ describe BeautifyRunner, "when is being ran" do
8
+
9
+ before do
10
+ @beautify = BeautifyRunner.new
11
+ @config = File.expand_path GEM_DIRECTORY+'/conventions/java.prefs'
12
+ @src = File.expand_path File.dirname(__FILE__)+'/fixture'
13
+ end
14
+
15
+ it "should error if src argument is not defined" do
16
+ @beautify.run({}, nil).should be_false
17
+ @beautify.run({}, '').should be_false
18
+ end
19
+
20
+ it "should use built-in default config if it and convention attributes were not defined" do
21
+ res = @beautify.run({}, @src)
22
+ res.should be_true
23
+ res.should include '-config'
24
+ end
25
+
26
+ it "should not pass -convention attribute to java" do
27
+ res = @beautify.run({:convention=>:Java}, @src)
28
+ res.should be_true
29
+ res.should include '-config'
30
+ res.should_not include '-convention'
31
+ end
32
+
33
+ it "should fail if -convention attribute does not exist" do
34
+ res = @beautify.run({:convention=>:Java_custom}, @src)
35
+ res.should be_false
36
+ end
37
+
38
+ it "should error if config does not exist" do
39
+ res = @beautify.run({:verbose=>'', :config=>@config+'_'}, @src)
40
+ res.should be_false
41
+ end
42
+
43
+ it "should error if src directory is does not exist" do
44
+ @beautify.run({:config=>"somepath"}, 'nilsrc').should be_false
45
+ @beautify.run({:config=>"somepath"}, 'nilsrc', 'sbfdgb').should be_false
46
+ end
47
+
48
+ it "should error if there is no JAVA_HOME environment var" do
49
+ previous = ENV['JAVA_HOME']
50
+ ENV.delete('JAVA_HOME')
51
+ @beautify.run({:config=>@config}, @src).should be_false
52
+ ENV['JAVA_HOME'] = previous
53
+ end
54
+
55
+ it "should have flatten to string source arguments" do
56
+ res = @beautify.run({:config=>@config}, @src, @src)
57
+ res.should_not be_false
58
+ res.should include @src
59
+ end
60
+
61
+ it "should contain prepared command line attributes as a string" do
62
+ res = @beautify.run({:config=>@config, :verbose=>''}, @src)
63
+ res.should be_true
64
+ res.should include "-config"
65
+ res.should include @config
66
+ res.should include "-verbose"
67
+ end
68
+
69
+ it "should prevent passing not supported configuration options" do
70
+ res = @beautify.run({:config=>@config, :verbose=>'true', :someother_property=>'someother_property', :some=>''}, @src)
71
+ res.should be_true
72
+ res.should_not include '-someother_property'
73
+ res.should_not include '-some'
74
+ res.should include 'config'
75
+ res.should include 'verbose'
76
+ end
77
+
78
+ end
@@ -0,0 +1,14 @@
1
+ /**
2
+ *
3
+ */
4
+ package name.webdizz.styler.fixture;
5
+
6
+ /**
7
+ * @author webdizz
8
+ *
9
+ */
10
+ public class FormatterFixture {
11
+
12
+ private static final String CONSTANT = "";
13
+
14
+ }
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ # Time to add your specs!
4
+ # http://rspec.info/
5
+ describe "Place your specs here" do
6
+
7
+ it "find this spec in spec directory" do
8
+ # violated "Be sure to write your specs"
9
+ end
10
+
11
+ end
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+
11
+ require 'rubygems'
12
+
13
+ require 'jstyler'
14
+ require 'jstyler/beautify'
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jstyler
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 4
10
+ version: 0.0.4
11
+ platform: ruby
12
+ authors:
13
+ - webdizz
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-03 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: buildr
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 17
30
+ segments:
31
+ - 1
32
+ - 3
33
+ - 5
34
+ version: 1.3.5
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: session
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 31
46
+ segments:
47
+ - 2
48
+ - 4
49
+ - 0
50
+ version: 2.4.0
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rubyforge
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 7
62
+ segments:
63
+ - 2
64
+ - 0
65
+ - 4
66
+ version: 2.0.4
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: hoe
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 23
78
+ segments:
79
+ - 2
80
+ - 6
81
+ - 0
82
+ version: 2.6.0
83
+ type: :development
84
+ version_requirements: *id004
85
+ description: |-
86
+ This is an extension for BuildR to provide abilities to format Java source code according to conventions.
87
+ This extension uses built-in Eclipse functionality to format code, but without requirements to have installed Eclipse.
88
+ email:
89
+ - webdizz@gmail.com
90
+ executables: []
91
+
92
+ extensions: []
93
+
94
+ extra_rdoc_files:
95
+ - History.txt
96
+ - Manifest.txt
97
+ files:
98
+ - EPL-LICENSE
99
+ - History.txt
100
+ - MIT-LICENSE
101
+ - Manifest.txt
102
+ - README.rdoc
103
+ - Rakefile
104
+ - TODO
105
+ - conventions/eclipse.prefs
106
+ - conventions/java.prefs
107
+ - javalibs/common-3.5.1.r35x_v20090807.jar
108
+ - javalibs/equinox.app-1.2.1.R35x_v20091203.jar
109
+ - javalibs/formatter-0.0.2.jar
110
+ - javalibs/jdt.core-3.6.0-customized.jar
111
+ - javalibs/jobs-3.4.100.v20090429.jar
112
+ - javalibs/osgi-3.5.2.R35x_v20100126.jar
113
+ - javalibs/resources-3.5.2.r35x_v20091203.jar
114
+ - javalibs/text-3.5.0.v20090513.jar
115
+ - lib/jstyler.rb
116
+ - lib/jstyler/beautify.rb
117
+ - lib/jstyler/settings.rb
118
+ - script/console
119
+ - script/destroy
120
+ - script/generate
121
+ - spec/beautify_spec.rb
122
+ - spec/fixture/name/webdizz/styler/fixture/FormatterFixture.java
123
+ - spec/jstyler_spec.rb
124
+ - spec/spec.opts
125
+ - spec/spec_helper.rb
126
+ - tasks/rspec.rake
127
+ has_rdoc: true
128
+ homepage: http://webdizz.name/posts/jstyler
129
+ licenses: []
130
+
131
+ post_install_message: "\n Usage: \n buildr format \n For details go to http://webdizz.name/posts/jstyler.\n "
132
+ rdoc_options:
133
+ - --main
134
+ - README.rdoc
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: 3
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ hash: 3
152
+ segments:
153
+ - 0
154
+ version: "0"
155
+ requirements: []
156
+
157
+ rubyforge_project: jstyler
158
+ rubygems_version: 1.3.7
159
+ signing_key:
160
+ specification_version: 3
161
+ summary: This is an extension for BuildR to provide abilities to format Java source code according to conventions
162
+ test_files: []
163
+