templatron 0.1.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: a053ddba2ec876941caa8fefb1fee3c4e56f1eb1
4
+ data.tar.gz: 01e553e049faae99fe03e49bfab31ce2f79476c7
5
+ SHA512:
6
+ metadata.gz: 7917dc639cec220966883f0f0a7ccc8312d718d8a43ba3982672094deea33c07fc73dfd19144f60ea926560b202d60cc243be82531358a636f9fcf2e38899eee
7
+ data.tar.gz: 511e16e9e14a082052f5bbd7530ddba41140e3a73550d5d05070e8f4c89334a191f7f9c2396d6ecd4ed75c453e6b5e3c9cc6f5b8875600ddffdd9d1960f0c99b
data/bin/templatron ADDED
@@ -0,0 +1,6 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #!/usr/bin/env ruby
3
+
4
+ require 'templatron/cli'
5
+
6
+ Templatron::execute
@@ -0,0 +1,80 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'templatron/version'
3
+ require 'templatron/config'
4
+ require 'templatron/generator'
5
+ require 'optparse'
6
+ require 'ostruct'
7
+
8
+ module Templatron
9
+
10
+ # Public: CLI Stuff, parse command line inputs to determine what to do
11
+ def self.execute
12
+
13
+ usage = 'Usage: templatron TEMPLATE_NAME [args] [-o output_dir]'
14
+
15
+ # If no argument has been given, print usage
16
+ if ARGV.length == 0
17
+ puts usage
18
+ exit
19
+ end
20
+
21
+ # Defines the structure and default values
22
+ options = OpenStruct.new
23
+ options.output_dir = Dir.pwd
24
+ options.verbose = false
25
+ options.delete_dir = false
26
+
27
+ # Defines options parser
28
+ opt_parser = OptionParser.new do |opts|
29
+ opts.banner = usage
30
+ opts.default_argv = '-h'
31
+
32
+ opts.separator ''
33
+ opts.separator 'Features:'
34
+
35
+ # Defines where to put generated files
36
+ opts.on('-o', '--output PATH', 'Where to put the generated files') do |dir|
37
+ options.output_dir = dir
38
+ end
39
+
40
+ # Should we remove the output directory first
41
+ opts.on('-d', '--delete', 'If set, clear the output directory first') do
42
+ options.delete_dir = true
43
+ end
44
+
45
+ opts.separator ''
46
+ opts.separator 'Common options:'
47
+
48
+ # Verbose mode
49
+ opts.on('-v', '--verbose', 'Verbose mode') do
50
+ options.verbose = true
51
+ end
52
+
53
+ # Print the help
54
+ opts.on_tail('-h', '--help', 'Show this message') do
55
+ puts opts
56
+ puts ''
57
+ puts "Templates path: #{Templatron::templates_path}"
58
+ exit
59
+ end
60
+
61
+ # Print version number
62
+ opts.on_tail('--version', 'Show version') do
63
+ puts Templatron::VERSION
64
+ exit
65
+ end
66
+ end
67
+
68
+ opt_parser.parse!(ARGV)
69
+
70
+ # Instantiate the generator and build the stuff
71
+ gen = Generator.new(
72
+ ARGV[0],
73
+ ARGV[1..ARGV.length],
74
+ options.output_dir,
75
+ options.delete_dir,
76
+ options.verbose)
77
+ gen.build
78
+ end
79
+
80
+ end
@@ -0,0 +1,11 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module Templatron
3
+
4
+ # This part will be joined to the user Dir.home
5
+ PREFIX = '.templatron'
6
+
7
+ # Public: Retrieve the full path which stores templates
8
+ def self.templates_path
9
+ File.join(Dir.home, PREFIX)
10
+ end
11
+ end
@@ -0,0 +1,127 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'FileUtils'
3
+ require 'templatron/config'
4
+
5
+ module Templatron
6
+
7
+ # Base class used to generates skeleton from template
8
+ # Basically, you just instantiate a Generator and call its #build method
9
+ class Generator
10
+
11
+ # Public: Initialize this Generator
12
+ #
13
+ # template_name - Name of the template to generate
14
+ # args - Array of arguments to use
15
+ # output_dir - Where to put the generated stuff
16
+ # delete_dir - Should I clean the output folder?
17
+ # verbose - Should I explain?
18
+ def initialize(template_name, args, output_dir, delete_dir, verbose)
19
+ @template = template_name
20
+ @output = File.expand_path(output_dir)
21
+ @verbose = verbose
22
+ @clear = delete_dir
23
+ @full_template_path = File.join(Templatron::templates_path, @template)
24
+
25
+ process_raw_arguments(args)
26
+ end
27
+
28
+ # Public: Effectively process a template to generate it
29
+ def build
30
+ # Check template existence
31
+ if !check_template_dir(@full_template_path)
32
+ puts "The template #{@template} does not appear to exist in #{@full_template_path}"
33
+ exit
34
+ end
35
+
36
+ # If sets, remove the output folder first
37
+ if @clear
38
+ puts "Clearing #{@output}" if @verbose
39
+ FileUtils.remove_dir(@output, true)
40
+ end
41
+
42
+ # Print details if verbose is on
43
+ if @verbose
44
+ puts "Starting building #{@full_template_path} to #{@output}"
45
+ puts "With:"
46
+ @arguments.each_with_index do |arg, i|
47
+ puts "\t{$#{i}} => #{arg}" if !arg.nil?
48
+ end
49
+ end
50
+
51
+ # And then process each files/folder
52
+ collect_str = File.join(@full_template_path, '**', '*')
53
+ # At this point, all file entries have been collected
54
+ entries = Dir[collect_str].map { |p| p if File.file?(p) }.compact
55
+ # So process them right now
56
+ process_files(entries)
57
+ end
58
+
59
+ protected
60
+
61
+ # Internal: Process each entries, copy the files and replaces variables
62
+ #
63
+ # entries - An array of files to process
64
+ def process_files(entries)
65
+ entries.each do |path|
66
+
67
+ # Get base path
68
+ new_path = path.gsub(@full_template_path, '')
69
+
70
+ # Apply arguments to the path
71
+ apply_arguments!(new_path)
72
+
73
+ # Now we can copy the entry
74
+ full_new_path = File.join(@output, new_path)
75
+
76
+ puts "Copying #{path} to #{full_new_path}" if @verbose
77
+
78
+ FileUtils.mkdir_p(File.dirname(full_new_path))
79
+ FileUtils.copy(path, full_new_path)
80
+
81
+ file_content = File.read(full_new_path)
82
+ apply_arguments!(file_content)
83
+ File.open(full_new_path, 'w') do |f|
84
+ f.puts file_content
85
+ end
86
+ end
87
+ end
88
+
89
+ # Internal: Process raw arguments to check for pairs
90
+ #
91
+ # args - Raw args send by the command line
92
+ def process_raw_arguments(args)
93
+ @arguments = []
94
+
95
+ args.each_with_index do |arg, i|
96
+ parts = arg.split('=')
97
+
98
+ if parts.length == 1
99
+ @arguments[i] = arg
100
+ elsif parts.length == 2
101
+ @arguments[parts[0].gsub(/[^0-9]/, '').to_i] = parts[1]
102
+ else
103
+ puts "Could not process argument #{arg}"
104
+ end
105
+ end
106
+ end
107
+
108
+ # Internal: Replace variable placeholders with given variable values
109
+ #
110
+ # str - Where to look & replace
111
+ def apply_arguments!(str)
112
+ str.scan(/{\$(\d*)\W?([\w\s]*)}/).each do |match|
113
+ match_i = match[0].to_i
114
+ arg_value = @arguments[match_i]
115
+ arg_value = match[1] if arg_value.nil?
116
+ str.gsub!(/{\$#{match_i}\W?([\w\s]*)}/, arg_value)
117
+ end
118
+ end
119
+
120
+ # Internal: Check if the template directory exists
121
+ #
122
+ # dir - Where to look
123
+ def check_template_dir(dir)
124
+ Dir.exist?(dir)
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module Templatron
3
+ VERSION = '0.1.1'
4
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: templatron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Julien Leicher
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Defines templates with variables and generates whatever you want
14
+ email:
15
+ - jleicher@gmail.com
16
+ executables:
17
+ - templatron
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/templatron
22
+ - lib/templatron/cli.rb
23
+ - lib/templatron/config.rb
24
+ - lib/templatron/generator.rb
25
+ - lib/templatron/version.rb
26
+ homepage: https://github.com/YuukanOO/templatron
27
+ licenses: []
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project: templatron
45
+ rubygems_version: 2.4.5
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: An easy to use scaffold generator
49
+ test_files: []