dumb_generator 0.0.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
+ SHA256:
3
+ metadata.gz: 212e50e575b56cc79d186ea9dab28b05533bd7fc745ec5fc98060011d77f017f
4
+ data.tar.gz: f8fa34490cc652ab0859578a7c68cb17f17fbf4cba55c411385b09764502e229
5
+ SHA512:
6
+ metadata.gz: 5798849ccebf89204cc34e7bf549d6d4e350f3bf70a0c4f835594a503e39fedb2722a79afc08323c5aa8ab61cb8a28b9618201480dfb1991a4b0d26537fe5e41
7
+ data.tar.gz: 9debaacc29c7a1685703ae659963a840815f13ff37dfbb98c062e09900fb9ece0527e043b00452b6bf29120613ab1a82d2f0da227cea43abf2de4c43a03b0021
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright <YEAR> <COPYRIGHT HOLDER>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ ## Introduction
2
+
3
+ This is a simple scaffolding generator. A Copy-Paste tool with some extra features like variable injection, and it intends on remaining like this.
4
+
5
+ ## Getting started
6
+
7
+ ```zsh
8
+ gem install dumb_generator
9
+ ```
10
+
11
+ ## Setup
12
+
13
+ Go to your project forder and run
14
+
15
+ ```zsh
16
+ dgen init
17
+ dgen template:default class_name:MyClass file_name:my_file
18
+ ```
19
+
20
+ This will generate the default example scaffold, 2 text files in different directories.
data/bin/dgen ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'dumb_generator'
4
+
5
+ Dumb::Generator::Base.new.execute!
@@ -0,0 +1,24 @@
1
+ require_relative 'template_config_assembler'
2
+ require_relative '../input/validator'
3
+ require_relative '../template/linker/base'
4
+
5
+ module Dumb
6
+ module Generator
7
+ class Runner
8
+ attr_reader :arguments
9
+
10
+ def initialize(arguments)
11
+ @arguments = arguments
12
+ end
13
+
14
+ def execute!
15
+ config = TemplateConfigAssembler.new(arguments).execute!
16
+
17
+ arguments.delete('template')
18
+ Dumb::Input::ArgumentVariablesValidator.new(arguments, config).validate!
19
+
20
+ Dumb::Template::Linker::Base.new(config, arguments).execute!
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,56 @@
1
+ require_relative '../template/validator'
2
+
3
+ module Dumb
4
+ module Generator
5
+ class TemplateConfigAssembler
6
+ attr_reader :arguments
7
+
8
+ def initialize(arguments)
9
+ @arguments = arguments
10
+ end
11
+
12
+ def execute!
13
+ templates_path = Dir.pwd + '/.dumb_templates'
14
+
15
+ if !Dir.exist?(templates_path)
16
+ print ".dumb_templates: does not exist in current directory.\nrun: $> dgen init\n"
17
+ exit
18
+ end
19
+
20
+ template_names = Dir.entries(templates_path).select do |entry|
21
+ files = File.directory?(File.join(templates_path, entry))
22
+
23
+ files and !(entry == '.' || entry == '..')
24
+ end
25
+
26
+ template_names.each do |template|
27
+ Dumb::Template::Validator.new(templates_path, template).validate!
28
+ end
29
+
30
+ selected_template = arguments.key?('template') ? arguments['template'] : nil
31
+ if selected_template.nil?
32
+ print "dumb_generator: template not specified\n"
33
+ exit
34
+ end
35
+
36
+ if !template_names.include?(selected_template)
37
+ print "dumb_generator: template `#{selected_template}` not found\nAvailable templates:\n"
38
+ print "#{template_names}\n"
39
+ exit
40
+ end
41
+
42
+ templates = {}
43
+ template_names.each do |template|
44
+ templates[template] = {
45
+ template_path: "#{templates_path}/#{template}",
46
+ root_path: "#{templates_path}/#{template}/root",
47
+ linker_path: "#{templates_path}/#{template}/linker.yml",
48
+ variables_path: "#{templates_path}/#{template}/variables.yml"
49
+ }
50
+ end
51
+
52
+ templates[selected_template]
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,34 @@
1
+ module Dumb
2
+ module Input
3
+ module Parser
4
+ def self.commands
5
+ passed_commands = ARGV.select { |a| !a.include?(':') }
6
+
7
+ if (passed_commands.length > 1)
8
+ print "dumb_generator: too many commands, run one at a time\n"
9
+ exit
10
+ end
11
+
12
+ available_commands = ["run", "init", "help"]
13
+ passed_command = passed_commands.first || "run"
14
+
15
+ if !available_commands.include?(passed_command)
16
+ print "dumb_generator: command `#{passed_command}` not found in:\n#{available_commands}\n"
17
+ exit
18
+ end
19
+
20
+ arguments = ARGV.each_with_object({}) do |argument_variable, memo|
21
+ var_name = argument_variable.split(':').first
22
+ var_value = argument_variable.split(':').last
23
+
24
+ memo[var_name] = var_value
25
+ end
26
+
27
+ {
28
+ command: passed_command,
29
+ arguments: arguments,
30
+ }
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,51 @@
1
+ require 'yaml'
2
+
3
+ module Dumb
4
+ module Input
5
+ class ArgumentVariablesValidator
6
+ attr_reader :variables, :defined_variables
7
+
8
+ UNKNOWN_VARIABLE_ERROR = 'Unkown variable `%s`'
9
+ MISSING_VARIABLES_ERROR = 'Missing variables %s'
10
+
11
+ def initialize(variables, config)
12
+ @variables = variables
13
+ @config = config
14
+
15
+ @defined_variables = YAML.load(
16
+ File.read(@config[:variables_path])
17
+ ).to_h['variables']
18
+ end
19
+
20
+ def validate!
21
+ validate_existing_variables!
22
+
23
+ validate_missing_variables!
24
+ end
25
+
26
+ private
27
+
28
+ def validate_existing_variables!
29
+ variables.keys.each do |variable|
30
+ if !defined_variables.include?(variable)
31
+ print "Unknown variable: #{variable} in:\n"
32
+ print UNKNOWN_VARIABLE_ERROR % [variable]
33
+
34
+ exit
35
+ end
36
+ end
37
+ end
38
+
39
+ def validate_missing_variables!
40
+ if !missing_variables?
41
+ print "Missing variables: #{defined_variables - variables.keys}\n"
42
+ exit
43
+ end
44
+ end
45
+
46
+ def missing_variables?
47
+ defined_variables & variables.keys == defined_variables
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,86 @@
1
+ module Dumb
2
+ module Template
3
+ class Initializer
4
+ def execute!
5
+ puts 'Initializing dumb_generator...'
6
+
7
+ # add .dumb_templates folder
8
+ if !File.directory?('.dumb_templates')
9
+ Dir.mkdir('.dumb_templates')
10
+ puts 'Created .dumb_templates folder'
11
+ end
12
+
13
+ # add default template
14
+ if !File.directory?('.dumb_templates/default')
15
+ Dir.mkdir('.dumb_templates/default')
16
+ end
17
+
18
+ createLinker!
19
+ createVariables!
20
+ createDgenFiles!
21
+
22
+ puts 'Done!'
23
+ end
24
+
25
+ private
26
+
27
+ def createDgenFiles!
28
+ # create root folder
29
+ if !File.directory?('.dumb_templates/default/root')
30
+ Dir.mkdir('.dumb_templates/default/root')
31
+ end
32
+
33
+ # create dgen files
34
+ file = File.new('.dumb_templates/default/root/file.dgen', 'w')
35
+ file.puts <<-TEXT
36
+ class {{class_name}}Class {}
37
+ TEXT
38
+
39
+ file.close
40
+
41
+ if !File.directory?('.dumb_templates/default/root/subdir')
42
+ Dir.mkdir('.dumb_templates/default/root/subdir')
43
+ end
44
+
45
+ file = File.new('.dumb_templates/default/root/subdir/file.dgen', 'w')
46
+ file.puts <<-TEXT
47
+ class Sub{{class_name}}Class {}
48
+ TEXT
49
+
50
+ file.close
51
+ end
52
+
53
+ def createLinker!
54
+ # create linker.yml
55
+ file = File.new('.dumb_templates/default/linker.yml', 'w')
56
+ file.puts <<-TEXT
57
+ # Suports variables defined in variables.yml
58
+ # All paths are relative
59
+
60
+ root:
61
+ link_destination_directory: "../../src/{{file_name}}"
62
+ links:
63
+ - link:
64
+ dgen_template: "subdir/file.dgen"
65
+ to_destinaiton_file: "subdir/{{file_name}}_file.txt"
66
+ - link:
67
+ dgen_template: "file.dgen"
68
+ to_destinaiton_file: "{{file_name}}_file.txt"
69
+ TEXT
70
+
71
+ file.close
72
+ end
73
+
74
+ def createVariables!
75
+ file = File.new('.dumb_templates/default/variables.yml', 'w')
76
+ file.puts <<-TEXT
77
+ variables:
78
+ - file_name
79
+ - class_name
80
+ TEXT
81
+
82
+ file.close
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,45 @@
1
+ require 'yaml'
2
+
3
+ require_relative 'content_parser'
4
+ require_relative 'path_parser'
5
+ require_relative 'file_creator'
6
+
7
+ module Dumb
8
+ module Template
9
+ module Linker
10
+ class Base
11
+ attr_reader :config, :variables
12
+
13
+ def initialize(config, variables)
14
+ @config = config
15
+ @variables = variables
16
+ end
17
+
18
+ def execute!
19
+ root = dgen_linker['root']
20
+ link_destination_directory = root['link_destination_directory']
21
+ links = root['links']
22
+ template_path = config[:template_path]
23
+
24
+ links.each do |link|
25
+ link.transform_keys!(&:to_sym)
26
+
27
+ file_path = template_path + '/' + link_destination_directory + '/' + link[:to_destinaiton_file]
28
+ template_content = File.read(config[:root_path] + '/' + link[:dgen_template])
29
+
30
+ parsed_path = PathParser.new(file_path, variables).parse
31
+ parsed_content = ContentParser.new(template_content, variables).parse
32
+
33
+ FileCreator.new(parsed_path, parsed_content).execute
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def dgen_linker
40
+ YAML.load(File.read(config[:linker_path])).to_h
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,26 @@
1
+ module Dumb
2
+ module Template
3
+ module Linker
4
+ class ContentParser
5
+ attr_reader :content, :variables
6
+
7
+ MATCH_REGEX = /({{(.*?)}})/.freeze
8
+
9
+ def initialize(content, variables)
10
+ @content = content
11
+ @variables = variables
12
+ end
13
+
14
+ def parse
15
+ template_variables = content.scan(MATCH_REGEX).to_h
16
+
17
+ template_variables.each do |full_string, variable_name|
18
+ content.gsub!(full_string, variables[variable_name])
19
+ end
20
+
21
+ content
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,32 @@
1
+ require 'fileutils'
2
+
3
+ module Dumb
4
+ module Template
5
+ module Linker
6
+ class FileCreator
7
+ attr_reader :path, :content
8
+
9
+ def initialize(path, content)
10
+ @path = path
11
+ @content = content
12
+ end
13
+
14
+ def execute
15
+ safe_create_hierarchy
16
+
17
+ File.open(path, 'w') { |file| file.write(content) }
18
+ end
19
+
20
+ private
21
+
22
+ def safe_create_hierarchy
23
+ file_dirname = File.dirname(path)
24
+
25
+ unless File.directory?(file_dirname)
26
+ FileUtils.mkdir_p(file_dirname)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ module Dumb
2
+ module Template
3
+ module Linker
4
+ class PathParser
5
+ attr_reader :path, :variables
6
+
7
+ MATCH_REGEX = /({{(.*?)}})/.freeze
8
+
9
+ def initialize(path, variables)
10
+ @path = path
11
+ @variables = variables
12
+ end
13
+
14
+ def parse
15
+ groups = path.scan(MATCH_REGEX).to_h
16
+ groups.each do |full_string, var_name|
17
+ path.gsub!(full_string, variables[var_name])
18
+ end
19
+
20
+ path
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ module Dumb
2
+ module Template
3
+ class Validator
4
+ attr_reader :templates_path, :template_name
5
+
6
+ def initialize(templates_path, template_name)
7
+ @templates_path = templates_path
8
+ @template_name = template_name
9
+ end
10
+
11
+ def validate!
12
+ validate_template_exists!
13
+ end
14
+
15
+ private
16
+ def validate_template_exists!
17
+ linker_missing = !File.exist?("#{templates_path}/#{template_name}/linker.yml")
18
+ variables_missing = !File.exist?("#{templates_path}/#{template_name}/variables.yml")
19
+ root_missing = !File.directory?("#{templates_path}/#{template_name}/root")
20
+
21
+ if linker_missing or variables_missing or root_missing
22
+ print ".dumb_templates/#{template_name}: Invalid template format. Should be:\n\n"
23
+ print "#{template_name}\n"
24
+ print "├──root/\n├──linker.yml\n└──variables.yml\n\n"
25
+ exit
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'dumb/input/parser'
2
+ require_relative 'dumb/generator/runner'
3
+ require_relative 'dumb/template/initializer'
4
+
5
+ module Dumb
6
+ module Generator
7
+ class Base
8
+ def execute!
9
+ commands = Dumb::Input::Parser.commands
10
+ arguments = commands[:arguments]
11
+
12
+ if commands[:command] == "run"
13
+ Dumb::Generator::Runner.new(arguments).execute!
14
+ elsif commands[:command] == "init"
15
+ Dumb::Template::Initializer.new.execute!
16
+ elsif commands[:command] == "help"
17
+ print <<-HELP
18
+ Usage: dgen [COMMAND] [ARGUMENT_NAME:ARGUMENT_VALUE]
19
+
20
+ Ex:
21
+ $> dgen template:default class_name:MyClass file_name:my_file
22
+
23
+ Commands:
24
+ run: run a template (default, if no command is specified)
25
+ init: initialize the dumb_generator, adds .dumb_templates to current directory and a default template
26
+ help: show this message
27
+
28
+ Arguments:
29
+ template: specify a template to run
30
+ HELP
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ Dumb::Generator::Base.new.execute!
38
+
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dumb_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cristian Cosneanu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple hello world gem
14
+ email: cosneanuc@gmail.com
15
+ executables:
16
+ - dgen
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - bin/dgen
23
+ - lib/dumb/generator/runner.rb
24
+ - lib/dumb/generator/template_config_assembler.rb
25
+ - lib/dumb/input/parser.rb
26
+ - lib/dumb/input/validator.rb
27
+ - lib/dumb/template/initializer.rb
28
+ - lib/dumb/template/linker/base.rb
29
+ - lib/dumb/template/linker/content_parser.rb
30
+ - lib/dumb/template/linker/file_creator.rb
31
+ - lib/dumb/template/linker/path_parser.rb
32
+ - lib/dumb/template/validator.rb
33
+ - lib/dumb_generator.rb
34
+ homepage: https://rubygems.org/gems/dumb_generator
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.4.10
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: A simple scaffolding generator
57
+ test_files: []