fs_template 0.1.2 → 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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MmJjZDU4ODM0MjgxMzk5Y2U0M2Q3OGQwZmU4OGYxNzA2ZWIzNjE0OA==
4
+ YmUxYzM1OGE1OTVjZGY3MDc5YWZlMzc3NjY2MGQwYmRjNGQ4ZWQ0ZA==
5
5
  data.tar.gz: !binary |-
6
- YThjNDQ1OGFkOTY1NmQ2NTkxMzk3MDkyMzlmNWZiNGJiOTFkNTFhNQ==
6
+ M2I0M2IyZWRjNTIxYjY3ZjdjYzY5NDBjMGMyNjQ1ZjRmNDhmYTdlYg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YmNmNTc3NzhlOTk0NTY5YWE5YzZhNTU0YjUxMzBmODQ4OGVkMjYxMDMwNjlm
10
- MTAzNjA1NGVlYWMyZjZlYWFiZDcyYmY3YjAyYTQ5ODc0YzVkNjY0NmU4ZDFk
11
- NTM0ODIxNzkzNDMzMTQyZGZiNTA3MDMyZTRlNDhhNjNjYjA3NWU=
9
+ NzczZjQ1MWMzM2VjZjczZWZkYWYzYWY2MmI1NTZkZTc5NjlmYzkyMTBjZWE0
10
+ YzZjZWY4MDAyYzNjOGE3NDVhZGE2M2I1MGE3MDBhMDY5OTcyZGNiMjFiZjU5
11
+ NzY5NTU3NWE0NjAxNWU5ZWQzYzRiYzVmNWNiNTA4YWU0M2Y0MjE=
12
12
  data.tar.gz: !binary |-
13
- MDczNmVkODZlY2NlN2IxN2YzNjg0YTRlNDI3YjlmYTlkODVmZDYxZmE5YTZh
14
- YmFjODlmMDNlYTkzMWI3NTllY2M1MTFmZGYwNTI2OWE4NTdmNmFlOTUyZmVm
15
- ZTJkNWQ2YmQ5YmEwYzcwNzRhOTRlMGYxYjc3NTcyYjliNDVlZDQ=
13
+ NmE3MmIzYjk1ZTBkOGMwYzE5ZDYzN2MxNWE4MzJiN2ViOTI2ZTQ4YTQzODli
14
+ MGE3NDI2YjUwMjA4M2RkYTU2ZjI0MDBhMTcxMTFjOTZmMGU5MzZlNzA2NGJj
15
+ MmQ4MWY1YTU1ODM2Yjg3OWIxNGMzNzczZmJjOWMwZDllNTEwZTY=
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
data/fs_template.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "fs_template"
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Mike Harris"]
12
- s.date = "2013-11-01"
12
+ s.date = "2013-11-07"
13
13
  s.description = "fs_template"
14
14
  s.email = "mharris717@gmail.com"
15
15
  s.executables = ["fs_template"]
@@ -31,9 +31,11 @@ Gem::Specification.new do |s|
31
31
  "fs_template.gemspec",
32
32
  "lib/fs_template.rb",
33
33
  "lib/fs_template/files.rb",
34
+ "lib/fs_template/from_command.rb",
34
35
  "lib/fs_template/project.rb",
35
36
  "lib/fs_template/template_file.rb",
36
37
  "lib/fs_template/thor_file.rb",
38
+ "spec/from_command_spec.rb",
37
39
  "spec/fs_template_spec.rb",
38
40
  "spec/input/top/.overlay",
39
41
  "spec/input/top/b.txt",
data/lib/fs_template.rb CHANGED
@@ -1,13 +1,38 @@
1
1
  require 'mharris_ext'
2
2
 
3
- %w(files template_file thor_file project).each do |f|
3
+ %w(files template_file thor_file project from_command).each do |f|
4
4
  load File.dirname(__FILE__) + "/fs_template/#{f}.rb"
5
5
  end
6
6
 
7
7
  module FsTemplate
8
8
  class << self
9
+ def with_repo_path(url)
10
+ dir = "/tmp/#{rand(100000000000000000000)}"
11
+ `mkdir #{dir}`
12
+ Dir.chdir(dir) do
13
+ `git clone #{url} .`
14
+ end
15
+ yield dir
16
+ ensure
17
+ `rm -rf #{dir}`
18
+ end
19
+ def with_local_path(overlay_path,&b)
20
+ if overlay_path =~ /git/
21
+ with_repo_path(overlay_path) do |dir|
22
+ b[dir]
23
+ end
24
+ else
25
+ yield overlay_path
26
+ end
27
+ end
9
28
  def write_project(overlay_path,output_path)
10
- FsTemplate::Project.new(:path => overlay_path).write_to!(output_path)
29
+ with_local_path(overlay_path) do |dir|
30
+ FsTemplate::Project.new(:path => dir).write_to!(output_path)
31
+ end
32
+ end
33
+
34
+ def ec(cmd,ops={})
35
+ `#{cmd}`
11
36
  end
12
37
  end
13
38
  end
@@ -1,6 +1,7 @@
1
1
  module FsTemplate
2
2
  class Files
3
3
  include FromHash
4
+ include Enumerable
4
5
  fattr(:files) { [] }
5
6
  fattr(:file_class) { TemplateFile }
6
7
  def add(ops)
@@ -37,10 +38,11 @@ module FsTemplate
37
38
  res = Dir["#{dir}/**/*"] + Dir["#{dir}/**/.*"]
38
39
  res - [".","..",".git"]
39
40
  end
40
- def load_dir(dir)
41
+ def load_dir(dir,ops={})
41
42
  raise "Bad dir" unless dir.present?
42
- raise "Dir not there" unless FileTest.exist?(dir)
43
+ raise "Dir not there #{dir}" unless FileTest.exist?(dir)
43
44
  res = new
45
+ res.file_class = ops[:file_class] if ops[:file_class]
44
46
  dir_files(dir).each do |full_file|
45
47
  if FileTest.file?(full_file)
46
48
  f = full_file.gsub("#{dir}/","")
@@ -51,12 +53,18 @@ module FsTemplate
51
53
  res
52
54
  end
53
55
 
54
- def load(descriptor)
56
+ def load_command(cmd,ops)
57
+ FromCommand.new(:command => cmd, :path => ops[:path]||".").files
58
+ end
59
+
60
+ def load(descriptor, ops={})
55
61
  raise "bad #{descriptor}" if descriptor.blank?
56
- if descriptor =~ /\.git/
62
+ if ops[:type] == :command
63
+ load_command(descriptor,ops)
64
+ elsif descriptor =~ /\.git/
57
65
  load_repo(descriptor)
58
66
  else
59
- load_dir(descriptor)
67
+ load_dir(descriptor,ops)
60
68
  end
61
69
  end
62
70
 
@@ -0,0 +1,23 @@
1
+ module FsTemplate
2
+ class FromCommand
3
+ include FromHash
4
+ attr_accessor :command, :path
5
+
6
+ def with_tmp_dir
7
+ dir = "/tmp/#{rand(1000000000000000000)}"
8
+ `mkdir #{dir}`
9
+ Dir.chdir(dir) do
10
+ yield dir
11
+ end
12
+ ensure
13
+ ec "rm -rf #{dir}", :silent => true
14
+ end
15
+
16
+ fattr(:files) do
17
+ with_tmp_dir do |dir|
18
+ FsTemplate.ec command, :silent => true
19
+ Files.load [dir,path].select { |x| x.present? }.join("/"), :file_class => BasicFile
20
+ end
21
+ end
22
+ end
23
+ end
@@ -24,12 +24,20 @@ module FsTemplate
24
24
  config.overlays + [path]
25
25
  end
26
26
 
27
+ def commands(phase)
28
+ config.commands.select { |x| x[:phase] == phase }.map { |x| x[:command] }
29
+ end
30
+
27
31
  fattr(:overlays) do
28
32
  overlay_paths.map { |x| Files.load(x) }
29
33
  end
30
34
 
31
35
  fattr(:base_files) do
32
- Files.load(config.base)
36
+ if config.base
37
+ Files.load(config.base,config.base_ops)
38
+ else
39
+ nil
40
+ end
33
41
  end
34
42
 
35
43
  fattr(:combined_files) do
@@ -40,27 +48,46 @@ module FsTemplate
40
48
  res
41
49
  end
42
50
 
51
+ def git_commit(output_path,message,init=false)
52
+ if init
53
+ `rm -rf #{output_path}/.git`
54
+ ec "cd #{output_path} && git init && git config user.email johnsmith@fake.com && git config user.name 'John Smith'", :silent => true
55
+ end
56
+ ec "cd #{output_path} && git add . && git commit -m '#{message}'", :silent => true
57
+ end
58
+
59
+
43
60
  def write_to!(output_path)
61
+ commands(:before).each do |cmd|
62
+ FsTemplate.ec "cd #{output_path} && #{cmd}", :silent => true
63
+ git_commit output_path, "Ran Command: #{cmd}"
64
+ end
65
+
44
66
  base_files.write_to! output_path
45
- `rm -rf #{output_path}/.git`
46
67
 
47
- full_init = 'git init && git config user.email johnsmith@fake.com && git config user.name "John Smith"'
48
- ec "cd #{output_path} && #{full_init} && git add . && git commit -m 'Base Files #{config.base}'", :silent => true
68
+ git_commit output_path, "Base Files #{config.base}", true
49
69
  combined_files.write_to!(output_path)
50
- ec "cd #{output_path} && git add . && git commit -m 'Overlay Files #{path}'", :silent => true
70
+ git_commit output_path, "Overlay Files #{path}"
71
+
72
+ commands(:after).each do |cmd|
73
+ FsTemplate.ec "cd #{output_path} && #{cmd}", :silent => true
74
+ git_commit output_path, "Ran Command: #{cmd}"
75
+ end
51
76
  end
52
77
  end
53
78
 
54
79
  class ProjectConfig
55
80
  include FromHash
56
- attr_accessor :body, :base
81
+ attr_accessor :body, :base, :base_ops
57
82
  fattr(:overlays) { [] }
83
+ fattr(:commands) { [] }
58
84
 
59
85
  def base(*args)
60
86
  if args.empty?
61
87
  @base
62
88
  else
63
89
  @base = args.first
90
+ @base_ops = args[1] || {}
64
91
  end
65
92
  end
66
93
 
@@ -68,6 +95,10 @@ module FsTemplate
68
95
  self.overlays << name
69
96
  end
70
97
 
98
+ def command(cmd,phase=:after)
99
+ self.commands << {:command => cmd, :phase => phase}
100
+ end
101
+
71
102
  def load!
72
103
  c = self
73
104
  eval(body)
@@ -22,7 +22,7 @@ module FsTemplate
22
22
  else
23
23
  lines.each do |line|
24
24
  parts = line.split(":").map { |x| x.strip }.select { |x| x.present? }
25
- raise "bad" unless parts.size == 2
25
+ raise "bad #{path} #{parts.inspect}" unless parts.size == 2
26
26
  res[parts[0].to_sym] = parts[1]
27
27
  end
28
28
  end
@@ -40,6 +40,12 @@ module FsTemplate
40
40
  raise "no change, couldn't find #{params[:after]} in \n#{base_body}"
41
41
  end
42
42
  end
43
+ elsif params[:action] == 'insert' && params[:before]
44
+ base_body.gsub(params[:before],"#{body}#{params[:before]}").tap do |subbed|
45
+ if subbed == base_body
46
+ raise "no change, couldn't find #{params[:before]} in \n#{base_body}"
47
+ end
48
+ end
43
49
  elsif params[:action] == 'replace' && params[:base]
44
50
  base_body.gsub(params[:base],body).tap do |subbed|
45
51
  if subbed == base_body
@@ -117,4 +123,10 @@ module FsTemplate
117
123
  File.create "#{dir}/#{path}",body
118
124
  end
119
125
  end
126
+
127
+ class BasicFile < TemplateFile
128
+ def body
129
+ full_body
130
+ end
131
+ end
120
132
  end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe 'FromCommand' do
4
+ let(:command) do
5
+ "mkdir abc && cd abc && echo stuff > abc.txt"
6
+ end
7
+
8
+ let(:from_command) do
9
+ FsTemplate::FromCommand.new(:command => command, :path => "abc")
10
+ end
11
+
12
+ it 'files' do
13
+ from_command.files.size.should == 1
14
+ from_command.files.first.path.should == 'abc.txt'
15
+ from_command.files.first.full_body.strip.should == 'stuff'
16
+ end
17
+ end
@@ -125,6 +125,23 @@ describe "combine - insert after" do
125
125
  end
126
126
  end
127
127
 
128
+ describe "combine - insert before" do
129
+ include_context "setup"
130
+
131
+ base_file "a.txt","stuff"
132
+ base_file "b.txt","123\n456\n789"
133
+ on_top_file "b.txt","<overlay>action: insert\nbefore: 456</overlay>abc\n"
134
+
135
+ it 'combined size' do
136
+ combined.size.should == 2
137
+ end
138
+
139
+ it 'combined file should overwrite' do
140
+ f = combined.files.find { |x| x.path == "b.txt" }
141
+ f.body.should == "123\nabc\n456\n789"
142
+ end
143
+ end
144
+
128
145
  describe "combine - replace" do
129
146
  include_context "setup"
130
147
 
@@ -62,6 +62,89 @@ describe 'Project' do
62
62
  end
63
63
  end
64
64
 
65
+ describe 'Project with command' do
66
+ let(:config_body) do
67
+ "c.base :foo
68
+ c.overlay :bar
69
+ c.command 'ls'"
70
+ end
71
+
72
+ let(:project) do
73
+ res = FsTemplate::Project.new(:path => "/fun")
74
+ res.stub(:config_body) { config_body }
75
+ res
76
+ end
77
+
78
+ before do
79
+ FsTemplate::Files.stub(:load) { FsTemplate::Files.new }
80
+ end
81
+
82
+ it 'commands' do
83
+ project.commands(:after).should == ["ls"]
84
+ end
85
+ end
86
+
87
+ describe 'Project order' do
88
+ let(:config_body) do
89
+ "c.base :foo
90
+ c.overlay :bar
91
+ c.command 'ls'"
92
+ end
93
+
94
+ let(:project) do
95
+ res = FsTemplate::Project.new(:path => "/tmp/a/b/c/fun")
96
+ res.stub(:config_body) { config_body }
97
+ res
98
+ end
99
+
100
+ before do
101
+ FsTemplate::Files.stub(:load) { FsTemplate::Files.new }
102
+ end
103
+
104
+ it 'write' do
105
+ output_path = "/tmp/f/t/r/r"
106
+
107
+ FsTemplate.should_receive(:ec).with("cd #{output_path} && ls", :silent => true)
108
+ project.stub(:git_commit)
109
+ project.combined_files.stub("write_to!")
110
+
111
+ project.write_to! output_path
112
+ end
113
+ end
114
+
115
+ describe 'Project with no base' do
116
+ let(:config_body) do
117
+ "c.base 'mkdir foo && echo stuff > foo/abc.txt', :type => :command, :path => :foo"
118
+ end
119
+
120
+ let(:output_path) do
121
+ res = "/tmp/#{rand(1000000000000000)}"
122
+ `mkdir #{res}`
123
+ res
124
+ end
125
+
126
+ after do
127
+ `rm -rf #{output_path}`
128
+ end
129
+
130
+ let(:project) do
131
+ res = FsTemplate::Project.new(:path => "/tmp/a/b/c/fun")
132
+ res.stub(:config_body) { config_body }
133
+ res
134
+ end
135
+
136
+ it 'write' do
137
+ #FsTemplate.should_receive(:ec).with("echo stuff > abc.txt", :silent => true)
138
+ project.stub(:git_commit)
139
+ project.stub(:overlay_paths) { [] }
140
+
141
+ project.write_to! output_path
142
+
143
+ File.read("#{output_path}/abc.txt").strip.should == 'stuff'
144
+ end
145
+ end
146
+
147
+
65
148
  describe "write project" do
66
149
  include_context "output dir"
67
150
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fs_template
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Harris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-01 00:00:00.000000000 Z
11
+ date: 2013-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mharris_ext
@@ -186,9 +186,11 @@ files:
186
186
  - fs_template.gemspec
187
187
  - lib/fs_template.rb
188
188
  - lib/fs_template/files.rb
189
+ - lib/fs_template/from_command.rb
189
190
  - lib/fs_template/project.rb
190
191
  - lib/fs_template/template_file.rb
191
192
  - lib/fs_template/thor_file.rb
193
+ - spec/from_command_spec.rb
192
194
  - spec/fs_template_spec.rb
193
195
  - spec/input/top/.overlay
194
196
  - spec/input/top/b.txt