skeleton_creator 0.2.0
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.
- data/LICENSE +20 -0
- data/README.rdoc +40 -0
- data/lib/file_system/directory.rb +10 -0
- data/lib/file_system/fake_directory.rb +25 -0
- data/lib/file_system/fake_file.rb +22 -0
- data/lib/file_system/file.rb +9 -0
- data/lib/file_system/traverser.rb +92 -0
- data/lib/skeleton_creator.rb +28 -0
- data/spec/skeleton_creator_spec.rb +40 -0
- data/spec/spec_helper.rb +9 -0
- metadata +73 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Kristian Mandrup
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
= File structure creator
|
2
|
+
|
3
|
+
Script to facilitate quick and easy creation of complex directory/file structures.
|
4
|
+
|
5
|
+
1. Create a YAML directory structure file
|
6
|
+
Structure should be similar to the examples in config-files
|
7
|
+
|
8
|
+
By default any leaf node results in a File being created.
|
9
|
+
To override this, postfix with DIR, fx
|
10
|
+
- my_dir DIR
|
11
|
+
|
12
|
+
If you want all nodes in a tree to be created as dirs, supply a meta: in the root of the tree
|
13
|
+
meta: ONLY_DIRS
|
14
|
+
|
15
|
+
This will ensure all nodes in the tree are treated as dirs
|
16
|
+
|
17
|
+
1. Run it!
|
18
|
+
require 'skeleton_creator'
|
19
|
+
|
20
|
+
runner = FileSystem::Runner.new file_name [, options]
|
21
|
+
runner.run root_dir [, options]
|
22
|
+
|
23
|
+
Fx to generate the skeleton structure inside ~/testing as per the apps.yml file
|
24
|
+
runner = FileSystem::Runner.new 'apps.yml'
|
25
|
+
runner.run "~/testing"
|
26
|
+
|
27
|
+
|
28
|
+
== Note on Patches/Pull Requests
|
29
|
+
|
30
|
+
* Fork the project.
|
31
|
+
* Make your feature addition or bug fix.
|
32
|
+
* Add tests for it. This is important so I don't break it in a
|
33
|
+
future version unintentionally.
|
34
|
+
* Commit, do not mess with rakefile, version, or history.
|
35
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
36
|
+
* Send me a pull request. Bonus points for topic branches.
|
37
|
+
|
38
|
+
== Copyright
|
39
|
+
|
40
|
+
Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
module FileSystem
|
4
|
+
class FakeDirectory
|
5
|
+
|
6
|
+
attr_accessor :name
|
7
|
+
def initialize(traverser, name)
|
8
|
+
traverser.visit_dir(name)
|
9
|
+
@name = traverser.current_dir
|
10
|
+
end
|
11
|
+
|
12
|
+
def create
|
13
|
+
puts "DIR: #{name}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def is_file?
|
17
|
+
false
|
18
|
+
end
|
19
|
+
|
20
|
+
def is_dir?
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module FileSystem
|
2
|
+
class FakeFile
|
3
|
+
attr_accessor :name
|
4
|
+
|
5
|
+
def initialize(traverser, name)
|
6
|
+
@name = ::File.join(traverser.current_dir, name)
|
7
|
+
end
|
8
|
+
|
9
|
+
def create
|
10
|
+
puts "FILE: #{name}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def is_file?
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
def is_dir?
|
18
|
+
false
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'file_system/fake_file'
|
2
|
+
require 'file_system/fake_directory'
|
3
|
+
require 'file_system/file'
|
4
|
+
require 'file_system/directory'
|
5
|
+
|
6
|
+
|
7
|
+
module FileSystem
|
8
|
+
class Traverser
|
9
|
+
attr_accessor :root_dir, :dir_stack, :options
|
10
|
+
|
11
|
+
def initialize(root_dir, options = {})
|
12
|
+
@root_dir ||= root_dir
|
13
|
+
@dir_stack = []
|
14
|
+
@options = options
|
15
|
+
end
|
16
|
+
|
17
|
+
def traverse(obj, &blk)
|
18
|
+
case obj
|
19
|
+
when Hash
|
20
|
+
handle_hash(obj, &blk)
|
21
|
+
when Array
|
22
|
+
handle_list(obj, &blk)
|
23
|
+
else
|
24
|
+
handle_single(obj, &blk)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def current_dir
|
29
|
+
::File.join(root_dir, dir_stack.join('/'))
|
30
|
+
end
|
31
|
+
|
32
|
+
def visit_dir(name)
|
33
|
+
dir_stack.push name
|
34
|
+
end
|
35
|
+
|
36
|
+
def leave_dir
|
37
|
+
dir_stack.pop
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
protected
|
42
|
+
def handle_hash(obj, &blk)
|
43
|
+
# Forget keys because I don't know what to do with them
|
44
|
+
obj.each do |dir_name, dir_content|
|
45
|
+
options.merge! parse_meta(dir_name, dir_content)
|
46
|
+
blk.call(create_directory dir_name) if !(options && options[:dirs_only])
|
47
|
+
options.delete :dirs_only
|
48
|
+
traverse(dir_content, &blk)
|
49
|
+
leave_dir
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def handle_list(obj, &blk)
|
54
|
+
obj.each do |dir_content|
|
55
|
+
traverse(dir_content, &blk)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def handle_single(obj, &blk)
|
60
|
+
if obj
|
61
|
+
file_name = obj
|
62
|
+
if file_name =~ /DIR/ ||
|
63
|
+
dir_name = file_name.gsub(/DIR/, '').strip
|
64
|
+
blk.call(create_directory dir_name)
|
65
|
+
leave_dir
|
66
|
+
else
|
67
|
+
blk.call(create_file file_name)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def create_directory(name)
|
73
|
+
return FileSystem::FakeDirectory.new(self, name) if options[:fake]
|
74
|
+
FileSystem::Directory.new(self, name)
|
75
|
+
end
|
76
|
+
|
77
|
+
def create_file(name)
|
78
|
+
return FileSystem::FakeFile.new(self, name) if options[:fake]
|
79
|
+
FileSystem::File.new(self, name)
|
80
|
+
end
|
81
|
+
|
82
|
+
def parse_meta(key, value)
|
83
|
+
return {dirs_only: true} if key == 'meta' && value == 'ONLY_DIRS'
|
84
|
+
{}
|
85
|
+
end
|
86
|
+
|
87
|
+
def dirs_only?
|
88
|
+
options && options[:dirs_only]
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'file_system/traverser'
|
3
|
+
|
4
|
+
# filename = 'structures/development/directory.yml'
|
5
|
+
|
6
|
+
module FileSystem
|
7
|
+
class Runner
|
8
|
+
attr_accessor :yml_content, :options
|
9
|
+
|
10
|
+
def initialize(filename, options = false)
|
11
|
+
@yml_content = YAML.load_file(filename)
|
12
|
+
@options = options
|
13
|
+
end
|
14
|
+
|
15
|
+
def run(root_dir, options = {})
|
16
|
+
@options = options if !options.nil?
|
17
|
+
root = root_dir.gsub /~/, "#{ENV['HOME']}"
|
18
|
+
yml_content.each do |key, value|
|
19
|
+
if key == 'DIRECTORY'
|
20
|
+
traverser = FileSystem::Traverser.new root, options
|
21
|
+
traverser.traverse(value) do |file_node|
|
22
|
+
file_node.create
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
describe "SkeletonCreator" do
|
5
|
+
it "should create a directory structure" do
|
6
|
+
file_name = File.expand_path(File.join(Dir.pwd, '../config-files/apps.yml'))
|
7
|
+
puts "=========="
|
8
|
+
puts file_name
|
9
|
+
runner = FileSystem::Runner.new file_name
|
10
|
+
root_dir = "~/testing"
|
11
|
+
runner.run root_dir
|
12
|
+
FileUtils.cd root_dir do
|
13
|
+
FileList['**/*'].size.should > 30
|
14
|
+
FileUtils.rm_rf root_dir
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should not create a directory structure if set to fake" do
|
19
|
+
file_name = File.expand_path(File.join(Dir.pwd, '../config-files/apps.yml'))
|
20
|
+
puts "=========="
|
21
|
+
puts file_name
|
22
|
+
runner = FileSystem::Runner.new file_name
|
23
|
+
root_dir = "~/testing4"
|
24
|
+
runner.run root_dir, :fake => true
|
25
|
+
File.directory?(root_dir).should == false
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should create multiple of the same directory structures in differet locaions" do
|
29
|
+
file_name = File.expand_path(File.join(Dir.pwd, '../config-files/apps.yml'))
|
30
|
+
runner = FileSystem::Runner.new file_name
|
31
|
+
root_dirs = ["~/testing", "#{ENV['HOME']}/testing2"]
|
32
|
+
root_dirs.each do |root_dir|
|
33
|
+
runner.run root_dir
|
34
|
+
FileUtils.cd root_dir
|
35
|
+
FileList['**/*'].size.should > 70
|
36
|
+
FileUtils.rm_rf root_dir
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: skeleton_creator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kristian Mandrup
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-25 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.0.0
|
24
|
+
version:
|
25
|
+
description: Generate a skeleton file structure from a YAML config file
|
26
|
+
email: kmandrup@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- lib/file_system/directory.rb
|
36
|
+
- lib/file_system/fake_directory.rb
|
37
|
+
- lib/file_system/fake_file.rb
|
38
|
+
- lib/file_system/file.rb
|
39
|
+
- lib/file_system/traverser.rb
|
40
|
+
- lib/skeleton_creator.rb
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/aslakhellesoy/cucumber-rails
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- --charset=UTF-8
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.5
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Generate a skeleton file structure from a YAML file
|
71
|
+
test_files:
|
72
|
+
- spec/skeleton_creator_spec.rb
|
73
|
+
- spec/spec_helper.rb
|