drudje 0.0.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/drudje +11 -0
- data/lib/drudje.rb +102 -0
- data/lib/drudje_io.rb +13 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2203c5ba667e105d476c9d8165107254dd7ea547
|
4
|
+
data.tar.gz: 5d80ac441733bf7b44afffeca555f440da9afb7a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 50eae938e6b6760fb5e677d456c2c4d21545030c651f2b759baed98e9a0899bacbc6084fb73b50f280e3e4f81547ccf1a931e5a0cc7a9c831de4e39d5b35ae79
|
7
|
+
data.tar.gz: e0fcd1a5b7c0562bfe5f4e72ca0f724d3411084832b4af8d95ec311d68b58780eded49a573ff64d43324cd3d5bf0123d8089804f4f2f0f1580807085f1c37a9c
|
data/bin/drudje
ADDED
data/lib/drudje.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
require_relative 'drudje_io.rb'
|
2
|
+
|
3
|
+
class Drudje
|
4
|
+
# TODO: Separate file access to separate object
|
5
|
+
# so that we can test everything
|
6
|
+
attr_accessor :src, :dest, :extension, :io
|
7
|
+
def initialize(src, dest, ext)
|
8
|
+
@extension = ext
|
9
|
+
@src = src
|
10
|
+
@dest = dest
|
11
|
+
@io = DrudjeIo.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def render(template, args)
|
15
|
+
template.gsub(/\[\[=([^\]]+)\]\]/){
|
16
|
+
if(args[$1] != nil)
|
17
|
+
args[$1]
|
18
|
+
else
|
19
|
+
''
|
20
|
+
end
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
def unquote(str)
|
25
|
+
if str.start_with?('"', "'")
|
26
|
+
str = str.slice(1..-1)
|
27
|
+
end
|
28
|
+
if str.end_with?('"', "'")
|
29
|
+
str = str.slice(0..-2)
|
30
|
+
end
|
31
|
+
str
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_args(call_str)
|
35
|
+
arg_str = call_str.strip.partition(/[\n\r\s]+/)[2]
|
36
|
+
if is_empty_args(arg_str)
|
37
|
+
{}
|
38
|
+
elsif is_arg_hash(arg_str)
|
39
|
+
get_arg_hash arg_str
|
40
|
+
else
|
41
|
+
{'contents' => arg_str}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def is_empty_args(arg_str)
|
46
|
+
arg_str =~ /\A[\s\n\r]*\z/
|
47
|
+
end
|
48
|
+
|
49
|
+
def is_arg_hash(arg_str)
|
50
|
+
arg_str =~ /\A([^ ]+=.+ *)+\z/
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_arg_hash(arg_str)
|
54
|
+
parts = arg_str.scan(/(?:"(?:\\.|[^"])*"|[^" ])+/)
|
55
|
+
res = {}
|
56
|
+
parts.each{|part|
|
57
|
+
key, val = part.split('=')
|
58
|
+
res[key] = unquote(val)
|
59
|
+
}
|
60
|
+
res
|
61
|
+
end
|
62
|
+
|
63
|
+
def template_file(call_str)
|
64
|
+
parts = call_str.strip.partition(/[\s\n\r]+/)
|
65
|
+
File.join self.src, parts[0] + self.extension
|
66
|
+
end
|
67
|
+
|
68
|
+
def output_file(file)
|
69
|
+
base = File.basename file
|
70
|
+
File.join self.dest, base
|
71
|
+
end
|
72
|
+
|
73
|
+
def expand(call_str)
|
74
|
+
template = io.read template_file(call_str)
|
75
|
+
args = get_args call_str
|
76
|
+
render template, args
|
77
|
+
end
|
78
|
+
|
79
|
+
def process_pass(str)
|
80
|
+
str.gsub(/\[\[([^\[\]]+)\]\]/){ expand($1) }
|
81
|
+
end
|
82
|
+
|
83
|
+
def process(str)
|
84
|
+
processed = process_pass(str)
|
85
|
+
while(processed != str)
|
86
|
+
str = processed
|
87
|
+
processed = process_pass(str)
|
88
|
+
end
|
89
|
+
processed
|
90
|
+
end
|
91
|
+
|
92
|
+
def run
|
93
|
+
pattern = File.join self.src, '*' + self.extension
|
94
|
+
files = Dir.glob(pattern)
|
95
|
+
files.each do |file|
|
96
|
+
puts "Processing file " + file
|
97
|
+
contents = self.io.read file
|
98
|
+
processed = process contents
|
99
|
+
self.io.write output_file(file), processed
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
data/lib/drudje_io.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
class DrudjeIo
|
4
|
+
def read(file)
|
5
|
+
throw "Could not find " + file if !File.exists?(file)
|
6
|
+
File.read file
|
7
|
+
end
|
8
|
+
def write(file, str)
|
9
|
+
path = File.dirname file
|
10
|
+
FileUtils.mkdir_p(path) unless File.exists?(path)
|
11
|
+
File.write file, str
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: drudje
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian DeJong
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple text file templating tool
|
14
|
+
email: brian.dejong@gmail.com
|
15
|
+
executables:
|
16
|
+
- drudje
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/drudje.rb
|
21
|
+
- lib/drudje_io.rb
|
22
|
+
- bin/drudje
|
23
|
+
homepage: http://github.com/badjer/drudje
|
24
|
+
licenses:
|
25
|
+
- Apache
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.0.3
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: drudje
|
47
|
+
test_files: []
|