file_templater 0.1.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 +7 -0
- data/bin/template +13 -0
- data/lib/file_templater/file_actions.rb +132 -0
- data/lib/file_templater/options_handler.rb +103 -0
- data/lib/file_templater/template.rb +61 -0
- data/lib/file_templater/variables.rb +11 -0
- data/lib/file_templater.rb +9 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d257cfba2b31bcafaee4785008f68e5d187401c2
|
4
|
+
data.tar.gz: c41333e40ac118c6abd00c45dd75ebd352ac7588
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 70fd4f23174142d21e63fda267855c60501945b18167cdc79705f98859c11f19bf89a88d65797f82e032adb26ee458b1aa8bcbdefb7215526f55c4e3ba996697
|
7
|
+
data.tar.gz: 629360b5ef0856d50a4438d48734ab6657699acd0028292141f1bd6fc2b47e4f554828298e9cd930f019343423c81dfea6b4b67c2835af69c683467b06e5dcda
|
data/bin/template
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
# Include rel-path ../lib/ in the $LOAD_PATH if it's not there already.
|
4
|
+
lib_directory = File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
|
5
|
+
$LOAD_PATH.unshift(lib_directory) unless $LOAD_PATH.map { |directory| File.expand_path(directory) }.include?(lib_directory)
|
6
|
+
|
7
|
+
require "file_templater"
|
8
|
+
|
9
|
+
FileTemplater::FileActions.create_hub_if_necessary
|
10
|
+
FileTemplater::FileActions.require_all_bindings
|
11
|
+
|
12
|
+
handler = FileTemplater::OptionsHandler.new(ARGV)
|
13
|
+
handler.process_actions
|
@@ -0,0 +1,132 @@
|
|
1
|
+
module FileTemplater
|
2
|
+
class FileActions
|
3
|
+
class << self
|
4
|
+
def create_hub_if_necessary
|
5
|
+
HUBS.each do |k, v|
|
6
|
+
Dir.mkdir(v) unless Dir.exists?(v)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def add(path)
|
11
|
+
expanded = File.expand_path(path)
|
12
|
+
# Three cases of what we are adding can arise:
|
13
|
+
# file of the template being added
|
14
|
+
# directory of the template being added
|
15
|
+
# binding of the template being added
|
16
|
+
type = File.directory?(expanded) ? :directory : (expanded.end_with?(".rb") ? :binding : :file)
|
17
|
+
hub = (type == :binding ? :binding : :template)
|
18
|
+
|
19
|
+
if type == :file
|
20
|
+
# If the file we are adding is a single file,
|
21
|
+
# make a directory and put the file in it.
|
22
|
+
expanded_sans_extension = File.join(HUBS[hub], File.basename(expanded, ".*"))
|
23
|
+
FileUtils.mkdir(expanded_sans_extension)
|
24
|
+
FileUtils.copy_entry(expanded, File.join(expanded_sans_extension, File.basename(expanded)))
|
25
|
+
elsif type == :binding
|
26
|
+
# If we are adding a binding,
|
27
|
+
# we need to modify the code to work with our system.
|
28
|
+
output_file = File.open(File.join(HUBS[hub], File.basename(expanded)), "w")
|
29
|
+
|
30
|
+
output_file.print "module Bindings\n"
|
31
|
+
File.open(expanded, "r").each do |line|
|
32
|
+
if line.lstrip.start_with?("class ")
|
33
|
+
output_file.print(line + "def get_binding\nbinding\nend\n")
|
34
|
+
else
|
35
|
+
output_file.print line
|
36
|
+
end
|
37
|
+
end
|
38
|
+
output_file.print "end\n"
|
39
|
+
|
40
|
+
output_file.close
|
41
|
+
|
42
|
+
# We will save the original file just in case.
|
43
|
+
FileUtils.copy_entry(expanded, File.join(HUBS[:original], File.basename(expanded)))
|
44
|
+
else
|
45
|
+
FileUtils.copy_entry(expanded, File.join(HUBS[hub], File.basename(expanded)))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def remove(path)
|
50
|
+
removing_template = !path.end_with?(".rb")
|
51
|
+
|
52
|
+
unless removing_template
|
53
|
+
# Remove the associated binding.
|
54
|
+
begin
|
55
|
+
FileUtils.remove_file(File.join(HUBS[:binding], path))
|
56
|
+
FileUtils.remove_file(File.join(HUBS[:original], path))
|
57
|
+
rescue StandardError
|
58
|
+
# If we failed to remove one of these files, try to remove a template by the same name.
|
59
|
+
removing_template = true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Remove the associated template.
|
64
|
+
FileUtils.remove_dir(File.join(HUBS[:template], path), true) if removing_template
|
65
|
+
end
|
66
|
+
|
67
|
+
def list
|
68
|
+
# In our list, we want to indicate if a template does not have a corresponding binding.
|
69
|
+
# As a result, we should list like so:
|
70
|
+
# Template Binding
|
71
|
+
# example example.rb
|
72
|
+
Terminal::Table.new do |t|
|
73
|
+
templates = list_templates.sort
|
74
|
+
bindings = list_bindings.sort
|
75
|
+
|
76
|
+
# table header
|
77
|
+
t.add_row ["Template", "Binding"]
|
78
|
+
t.add_separator
|
79
|
+
|
80
|
+
templates.each do |tm|
|
81
|
+
bind = tm + ".rb"
|
82
|
+
bind = nil unless bindings.include?(bind)
|
83
|
+
bindings.delete(bind) if bind
|
84
|
+
|
85
|
+
t.add_row [tm, bind]
|
86
|
+
end
|
87
|
+
|
88
|
+
bindings.each do |b|
|
89
|
+
t.add_row [nil, b]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def copy(path)
|
95
|
+
if File.exist?(path)
|
96
|
+
puts "A file of this name already exists.\nPlease remove it, or else this command will not work."
|
97
|
+
return
|
98
|
+
end
|
99
|
+
|
100
|
+
# This code is very similar to that of remove.
|
101
|
+
copying_template = !(path.end_with?(".rb") && File.exist?(File.join(HUBS[:binding], path)))
|
102
|
+
|
103
|
+
FileUtils.copy_entry(File.join(HUBS[copying_template ? :template : :original], path), path)
|
104
|
+
end
|
105
|
+
|
106
|
+
def combined_list
|
107
|
+
list_templates + list_bindings
|
108
|
+
end
|
109
|
+
|
110
|
+
def list_templates
|
111
|
+
unique_directory_list(HUBS[:template])
|
112
|
+
end
|
113
|
+
|
114
|
+
def list_bindings
|
115
|
+
unique_directory_list(HUBS[:binding])
|
116
|
+
end
|
117
|
+
|
118
|
+
# Requires all of the .rb files in HUBS[:binding].
|
119
|
+
def require_all_bindings
|
120
|
+
unique_directory_list(HUBS[:binding]).each do |f|
|
121
|
+
if f.end_with?(".rb")
|
122
|
+
require File.join(HUBS[:binding], f)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def unique_directory_list(path)
|
128
|
+
Dir.entries(path).reject { |d| d == "." || d == ".." }
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module FileTemplater
|
2
|
+
class OptionsHandler
|
3
|
+
def initialize(argv)
|
4
|
+
@nomodify = false
|
5
|
+
@binding = nil
|
6
|
+
|
7
|
+
@actions = []
|
8
|
+
|
9
|
+
parser = OptionParser.new do |o|
|
10
|
+
o.banner = "Usage: template [options] [binding arguments]"
|
11
|
+
|
12
|
+
o.separator ""
|
13
|
+
o.separator "Specific options:"
|
14
|
+
|
15
|
+
o.on("-t", "--template TEMPLATE",
|
16
|
+
"Load TEMPLATE to insert",
|
17
|
+
"into the current directory") do |t|
|
18
|
+
@actions << [:template, t]
|
19
|
+
end
|
20
|
+
|
21
|
+
o.on("-b", "--binding BINDING",
|
22
|
+
"Load BINDING as the binding",
|
23
|
+
"for the loaded template") do |b|
|
24
|
+
@binding = b
|
25
|
+
end
|
26
|
+
|
27
|
+
o.on("-a", "--add THING", Array,
|
28
|
+
"Add THING, a template or a binding,",
|
29
|
+
"to the template or binding directory") do |t|
|
30
|
+
@actions << [:add, t]
|
31
|
+
end
|
32
|
+
|
33
|
+
o.on("-r", "--remove THING", Array,
|
34
|
+
"Removes template or binding THING") do |tb|
|
35
|
+
@actions << [:remove, tb]
|
36
|
+
end
|
37
|
+
|
38
|
+
o.on("-l", "--list",
|
39
|
+
"Lists the templates and bindings",
|
40
|
+
"that are loaded") do
|
41
|
+
@actions << [:list]
|
42
|
+
end
|
43
|
+
|
44
|
+
o.on("-m", "--no-modify",
|
45
|
+
"Prevents modifying the template source",
|
46
|
+
"when loading") do
|
47
|
+
@nomodify = true
|
48
|
+
end
|
49
|
+
|
50
|
+
o.on("-c", "--copy TEMPLATE", Array,
|
51
|
+
"Copies TEMPLATE and corresponding binding",
|
52
|
+
"into current directory") do |tb|
|
53
|
+
@actions << [:copy, tb]
|
54
|
+
end
|
55
|
+
|
56
|
+
o.separator ""
|
57
|
+
o.separator "Common options:"
|
58
|
+
|
59
|
+
o.on_tail("-v", "--version", "Display the version") do
|
60
|
+
puts "File Templater (template) version " + VERSION
|
61
|
+
exit
|
62
|
+
end
|
63
|
+
|
64
|
+
o.on_tail("-h", "--help", "Show this message") do
|
65
|
+
puts o
|
66
|
+
exit
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
parser.parse!(argv)
|
71
|
+
@arguments = argv
|
72
|
+
end
|
73
|
+
|
74
|
+
def process_actions
|
75
|
+
@actions.each do |a|
|
76
|
+
command = a.first
|
77
|
+
arguments = a[1]
|
78
|
+
|
79
|
+
case command
|
80
|
+
when :template
|
81
|
+
# arguments is the template name,
|
82
|
+
# @arguments are the extraneous arguments
|
83
|
+
template = Template.new(arguments, @arguments, nomodify: @nomodify, bind: @binding)
|
84
|
+
template.load
|
85
|
+
when :add
|
86
|
+
arguments.each do |ar|
|
87
|
+
FileActions.add(ar)
|
88
|
+
end
|
89
|
+
when :remove
|
90
|
+
arguments.each do |ar|
|
91
|
+
FileActions.remove(ar)
|
92
|
+
end
|
93
|
+
when :list
|
94
|
+
puts FileActions.list
|
95
|
+
when :copy
|
96
|
+
arguments.each do |ar|
|
97
|
+
FileActions.copy(ar)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module FileTemplater
|
2
|
+
class Template
|
3
|
+
# options can include:
|
4
|
+
# bind: which binding rather than the default to use
|
5
|
+
# nomodify: if the template ERB will be loaded or not
|
6
|
+
def initialize(template, arguments, options = {})
|
7
|
+
@nomodify = options[:nomodify]
|
8
|
+
|
9
|
+
@template = File.join(HUBS[:template], template)
|
10
|
+
binding_string = options[:bind] || template + ".rb"
|
11
|
+
using_binding = File.exist?(File.join(HUBS[:binding], binding_string))
|
12
|
+
|
13
|
+
if using_binding
|
14
|
+
binding_string = File.basename(binding_string, ".*")
|
15
|
+
|
16
|
+
# Convert binding_string to a class object.
|
17
|
+
binding_string = "Bindings::" + binding_string.split("_").map { |w| w.capitalize }.join
|
18
|
+
binding_class = Object.const_get(binding_string)
|
19
|
+
@bind = binding_class.new(*arguments)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def load(folder = @template)
|
24
|
+
FileActions.unique_directory_list(folder).each do |f|
|
25
|
+
# We need the whole path to f, but we will keep the short name.
|
26
|
+
short_name = f
|
27
|
+
f = File.join(folder, f)
|
28
|
+
|
29
|
+
if File.directory?(f)
|
30
|
+
self.load f
|
31
|
+
else
|
32
|
+
if !@nomodify && f.end_with?(".erb")
|
33
|
+
output_file = File.open(File.join(Dir.pwd, transform_file_name(short_name)), "w")
|
34
|
+
|
35
|
+
input_file = File.open(f, "r")
|
36
|
+
output_file.print(ERB.new(input_file.read, nil, "<>").result(@bind && @bind.get_binding))
|
37
|
+
input_file.close
|
38
|
+
|
39
|
+
output_file.close
|
40
|
+
else
|
41
|
+
FileUtils.copy_entry(f, File.join(Dir.pwd, transform_file_name(short_name)))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Expands the variable-in-file-name notation.
|
48
|
+
# file is expected to be a short name
|
49
|
+
def transform_file_name(file)
|
50
|
+
if @bind
|
51
|
+
variables = file.scan(/{{([^}]*)}}/).flatten
|
52
|
+
|
53
|
+
variables.each do |v|
|
54
|
+
file.sub!("{{#{v}}}", @bind.get_binding.eval(v))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
!@nomodify && file.end_with?(".erb") ? File.basename(file, ".*") : file
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module FileTemplater
|
2
|
+
VERSION = "0.1.0"
|
3
|
+
|
4
|
+
# The hub is where we store our templates and bindings.
|
5
|
+
HUBS = {
|
6
|
+
:main => File.join(Dir.home, ".templater"),
|
7
|
+
:template => File.join(Dir.home, ".templater", "templates"),
|
8
|
+
:binding => File.join(Dir.home, ".templater", "bindings"),
|
9
|
+
:original => File.join(Dir.home, ".templater", "original")
|
10
|
+
}
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file_templater
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sam Craig
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: terminal-table
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
description:
|
28
|
+
email: sammidysam@gmail.com
|
29
|
+
executables:
|
30
|
+
- template
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- bin/template
|
35
|
+
- lib/file_templater.rb
|
36
|
+
- lib/file_templater/file_actions.rb
|
37
|
+
- lib/file_templater/options_handler.rb
|
38
|
+
- lib/file_templater/template.rb
|
39
|
+
- lib/file_templater/variables.rb
|
40
|
+
homepage: https://github.com/Sammidysam/file_templater
|
41
|
+
licenses:
|
42
|
+
- gpl-3.0
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.4.8
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: A simple system to create files/file structures from ERB templates.
|
64
|
+
test_files: []
|