/
lotusrc.rb
146 lines (132 loc) · 3.65 KB
/
lotusrc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require 'pathname'
require 'dotenv'
module Lotus
# Create and read the .lotusrc file in the root of the application
#
# @since 0.3.0
# @api private
class Lotusrc
# Lotusrc name file
#
# @since 0.3.0
# @api private
#
# @see Lotus::Lotusrc#path_file
FILE_NAME = '.lotusrc'.freeze
# Architecture default value
#
# @since 0.3.0
# @api private
#
# @see Lotus::Lotusrc#read
DEFAULT_ARCHITECTURE = 'container'.freeze
# Architecture key for writing the lotusrc file
#
# @since 0.3.0
# @api private
#
# @see Lotus::Lotusrc#read
ARCHITECTURE_KEY = 'architecture'.freeze
# Test suite default value
#
# @since 0.3.0
# @api private
#
# @see Lotus::Lotusrc#read
DEFAULT_TEST_SUITE = 'minitest'.freeze
# Test suite key for writing the lotusrc file
#
# @since 0.3.0
# @api private
#
# @see Lotus::Lotusrc#read
TEST_KEY = 'test'.freeze
# Template default value
#
# @since 0.3.0
# @api private
#
# @see Lotus::Lotusrc#read
DEFAULT_TEMPLATE = 'erb'.freeze
# Template key for writing the lotusrc file
#
# @since 0.3.0
# @api private
#
# @see Lotus::Lotusrc#read
TEMPLATE_KEY = 'template'.freeze
# Initialize Lotusrc class with application's root and enviroment options.
# Create the lotusrc file if it doesn't exist in the root given.
#
# @param root [Pathname] Application's root
# @param options [Hash] Environment's options
#
# @see Lotus::Environment#initialize
def initialize(root, options = {})
@root = root
@options = options
# NOTE this line is here in order to auto-upgrade applications generated
# with lotusrb < 0.3.0. Consider to remove it in the future.
create
end
# Read lotusrc file and parse it's values.
#
# @return [Lotus::Utils::Hash] parsed values
#
# @example Default values if file doesn't exist
# Lotus::Lotusrc.new(Pathname.new(Dir.pwd)).read
# # => { architecture: 'container', test: 'minitest', template: 'erb' }
#
# @example Custom values if file doesn't exist
# options = { architect: 'application', test: 'rspec', template: 'slim' }
# Lotus::Lotusrc.new(Pathname.new(Dir.pwd), options).read
# # => { architecture: 'application', test: 'rspec', template: 'slim' }
def read
if exists?
lotusrc_options = Dotenv.load path_file
Utils::Hash.new(lotusrc_options).symbolize!
end
end
private
# Create lotusrc file if exists
#
# @since 0.3.0
# @api private
#
# @see Lotus::Lotusrc::DEFAULT_ARCHITECTURE
# @see Lotus::Lotusrc::ARCHITECTURE_KEY
# @see Lotus::Lotusrc::DEFAULT_TEST_SUITE
# @see Lotus::Lotusrc::TEST_KEY
# @see Lotus::Lotusrc::DEFAULT_TEMPLATE
# @see Lotus::Lotusrc::TEMPLATE_KEY
def create
unless exists?
rcfile = File.new(path_file, "w")
rcfile.puts "#{ ARCHITECTURE_KEY }=#{ @options.fetch(:architecture, DEFAULT_ARCHITECTURE) }"
rcfile.puts "#{ TEST_KEY }=#{ @options.fetch(:test, DEFAULT_TEST_SUITE) }"
rcfile.puts "#{ TEMPLATE_KEY }=#{ @options.fetch(:template, DEFAULT_TEMPLATE) }"
rcfile.close
end
end
# Check if lotusrc file exists
#
# @since 0.3.0
# @api private
#
# @return [Boolean] lotusrc file's path existing
def exists?
path_file.exist?
end
# Return the lotusrc file's path
#
# @since 0.3.0
# @api private
#
# @return [Pathname] lotusrc file's path
#
# @see Lotus::Lotusrc::FILE_NAME
def path_file
@root.join FILE_NAME
end
end
end