procfile_split 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,6 @@
1
+ = procfile_utils
2
+
3
+ Describe your project here
4
+
5
+ :include:procfile_utils.rdoc
6
+
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'optparse'
4
+ require 'procfile_split'
5
+ require 'fileutils'
6
+
7
+ include ProcfileSplit
8
+
9
+ options = {}
10
+ begin
11
+ OptionParser.new do |opts|
12
+ opts.banner = "Usage: procfile-split [[--help] [--split] [--task app-name]]"
13
+ opts.separator ""
14
+
15
+ opts.on("-h", "--help", "This help screen") do |h|
16
+ $stderr.puts opts
17
+ exit
18
+ end
19
+
20
+ opts.on("-s", "--split", "Split the Procfile and commit them into branches") do |s|
21
+ options[:split] = s
22
+ options[:help] = false
23
+ end
24
+
25
+ opts.on("-t app-name", "--task app-name", "Create rake tasks (lib/tasks/heroku.rake) for heroku apps") do |t|
26
+ options[:task] = t
27
+ options[:help] = false
28
+ end
29
+ end.parse!
30
+
31
+ rescue OptionParser::MissingArgument => e
32
+ $stderr.puts e.message
33
+ exit 1
34
+
35
+ end
36
+
37
+ begin
38
+ if options.size == 0
39
+ $stderr.puts "Try: procfile_utils --help"
40
+ exit 0
41
+ end
42
+
43
+ procfile = "Procfile"
44
+ processes = Procfile.processes open(procfile).read
45
+
46
+ if options[:split]
47
+ $stderr.puts "Split Procfile and commit them into branches"
48
+ Validator.branches_exists? processes.keys
49
+ Spliter.perform processes
50
+ Git.checkout_master
51
+ end
52
+
53
+ if options[:task]
54
+ $stderr.puts "Generate tasks lib/tasks/heroku.rake"
55
+ file = 'lib/tasks/heroku.rake'
56
+ if File.exists?(file)
57
+ $stderr.puts "File #{file} already exists!"
58
+ else
59
+ FileUtils.mkdir_p "lib/tasks"
60
+ File.open(file, 'w') do |f|
61
+ f.write(Raketask.generate(processes.keys, options[:task]))
62
+ end
63
+ end
64
+ end
65
+
66
+ rescue StandardError => e
67
+ puts e.message
68
+ end
@@ -0,0 +1,35 @@
1
+ require 'open3'
2
+
3
+ module ProcfileSplit
4
+ class Git
5
+ def self.list_branch
6
+ Open3.popen3("git branch") do |stdin, stdout, stderr, wait_thr|
7
+ exit_status = wait_thr.value
8
+
9
+ if exit_status == 0
10
+ stdout.read.split("\n").collect(){|branch| branch.gsub(/\\*\W/, "") }
11
+ else
12
+ raise stderr.read
13
+ end
14
+ end
15
+ end
16
+
17
+ def self.checkout(branch)
18
+ system "git checkout -b \"#{branch}\""
19
+ if $? != 0
20
+ raise "abort: git checkout failed"
21
+ end
22
+ end
23
+
24
+ def self.add_and_commit(filename, message)
25
+ system "git add '#{filename}'; git commit -m '#{message}'"
26
+ if $? != 0
27
+ raise "abort: git commit failed"
28
+ end
29
+ end
30
+
31
+ def self.checkout_master
32
+ system "git checkout master"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ module ProcfileSplit
2
+ class Procfile
3
+ def self.processes(procfile)
4
+ lines = procfile.split("\n")
5
+ lines.inject({}) do |map, line|
6
+ name, command = line.split(":")
7
+ if name && command
8
+ map[name] = command.strip
9
+ end
10
+ map
11
+ end
12
+ end
13
+
14
+ def self.create_procfile(file, process, command)
15
+ procfile_line = "#{process}: #{command}"
16
+ File.open(file, 'w') do |f|
17
+ f.write(procfile_line)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,37 @@
1
+ module ProcfileSplit
2
+ class Raketask
3
+ def self.generate(processes, appname)
4
+ cmd_create_app = processes.collect do |p|
5
+ "system \"heroku create '#{appname}-#{p}' --stack cedar\""
6
+ end.join("\n ")
7
+
8
+ cmd_setup_remote = processes.collect do |p|
9
+ "system \"git remote add '#{appname}-#{p}' 'git@heroku.com:#{appname}-#{p}.git'\""
10
+ end.join("\n ")
11
+
12
+ cmd_merge = processes.collect do |p|
13
+ "system \"git checkout #{p} && git merge master\""
14
+ end.join("\n ")
15
+
16
+ cmd_push = processes.collect do |p|
17
+ "system \"git push #{appname}-#{p} #{p}:master\""
18
+ end.join("\n ")
19
+
20
+ content = <<EOF
21
+ namespace :heroku do
22
+ desc "Create heroku apps based on branches"
23
+ task :create
24
+ #{cmd_create_app}
25
+ #{cmd_setup_remote}
26
+ end
27
+
28
+ desc "Deploy to heroku"
29
+ task :deploy
30
+ #{cmd_merge}
31
+ #{cmd_push}
32
+ end
33
+ end
34
+ EOF
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ module ProcfileSplit
2
+ class Spliter
3
+ def self.branches_exists?(names)
4
+ branches = Git.list_branch
5
+ names.each do |name|
6
+ if branches.include?(name)
7
+ raise "abort: branch \"#{name}\" already exist!"
8
+ end
9
+ end
10
+ end
11
+
12
+ def self.perform(processes)
13
+ processes.keys.each do |process|
14
+ # create branch
15
+ Git.checkout process
16
+
17
+ # overwrite procfile
18
+ Procfile.create_procfile "Procfile", process, processes[process]
19
+
20
+ # commit changes
21
+ Git.add_and_commit "Procfile", "update Procfile for #{process}"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ module ProcfileSplit
2
+ class Validator
3
+ def self.branches_exists?(names)
4
+ branches = Git.list_branch
5
+ names.each do |name|
6
+ if branches.include?(name)
7
+ raise "abort: branch \"#{name}\" already exist!"
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ unless File.respond_to? :realpath
2
+ class File #:nodoc:
3
+ def self.realpath path
4
+ return realpath(File.readlink(path)) if symlink?(path)
5
+ path
6
+ end
7
+ end
8
+ end
9
+ $: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib')
10
+
11
+ require 'procfile_split/git'
12
+ require 'procfile_split/procfile'
13
+ require 'procfile_split/split'
14
+ require 'procfile_split/raketask'
15
+ require 'procfile_split/validator'
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: procfile_split
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Francis Chong
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70153426880140 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70153426880140
25
+ - !ruby/object:Gem::Dependency
26
+ name: jeweler
27
+ requirement: &70153426879280 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70153426879280
36
+ description: Split the Procfile and create rake tasks for heroku deployment
37
+ email: francis@ignition.hk
38
+ executables:
39
+ - procfile_split
40
+ extensions: []
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ files:
44
+ - bin/procfile_split
45
+ - lib/procfile_split.rb
46
+ - lib/procfile_split/git.rb
47
+ - lib/procfile_split/procfile.rb
48
+ - lib/procfile_split/raketask.rb
49
+ - lib/procfile_split/split.rb
50
+ - lib/procfile_split/validator.rb
51
+ - README.rdoc
52
+ homepage: http://git.ignition.hk/procfile_split
53
+ licenses:
54
+ - MIT License
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ segments:
66
+ - 0
67
+ hash: -1964906980798540168
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.10
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Split the Procfile and create rake tasks for heroku deployment
80
+ test_files: []