skeleton_creator 0.2.0 → 0.4.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/README ADDED
File without changes
@@ -2,19 +2,103 @@
2
2
 
3
3
  Script to facilitate quick and easy creation of complex directory/file structures.
4
4
 
5
- 1. Create a YAML directory structure file
6
- Structure should be similar to the examples in config-files
7
5
 
8
- By default any leaf node results in a File being created.
9
- To override this, postfix with DIR, fx
6
+ == YAML config file
7
+
8
+ Create a YAML directory structure file
9
+ The structure should be similar to the examples in config-files, links.yml shows how to define symbolic links
10
+
11
+ By default any leaf node results in a File being created.
12
+
13
+ root:
14
+ meta: ONLY_DIRS
15
+ a:
16
+ - bee
17
+ - cee
18
+
19
+ Here a is created as a dir, with bee and cee as empty files
20
+
21
+ To override this, postfix entry with DIR, fx
10
22
  - my_dir DIR
11
23
 
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
24
+ Example: DIR
25
+ root:
26
+ a:
27
+ - bee DIR
28
+ - cee
29
+
30
+ Here a and bee are created as a dirs, while cee is an empty file
31
+
32
+ === Special meta data
33
+
34
+ If you want all nodes in a tree to be created as dirs, supply a meta: in the root of the tree
35
+
36
+ meta: ONLY_DIRS
37
+
38
+ This will ensure all the following nodes in the tree are treated as dirs, until a REVERT is encountered (see below)
39
+
40
+ Example: ONLY_DIRS
41
+
42
+ root:
43
+ meta: ONLY_DIRS
44
+ a:
45
+ - bee
46
+ - cee
47
+
48
+ Here bee and cee are created as dirs
49
+
50
+ Example:
51
+ Using ONLY_DIRS and REVERT
52
+
53
+ root:
54
+ - ONLY_DIRS
55
+ - bee
56
+ - cee
57
+ - REVERT
58
+ - dee
59
+ -
60
+ eee:
61
+ meta: ONLY_DIRS
62
+ x:
63
+ - y
64
+ - z
14
65
 
15
- This will ensure all nodes in the tree are treated as dirs
66
+ Here bee and cee will be created as dirs while dee and eee become empty files. Later y and x also are dirs because of the meta: ONLY_DIRS
67
+
68
+ Symbolic links:
69
+
70
+ apps:
71
+ category: &app-category
72
+ - games ALIAS c-games
73
+ - business
74
+ - utils
75
+ - other
76
+ all: &app-runtype
77
+ - my_games REF c-games
78
+
79
+ Here an alias c-games is created for the entry apps/category/games
80
+ Later the symbolic link my-games is created to point at whatever the alias c-games currently points to, in this case apps/category/games
81
+ An alias can be overridden later... this is illustrated below.
82
+
83
+ apps:
84
+ category: &app-category
85
+ - games ALIAS c-games
86
+ - business
87
+ - my_games REF c-games
88
+ - other
89
+ - cool-games ALIAS c-games
90
+ all: &app-runtype
91
+ - my_cool_games REF c-games
92
+
93
+ Results in this:
94
+
95
+ my_games --> (c-games) --> games
96
+ my_cool_games --> (c-games) --> cool-games
16
97
 
17
- 1. Run it!
98
+ == Usage
99
+
100
+ Use the following ruby code as a 'template'. As shown, the options argument is optional
101
+
18
102
  require 'skeleton_creator'
19
103
 
20
104
  runner = FileSystem::Runner.new file_name [, options]
@@ -24,6 +108,22 @@ Fx to generate the skeleton structure inside ~/testing as per the apps.yml file
24
108
  runner = FileSystem::Runner.new 'apps.yml'
25
109
  runner.run "~/testing"
26
110
 
111
+ OR using fake file and directory creators to simulate first
112
+ runner = FileSystem::Runner.new 'apps.yml', :fake => true
113
+ runner.run "~/testing"
114
+ runner.run "~/testing2"
115
+
116
+ OR
117
+ runner = FileSystem::Runner.new 'apps.yml'
118
+ runner.run "~/testing"
119
+ runner.run "~/testing2", :fake => true
120
+
121
+ The options :fake is false by default. Setting it on the call to run overrides the initial value (set with Runner.new) for that particular run
122
+
123
+ OR
124
+ runner.run ["testing2", "testing4"], :fake => true, :root => '~'
125
+
126
+ Creates dirs in '~/testing2' and '~/testing4'
27
127
 
28
128
  == Note on Patches/Pull Requests
29
129
 
@@ -0,0 +1,12 @@
1
+ require 'fileutils'
2
+ require 'file_system/directory/fake_directory'
3
+
4
+ module FileSystem
5
+ class DirectoryCreator < FakeDirectory
6
+ def create
7
+ # puts "DIR: #{name}"
8
+ FileUtils.mkdir_p name
9
+ end
10
+
11
+ end
12
+ end
@@ -1,5 +1,3 @@
1
- require 'fileutils'
2
-
3
1
  module FileSystem
4
2
  class FakeDirectory
5
3
 
@@ -3,7 +3,7 @@ module FileSystem
3
3
  attr_accessor :name
4
4
 
5
5
  def initialize(traverser, name)
6
- @name = ::File.join(traverser.current_dir, name)
6
+ @name = File.join(traverser.current_dir, name)
7
7
  end
8
8
 
9
9
  def create
@@ -0,0 +1,12 @@
1
+ require 'file_system/file/fake_file'
2
+
3
+ module FileSystem
4
+ class FileCreator < FakeFile
5
+
6
+ def create
7
+ # puts "FILE: #{name}"
8
+ FileUtils.touch name
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,24 @@
1
+ module FileSystem
2
+ class FakeSymbolicLink
3
+
4
+ attr_accessor :old_name, :new_name
5
+ def initialize(traverser, old_name, new_name)
6
+ @old_name = old_name
7
+ @new_name = File.join(traverser.current_dir, new_name)
8
+ end
9
+
10
+ def create
11
+ puts "SYMBOLIC LINK: #{new_name} --> #{old_name}"
12
+ # puts "ln_s #{old_name} #{new_name}"
13
+ end
14
+
15
+ def is_file?
16
+ false
17
+ end
18
+
19
+ def is_dir?
20
+ true
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ require 'fileutils'
2
+ require 'file_system/link/fake_symbolic_link'
3
+
4
+ module FileSystem
5
+ class SymbolicLinkCreator < FakeSymbolicLink
6
+ def create
7
+ if !File.exist?(old_name)
8
+ puts "Can't to link to '#{old_name}' since it does not exist"
9
+ return
10
+ end
11
+ if File.exist?(new_name)
12
+ puts "Can't create alias '#{new_name}' since a file of that name already exists"
13
+ return
14
+ end
15
+ FileUtils.ln_s old_name, new_name
16
+ end
17
+
18
+ def exist?(name)
19
+ File.exist?(name) || File.symlink?(name)
20
+ end
21
+ end
22
+ end
@@ -1,17 +1,22 @@
1
- require 'file_system/fake_file'
2
- require 'file_system/fake_directory'
3
- require 'file_system/file'
4
- require 'file_system/directory'
5
-
1
+ require 'file_system/directory/directory_creator'
2
+ require 'file_system/link/symbolic_link_creator'
3
+ require 'file_system/file/file_creator'
6
4
 
7
5
  module FileSystem
8
6
  class Traverser
9
- attr_accessor :root_dir, :dir_stack, :options
7
+ attr_accessor :root_dir, :dir_stack, :options, :reference_hash, :dirs_only
8
+
9
+ ONLY_DIRS = 'ONLY_DIRS'
10
+ REVERT = 'REVERT'
11
+ ALIAS = 'ALIAS'
12
+ REF = 'REF'
13
+ DIR = 'DIR'
10
14
 
11
15
  def initialize(root_dir, options = {})
12
16
  @root_dir ||= root_dir
13
17
  @dir_stack = []
14
18
  @options = options
19
+ @dirs_only = false
15
20
  end
16
21
 
17
22
  def traverse(obj, &blk)
@@ -26,7 +31,7 @@ module FileSystem
26
31
  end
27
32
 
28
33
  def current_dir
29
- ::File.join(root_dir, dir_stack.join('/'))
34
+ File.join(root_dir, dir_stack.join('/'))
30
35
  end
31
36
 
32
37
  def visit_dir(name)
@@ -42,9 +47,7 @@ module FileSystem
42
47
  def handle_hash(obj, &blk)
43
48
  # Forget keys because I don't know what to do with them
44
49
  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
50
+ blk.call(create_directory dir_name)
48
51
  traverse(dir_content, &blk)
49
52
  leave_dir
50
53
  end
@@ -58,34 +61,103 @@ module FileSystem
58
61
 
59
62
  def handle_single(obj, &blk)
60
63
  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)
64
+ return if parse_meta(obj)
65
+ file_name = obj
66
+ if ref?(obj)
67
+ blk.call(create_symbolic_link ref_name(obj), name(obj))
68
+ return
69
+ end
70
+ name = name(obj)
71
+ if alias?(obj)
72
+ create_reference alias_name(obj), name
68
73
  end
74
+ handle_single_entry(name, &blk)
75
+ end
76
+ end
77
+
78
+ def handle_single_entry(file_name, &blk)
79
+ if enforce_dir? file_name
80
+ dir_name = file_name.gsub( /\s#{DIR}\s/, '').strip
81
+ blk.call(create_directory dir_name)
82
+ leave_dir
83
+ else
84
+ blk.call(create_file file_name)
69
85
  end
70
86
  end
87
+
88
+ def enforce_dir?(file_name)
89
+ dirs_only || !/\sDIR\s/.match(file_name).nil?
90
+ end
91
+
92
+ def create_reference(alias_name, name)
93
+ @reference_hash ||= {}
94
+ dir = File.join(root_dir, dir_stack.join(File::SEPARATOR), name)
95
+ @reference_hash.merge!({alias_name.to_sym => dir})
96
+ end
71
97
 
72
98
  def create_directory(name)
73
99
  return FileSystem::FakeDirectory.new(self, name) if options[:fake]
74
- FileSystem::Directory.new(self, name)
100
+ FileSystem::DirectoryCreator.new(self, name)
75
101
  end
76
102
 
77
103
  def create_file(name)
78
104
  return FileSystem::FakeFile.new(self, name) if options[:fake]
79
- FileSystem::File.new(self, name)
105
+ FileSystem::FileCreator.new(self, name)
106
+ end
107
+
108
+ def create_symbolic_link(ref_name, new_name)
109
+ old_name = reference_hash[ref_name.to_sym].strip
110
+ # puts "create_symbolic_link: r:#{ref_name}, n:#{new_name}, o:#{old_name}"
111
+ return FileSystem::FakeSymbolicLink.new(self, old_name, new_name) if options[:fake]
112
+ FileSystem::SymbolicLinkCreator.new(self, old_name, new_name)
80
113
  end
114
+
81
115
 
82
- def parse_meta(key, value)
83
- return {dirs_only: true} if key == 'meta' && value == 'ONLY_DIRS'
84
- {}
116
+ def parse_meta(key, value = "")
117
+ if revert?(key, value)
118
+ @dirs_only = false
119
+ return true
120
+ end
121
+ if only_dirs?(key, value)
122
+ @dirs_only = true
123
+ return true
124
+ end
125
+ false
126
+ end
127
+
128
+ def only_dirs?(key, value = "")
129
+ key == ONLY_DIRS || key == 'meta' && value == ONLY_DIRS
130
+ end
131
+
132
+ def revert?(key, value = "")
133
+ key == REVERT || key == 'meta' && value == REVERT
134
+ end
135
+
136
+
137
+ def name(value)
138
+ value = value.gsub(/\s#{ALIAS}\s/, ',').gsub(/\s#{REF}\s/, ',')
139
+ value.split(',').first.strip
140
+ end
141
+
142
+ def alias?(value)
143
+ value.include?(ALIAS)
85
144
  end
86
145
 
146
+ def alias_name(value)
147
+ value.split(ALIAS).last.strip
148
+ end
149
+
150
+ def ref?(value)
151
+ value.include?(REF)
152
+ end
153
+
154
+ def ref_name(value)
155
+ value.split(REF).last.strip
156
+ end
157
+
158
+
87
159
  def dirs_only?
88
- options && options[:dirs_only]
160
+ dirs_only
89
161
  end
90
162
 
91
163
  end
@@ -6,23 +6,35 @@ require 'file_system/traverser'
6
6
  module FileSystem
7
7
  class Runner
8
8
  attr_accessor :yml_content, :options
9
+ attr_reader :root_dir
9
10
 
10
- def initialize(filename, options = false)
11
+ def initialize(filename, options = {})
11
12
  @yml_content = YAML.load_file(filename)
12
13
  @options = options
13
14
  end
14
15
 
15
- def run(root_dir, options = {})
16
+ def run(dir, options = {})
17
+ return run_list(dir, options) if dir.respond_to? :each
18
+
16
19
  @options = options if !options.nil?
17
- root = root_dir.gsub /~/, "#{ENV['HOME']}"
20
+ @root_dir = dir.gsub /\~/, "#{ENV['HOME']}"
18
21
  yml_content.each do |key, value|
19
- if key == 'DIRECTORY'
20
- traverser = FileSystem::Traverser.new root, options
22
+ if key == 'DIRECTORY'
23
+ traverser = FileSystem::Traverser.new root_dir, options
21
24
  traverser.traverse(value) do |file_node|
22
25
  file_node.create
23
26
  end
24
27
  end
25
28
  end
26
29
  end
30
+
31
+ protected
32
+ def run_list(dir_list, options = {})
33
+ dir_list.each do |dir|
34
+ dir = File.join(options[:root], dir) if options[:root]
35
+ run(dir, options)
36
+ end
37
+ end
38
+
27
39
  end
28
40
  end
@@ -4,37 +4,44 @@ require 'rake'
4
4
  describe "SkeletonCreator" do
5
5
  it "should create a directory structure" do
6
6
  file_name = File.expand_path(File.join(Dir.pwd, '../config-files/apps.yml'))
7
- puts "=========="
8
- puts file_name
9
7
  runner = FileSystem::Runner.new file_name
10
8
  root_dir = "~/testing"
11
9
  runner.run root_dir
12
- FileUtils.cd root_dir do
10
+ FileUtils.cd runner.root_dir do
13
11
  FileList['**/*'].size.should > 30
14
- FileUtils.rm_rf root_dir
12
+ FileUtils.rm_rf runner.root_dir
15
13
  end
16
14
  end
17
15
 
16
+ it "should create a directory structure with symbolic links" do
17
+ file_name = File.expand_path(File.join(Dir.pwd, '../config-files/links.yml'))
18
+ runner = FileSystem::Runner.new file_name
19
+ root_dir = "~/testing"
20
+ runner.run root_dir
21
+ FileUtils.cd File.join(runner.root_dir, '_tags/all') do
22
+ File.symlink?('my_games').should == true
23
+ end
24
+ FileUtils.rm_rf runner.root_dir
25
+ end
26
+
18
27
  it "should not create a directory structure if set to fake" do
19
28
  file_name = File.expand_path(File.join(Dir.pwd, '../config-files/apps.yml'))
20
- puts "=========="
21
- puts file_name
22
29
  runner = FileSystem::Runner.new file_name
23
30
  root_dir = "~/testing4"
24
31
  runner.run root_dir, :fake => true
25
- File.directory?(root_dir).should == false
26
- end
32
+ File.directory?(runner.root_dir).should == false
33
+ end
27
34
 
28
- it "should create multiple of the same directory structures in differet locaions" do
35
+ it "should create multiple of the same directory structures in differet locations" do
29
36
  file_name = File.expand_path(File.join(Dir.pwd, '../config-files/apps.yml'))
30
37
  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
38
+ root_dirs = ["testing2", "testing4"]
39
+ runner.run root_dirs, :root => '~'
40
+ root_dirs.each do |root_dir|
41
+ dir = File.join("#{ENV['HOME']}", root_dir)
42
+ File.directory?(dir).should == true
43
+ FileUtils.rm_rf dir
37
44
  end
38
- end
45
+ end
39
46
 
40
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skeleton_creator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristian Mandrup
@@ -30,15 +30,19 @@ extensions: []
30
30
 
31
31
  extra_rdoc_files:
32
32
  - LICENSE
33
+ - README
33
34
  - README.rdoc
34
35
  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
36
+ - lib/file_system/directory/directory_creator.rb
37
+ - lib/file_system/directory/fake_directory.rb
38
+ - lib/file_system/file/fake_file.rb
39
+ - lib/file_system/file/file_creator.rb
40
+ - lib/file_system/link/fake_symbolic_link.rb
41
+ - lib/file_system/link/symbolic_link_creator.rb
39
42
  - lib/file_system/traverser.rb
40
43
  - lib/skeleton_creator.rb
41
44
  - LICENSE
45
+ - README
42
46
  - README.rdoc
43
47
  has_rdoc: true
44
48
  homepage: http://github.com/aslakhellesoy/cucumber-rails
@@ -1,10 +0,0 @@
1
- require 'fileutils'
2
-
3
- module FileSystem
4
- class Directory < FakeDirectory
5
- def create
6
- FileUtils.mkdir_p name
7
- end
8
-
9
- end
10
- end
@@ -1,9 +0,0 @@
1
- module FileSystem
2
- class File < FakeFile
3
-
4
- def create
5
- FileUtils.touch name
6
- end
7
-
8
- end
9
- end