genius-builder 0.1.0 → 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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/bin/genius +149 -0
  3. data/lib/genius.rh +10 -1
  4. metadata +2 -2
  5. data/bin/genius +0 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ce95e868a8c02fe55e35c2750cd75789d4fba48c4c4c523f6f63bad81d34ff9
4
- data.tar.gz: 6b2248733ce5085133832037dd7b88504188f64971d85b239c4c116f5cec9331
3
+ metadata.gz: f3b88f0b419d9b88da858d6ace39842c451d11c26c49e6f20e654b4f8d64e602
4
+ data.tar.gz: a5f4b88ac27ff2f2b3bf46eab490e89736366a30f9bc65671426762e5af0a4cc
5
5
  SHA512:
6
- metadata.gz: 8459891a20549144918577faf8971cd573ca10b40eb78cb17ce4fc0335f0468653a862656f36481950e0ec054d94975b3372476a877239729a7ece1145419bc2
7
- data.tar.gz: 28486432edd067e18cd7937a7f92571598791d9a82f966be935aaf93be824eebded9d068e5e94f28bc8a971f3e8192fa6263e4a9c2638555e627896ad516e8e2
6
+ metadata.gz: 40b9ab31c924e9928580ea4360f58b17004a3962065ee53fc421379e963c4d4cbefde63ca6b51603c4605798d3df7a75f0116f86989a491c54358d2cffd9594a
7
+ data.tar.gz: 9f38a77fad22d58dee046d5299aaa58424fef0789823b085f03fa14deba1c1452923965b200e7576c3a17fe3a98b21237c899734b4dfec2aecff7bad16118d52
@@ -0,0 +1,149 @@
1
+ #!/usr/bin/env rash
2
+ require "rash"
3
+
4
+ module Genius
5
+ @@build_tools = {}
6
+ @@build_groups = {}
7
+
8
+ def self.build_tools
9
+ @@build_tools.dup
10
+ end
11
+
12
+ def self.build_groups
13
+ @@build_groups.dup
14
+ end
15
+
16
+ # Build tool methods
17
+ def self.register_build_tool(name, *files, &command)
18
+ @@build_tools[name.to_s] = BuildTool.new(name, *files, &command)
19
+ end
20
+
21
+ def self.define_build_tool_files(name, *files)
22
+ @@build_tools[name.to_s].files = files.flatten.map(&:to_s)
23
+ end
24
+
25
+ def self.add_build_tool_file(name, file)
26
+ @@build_tools[name.to_s].files << file.to_s
27
+ end
28
+
29
+ def self.define_build_tool_command(name, &command)
30
+ @@build_tools[name.to_s].commands = [command]
31
+ end
32
+
33
+ def self.add_build_tool_command(name, &command)
34
+ @@build_tools[name.to_s].commands << command
35
+ end
36
+
37
+ # Build group methods
38
+ def self.register_group(name, *extensions, &command)
39
+ @@build_groups[name.to_s] = BuildGroup.new(name, *extensions, &command)
40
+ end
41
+
42
+ def self.define_group_extensions(name, *exts)
43
+ @@build_groups[name.to_s].extensions = exts.flatten.map {|e| e.to_s.delete_prefiox(".")}
44
+ end
45
+
46
+ def self.add_group_extension(name, ext)
47
+ # requires 2.5
48
+ @@build_groups[name.to_s].extensions << ext.to_s.delete_prefix(".")
49
+ end
50
+
51
+ def self.define_group_command(name, &command)
52
+ @@build_groups[name.to_s].commands = [command]
53
+ end
54
+
55
+ def self.add_group_command(name, &command)
56
+ @@build_groups[name.to_s].commands << command
57
+ end
58
+
59
+
60
+ private
61
+
62
+ class BuildTool
63
+ attr_accessor :name, :files, :commands
64
+ def initialize(name, *files, &command)
65
+ @name = name
66
+ @files = files.flatten.map(&:to_s)
67
+ @commands = [command]
68
+ end
69
+
70
+ def build(files)
71
+ @commands.each do |command|
72
+ command.call(files)
73
+ end
74
+ end
75
+ end
76
+
77
+ class BuildGroup
78
+ attr_accessor :name, :extensions, :commands
79
+ def initialize(name, *extensions, &command)
80
+ @name = name
81
+ @extensions = extensions.flatten.map(&:to_s)
82
+ @commands = [command]
83
+ end
84
+
85
+ def build(files)
86
+ @commands.each do |command|
87
+ command.call(files)
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ $GENIUS_HOME = File.expand_path("~/.genius")
94
+
95
+ # Sanity check for if genius's dependencies exist.
96
+ if !Dir.exist?($GENIUS_HOME)
97
+ with_stdout_as_stderr do
98
+ puts ".genius - No such directory"
99
+ puts "\nTo use genius builder, put config files in the ~/.genius directory."
100
+ puts "Documentation for creating a genius config file can be found at <insert URL here>"
101
+ end
102
+ exit 1
103
+ end
104
+
105
+ # Load all config files in .genius, assuming all non-directory files are config files.
106
+ Dir[$GENIUS_HOME + "/*"].each do |config|
107
+ next if File.directory?(config)
108
+ exit unless load config
109
+ end
110
+
111
+ # Sort files by extension. Only files that have a name and extension are considered.
112
+ # i.e. Files that have no extension or are only extensions (including extensionless
113
+ # dotfiles) will not be considered. The final extension is used if multiple are
114
+ # present.
115
+ file_set = Hash.new {|h,k| h[k] = []}
116
+ Dir["**/*"].each do |file|
117
+ split_name = file.split(".")
118
+ if split_name.size > 1
119
+ file_set[split_name[-1]] << file
120
+ end
121
+ end
122
+
123
+ # Check for build tool patterns, terminating after build completion if one is matched
124
+ Genius.build_tools.each do |name, tool|
125
+ if tool.files.any? {|f| File.exist?(f) && !File.directory?(f)}
126
+ puts "Matched build tool pattern: #{tool.name}"
127
+ tool.build(file_set) # Does not contain all files, so may not be useful.
128
+ exit
129
+ end
130
+ end
131
+
132
+ # Get counts of files matching each build group.
133
+ group_counts = Genius.build_groups.map do |name, group|
134
+ file_count = group.extensions.inject(0) {|acc, ext| acc += file_set[ext].size}
135
+ [group, file_count]
136
+ end
137
+
138
+ # check if all patterns match
139
+ if group_counts.all? {|k,v| v.zero?}
140
+ with_stdout_as_stderr do
141
+ puts "No matching build patterns for the current directory"
142
+ exit 1
143
+ end
144
+ end
145
+
146
+ # Build based on commands specified by most populous build group,
147
+ # passing only the files with extensions specified by that group.
148
+ group = group_counts.max_by{|k,v| v}[0]
149
+ Genius.build_groups[group.name].build(file_set.filter{|k,v| group.extensions.include?(k)})
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env rash
2
+ require "rash"
2
3
 
3
4
  module Genius
4
5
  @@build_tools = {}
@@ -120,7 +121,7 @@ Dir["**/*"].each do |file|
120
121
  end
121
122
 
122
123
  # Check for build tool patterns, terminating after build completion if one is matched
123
- Genius.build_tools.each do |tool|
124
+ Genius.build_tools.each do |name, tool|
124
125
  if tool.files.any? {|f| File.exist?(f) && !File.directory?(f)}
125
126
  puts "Matched build tool pattern: #{tool.name}"
126
127
  tool.build(file_set) # Does not contain all files, so may not be useful.
@@ -134,6 +135,14 @@ group_counts = Genius.build_groups.map do |name, group|
134
135
  [group, file_count]
135
136
  end
136
137
 
138
+ # check if all patterns match
139
+ if group_counts.all? {|k,v| v.zero?}
140
+ with_stdout_as_stderr do
141
+ puts "No matching build patterns for the current directory"
142
+ exit 1
143
+ end
144
+ end
145
+
137
146
  # Build based on commands specified by most populous build group,
138
147
  # passing only the files with extensions specified by that group.
139
148
  group = group_counts.max_by{|k,v| v}[0]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genius-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kellen Watt
@@ -52,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
54
  requirements: []
55
- rubygems_version: 3.0.8
55
+ rubygems_version: 3.1.4
56
56
  signing_key:
57
57
  specification_version: 4
58
58
  summary: An intelligent compiler selection tool
data/bin/genius DELETED
@@ -1 +0,0 @@
1
- ../lib/genius.rh