crafti 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: 8299ece2f642e270e5b6c4b01439c77588fc809a
4
+ data.tar.gz: 604abfbcf1bb50eb6ed5c97b97a59a2133d67fbd
5
+ SHA512:
6
+ metadata.gz: 948efff771bb9bf32ba10f6e9aa07b86248df6f0136b2612231dfc08fb371cf4bc2aa09baadfe3fc59b56a5abb62907df7edaac4faf8f1615e58901aadae496f
7
+ data.tar.gz: 8ef0627c439ab35a87e23e2c989d3678348960e803f900d706909478738f29f556f846763967710ad1ed20b315b3b0834f3c0e194693de6b56e78325a1c1931b
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Robert Evans
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 @@
1
+ # Craft
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/**/**/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => 'test'
data/bin/craft ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # require 'rubygems'
4
+ # require 'craft'
5
+
6
+ case ARGV.first
7
+ when "template"
8
+ # generate a root app_template
9
+ else
10
+ puts "That command is not supported."
11
+ end
data/craft.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 'craft/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "crafti"
8
+ spec.version = Craft.version
9
+ spec.authors = ["Robert Evans"]
10
+ spec.email = ["robert@codewranglers.org"]
11
+ spec.summary = %q{Generate directories and files with/out templates.}
12
+ spec.description = %q{Generate directories and files with/out templates.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ end
@@ -0,0 +1,3 @@
1
+ require_relative '../craft'
2
+
3
+ Kernel.extend(Craft::KernelExtension)
@@ -0,0 +1,11 @@
1
+ module Craft
2
+ MAJOR = 0
3
+ MINOR = 0
4
+ PATCH = 1
5
+
6
+ def self.version
7
+ [
8
+ MAJOR, MINOR, PATCH
9
+ ].join('.')
10
+ end
11
+ end
data/lib/craft.rb ADDED
@@ -0,0 +1,110 @@
1
+ require "craft/version"
2
+
3
+ require 'fileutils'
4
+ require 'pathname'
5
+ require 'erb'
6
+ require 'ostruct'
7
+
8
+ module Craft
9
+ module KernelExtension
10
+ def self.extended(mod)
11
+ mod.module_eval do
12
+ def root(name, &block)
13
+ ::Craft::Root.root(name, &block)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ class TemplateBinding
20
+ def initialize(hash)
21
+ hash.each do |key, value|
22
+ singleton_class.send(:define_method, key) { value }
23
+ end
24
+ end
25
+
26
+ def get_binding
27
+ binding
28
+ end
29
+ end
30
+
31
+ module FileUtilsExtension
32
+ def mkdir(dir, options = {})
33
+ ::FileUtils.mkdir_p(app_path.join(dir).to_s, options)
34
+ end
35
+ alias_method :mkdir_p, :mkdir
36
+
37
+ def mkdirs(*directories, mode: 0644, noop: false)
38
+ directories.each do |dir|
39
+ mkdir(dir, mode: mode, verbose: false, noop: noop)
40
+ end
41
+ end
42
+
43
+ def touch(*files, noop: false)
44
+ files.each do |file|
45
+ ::FileUtils.touch(app_path.join(file).to_s, verbose: false, noop: noop)
46
+ end
47
+ end
48
+
49
+ def copy(list, dir, options = {})
50
+ ::FileUtils.cp(list, app_path.join(dir).to_s, options)
51
+ end
52
+ alias_method :cp, :copy
53
+
54
+ def chmod(mode, file)
55
+ file = [file].flatten.map { |f| app_path.join(f).to_s }
56
+ ::FileUtils.chmod(mode, file)
57
+ end
58
+
59
+ def chmod_r(mode, file)
60
+ file = [file].flatten.map { |f| app_path.join(f).to_s }
61
+ ::FileUtils.chmod_R(mode, file)
62
+ end
63
+
64
+ def run(command)
65
+ system("cd #{app_path} && #{command}")
66
+ end
67
+
68
+ def sudo(command)
69
+ run "sudo #{command}"
70
+ end
71
+
72
+ def template(dest, erb_template, values)
73
+ temp = ::Pathname.new(erb_template)
74
+
75
+ namespace = TemplateBinding.new(values)
76
+ content = ::ERB.new(temp.read).
77
+ result(namespace.get_binding)
78
+
79
+ ::File.open(app_path.join(dest).to_s, 'w+') do |f|
80
+ f.puts content
81
+ end
82
+ end
83
+ end
84
+
85
+ class Root
86
+ include FileUtilsExtension
87
+
88
+ def self.root(appname, &block)
89
+ app = new(appname)
90
+ app.create_root_directory
91
+ app.evaluate(&block) if block_given?
92
+ end
93
+
94
+ attr_reader :appname, :app_path
95
+
96
+ def initialize(appname)
97
+ @appname = appname
98
+ @app_path = ::Pathname.new(appname).expand_path
99
+ end
100
+
101
+ def evaluate(&block)
102
+ instance_eval &block
103
+ end
104
+
105
+ def create_root_directory
106
+ ::FileUtils.mkdir_p(app_path)
107
+ end
108
+
109
+ end
110
+ end
data/spike.rb ADDED
@@ -0,0 +1,110 @@
1
+ require 'fileutils'
2
+ require 'pathname'
3
+
4
+ module Generate
5
+ # generate files from templates
6
+ # generate directories
7
+ # copy files
8
+ # generate files via touch
9
+ #
10
+ # TODO:
11
+ #
12
+ # Turn FileUtils into a DSL
13
+
14
+ # Generate.project!(options.project_name, options.template_path)
15
+ def self.project!(project_name, template_path)
16
+ Project.new(project_name, template_path).generate
17
+ end
18
+
19
+ class Project
20
+ attr_reader :name, :path
21
+
22
+ def initialize(name, path)
23
+ @name, @path = name, ::Pathname.new(path).expand_path
24
+ end
25
+
26
+ def generate
27
+ eval do
28
+ include Template
29
+ path.read
30
+ end
31
+ end
32
+ end
33
+
34
+ module Template
35
+ extend FileUtils
36
+
37
+ def self.root(name, &block)
38
+ mkdir_p name
39
+ module_eval &block
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ # bin/generate file:
46
+ require 'rubygems'
47
+ require 'generate'
48
+ require 'optparse'
49
+ require 'ostruct'
50
+
51
+ class ProjectParser
52
+ def self.parse(args)
53
+ options = OpenStruct.new
54
+ options.project_name = args.reverse.pop
55
+ options.root_path = Dir.pwd
56
+
57
+ opt_parser = OptionParser.new do |opts|
58
+ opts.banner = "Usage: generate project_name app_template.rb"
59
+
60
+ opts.seperator ""
61
+ opts.on("-m", "--template", "The Template File used to create your project") do |template|
62
+ options.template_path = template
63
+ end
64
+
65
+ opts.on("-v", "--version", "Generate Version") do |v|
66
+ puts "Version Number 0.0.1"
67
+ exit
68
+ end
69
+
70
+ opts.on_tail("-h", "--help", "Get some Help") do
71
+ puts opts
72
+ exit
73
+ end
74
+ end
75
+
76
+ opt_parser.parse!(args)
77
+ options
78
+ end
79
+ end
80
+
81
+
82
+ options = ProjectParser.parse(ARGV)
83
+ puts options
84
+ puts ARGV
85
+
86
+
87
+ Generate.project!(options.project_name, options.template_path)
88
+
89
+
90
+
91
+ # Run this by doing:
92
+ #
93
+ # generate project -m app_template.rb
94
+
95
+
96
+ # This is the app_template.rb file
97
+ #
98
+ root(appname) do
99
+ mkdir "config"
100
+ mkdir "log"
101
+ mkdir "test"
102
+
103
+ touch "Readme.mkd"
104
+ template "config.ru", file_or_string
105
+ template "config/application.rb", file_or_string
106
+ cp "test/test_helper.rb", file_or_string
107
+ template "bin/script", file_or_string
108
+ chmod "bin/script", 700
109
+ ln "db/database.yml", "db/database.yml.example"
110
+ end
@@ -0,0 +1,14 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require 'ostruct'
4
+
5
+ module TestHelper
6
+ def create_user
7
+ @user ||= begin
8
+ OpenStruct.new(name: '<%= name %>',
9
+ age: '<%= age %>',
10
+ gender: '<%= gender %>')
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,178 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+
4
+ require_relative '../lib/craft'
5
+
6
+ module Craft
7
+ class RootTest < ::Minitest::Test
8
+ def test_create_root_directory
9
+ ::Craft::Root.root("appname")
10
+
11
+ assert ::File.exists?("appname")
12
+
13
+ ::FileUtils.rm_rf("appname")
14
+ refute ::File.exists?("appname")
15
+ end
16
+
17
+ def test_creating_subdirectories
18
+ ::Craft::Root.root("appname") do
19
+ mkdir "config"
20
+ mkdir "log"
21
+ mkdir "test"
22
+ end
23
+
24
+ assert ::File.exists?("appname")
25
+ assert ::File.exists?("appname/config")
26
+ assert ::File.exists?("appname/log")
27
+ assert ::File.exists?("appname/test")
28
+
29
+ ::FileUtils.rm_rf("appname")
30
+ refute ::File.exists?("appname")
31
+ end
32
+
33
+ def test_creating_multiple_subdirectories
34
+ ::Craft::Root.root("appname") do
35
+ mkdirs "config", "log", "test", mode: 0700
36
+ end
37
+
38
+ assert ::File.exists?("appname")
39
+ assert ::File.exists?("appname/config")
40
+ assert ::File.exists?("appname/log")
41
+ assert ::File.exists?("appname/test")
42
+
43
+ ::FileUtils.rm_rf("appname")
44
+ refute ::File.exists?("appname")
45
+ end
46
+
47
+ def test_touching_a_file
48
+ ::Craft::Root.root("appname") do
49
+ touch "Readme.mkd"
50
+ end
51
+
52
+ assert ::File.exists?("appname")
53
+ assert ::File.exists?("appname/Readme.mkd")
54
+
55
+ ::FileUtils.rm_rf("appname")
56
+ refute ::File.exists?("appname")
57
+ end
58
+
59
+ def test_touching_files
60
+ ::Craft::Root.root("appname") do
61
+ touch "Readme.mkd", "Rakefile", "Guardfile"
62
+ end
63
+
64
+ assert ::File.exists?("appname")
65
+ assert ::File.exists?("appname/Readme.mkd")
66
+ assert ::File.exists?("appname/Rakefile")
67
+ assert ::File.exists?("appname/Guardfile")
68
+
69
+ ::FileUtils.rm_rf("appname")
70
+ refute ::File.exists?("appname")
71
+ end
72
+
73
+ def test_copying_files
74
+ ::Craft::Root.root("appname") do
75
+ mkdir "test"
76
+ copy Pathname.new(__dir__).join("test_helper.rb"), "test"
77
+ end
78
+
79
+ assert ::File.exists?("appname")
80
+ assert ::File.exists?("appname/test")
81
+ assert ::File.exists?("appname/test/test_helper.rb")
82
+
83
+ ::FileUtils.rm_rf("appname")
84
+ refute ::File.exists?("appname")
85
+ end
86
+
87
+ def test_chmod
88
+ ::Craft::Root.root("appname") do
89
+ mkdir "test"
90
+ copy ::Pathname.new(__dir__).join("test_helper.rb"), "test"
91
+ chmod 0777, "test/test_helper.rb"
92
+ end
93
+
94
+ assert ::File.exists?("appname")
95
+ assert ::File.exists?("appname/test")
96
+ assert ::File.exists?("appname/test/test_helper.rb")
97
+
98
+ assert ::File.readable?("appname/test/test_helper.rb")
99
+ assert ::File.writable?("appname/test/test_helper.rb")
100
+ assert ::File.executable?("appname/test/test_helper.rb")
101
+
102
+ ::FileUtils.rm_rf("appname")
103
+ refute ::File.exists?("appname")
104
+ end
105
+
106
+ def test_chmod_r
107
+ ::Craft::Root.root("appname") do
108
+ mkdir "test"
109
+ copy ::Pathname.new(__dir__).join("test_helper.rb"), "test"
110
+ chmod_r 0777, "test"
111
+ end
112
+
113
+ assert ::File.exists?("appname")
114
+ assert ::File.exists?("appname/test")
115
+ assert ::File.exists?("appname/test/test_helper.rb")
116
+
117
+ assert ::File.readable?("appname/test/test_helper.rb")
118
+ assert ::File.writable?("appname/test/test_helper.rb")
119
+ assert ::File.executable?("appname/test/test_helper.rb")
120
+
121
+ ::FileUtils.rm_rf("appname")
122
+ refute ::File.exists?("appname")
123
+ end
124
+
125
+ def test_run
126
+ ::Craft::Root.root("appname") do
127
+ run "mkdir foobar"
128
+ end
129
+
130
+ assert ::File.exists?("appname")
131
+ assert ::File.exists?("appname/foobar")
132
+
133
+ ::FileUtils.rm_rf("appname")
134
+ refute ::File.exists?("appname")
135
+ end
136
+
137
+ # Commented out because the test stops to get the users password and that's annoying.
138
+ # I'm keeping this here though, so we can test it when we want to.
139
+ #
140
+ # def test_sudo
141
+ # ::Craft::Root.root("appname") do
142
+ # sudo "echo 'RACK_ENV=development' > .env"
143
+ # end
144
+
145
+ # assert ::File.exists?("appname")
146
+ # assert ::File.exists?("appname/.env")
147
+ # assert_equal 'RACK_ENV=development', ::File.read("appname/.env").chomp
148
+
149
+ # ::FileUtils.rm_rf("appname")
150
+ # refute ::File.exists?("appname")
151
+ # end
152
+
153
+ def test_template
154
+ ::Craft::Root.root("appname") do
155
+ mkdir "test"
156
+ template "test/test_helper.rb",
157
+ ::Pathname.new(__dir__).
158
+ join('assets/test_helper.rb.erb'),
159
+ {
160
+ name: 'Robert Evans',
161
+ age: 31,
162
+ gender: 'male'
163
+ }
164
+ end
165
+
166
+ assert ::File.exists?("appname")
167
+ assert ::File.exists?("appname/test")
168
+ assert ::File.exists?("appname/test/test_helper.rb")
169
+
170
+ assert_match 'Robert Evans', ::File.read("appname/test/test_helper.rb")
171
+ assert_match '31', ::File.read("appname/test/test_helper.rb")
172
+ assert_match 'male', ::File.read("appname/test/test_helper.rb")
173
+
174
+ ::FileUtils.rm_rf("appname")
175
+ refute ::File.exists?("appname")
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,2 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crafti
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Robert Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-09 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Generate directories and files with/out templates.
56
+ email:
57
+ - robert@codewranglers.org
58
+ executables:
59
+ - craft
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/craft
69
+ - craft.gemspec
70
+ - lib/craft.rb
71
+ - lib/craft/project.rb
72
+ - lib/craft/version.rb
73
+ - spike.rb
74
+ - test/assets/test_helper.rb.erb
75
+ - test/craft_test.rb
76
+ - test/test_helper.rb
77
+ homepage: ''
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.0
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Generate directories and files with/out templates.
101
+ test_files:
102
+ - test/assets/test_helper.rb.erb
103
+ - test/craft_test.rb
104
+ - test/test_helper.rb