hw 1.1.8 → 1.1.9

Sign up to get free protection for your applications and to get access to all the features.
data/bin/hw CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+
2
3
  $:.unshift(File.expand_path('../../lib', __FILE__))
3
- require 'hw'
4
- HW::CLI.start
4
+
5
+ require 'hw/runner'
6
+ HW::Runner.start
data/lib/hw.rb CHANGED
@@ -1,21 +1 @@
1
- require 'git'
2
- require 'thor'
3
- require 'thor/group'
4
- require 'rails'
5
- require 'rails/generators/base'
6
-
7
- module HW
8
- DEFAULT_SOURCE = "git@github.com:carrot/hw-packages.git" # CLI
9
- DIRECTORY = File.expand_path("~/.hw/") # CLI, HW
10
- CONFIG_PATH = "#{DIRECTORY}/config" # CLI, Sources
11
- SOURCES_PATH = "#{DIRECTORY}/sources/" # CLI, Packages
12
- RESERVED_WORDS = %w(help) # Packages
13
- end
14
-
15
- require 'hw/core_ext/string'
16
- require 'hw/version'
17
- require 'hw/actions'
18
- require 'hw/thor'
19
- require 'hw/sources'
20
- require 'hw/packages'
21
- require 'hw/cli'
1
+ require 'hw/base'
@@ -1,49 +1,53 @@
1
- module HW::Actions
2
- def header msg
3
- say_status ">>>", "\e[36m#{msg}\e[0m", :cyan
4
- end
1
+ class HW
2
+ module Actions
3
+ extend Thor::Actions
5
4
 
6
- def info msg; say_status "info", msg, :white; end
7
- def success msg; say_status "success", msg, :green; end
8
- def error msg; say_status "error", msg, :red; end
9
- def warn msg; say_status "warning", msg, :yellow; end
5
+ def header msg
6
+ say_status ">>>", "\e[36m#{msg}\e[0m", :cyan
7
+ end
10
8
 
11
- def replace_file(destination, data = nil, config = {}, &block)
12
- remove_file(destination)
13
- create_file(destination, data, config, block)
14
- end
9
+ def info msg; say_status "info", msg, :white; end
10
+ def success msg; say_status "success", msg, :green; end
11
+ def error msg; say_status "error", msg, :red; end
12
+ def warn msg; say_status "warning", msg, :yellow; end
15
13
 
16
- def bundle
17
- header "Bundling gems"
18
- run "bundle install --quiet"
19
- end
14
+ def replace_file(destination, data = nil, config = {}, &block)
15
+ remove_file(destination)
16
+ create_file(destination, data, config, block)
17
+ end
20
18
 
21
- def migrate(options={})
22
- header "Migrating the database"
23
- env = options[:env] || 'development'
24
- rake("db:migrate", env: env, silence: true)
25
- end
19
+ def bundle
20
+ header "Bundling gems"
21
+ run "bundle install --quiet"
22
+ end
26
23
 
27
- def worker filename, data=nil, &block
28
- create_file("app/workers/#{filename}", data, verbose: false, &block)
29
- end
24
+ def migrate(options={})
25
+ header "Migrating the database"
26
+ env = options[:env] || 'development'
27
+ rake("db:migrate", env: env, silence: true)
28
+ end
29
+
30
+ def worker filename, data=nil, &block
31
+ create_file("app/workers/#{filename}", data, verbose: false, &block)
32
+ end
30
33
 
31
- def rake(command, options={})
32
- env = options[:env] || 'development'
33
- capture = options[:silence] || false
34
- sudo = options[:sudo] ? 'sudo ' : ''
34
+ def rake(command, options={})
35
+ env = options[:env] || 'development'
36
+ capture = options[:silence] || false
37
+ sudo = options[:sudo] ? 'sudo ' : ''
35
38
 
36
- #run rake db:create from "./testing"
37
- #success
38
- run("#{sudo}rake #{command} RAILS_ENV=#{env}", verbose: false, capture: capture)
39
- end
39
+ #run rake db:create from "./testing"
40
+ #success
41
+ run("#{sudo}rake #{command} RAILS_ENV=#{env}", verbose: false, capture: capture)
42
+ end
40
43
 
41
- def git(commands={})
42
- if commands.is_a?(Symbol)
43
- run "git #{commands}"
44
- else
45
- commands.each do |cmd, options|
46
- run "git #{cmd} #{options}"
44
+ def git(commands={})
45
+ if commands.is_a?(Symbol)
46
+ run "git #{commands}"
47
+ else
48
+ commands.each do |cmd, options|
49
+ run "git #{cmd} #{options}"
50
+ end
47
51
  end
48
52
  end
49
53
  end
@@ -0,0 +1,35 @@
1
+ require 'git'
2
+ require 'thor'
3
+ require 'thor/group'
4
+ require 'hw/core_ext/string'
5
+
6
+ class HW < Thor
7
+ autoload :Actions, 'hw/actions'
8
+ autoload :Sources, 'hw/sources'
9
+ autoload :Packages, 'hw/packages'
10
+
11
+ DEFAULT_SOURCE = "git@github.com:carrot/hw-packages.git"
12
+ DIRECTORY = File.expand_path("~/.hw/")
13
+ CONFIG_PATH = "#{DIRECTORY}/config"
14
+ SOURCES_PATH = "#{DIRECTORY}/sources/"
15
+ RESERVED_WORDS = %w(help)
16
+
17
+ module Base
18
+
19
+ class << self
20
+ def included(base) # :nodoc:
21
+ base.send :extend, ClassMethods
22
+ base.send :include, Thor::Actions
23
+ base.send :include, Thor::Shell
24
+ base.send :include, Actions
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+ def reserved?(arg)
30
+ arg = arg.first if arg.kind_of?(Array)
31
+ Thor::THOR_RESERVED_WORDS.include?(arg) or Thor::HELP_MAPPINGS.include?(arg)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,51 +1,64 @@
1
- module HW::Packages
2
- extend self
1
+ class HW
2
+ class Packages
3
+ extend Thor::Actions
4
+ extend Thor::Shell
5
+ extend Actions
3
6
 
4
- @@packages = {}
7
+ class << self
8
+ @@packages = {}
5
9
 
6
- def add name
7
- HW::Sources.all.each do |key, path|
8
- full_path = if HW::Sources.local_source?(path)
9
- File.join(path, "#{name}.rb")
10
- else
11
- File.join(HW::SOURCES_PATH, key, "#{name}.rb")
12
- end
10
+ def add name
11
+ Sources.all.each do |key, path|
12
+ full_path = if HW::Sources.local_source?(path)
13
+ File.join(path, "#{name}.rb")
14
+ else
15
+ File.join(SOURCES_PATH, key, "#{name}.rb")
16
+ end
13
17
 
14
- @@packages[name] = full_path if File.exists?(full_path)
15
- end
18
+ @@packages[name] = full_path if File.exists?(full_path)
19
+ end
16
20
 
17
- if reserved?(name)
18
- puts "'#{name}' is a reserved word and cannot be defined as a package"
19
- exit(1)
20
- end
21
+ if reserved?(name)
22
+ puts "'#{name}' is a reserved word and cannot be defined as a package"
23
+ exit(1)
24
+ end
21
25
 
22
- register(name, @@packages[name])
23
- end
26
+ register(name, @@packages[name])
27
+ end
24
28
 
25
- def register name, file
26
- require file
27
- klass = HW::Packages::const_get(name.to_pkg)
28
- Thor.register(klass, name, "#{name} <command>", "")
29
- rescue LoadError, TypeError
30
- puts "The package '#{name}' was not found in any of the sources"
31
- exit(1)
32
- end
29
+ def register name, file
30
+ raise LoadError if file.nil? # TODO: Don't use exception handling.
31
+ require file
32
+ klass = Packages::const_get(name.to_pkg)
33
+ Thor.register(klass, name, "#{name} <command>", "")
34
+ rescue LoadError
35
+ puts "The package '#{name}' was not found in any of the sources"
36
+ exit(1)
37
+ end
33
38
 
34
- def list
35
- HW::Sources.all.keys.each do |directory|
36
- full_path = File.join(HW::SOURCES_PATH, directory, "*.rb")
39
+ def list
40
+ header "Listing available packages"
37
41
 
38
- Dir[full_path].each do |file|
39
- key = File.basename(file, '.rb')
40
- @@packages[key] = file
42
+ Sources.all.keys.each do |directory|
43
+ full_path = File.join(SOURCES_PATH, directory, "*.rb")
44
+
45
+ Dir[full_path].each do |file|
46
+ key = File.basename(file, '.rb')
47
+ @@packages[key] = file
48
+ end
49
+ end
50
+
51
+ @@packages.merge!({ 'system' => nil, 'help' => nil })
52
+ @@packages.keys.sort
41
53
  end
42
- end
43
54
 
44
- @@packages.merge!({ 'system' => nil, 'help' => nil })
45
- @@packages.keys.sort
46
- end
55
+ def formatted_list
56
+ print_in_columns(Packages.list)
57
+ end
47
58
 
48
- def reserved? name
49
- HW::RESERVED_WORDS.include?(name.downcase)
59
+ def reserved? name
60
+ RESERVED_WORDS.include?(name.downcase)
61
+ end
62
+ end
50
63
  end
51
64
  end
@@ -0,0 +1,53 @@
1
+ require 'hw/base'
2
+
3
+ class HW::Runner < HW
4
+ include Base
5
+
6
+ def self.start(given_args = ARGV, config = {})
7
+ unless self.is_a_reserved_task?(given_args)
8
+ name = ARGV.first
9
+
10
+ if Packages.reserved?(name)
11
+ Packages.register(name, "hw/packages/#{name}")
12
+ else
13
+ Packages.add(name)
14
+ end
15
+ end
16
+
17
+ super(given_args, config)
18
+ end
19
+
20
+ desc "list", "list available packages"
21
+ def list
22
+ Packages.formatted_list
23
+ end
24
+
25
+ desc "add_source <name> <source>", "Add a source to ~/.hw/config"
26
+ def add_source name, source
27
+ Sources.add(name, source)
28
+ end
29
+
30
+ desc "update", "Update installed packages"
31
+ method_option :pull, :aliases => "-p", :type => :boolean, :default => "true", :desc => "Pull from remote sources"
32
+ def update
33
+ header "Updating hw"
34
+ Sources.ensure_defaults
35
+
36
+ if options["pull"]
37
+ Sources.fetch
38
+ else
39
+ warn "Sources not updated"
40
+ info "Remove `-p false` to update your sources"
41
+ end
42
+ end
43
+
44
+ desc "install", "Update installed packages"
45
+ method_option :pull, :aliases => "-p", :type => :boolean, :default => "true", :desc => "Pull from remote sources"
46
+ alias :install :update
47
+
48
+ no_tasks do
49
+ def self.is_a_reserved_task? args
50
+ ARGV.empty? or reserved?(args) or self.instance_methods(false).include?(args.first.to_sym)
51
+ end
52
+ end
53
+ end
@@ -1,38 +1,80 @@
1
- require 'thor/actions'
1
+ class HW
2
+ class Sources < Thor
3
+ extend Thor::Actions
4
+ extend Thor::Shell
5
+ extend Actions
2
6
 
3
- module HW::Sources
4
- extend self
7
+ class << self
8
+ @@sources = {}
5
9
 
6
- @@sources = {}
7
- @@default_source = "git@github.com:carrot/hw-packages.git"
10
+ def all
11
+ if File.exists? CONFIG_PATH
12
+ File.readlines(CONFIG_PATH).each do |line|
13
+ source = line.split("=")
14
+ path = source[1].strip
15
+ path = File.expand_path(path) if local_source?(path)
16
+ @@sources[source[0]] = path
17
+ end
18
+ end
8
19
 
9
- def all
10
- if File.exists? HW::CONFIG_PATH
11
- File.readlines(HW::CONFIG_PATH).each do |line|
12
- source = line.split("=")
13
- path = source[1].strip
14
- path = File.expand_path(path) if local_source?(path)
15
- @@sources[source[0]] = path
20
+ @@sources
16
21
  end
17
- end
18
22
 
19
- @@sources
20
- end
23
+ def add name, source
24
+ self.destination_root = "."
25
+ header "Appending #{name} to #{CONFIG_PATH}"
26
+ append_file CONFIG_PATH, "#{name}=#{source}\n"
27
+ end
21
28
 
22
- def add
23
- # TODO: Currently a part of the System package
24
- end
29
+ def ensure_defaults
30
+ empty_directory DIRECTORY unless File.exists? DIRECTORY
31
+ empty_directory SOURCES_PATH unless File.exists? SOURCES_PATH
25
32
 
26
- def git_clone
27
- # TODO: Clone git repo
28
- end
33
+ unless File.exists? CONFIG_PATH
34
+ header "Adding default source to #{CONFIG_PATH}"
29
35
 
30
- def git_pull
31
- # TODO: Pull changes from git repo
32
- end
36
+ create_file CONFIG_PATH
37
+ self.class.new.invoke :add_source, ["default", DEFAULT_SOURCE] # self.class.new added else RSpec screws up.
38
+ end
39
+ end
40
+
41
+ def fetch
42
+ self.all.each do |name, url|
43
+ begin
44
+ path = "#{SOURCES_PATH}/#{name}"
45
+ local = self.local_source?(url)
46
+
47
+ unless local
48
+ if File.exists?(path)
49
+ Git.open(path).pull
50
+ else
51
+ Git.clone(url, name, :path => SOURCES_PATH) unless local
52
+ end
53
+ end
33
54
 
34
- def local_source? path
35
- git_regex = /([A-Za-z0-9]+@|http(|s)\:\/\/)([A-Za-z0-9.]+)(:|\/)([A-Za-z0-9\-\/]+)/
36
- (path =~ git_regex).nil?
55
+ if local and !File.exists?(url)
56
+ error "Local directory '#{url}' not found. Please check your sources at #{CONFIG_PATH}"
57
+ else
58
+ success "Successfully pulled updates from #{url} to #{SOURCES_PATH}#{name}"
59
+ end
60
+ rescue Git::GitExecuteError => e
61
+ warn "Nothing was pulled from #{url}"
62
+ end
63
+ end
64
+ end
65
+
66
+ def git_clone
67
+ # TODO: Clone git repo
68
+ end
69
+
70
+ def git_pull
71
+ # TODO: Pull changes from git repo
72
+ end
73
+
74
+ def local_source? path
75
+ git_regex = /([A-Za-z0-9]+@|http(|s)\:\/\/)([A-Za-z0-9.]+)(:|\/)([A-Za-z0-9\-\/]+)/
76
+ (path =~ git_regex).nil?
77
+ end
78
+ end
37
79
  end
38
80
  end
@@ -1,7 +1,7 @@
1
- module HW
1
+ class HW
2
2
  MAJOR = 1
3
3
  MINOR = 1
4
- TINY = 8
4
+ TINY = 9
5
5
  PRE = nil
6
6
 
7
7
  VERSION = [MAJOR, MINOR, TINY, PRE].compact.join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hw
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.8
4
+ version: 1.1.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-04 00:00:00.000000000 Z
12
+ date: 2013-05-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
@@ -89,11 +89,11 @@ extensions: []
89
89
  extra_rdoc_files: []
90
90
  files:
91
91
  - lib/hw/actions.rb
92
- - lib/hw/cli.rb
92
+ - lib/hw/base.rb
93
93
  - lib/hw/core_ext/string.rb
94
94
  - lib/hw/packages.rb
95
+ - lib/hw/runner.rb
95
96
  - lib/hw/sources.rb
96
- - lib/hw/thor.rb
97
97
  - lib/hw/version.rb
98
98
  - lib/hw.rb
99
99
  - bin/hw
@@ -114,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
114
  version: '0'
115
115
  segments:
116
116
  - 0
117
- hash: -760293877887696916
117
+ hash: -3696391133987059405
118
118
  required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  none: false
120
120
  requirements:
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  version: '0'
124
124
  segments:
125
125
  - 0
126
- hash: -760293877887696916
126
+ hash: -3696391133987059405
127
127
  requirements: []
128
128
  rubyforge_project:
129
129
  rubygems_version: 1.8.25
@@ -1,96 +0,0 @@
1
- module HW
2
- class CLI < HW::Thor
3
- def self.start(given_args = ARGV, config = {})
4
- unless ARGV.empty? or self.instance_methods(false).include?(ARGV.first.to_sym)
5
- name = ARGV.first
6
-
7
- if HW::Packages.reserved?(name)
8
- HW::Packages.register(name, "hw/packages/#{name}")
9
- else
10
- HW::Packages.add(name)
11
- end
12
- end
13
-
14
- super(given_args, config)
15
- end
16
-
17
- desc "list", "list available packages"
18
- def list
19
- header "Listing available packages"
20
- Thor::Shell::Basic.new.print_in_columns(HW::Packages.list)
21
- end
22
-
23
- desc "add_source <name> <source>", "Add a source to ~/.hw/config"
24
- def add_source name, source
25
- header "Appending #{name} to #{CONFIG_PATH}"
26
- append_file CONFIG_PATH, "#{name}=#{source}\n"
27
- end
28
-
29
- desc "version", "display gem version information"
30
- def version
31
- dependency = Gem::Dependency.new('hw', Gem::Requirement::default)
32
- versions = Gem::SpecFetcher::fetcher.find_matching(dependency, true, false, false).collect do |entry|
33
- if tuple = entry.first
34
- tuple[1]
35
- end
36
- end
37
-
38
- latest = versions.last.to_s
39
-
40
- if latest != VERSION
41
- error "Your version is out of date."
42
- info "Your Version: #{VERSION}"
43
- info "Current Version: #{latest}"
44
- else
45
- success "Your version is up to date."
46
- info "Your Version: #{VERSION}"
47
- end
48
- end
49
-
50
- desc "update", "Update installed packages"
51
- method_option :pull, :aliases => "-p", :type => :boolean, :default => "true", :desc => "Pull from remote sources"
52
- def update
53
- header "Updating hw"
54
-
55
- # Setup File Structure
56
- empty_directory DIRECTORY unless File.exists? DIRECTORY
57
- empty_directory SOURCES_PATH unless File.exists? SOURCES_PATH
58
-
59
- unless File.exists? CONFIG_PATH
60
- header "Adding default source to #{CONFIG_PATH}"
61
-
62
- create_file CONFIG_PATH
63
- self.class.new.invoke :add_source, ["default", DEFAULT_SOURCE] # self.class.new added else RSpec screws up.
64
- end
65
-
66
- # Iterate through sources and take appropriate actions. TODO: Refactor.
67
- if options["pull"]
68
- HW::Sources.all.each do |name, url|
69
- begin
70
- path = "#{SOURCES_PATH}/#{name}"
71
- local = HW::Sources.local_source?(url)
72
-
73
- unless local
74
- if File.exists?(path)
75
- Git.open(path).pull
76
- else
77
- Git.clone(url, name, :path => SOURCES_PATH) unless local
78
- end
79
- end
80
-
81
- if local and !File.exists?(url)
82
- error "Local directory '#{url}' not found. Please check your sources at #{CONFIG_PATH}"
83
- else
84
- success "Successfully pulled updates from #{url} to #{SOURCES_PATH}#{name}"
85
- end
86
- rescue Git::GitExecuteError => e
87
- warn "Nothing was pulled from #{url}"
88
- end
89
- end
90
- else
91
- warn "Sources not updated"
92
- info "Remove --pull=false to update your sources"
93
- end
94
- end
95
- end
96
- end
@@ -1,7 +0,0 @@
1
- module HW
2
- class Thor < ::Thor
3
- include Thor::Actions
4
- include Rails::Generators::Actions if defined?(Rails)
5
- include HW::Actions
6
- end
7
- end