rpack 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "yaml"
5
+ require "zip/zip"
6
+ require "fileutils"
7
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/parser.rb"
8
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/rpack.rb"
9
+
10
+ parser = Rpack::Parser.new(ARGV)
11
+ rpack = Rpack::Rpack.new(ARGV,parser)
12
+ rpack.run
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "yaml"
5
+ require "zip/zip"
6
+ require "fileutils"
7
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/parser.rb"
8
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/rpack.rb"
9
+
10
+ parser = Rpack::Parser.new(%w(--unpack)+ARGV)
11
+ rpack = Rpack::Rpack.new(ARGV,parser)
12
+ rpack.run
@@ -0,0 +1,71 @@
1
+ controller:
2
+ plural: true
3
+ suffix: _controller.rb
4
+ paths:
5
+ - app/controllers/
6
+
7
+ model:
8
+ suffix: .rb
9
+ paths:
10
+ - app/models/
11
+
12
+ view:
13
+ plural: true
14
+ dir: true
15
+ paths:
16
+ - app/views/
17
+
18
+ helper:
19
+ plural: true
20
+ suffix: _helper.rb
21
+ paths:
22
+ - app/helpers/
23
+
24
+ mailer:
25
+ paths:
26
+ - app/mailers/
27
+
28
+ migration:
29
+ plural: true
30
+ update_filename: true
31
+ inside: true
32
+ dir: true
33
+ paths:
34
+ - db/migrate/
35
+
36
+ unit:
37
+ suffix: "*_test.rb"
38
+ paths:
39
+ - test/unit/
40
+ - test/unit/helpers/
41
+
42
+ functional:
43
+ plural: true
44
+ suffix: _controller_test.rb
45
+ paths:
46
+ - test/functional/
47
+
48
+ integration:
49
+ paths:
50
+ - test/integration/
51
+
52
+ performance:
53
+ paths:
54
+ - test/performance/
55
+
56
+ fixture:
57
+ plural: true
58
+ suffix: .yml
59
+ paths:
60
+ - test/fixtures/
61
+
62
+ route:
63
+ plural: true
64
+ extract: true
65
+ begin_pattern: "^[\\w:\.]+\.draw do"
66
+ end_pattern: "^end"
67
+ begin_string: "check_your_rails_version.draw do"
68
+ end_string: "end"
69
+ inside: true
70
+ paths:
71
+ - config/routes.rb
@@ -0,0 +1,68 @@
1
+ module Rpack
2
+ module Packer
3
+ def pack
4
+ filename = @parser.package || "#{@patterns.sort.join}.zip"
5
+ puts "Using basedir #{@basedir}"
6
+ puts "Packing to #{filename} ..."
7
+
8
+ zip = Zip::ZipOutputStream.open(filename)
9
+ list, extracted = get_pack_list
10
+
11
+ for key,value in list
12
+ for file in value.sort
13
+ content = extracted[file]
14
+ entry = file.gsub(@basedir,"").gsub(/^\//,"")
15
+ zip.put_next_entry(entry)
16
+ zip.write content.join
17
+ end
18
+ end
19
+ zip.close
20
+ end
21
+
22
+ def get_pack_list(verbose=true)
23
+ list = {}
24
+ extracted = {}
25
+
26
+ for pattern in @patterns
27
+ @singular = pattern.singularize
28
+ @plural = singular.pluralize
29
+ puts "processing #{@plural} ..." if verbose
30
+
31
+ for option in @parser.options
32
+ config = @config[option]
33
+ paths = config["paths"]
34
+ key = config["plural"] ? @plural : @singular
35
+ suffix = config["suffix"]
36
+ dir = config["dir"]
37
+ inside = config["inside"]
38
+ extract = config["extract"]
39
+ begin_p = config["begin_pattern"]
40
+ end_p = config["end_pattern"]
41
+
42
+ list[option] ||= []
43
+
44
+ for path in paths
45
+ file = File.expand_path("#{@basedir}/#{path}#{inside ? '' : key}#{suffix}")
46
+ flist = dir ? Dir.glob(File.expand_path("#{file}/**")) : Dir.glob(file)
47
+ flist.sort!
48
+ for f in flist
49
+ incfile = true
50
+ next if !File.exist?(f)
51
+ contents = File.readlines(f)
52
+ if extract
53
+ contents = extract_contents(contents,key,begin_p,end_p)
54
+ incfile = contents.size>0
55
+ if incfile && extracted[f]
56
+ contents = extracted[f]+contents
57
+ end
58
+ end
59
+ extracted[f] = contents
60
+ list[option] << f if incfile
61
+ end
62
+ end
63
+ end
64
+ end
65
+ [list,extracted]
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,66 @@
1
+ require "optparse"
2
+
3
+ module Rpack
4
+ class Parser
5
+ attr_accessor :parser, :options, :basedir, :inflections, :package
6
+ attr_reader :full_options
7
+
8
+ def initialize(argv)
9
+ @options = []
10
+ @parser = nil
11
+ @valid = true
12
+ @unpack = false
13
+ @full_options = %w(controller model view helper mailer migration
14
+ unit functional integration performance fixture route)
15
+ begin
16
+ OptionParser.new do |opts|
17
+ @parser = opts
18
+
19
+ opts.banner = "Usage: rpack <name> [options]"
20
+ opts.on("-a","--all") { @options = @full_options }
21
+ opts.on("-c","--controller") { @options << "controller" }
22
+ opts.on("-m","--model") { @options << "model" }
23
+ opts.on("-v","--view") { @options << "view" }
24
+ opts.on("-h","--helper") { @options << "helper" }
25
+ opts.on("-l","--mailer") { @options << "mailer" }
26
+ opts.on("-g","--migration") { @options << "migration" }
27
+ opts.on("-u","--unit") { @options << "unit" }
28
+ opts.on("-f","--functional") { @options << "functional" }
29
+ opts.on("-i","--integration") { @options << "integration"}
30
+ opts.on("-o","--performance") { @options << "performance"}
31
+ opts.on("-x","--fixture") { @options << "fixture" }
32
+ opts.on("-r","--route") { @options << "route" }
33
+
34
+ opts.on("-k","--unpack") do
35
+ @unpack = true
36
+ end
37
+
38
+ opts.on("-t","--inflections FILE") do |file|
39
+ @inflections = File.expand_path(file.strip)
40
+ end
41
+
42
+ opts.on("-d","--dir DIR") do |dir|
43
+ @basedir = dir.strip
44
+ end
45
+
46
+ opts.on("-p","--package FILE") do |file|
47
+ @package = File.expand_path(file.strip)
48
+ end
49
+ end.parse! argv
50
+ @options = @full_options if @options.size<1
51
+ rescue => e
52
+ puts "Error: #{e}"
53
+ puts @parser
54
+ @valid = false
55
+ end
56
+ end
57
+
58
+ def valid?
59
+ @valid
60
+ end
61
+
62
+ def unpack?
63
+ @unpack
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,48 @@
1
+ require "rubygems"
2
+ require "yaml"
3
+ require "fileutils"
4
+ require "zip/zip"
5
+ require "active_support"
6
+ require "active_support/inflector"
7
+ require "#{File.expand_path(File.dirname(__FILE__))}/packer.rb"
8
+ require "#{File.expand_path(File.dirname(__FILE__))}/unpacker.rb"
9
+ require "#{File.expand_path(File.dirname(__FILE__))}/utils.rb"
10
+
11
+ module Rpack
12
+ class Rpack
13
+ include Packer
14
+ include Unpacker
15
+ include Utils
16
+ attr_reader :plural, :singular, :config, :pack_list
17
+
18
+ def initialize(patterns,parser,basedir=".")
19
+ # check for pattern and options
20
+ @patterns = patterns
21
+ @parser = parser
22
+ @basedir = @parser.basedir || basedir
23
+ exit if !@parser.valid?
24
+
25
+ if @patterns.nil? || @patterns.size<1
26
+ puts "No name given #{@parser.parser}"
27
+ exit
28
+ end
29
+
30
+ # plural and singular forms
31
+ load_inflections(@parser.inflections)
32
+
33
+ # load the configs and get the file list
34
+ @config = YAML.load(File.open(File.expand_path("#{File.dirname(__FILE__)}/../config/config.yml")))
35
+ @unpack = @parser.unpack?
36
+ end
37
+
38
+ def run
39
+ return unpack if @unpack
40
+ return pack if !@unpack
41
+ end
42
+
43
+ def load_inflections(file=nil)
44
+ file = "./config/initializers/inflections.rb" if file.nil?
45
+ require file if !file.nil? && File.exist?(file)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,66 @@
1
+ require "fileutils"
2
+
3
+ module Rpack
4
+ module Unpacker
5
+ def unpack
6
+ file = File.expand_path(@patterns.first)
7
+ count = 0
8
+ puts "Unpacking #{file} to #{@basedir} ..."
9
+ if !File.exist?(file) || !File.file?(file)
10
+ puts "File #{file} does not exist."
11
+ return false
12
+ end
13
+ Zip::ZipFile.open(file) do |zip|
14
+ zip.each do |zfile|
15
+ file = File.expand_path("#{@basedir}/#{zfile.to_s}")
16
+ dir = File.dirname(file.to_s)
17
+ config = find_config_by_path(zfile.to_s)
18
+ verb = "Extracting"
19
+ next if !config
20
+
21
+ extract = config["extract"]
22
+ update = config["update_filename"]
23
+ begin_p = config["begin_pattern"]
24
+ end_p = config["end_pattern"]
25
+ begin_str = config["begin_string"]
26
+ end_str = config["end_string"]
27
+ contents = zfile.get_input_stream.readlines
28
+
29
+ FileUtils.mkpath(dir) if !File.directory?(dir)
30
+
31
+ if extract
32
+ if !File.exist?(file)
33
+ contents = ["# you MUST check this\n","#{begin_str}\n",contents,"#{end_str}\n"]
34
+ else
35
+ newcontent = []
36
+ fcontents = File.readlines(file)
37
+ begin_p, end_p = pattern_positions(fcontents,begin_p,end_p)
38
+ if begin_p && end_p
39
+ start_range = fcontents[0..begin_p]
40
+ mid_range = fcontents[(begin_p+1)...end_p]
41
+ end_range = fcontents[end_p..-1]
42
+ for lcontent in contents
43
+ newcontent << lcontent if !fcontents.any? {|e| e.chomp.strip==lcontent.chomp.strip}
44
+ end
45
+ contents = start_range+mid_range+newcontent+end_range
46
+ verb = "Merging"
47
+ end
48
+ end
49
+ end
50
+
51
+ if update
52
+ mtime = Time.now.strftime("%Y%m%d%H%M%S").to_i+count
53
+ file = file.sub(/[0-9]{14}/,mtime.to_s)
54
+ count += 1
55
+ end
56
+ file = "#{file}.rpack" if File.exist?(file) && !extract
57
+ puts "#{verb}:\n#{zfile.to_s} to\n#{file}\n\n"
58
+ File.open(file,"w") do |handle|
59
+ handle << contents.join
60
+ end
61
+ end
62
+ end
63
+ return true
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,30 @@
1
+ module Rpack
2
+ module Utils
3
+ def pattern_positions(contents,begin_pattern,end_pattern)
4
+ begin_e = Regexp.new(begin_pattern)
5
+ end_e = Regexp.new(end_pattern)
6
+ begin_p = contents.find_index {|e| e =~ begin_e}
7
+ end_p = contents.find_index {|e| e =~ end_e}
8
+ [begin_p,end_p]
9
+ end
10
+
11
+ def extract_contents(contents,key,begin_pattern=nil,end_pattern=nil)
12
+ regexp = Regexp.new(":\\b#{key}\\b")
13
+ if begin_pattern && end_pattern
14
+ begin_p, end_p = pattern_positions(contents,begin_pattern,end_pattern)
15
+ return [] if !begin_p || !end_p
16
+ contents = contents[begin_p..end_p]
17
+ end
18
+ contents.select { |line| line =~ regexp }
19
+ end
20
+
21
+ def find_config_by_path(path)
22
+ for key,value in @config
23
+ paths = value["paths"].map { |e| Regexp.new("^#{e}") }
24
+ found = paths.any? {|e| e =~ path}
25
+ return @config[key] if found
26
+ end
27
+ nil
28
+ end
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rpack
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Eustaquio 'TaQ' Rangel
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-12 00:00:00 -02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rubyzip
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 3
44
+ - 10
45
+ version: 2.3.10
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: Rails packager
49
+ email: eustaquiorangel@gmail.com
50
+ executables:
51
+ - rpack
52
+ - runpack
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - bin/rpack
59
+ - bin/runpack
60
+ - lib/rpack.rb
61
+ - lib/parser.rb
62
+ - lib/packer.rb
63
+ - lib/unpacker.rb
64
+ - lib/utils.rb
65
+ - config/config.yml
66
+ has_rdoc: true
67
+ homepage: http://github.com/taq/rpack
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options: []
72
+
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.7
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Simple Rails packager
98
+ test_files: []
99
+