rails_component 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,4 +6,10 @@ This is applying-components util for rails
6
6
  cd example_project
7
7
  rails_component -u git@github.com:Paxa/sass_component.git
8
8
 
9
- So now you have new rails project with included awesome sass mixins what can be easily edited.
9
+ So now you have new rails project with included awesome sass mixins what can be easily edited.
10
+
11
+ List of components:
12
+
13
+ * `rails_component -u git@github.com:Paxa/sass_component.git`
14
+ * `rails_component -r https://raw.github.com/Paxa/rails_component/master/recepts/lsd.rb`
15
+ * `rails_component -u git@github.com:Paxa/social_component.git`
data/bin/rails_component CHANGED
@@ -3,25 +3,33 @@
3
3
  require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'rails_component'))
4
4
  require "slop"
5
5
 
6
+ RComponent::Trusted::URL = 'https://raw.github.com/Paxa/rails_component/master/recepts.yml'
7
+
6
8
  opts = Slop.parse do
7
9
  on :u, :url, 'Apply component from url', true
8
10
  on :d, :dir, 'Apply component from local directory', true
9
11
  on :r, :recept, 'Apply only recept. from file or url', true
10
12
  on :v, :verbose, 'Enable verbose mode'
11
-
13
+ on :l, :list, "Show list of known components"
14
+ on :a, :apply, "Apply components from trusted list", true
12
15
  on :h, :help, 'Print this help message', :tail => true do
13
16
  puts help
14
17
  exit
15
18
  end
16
19
  end
17
20
 
18
- if opts[:url]
21
+ if opts[:list]
22
+ puts RComponent::Trusted.list
23
+ elsif opts[:apply]
24
+ opts[:apply].split(" ").each do |component|
25
+ RComponent::Trusted.apply(component)
26
+ end
27
+ elsif opts[:url]
19
28
  RComponent::Applier.remote(opts[:url], Dir.pwd)
20
29
  elsif opts[:dir]
21
30
  RComponent::Applier.new(opts[:dir], Dir.pwd)
22
31
  elsif opts[:recept]
23
- require "open-uri"
24
- RComponent::Applier.new(Dir.pwd, Dir.pwd, false).fire_recept!(open(opts[:recept]).read)
32
+ RComponent::Applier.remote_recept(opts[:recept], Dir.pwd)
25
33
  else
26
34
  puts opts.help
27
35
  end
@@ -1,4 +1,8 @@
1
1
  require "tmpdir"
2
+ require "fileutils"
3
+ require "pathname"
4
+ require "open-uri"
5
+ require "yaml"
2
6
 
3
7
  module RComponent
4
8
 
@@ -11,6 +15,30 @@ module RComponent
11
15
 
12
16
  end
13
17
 
18
+ module Trusted
19
+ extend self
20
+
21
+ def list
22
+ fetch_info['trusted'].keys
23
+ end
24
+
25
+ def apply(component)
26
+ info = fetch_info['trusted'][component]
27
+ remote_url = info['url']
28
+ if info['type'] == 'component'
29
+ puts "Applying component from #{remote_url}"
30
+ RComponent::Applier.remote(remote_url, Dir.pwd)
31
+ elsif info['type'] == 'recept'
32
+ puts "Applying recept from #{remote_url}"
33
+ RComponent::Applier.remote_recept(remote_url, Dir.pwd)
34
+ end
35
+ end
36
+
37
+ def fetch_info
38
+ @info ||= YAML::load(open(RComponent::Trusted::URL))
39
+ end
40
+ end
41
+
14
42
  class Applier
15
43
 
16
44
  def self.remote(url, rails_root)
@@ -19,12 +47,15 @@ module RComponent
19
47
  new(dir, rails_root)
20
48
  end
21
49
 
50
+ def self.remote_recept(url, rails_root)
51
+ new(rails_root, rails_root, false).fire_recept!(open(url).read)
52
+ end
53
+
22
54
  def initialize(component_root, rails_root, autofire = true)
23
- @rails_root = rails_root
24
- @component_root = component_root
55
+ @rails_root = Pathname.new(rails_root)
56
+ @component_root = Pathname.new(component_root)
25
57
 
26
- recept_path = File.join(component_root, 'recept.rb')
27
- fire_recept!(File.read(recept_path)) if autofire
58
+ fire_recept!(File.read(@component_root + 'recept.rb')) if autofire
28
59
  end
29
60
 
30
61
  def fire_recept!(source)
@@ -35,7 +66,7 @@ module RComponent
35
66
  def clone_files(*list)
36
67
  Dir.chdir(@component_root) do
37
68
  list.each do |file|
38
- if File.exist?(File.join(@component_root, file))
69
+ if File.exist?(@component_root + file)
39
70
  copy_file(file)
40
71
  else
41
72
  Dir.glob(file).each {|f| copy_file(f) }
@@ -46,8 +77,8 @@ module RComponent
46
77
 
47
78
  def mkdirs(*list)
48
79
  list.each do |dir|
49
- unless File.directory?(File.join(@rails_root, dir))
50
- exec "mkdir -p #{File.join(@rails_root, dir)}"
80
+ unless File.directory?(@rails_root + dir)
81
+ pretty_make_dir(dir)
51
82
  end
52
83
  end
53
84
  end
@@ -59,39 +90,55 @@ module RComponent
59
90
  end
60
91
 
61
92
  def with_file(file, &block)
62
- File.open(File.join(@rails_root, file), 'a+:utf-8') {|f| block.call(f) }
93
+ FileUtils.mkdir_p(File.dirname(@rails_root + file))
94
+ FileUtils.touch(@rails_root + file) unless File.exist?(@rails_root + file)
95
+ File.open(@rails_root + file, 'a+:utf-8') {|f| block.call(f) }
63
96
  end
64
97
 
65
98
  def run(cmd)
66
99
  Dir.chdir(@rails_root) do
67
- exec(cmd)
100
+ puts blue(cmd)
101
+ `#{cmd}`
68
102
  end
69
103
  end
70
104
 
71
105
  def commit_changes(message)
72
106
  Dir.chdir(@rails_root) do
73
- exec "git init ." unless File.directory?(File.join(@rails_root, '.git'))
74
- exec "git add . && git commit -m \"#{message.gsub(/"/, "\\\"")}\""
107
+ exec("git init .", method(:magenta)) unless File.directory?(@rails_root + '.git')
108
+ exec("git add . && git commit -m \"#{message.gsub(/"/, "\\\"")}\"", method(:magenta))
75
109
  end
76
110
  end
77
111
 
78
112
  private
79
113
 
80
114
  def copy_file(file)
81
- if !File.directory?(File.dirname(File.join(@rails_root, file)))
82
- exec "mkdir #{File.dirname(File.join(@rails_root, file))}"
83
- end
84
-
85
- if File.directory?(File.join(@component_root, file))
86
- exec("mkdir -p #{File.join(@component_root, file)}")
115
+ if File.directory?(@component_root + file)
116
+ pretty_make_dir(file)
87
117
  else
88
- exec("cp #{File.join(@component_root, file)} #{File.join(@rails_root, file)}")
118
+ FileUtils.mkdir_p(File.dirname(@rails_root + file))
119
+ puts "#{green('Clone file')} #{file}"
120
+ FileUtils.cp(@component_root + file, @rails_root + file)
89
121
  end
90
122
  end
91
123
 
92
- def exec(cmd)
93
- puts "--- #{cmd}"
124
+ def pretty_make_dir(dir)
125
+ puts "#{green('Make dir')} #{dir}"
126
+ FileUtils.mkdir_p(@rails_root + dir)
127
+ end
128
+
129
+ def exec(cmd, filter)
130
+ puts filter.call("#{cmd}")
94
131
  `#{cmd}`
95
132
  end
133
+
134
+ def colorize(text, color_code)
135
+ "#{color_code}#{text}\e[0m"
136
+ end
137
+
138
+ def red(text); colorize(text, "\e[31m"); end
139
+ def green(text); colorize(text, "\e[32m"); end
140
+ def yellow(text); colorize(text, "\e[33m"); end
141
+ def blue(text); colorize(text, "\e[34m"); end
142
+ def magenta(text); colorize(text, "\e[35m"); end
96
143
  end
97
144
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "rails_component"
3
- s.version = "0.1.1"
3
+ s.version = "0.1.2"
4
4
  s.summary = "Applyng components for rails"
5
5
  s.description = ""
6
6
  s.author = "Pavel Evstigneev"
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rails_component
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Pavel Evstigneev
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-09 00:00:00 Z
13
+ date: 2011-06-10 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: slop