boilerplater 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/bin/bp +43 -0
  2. data/lib/boilerplater.rb +11 -0
  3. data/lib/parser.rb +111 -0
  4. metadata +66 -0
data/bin/bp ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'optparse'
5
+
6
+ $:.unshift File::expand_path(File.join(File.dirname(__FILE__), '..'))
7
+
8
+ require 'lib/boilerplater.rb'
9
+
10
+ options = {}
11
+
12
+ optparse = OptionParser.new do |opts|
13
+ opts.on( '-s', '--search BOILERPLATE', 'Search for specific boilerplate') do |keyword|
14
+ puts BoilerPlater.search keyword
15
+ exit 0
16
+ end
17
+ opts.on '-u', '--use ID', 'Download and install specific boilerplate' do |id|
18
+ options[:id] = id
19
+ end
20
+ opts.on '-p', '--prefix PREFIX', 'Use prefix for installation' do |prefix|
21
+ options[:prefix] = prefix
22
+ end
23
+ opts.on('-h', '--help') { options[:help] = true }
24
+ end
25
+
26
+ optparse.parse!
27
+
28
+ if options[:help]
29
+ puts optparse
30
+ exit(0)
31
+ end
32
+
33
+ BoilerPlater.setup do |b|
34
+ b.prefix = options[:prefix] || '.'
35
+ end
36
+
37
+ if options[:id]
38
+ BoilerPlater.use(options[:id])
39
+ else
40
+ puts "ERROR: You must specify the boiler plate ID"
41
+ exit 1
42
+ end
43
+
@@ -0,0 +1,11 @@
1
+ if RUBY_VERSION =~ /^1\.8/
2
+ require 'rubygems'
3
+ require 'require_relative'
4
+ end
5
+
6
+ require 'gist'
7
+ require 'ostruct'
8
+ require 'fileutils'
9
+ require 'open-uri'
10
+
11
+ require_relative './parser.rb'
data/lib/parser.rb ADDED
@@ -0,0 +1,111 @@
1
+ module BoilerPlater
2
+
3
+ def self.config(c=nil)
4
+ @config = c if !c.nil?
5
+ @config || OpenStruct.new
6
+ end
7
+
8
+ def self.setup(&block)
9
+ c = config
10
+ yield c && config(c)
11
+ end
12
+
13
+ def self.get(gist_id)
14
+ Gist.read(gist_id)
15
+ end
16
+
17
+ def self.use(gist_id)
18
+ BoilerPlater.sections(get(gist_id)).each do |f|
19
+ f.download_content! unless f.content?
20
+ f.save!
21
+ end
22
+ end
23
+
24
+ def self.list
25
+ get(3266785).each_line.map { |l| l.split(';') }.inject({}) { |r, l| r[l.first.to_i] = l.last.strip; r }
26
+ end
27
+
28
+ def self.search(name)
29
+ list.find { |k, v| v =~ /#{name}/ }.first
30
+ end
31
+
32
+ def self.sections(content)
33
+ sections, section = [], ''
34
+ content.each_line do |line|
35
+ if line =~ /^##/
36
+ sections << Parser::Section.new(section) if !section.empty?
37
+ section = line
38
+ else
39
+ section += line
40
+ end
41
+ end
42
+ sections << Parser::Section.new(section)
43
+ end
44
+
45
+ module Parser
46
+
47
+ def self.new(content)
48
+ BoilerPlate.new(content)
49
+ end
50
+
51
+ class BoilerPlate
52
+ attr_reader :content
53
+
54
+ def initialize(content)
55
+ @content = content
56
+ end
57
+
58
+ end
59
+
60
+ class Section
61
+ attr_reader :file
62
+ attr_reader :metadata
63
+ attr_reader :content
64
+
65
+ def initialize(content)
66
+ @content = content.split("\n")
67
+ @metadata = parse_metadata(@content.shift.strip)
68
+ @content = @content.join("\n")
69
+ @file = @metadata[:filename]
70
+ end
71
+
72
+ def save!
73
+ FileUtils.mkdir_p(File.dirname(path)) unless File.directory?(File.dirname(path))
74
+ File.open(path, 'w') do |f|
75
+ f.puts(content)
76
+ end
77
+ end
78
+
79
+ def path
80
+ FileUtils.mkdir_p(BoilerPlater.config.prefix) if !BoilerPlater.config.prefix.nil?
81
+ File.join(BoilerPlater.config.prefix || '.', File.dirname(@file), File.basename(@file))
82
+ end
83
+
84
+ def download_content!
85
+ @content = open(metadata[:url]).read
86
+ end
87
+
88
+ def content?
89
+ !content.empty?
90
+ end
91
+
92
+ private
93
+
94
+ def parse_metadata(line)
95
+ raise 'Each section must start with ##' unless line =~ /^##/
96
+ line.gsub!(/^##(\s+)/, '')
97
+ line.split('->').inject({}) { |result, part|
98
+ if part.strip =~ /^(\w+):\/\//
99
+ result[:url] = part.strip
100
+ elsif !part.nil?
101
+ result[:filename] = part.strip
102
+ end
103
+ result
104
+ }
105
+ end
106
+
107
+ end
108
+
109
+ end
110
+
111
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boilerplater
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michal Fojtik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: gist
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Speed up your development using prepared boiler plate templates
31
+ email:
32
+ - mi@mifo.sk
33
+ executables:
34
+ - bp
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - bin/bp
39
+ - lib/boilerplater.rb
40
+ - lib/parser.rb
41
+ homepage: http://github.com/mifo/boilerplater
42
+ licenses:
43
+ - MIT
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: 1.3.6
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.24
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Simple boiler plate system to bootstrap dev of new applications
66
+ test_files: []