rubiclifier 0.1.0 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd4ba1b9ad523fe07ff82c350cb25fdbc4e888b05d3706acb65939af67fb3fed
4
- data.tar.gz: b99ec401e3abe0a155c7548babe520dad1385ab64151162237bb92a3080361a6
3
+ metadata.gz: 8d6ce2340c532ee4fb852c7039096830969ea1e45e5de1ad61d1a127fb4b8cec
4
+ data.tar.gz: cd172e3bbafc3f12ab49e9604461925c663fa6a2beeb29dd98e0f44815171979
5
5
  SHA512:
6
- metadata.gz: e543847ffc6a0f3a9de7a6fdc19355bd5a13157f721c3924d1b5e9ac839f8d7e56660e63aca4a21f497b31dc4393fb7ffa0af02ed8692d21c4e8cf497e3e2199
7
- data.tar.gz: b3b203e291003208172f463cb2d9241fb7b99b5ad2e185513b0002701d1c4466c71bed82497d14671fa7f7b3281e20fea2623e0af4fb0971e55e94a761c5fdae
6
+ metadata.gz: 26e52fec6adaa98607ff9f2c60452689918899dd3fb725a8fe77923682f50aea357327e843468668e000c93020b287111dd69c8516952fa76de777ea3d60cd69
7
+ data.tar.gz: a355033e4c0aac8214030b9be3fe1c58210a6578870b93b656186401cc43916f925ad968d2dd3556e75eb0297f8f4e259c9af64623766b8a5a338e23bf285081
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/cli/rubiclifier_cli.rb"
4
+
5
+ RubiclifierCli.new(ARGV).call
@@ -11,7 +11,7 @@ module Rubiclifier
11
11
  args[0]
12
12
  end
13
13
 
14
- def subcommand
14
+ def first_option
15
15
  args[1]
16
16
  end
17
17
 
@@ -10,12 +10,13 @@ module Rubiclifier
10
10
 
11
11
  def initialize(args)
12
12
  @args = Args.new(args)
13
- DB.hydrate(data_directory, migrations_location)
13
+ Feature.set_enabled(all_features)
14
+ DB.hydrate(data_directory, migrations_location) if Feature.enabled?(Feature::DATABASE)
14
15
  end
15
16
 
16
17
  def call
17
18
  show_help if args.command == "help" || args.boolean("help", "h")
18
- setup_or_fail
19
+ setup_or_fail if needs_setup?
19
20
  run_application
20
21
  end
21
22
 
@@ -33,16 +34,12 @@ module Rubiclifier
33
34
  def not_setup
34
35
  end
35
36
 
36
- def is_background_service?
37
- raise NotImplementedError
38
- end
39
-
40
- def sends_notifications?
41
- raise NotImplementedError
37
+ def features
38
+ []
42
39
  end
43
40
 
44
41
  def settings
45
- raise NotImplementedError
42
+ []
46
43
  end
47
44
 
48
45
  def executable_name
@@ -50,7 +47,7 @@ module Rubiclifier
50
47
  end
51
48
 
52
49
  def data_directory
53
- raise NotImplementedError
50
+ raise NotImplementedError if Feature.enabled?(Feature::DATABASE)
54
51
  end
55
52
 
56
53
  def migrations_location
@@ -61,14 +58,48 @@ module Rubiclifier
61
58
  []
62
59
  end
63
60
 
61
+ private
62
+
63
+ def all_brew_dependencies
64
+ @abd ||= [
65
+ ("sqlite" if Feature.enabled?(Feature::DATABASE)),
66
+ ("terminal-notifier" if Feature.enabled?(Feature::NOTIFICATIONS)),
67
+ ("sleepwatcher" if Feature.enabled?(Feature::IDLE_DETECTION))
68
+ ].concat(brew_dependencies).compact
69
+ end
70
+
71
+ def brew_dependencies_installed?
72
+ all_brew_dependencies.all? do |dep|
73
+ system("brew list #{dep} &> /dev/null")
74
+ end
75
+ end
76
+
77
+ def brew_installed_or_fail
78
+ unless system("which brew &> /dev/null")
79
+ puts
80
+ puts("You must install Homebrew in order to install these dependencies: #{all_brew_dependencies.join(", ")}".red)
81
+ puts("Go to https://brew.sh/")
82
+ exit
83
+ end
84
+ end
85
+
86
+ def all_features
87
+ [
88
+ (Feature::DATABASE if !settings.empty?)
89
+ ].concat(features).compact.uniq
90
+ end
91
+
92
+ def needs_setup?
93
+ !all_brew_dependencies.empty? || !settings.empty? || Feature.enabled?(Feature::BACKGROUND)
94
+ end
95
+
64
96
  def setup_or_fail
65
97
  if args.command == "setup" || args.boolean("setup", "s")
66
- puts("Installing brew dependencies...")
67
- all_brew_dependencies = [
68
- "sqlite",
69
- ("terminal-notifier" if sends_notifications?)
70
- ].concat(brew_dependencies).compact
71
- system("brew install #{all_brew_dependencies.join(" ")}")
98
+ unless all_brew_dependencies.empty?
99
+ brew_installed_or_fail
100
+ puts("Installing brew dependencies...")
101
+ system("brew install #{all_brew_dependencies.join(" ")}")
102
+ end
72
103
 
73
104
  puts
74
105
 
@@ -80,26 +111,24 @@ module Rubiclifier
80
111
 
81
112
  puts
82
113
 
83
- if is_background_service?
114
+ if Feature.enabled?(Feature::BACKGROUND)
84
115
  setup_as_background_service
85
116
  else
86
- puts("Finished setup! Run with `#{executable_name}`".green)
117
+ puts("Finished setup! Run with `".green + "#{executable_name}" + "`".green)
87
118
  end
88
119
  exit
89
- end
90
-
91
- unless settings.all?(&:is_setup?)
120
+ elsif !settings.all?(&:is_setup?) || !brew_dependencies_installed?
92
121
  not_setup
93
122
  puts
94
- puts("Oops! You must finish setup first by running with the `--setup` option.".red)
95
- puts(" `#{executable_name} --setup`".red)
123
+ puts("Oops! You must finish setup first.".red)
124
+ puts(" `".red + "#{executable_name} --setup" + "`".red)
96
125
  exit
97
126
  end
98
127
  end
99
128
 
100
129
  def setup_as_background_service
101
- puts("It's recommended that you set this up as a system service with serviceman. You can check it out here: https://git.rootprojects.org/root/serviceman".blue)
102
- puts("Set #{executable_name} to run on startup with `serviceman add --name #{executable_name} #{executable_name}`".blue)
130
+ puts("It's recommended that you set this up as a system service with serviceman. You can check it out here: https://git.rootprojects.org/root/serviceman".yellow)
131
+ puts("Set #{executable_name} to run on startup with `".yellow + "serviceman add --name #{executable_name} #{executable_name}" + "`".yellow)
103
132
  puts
104
133
  print("Would you like this script to set it up for you? (y/n) ")
105
134
  if STDIN.gets.chomp.downcase == "y"
@@ -0,0 +1,69 @@
1
+ require "rubiclifier"
2
+
3
+ class RubiclifierCli < Rubiclifier::BaseApplication
4
+ attr_reader :project_name
5
+ private :project_name
6
+
7
+ def show_help
8
+ puts
9
+ puts("rubiclifier helps you generate new Ruby CLI and background service tools")
10
+ puts
11
+ puts("Usage:")
12
+ puts(" rubiclifier --help | Shows this help menu")
13
+ puts(" rubiclifier new [project_name] | Creates a new rubiclifier project")
14
+ puts(" --background | Generate with background service setup steps")
15
+ puts(" --database | Generate with a persistent database")
16
+ puts(' --homebrew "[first [second]]" | Require specific homebrew kegs')
17
+ puts(" --notifications | Generate with notification functionality")
18
+ puts(" --settings | Generate with persistent setting functionality")
19
+ puts
20
+ exit
21
+ end
22
+
23
+ def run_application
24
+ case args.command
25
+ when "new"
26
+ create_project
27
+ else
28
+ show_help
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def create_project
35
+ @project_name = args.first_option
36
+ validate_project_name
37
+ system("mkdir -p #{project_name}")
38
+ system("cp -R \"#{File.dirname(__FILE__)}/../../project_template/.\" #{project_name}")
39
+ system("cp \"#{File.dirname(__FILE__)}/../../project_template/.gitignore\" #{project_name}")
40
+ Dir.glob("#{project_name}/**/*.erb").each do |template_file|
41
+ hydrate_template_file(template_file)
42
+ system("mv #{template_file} #{template_file.sub(/\.erb$/, "").sub("PROJECT_NAME", project_name)}")
43
+ end
44
+ unless template_hydrator.feature_enabled?(Rubiclifier::Feature::DATABASE)
45
+ system("rm #{project_name}/migrations.rb")
46
+ end
47
+ puts("Finished creating project #{project_name}! Build out the application in #{project_name}/lib/#{project_name}.rb".green)
48
+ end
49
+
50
+ def validate_project_name
51
+ if project_name.nil?
52
+ puts("You must specify a project name.".red)
53
+ exit
54
+ elsif !project_name.match(/^[a-z_]+$/)
55
+ puts("Your project name must be lowercase letters and _".red)
56
+ exit
57
+ end
58
+ end
59
+
60
+ def hydrate_template_file(template_file)
61
+ template_string = File.read(template_file)
62
+ output = template_hydrator.hydrate(template_string)
63
+ File.write(template_file, output)
64
+ end
65
+
66
+ def template_hydrator
67
+ @template_hydrator ||= TemplateHydrator.new(args, project_name)
68
+ end
69
+ end
@@ -0,0 +1,47 @@
1
+ require "rubiclifier"
2
+
3
+ class TemplateHydrator
4
+ attr_reader :args, :project_name
5
+ private :args
6
+
7
+ def initialize(args, project_name)
8
+ @args = args
9
+ @project_name = project_name
10
+ end
11
+
12
+ def hydrate(template_string)
13
+ ERB.new(template_string, nil, "-").result(binding)
14
+ end
15
+
16
+ def project_name_camel_case
17
+ @pnsc ||= project_name.split("_").collect(&:capitalize).join
18
+ end
19
+
20
+ def include_settings?
21
+ args.boolean("settings")
22
+ end
23
+
24
+ def brew_dependencies
25
+ args.string("homebrew")&.split(" ") || []
26
+ end
27
+
28
+ def uses_brew?
29
+ !brew_dependencies.empty?
30
+ end
31
+
32
+ def features
33
+ @features ||= [
34
+ (Rubiclifier::Feature::BACKGROUND if args.boolean("background")),
35
+ (Rubiclifier::Feature::DATABASE if (args.boolean("database") || include_settings?)),
36
+ (Rubiclifier::Feature::NOTIFICATIONS if args.boolean("notifications")),
37
+ ].compact
38
+ end
39
+
40
+ def needs_setup?
41
+ !features.empty? || uses_brew?
42
+ end
43
+
44
+ def feature_enabled?(feature)
45
+ features.include?(feature)
46
+ end
47
+ end
@@ -12,6 +12,7 @@ module Rubiclifier
12
12
  end
13
13
 
14
14
  def self.conn
15
+ Feature.fail_unless_enabled(Feature::DATABASE)
15
16
  @conn
16
17
  end
17
18
 
@@ -0,0 +1,20 @@
1
+ module Rubiclifier
2
+ module Feature
3
+ BACKGROUND = "BACKGROUND"
4
+ DATABASE = "DATABASE"
5
+ NOTIFICATIONS = "NOTIFICATIONS"
6
+ IDLE_DETECTION = "IDLE_DETECTION"
7
+
8
+ def self.set_enabled(features)
9
+ @enabled = features
10
+ end
11
+
12
+ def self.enabled?(feature)
13
+ @enabled.include?(feature)
14
+ end
15
+
16
+ def self.fail_unless_enabled(feature)
17
+ raise "Looks like you're trying to use feature 'Rubiclifier::Feature::#{feature}' without specifying it in your features." unless Feature.enabled?(feature)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ require_relative "./feature.rb"
2
+
3
+ module Rubiclifier
4
+ class IdleDetector
5
+ def self.set_idle_seconds_threshold(idle_seconds_threshold)
6
+ @idle_seconds_threshold = idle_seconds_threshold
7
+ end
8
+
9
+ def self.idle_seconds_threshold
10
+ @idle_seconds_threshold || 120
11
+ end
12
+
13
+ def self.is_idle?
14
+ Feature.fail_unless_enabled(Feature::IDLE_DETECTION)
15
+ seconds_idle = `/usr/local/sbin/sleepwatcher -g`.to_i / 10
16
+ seconds_idle > idle_seconds_threshold
17
+ end
18
+ end
19
+ end
@@ -1,8 +1,12 @@
1
+ require_relative "./feature.rb"
2
+
1
3
  module Rubiclifier
2
4
  class Notification
3
5
  attr_reader :title, :message, :subtitle, :icon, :url
4
6
  private :title, :message, :subtitle, :icon, :url
7
+
5
8
  def initialize(title, message, subtitle = nil, icon = nil, url = nil)
9
+ Feature.fail_unless_enabled(Feature::NOTIFICATIONS)
6
10
  @title = title
7
11
  @message = message
8
12
  @subtitle = subtitle
@@ -1,8 +1 @@
1
- require_relative "./args.rb"
2
- require_relative "./base_api.rb"
3
- require_relative "./base_application.rb"
4
- require_relative "./cipher.rb"
5
- require_relative "./colorized_strings.rb"
6
- require_relative "./database.rb"
7
- require_relative "./notification.rb"
8
- require_relative "./setting.rb"
1
+ Dir.glob("#{File.dirname(__FILE__)}/**/*.rb").each {|file| require file }
@@ -12,14 +12,19 @@ module Rubiclifier
12
12
  end
13
13
 
14
14
  def populate
15
- print("What's the #{label}? #{explanation_text}")
16
- input = if is_secret
17
- STDIN.noecho(&:gets).chomp.tap { puts }
18
- else
19
- STDIN.gets.chomp
20
- end
21
- if input == ""
22
- input = nil
15
+ input = nil
16
+ loop do
17
+ print("What's the #{label}? #{explanation_text}".blue)
18
+ input = if is_secret
19
+ STDIN.noecho(&:gets).chomp.tap { puts }
20
+ else
21
+ STDIN.gets.chomp
22
+ end
23
+ if input == ""
24
+ input = nil
25
+ puts("This value can't be empty.".red) unless nullable
26
+ end
27
+ break if input || nullable
23
28
  end
24
29
  DB.save_setting(key, input, is_secret: is_secret) unless input.nil?
25
30
  end
@@ -0,0 +1,4 @@
1
+ .byebug_history
2
+ Gemfile.lock
3
+ Brewfile.lock.json
4
+ *.gem
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "rubiclifier"
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "<%= project_name %>"
3
+ s.version = File.read("VERSION").strip
4
+ s.licenses = ["MIT"]
5
+ s.summary = "SUMMARY HERE"
6
+ s.authors = ["AUTHOR NAME"]
7
+ s.email = "AUTHOR EMAIL"
8
+ s.files = Dir.glob("{lib}/**/*") + ["Gemfile"]
9
+ s.homepage = "https://rubygems.org/gems/<%= project_name %>"
10
+ s.metadata = { "source_code_uri" => "https://github.com/PATH_TO_PROJECT" }
11
+ s.require_path = "lib"
12
+ s.platform = Gem::Platform::RUBY
13
+ s.executables = ["<%= project_name %>"]
14
+ s.post_install_message = <<MSG
15
+
16
+ \e[32mThanks for installing <%= project_name %>!\e[0m
17
+ \e[32mSet it up by running `\e[0m<%= project_name %> --setup\e[32m`\e[0m
18
+
19
+ MSG
20
+ end
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/<%= project_name %>.rb"
4
+
5
+ <%= project_name_camel_case %>.new(ARGV).call
@@ -0,0 +1,61 @@
1
+ require "rubiclifier"
2
+
3
+ class <%= project_name_camel_case %> < Rubiclifier::BaseApplication
4
+ def show_help
5
+ puts
6
+ puts("DESCRIPTION")
7
+ puts
8
+ puts("Usage:")
9
+ puts(" <%= project_name %> --help | Shows this help menu")
10
+ <%- if needs_setup? -%>
11
+ puts(" <%= project_name -%> --setup | Runs setup")
12
+ <%- end -%>
13
+ puts(" <%= project_name %> command --option <argument> | Executes command")
14
+ puts
15
+ exit
16
+ end
17
+
18
+ def run_application
19
+ while true
20
+ puts "Running!"
21
+ sleep 5
22
+ end
23
+ end
24
+ <%- if !features.empty? -%>
25
+
26
+ def features
27
+ [<%= features.map { |f| "Rubiclifier::Feature::#{f}" }.join(", ") %>]
28
+ end
29
+ <%- end -%>
30
+ <%- if include_settings? -%>
31
+
32
+ def settings
33
+ @settings ||= [
34
+ Rubiclifier::Setting.new("username", "username", explanation: ->{"find it at this url"}, nullable: true),
35
+ Rubiclifier::Setting.new("password", "password", explanation: "input hidden", is_secret: true)
36
+ ]
37
+ end
38
+ <%- end -%>
39
+ <%- if needs_setup? -%>
40
+
41
+ def executable_name
42
+ "<%= project_name %>"
43
+ end
44
+ <%- end -%>
45
+ <%- if feature_enabled?(Rubiclifier::Feature::DATABASE) -%>
46
+
47
+ def data_directory
48
+ "~/.<%= project_name %>"
49
+ end
50
+
51
+ def migrations_location
52
+ "#{File.expand_path(File.dirname(__FILE__) + "/..")}/migrations.rb"
53
+ end
54
+ <%- end -%>
55
+ <%- if uses_brew? -%>
56
+
57
+ def brew_dependencies
58
+ <%= brew_dependencies.inspect %>
59
+ end
60
+ <%- end -%>
61
+ end
@@ -0,0 +1,4 @@
1
+ require "rubiclifier"
2
+
3
+ class Api < Rubiclifier::BaseApi
4
+ end
@@ -0,0 +1,7 @@
1
+ migrations = []
2
+ migrations << <<SQL
3
+ CREATE TABLE IF NOT EXISTS test (
4
+ id INT PRIMARY KEY
5
+ );
6
+ SQL
7
+ migrations
metadata CHANGED
@@ -1,31 +1,46 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubiclifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Grinstead
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-08 00:00:00.000000000 Z
11
+ date: 2020-07-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: kyleag@hey.com
15
- executables: []
15
+ executables:
16
+ - rubiclifier
16
17
  extensions: []
17
18
  extra_rdoc_files: []
18
19
  files:
19
20
  - Gemfile
21
+ - bin/rubiclifier
20
22
  - lib/args.rb
21
23
  - lib/base_api.rb
22
24
  - lib/base_application.rb
23
25
  - lib/cipher.rb
26
+ - lib/cli/rubiclifier_cli.rb
27
+ - lib/cli/template_hydrator.rb
24
28
  - lib/colorized_strings.rb
25
29
  - lib/database.rb
30
+ - lib/feature.rb
31
+ - lib/idle_detector.rb
26
32
  - lib/notification.rb
27
33
  - lib/rubiclifier.rb
28
34
  - lib/setting.rb
35
+ - project_template/.gitignore
36
+ - project_template/Gemfile
37
+ - project_template/Gemfile.lock
38
+ - project_template/PROJECT_NAME.gemspec.erb
39
+ - project_template/VERSION
40
+ - project_template/bin/PROJECT_NAME.erb
41
+ - project_template/lib/PROJECT_NAME.rb.erb
42
+ - project_template/lib/api.rb
43
+ - project_template/migrations.rb
29
44
  homepage: https://rubygems.org/gems/rubiclifier
30
45
  licenses:
31
46
  - MIT