app_up 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eef6f3f77e220c881a1ee5b8e4807976c6915a9c
4
+ data.tar.gz: 1fecafff7c0e6c14a60cc29f3097c8dcbe4a3fd7
5
+ SHA512:
6
+ metadata.gz: 200f718b10203e5b2db99ee188b2394d9e9acf4657d1792b346bf6efceda300753242458591bd0e60340cce68fea7859a2b134a4a7ff4209ee6f4945832547e8
7
+ data.tar.gz: 2f2abe122a1cfbe867010adcca4703a1a9a2f93b69fe11b9db73c3aa5b98f27b804bdf4daadf2e38b20458412dc712f5607be9362fa5f6e275e361fc9de56250
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in app_up.gemspec
4
+ gemspec
5
+
6
+ gem 'minitest'
7
+ gem 'pry'
8
+ gem 'mocha'
9
+
10
+ ruby '2.1.5'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Pete Kinnecom
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # GitUp
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'app_up'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install git_up
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/git_up/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'lib'
7
+ t.libs << 'test'
8
+ t.pattern = "test/**/*_test.rb"
9
+ end
10
+
data/app_up.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'app_up/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "app_up"
8
+ spec.version = AppUp::VERSION
9
+ spec.authors = ["Pete Kinnecom"]
10
+ spec.email = ["pete.kinnecom@appfolio.com"]
11
+ spec.summary = %q{Git wrapper for running scripts}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_dependency('thor', ['>= 0.19', '< 1.0'])
23
+ spec.add_dependency('work_queue', ['>= 2.5.3', '< 3.0'])
24
+ end
data/bin/app_up ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'thor'
4
+ require_relative '../lib/app_up'
5
+
6
+ class AppUpCLI < Thor
7
+
8
+ desc :run, description: "do stuff"
9
+ option :diff, type: :array, desc: "SHAs to be diffed"
10
+ option :all, type: :boolean, desc: "Run all of the hooks"
11
+ option :verbose, type: :boolean, desc: "say all things"
12
+ option :db_reset, aliases: ['--reset', '--drop-dbs'], type: :boolean, desc: "Drop and recreate all databases"
13
+
14
+ def run_hooks
15
+ shell = AppUp::ShellRunner.new(
16
+ log_path: '/tmp/app_up.log',
17
+ working_directory: '.'
18
+ )
19
+
20
+ files = AppUp::Repo.new(shell, options).files
21
+ AppUp::HookRunner.new(shell, files, options).run
22
+ end
23
+
24
+ default_task :run_hooks
25
+ end
26
+
27
+ AppUpCLI.start(ARGV)
data/bin/git_up ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #PETE: make this a bash script
4
+
5
+ def get_sha
6
+ %x{git rev-parse HEAD}.chomp
7
+ end
8
+
9
+ start_sha = get_sha
10
+
11
+ command = (ARGV.size > 0) ? ARGV.join(' ') : 'pull --rebase origin master'
12
+ puts %x{git #{command}}
13
+ raise Error unless $?.success?
14
+ end_sha = get_sha
15
+
16
+ Kernel.exec "/Users/pete/personal/app_up/bin/app_up --diff #{start_sha} #{end_sha}"
data/lib/app_up.rb ADDED
@@ -0,0 +1,8 @@
1
+ require_relative "app_up/version"
2
+ require_relative "app_up/hook_runner"
3
+ require_relative 'app_up/repo'
4
+ require_relative 'app_up/shell_runner'
5
+
6
+ module AppUp
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,12 @@
1
+ require "app_up/hooks/rails_up"
2
+
3
+ module AppUp
4
+ class HookLoader
5
+
6
+ def self.load
7
+ [
8
+ Hooks::RailsUp,
9
+ ]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ require "app_up/hook_loader"
2
+
3
+ module AppUp
4
+ class HookRunner < Struct.new(:shell, :files, :options)
5
+
6
+ def run
7
+ hooks = HookLoader.load
8
+
9
+ hooks.each do |hook|
10
+ hook.run(shell, files, options)
11
+ end
12
+
13
+ shell.flush
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require "app_up/hooks/hook"
2
+
3
+ module AppUp
4
+ module Hooks
5
+ class Hook < Struct.new(:shell, :files, :options)
6
+
7
+ def self.run(shell, files, options)
8
+ new(shell, files, options).run
9
+ end
10
+
11
+ def run
12
+ raise "Must be implemented"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,69 @@
1
+ require "app_up/hooks/hook"
2
+
3
+ module AppUp
4
+ module Hooks
5
+ class RailsUp < Hook
6
+ BUNDLE_COMMAND = "bundle --local || bundle"
7
+
8
+ def initialize(*args)
9
+ @commands = {}
10
+ super(*args)
11
+ end
12
+
13
+ def run
14
+ shell.notify( "Running RailsUp\n----------")
15
+
16
+ files.each do |file|
17
+ if File.basename(file) == "Gemfile.lock"
18
+ add_command(:bundle, dir: File.split(file)[0])
19
+ end
20
+
21
+ if file.match(/db\/migrate/)
22
+ add_command(:migrate, dir: file.sub(/\/db\/migrate\/.*/, ''))
23
+ end
24
+ end
25
+
26
+ enqueue_commands
27
+ end
28
+
29
+ private
30
+
31
+ def add_command(action, dir:)
32
+ @commands[dir] ||= []
33
+ @commands[dir] << action unless @commands[dir].include? action
34
+ end
35
+
36
+ # We need to ensure that bundle is run before migrate.
37
+ # So we group the commands by their root folder, and
38
+ # bundle first.
39
+ def enqueue_commands
40
+ command_count = @commands.values.flatten.size.to_s
41
+ i = 1
42
+ @commands.each do |dir, actions|
43
+ migrate_block = Proc.new {
44
+ ['test', 'development'].each do |env|
45
+ shell.enqueue(:run, migrate(env), dir: dir)
46
+ end
47
+ }
48
+
49
+ actions.each do |command|
50
+ shell.enqueue(:notify, "#{i.to_s.rjust(command_count.length)}/#{command_count.to_s} #{command.to_s.ljust(7)} : #{dir}")
51
+ i+=1
52
+ end
53
+
54
+ if [:bundle, :migrate].all? { |c| actions.include?(c) }
55
+ shell.enqueue(:run, BUNDLE_COMMAND, dir: dir, &migrate_block)
56
+ elsif actions.include? :bundle
57
+ shell.enqueue(:run, BUNDLE_COMMAND, dir: dir)
58
+ elsif actions.include? :migrate
59
+ migrate_block.call
60
+ end
61
+ end
62
+ end
63
+
64
+ def migrate(env)
65
+ "RAILS_ENV=#{env} bundle exec rake #{options[:db_reset] ? 'db:drop' : ''} db:create 2> /dev/null;\n RAILS_ENV=#{env} bundle exec rake db:migrate"
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,22 @@
1
+ module AppUp
2
+ class Repo < Struct.new(:shell, :options)
3
+
4
+ def files
5
+ if options[:diff]
6
+ diff(*options[:diff])
7
+ else
8
+ all
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def all
15
+ shell.run "git ls-tree --full-tree -r HEAD --name-only"
16
+ end
17
+
18
+ def diff(start_sha, end_sha)
19
+ shell.run "git diff --name-only #{start_sha} #{end_sha}"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,99 @@
1
+ require 'work_queue'
2
+
3
+ module AppUp
4
+ class ShellRunner
5
+ MAX_PROCESS_COUNT = 8
6
+ CommandFailureError = Class.new(StandardError)
7
+
8
+ attr_accessor :working_directory, :log_path, :queue
9
+ private :working_directory, :log_path, :queue
10
+
11
+ def initialize(log_path:, working_directory:, verbose: false)
12
+ @working_directory = working_directory
13
+ @log_path = log_path
14
+ @queue = WorkQueue.new(MAX_PROCESS_COUNT, nil)
15
+ @verbose = verbose
16
+ Dir.chdir(%x[ git rev-parse --show-toplevel ].chomp)
17
+
18
+ reset_log
19
+ end
20
+
21
+ # The block passed to run is a callback. It is used
22
+ # to add a dependent command to the queue.
23
+ def run(cmd, dir: working_directory, &block)
24
+ command = "cd #{dir} && #{cmd}"
25
+ handle_output_for(command)
26
+
27
+ shell_out(command).split("\n").tap do
28
+ block.call if block_given?
29
+ end
30
+ end
31
+
32
+ def warn(msg)
33
+ log msg.to_s
34
+ print "#{msg.to_s.red}\n"
35
+ end
36
+
37
+ def notify(msg)
38
+ log msg.to_s
39
+ print "#{msg.to_s.yellow}\n"
40
+ end
41
+
42
+ def enqueue(method, *args, &block)
43
+ queue.enqueue_b do
44
+ send(method, *args, &block)
45
+ end
46
+ end
47
+
48
+ def flush
49
+ queue.join
50
+ end
51
+
52
+ private
53
+
54
+ def log(msg)
55
+ %x{echo "#{msg.to_s}" >> #{log_path}}
56
+ end
57
+
58
+ def reset_log
59
+ %x{echo "" > #{log_path}}
60
+ end
61
+
62
+ def handle_output_for(cmd)
63
+ puts cmd if @verbose
64
+ log(cmd)
65
+ end
66
+
67
+ def shell_out(command)
68
+ %x{ set -o pipefail && #{command} 2>> #{log_path} | tee -a #{log_path} }.chomp.tap do
69
+ warn "The following command has failed: #{command}. See #{log_path} for a full log." if ($?.exitstatus != 0)
70
+ end
71
+ end
72
+
73
+ end
74
+ end
75
+
76
+ #PETE: hack
77
+
78
+ class String
79
+ # colorization
80
+ def colorize(color_code)
81
+ "\e[#{color_code}m#{self}\e[0m"
82
+ end
83
+
84
+ def red
85
+ colorize(31)
86
+ end
87
+
88
+ def green
89
+ colorize(32)
90
+ end
91
+
92
+ def yellow
93
+ colorize(33)
94
+ end
95
+
96
+ def pink
97
+ colorize(35)
98
+ end
99
+ end
@@ -0,0 +1,3 @@
1
+ module AppUp
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,75 @@
1
+ require 'test_helper'
2
+
3
+ module AppUp
4
+ module Hooks
5
+ class DummyShell
6
+ attr_reader :history
7
+
8
+ def initialize
9
+ @history = {
10
+ enqueue: [],
11
+ notify: [],
12
+ }
13
+ end
14
+
15
+ def enqueue(*args)
16
+ history[:enqueue] << args
17
+ end
18
+
19
+ def notify(*args)
20
+ history[:notify] << args
21
+ end
22
+
23
+ end
24
+
25
+ class RailsUpTest < Minitest::Test
26
+
27
+ def setup
28
+ super
29
+ @shell = DummyShell.new
30
+ end
31
+
32
+ def test_bundle_directories
33
+ files = [
34
+ 'folder/sub_folder/Gemfile.lock',
35
+ 'other_folder/other_subfolder/more/Gemfile.lock',
36
+ 'unused/Gemfile'
37
+ ]
38
+
39
+ hook = RailsUp.new(@shell, files, {})
40
+
41
+ hook.run
42
+ assert_equal 2, @shell.history[:enqueue].select {|c| c[0]==:run}.size
43
+ assert @shell.history[:enqueue].include?([:run, RailsUp::BUNDLE_COMMAND, dir: 'folder/sub_folder']), @shell.history
44
+ assert @shell.history[:enqueue].include?([:run, RailsUp::BUNDLE_COMMAND, dir: 'other_folder/other_subfolder/more']), @shell.history
45
+ end
46
+
47
+ def test_migrate_directories
48
+ files = [
49
+ 'folder/sub_folder/db/migrate/migration.rb',
50
+ 'other_folder/other_subfolder/more/db/migrate/migration.rb',
51
+ 'unused/db/config.rb',
52
+ ]
53
+
54
+ hook = RailsUp.new(@shell, files, {})
55
+ hook.stubs(:migrate).with('test').returns('migrate_test')
56
+ hook.stubs(:migrate).with('development').returns('migrate_development')
57
+
58
+ hook.run
59
+ assert_equal 4, @shell.history[:enqueue].select {|c| c[0]==:run}.size
60
+ assert @shell.history[:enqueue].include?([:run, 'migrate_test', dir: 'folder/sub_folder']), @shell.history
61
+ assert @shell.history[:enqueue].include?([:run, 'migrate_development', dir: 'folder/sub_folder']), @shell.history
62
+ assert @shell.history[:enqueue].include?([:run, 'migrate_test', dir: 'other_folder/other_subfolder/more']), @shell.history
63
+ assert @shell.history[:enqueue].include?([:run, 'migrate_development', dir: 'other_folder/other_subfolder/more']), @shell.history
64
+ end
65
+
66
+ def test_migrate__drops_db
67
+ no_drop_hook = RailsUp.new('stub', 'stub', {})
68
+ assert_equal nil, no_drop_hook.send(:migrate, 'test').match(/db:drop/)
69
+
70
+ no_drop_hook = RailsUp.new('stub', 'stub', {db_reset: true})
71
+ assert no_drop_hook.send(:migrate, 'test').match(/db:drop/)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
2
+
3
+ require 'pry'
4
+ require 'minitest/autorun'
5
+ require 'app_up'
6
+ require 'mocha/mini_test'
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: app_up
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pete Kinnecom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0.19'
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: '1.0'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0.19'
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: work_queue
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.5.3
68
+ - - "<"
69
+ - !ruby/object:Gem::Version
70
+ version: '3.0'
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 2.5.3
78
+ - - "<"
79
+ - !ruby/object:Gem::Version
80
+ version: '3.0'
81
+ description:
82
+ email:
83
+ - pete.kinnecom@appfolio.com
84
+ executables:
85
+ - app_up
86
+ - git_up
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - app_up.gemspec
96
+ - bin/app_up
97
+ - bin/git_up
98
+ - lib/app_up.rb
99
+ - lib/app_up/hook_loader.rb
100
+ - lib/app_up/hook_runner.rb
101
+ - lib/app_up/hooks/hook.rb
102
+ - lib/app_up/hooks/rails_up.rb
103
+ - lib/app_up/repo.rb
104
+ - lib/app_up/shell_runner.rb
105
+ - lib/app_up/version.rb
106
+ - test/git_up/hooks/rails_up_test.rb
107
+ - test/test_helper.rb
108
+ homepage: ''
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.3
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Git wrapper for running scripts
132
+ test_files:
133
+ - test/git_up/hooks/rails_up_test.rb
134
+ - test/test_helper.rb