gpt3-builder 0.0.3
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/.github/workflows/main.yml +31 -0
- data/.gitignore +50 -0
- data/.rspec +3 -0
- data/.rubocop.yml +82 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +25 -0
- data/Guardfile +30 -0
- data/LICENSE.txt +21 -0
- data/README.md +89 -0
- data/Rakefile +33 -0
- data/STORIES.md +44 -0
- data/USAGE.md +19 -0
- data/bin/console +16 -0
- data/bin/k +36 -0
- data/bin/kgitsync +76 -0
- data/bin/khotfix +244 -0
- data/bin/setup +11 -0
- data/gpt3-builder.gemspec +45 -0
- data/hooks/pre-commit +87 -0
- data/hooks/update-version +33 -0
- data/lib/gpt3/builder/gpt3_builder.rb +259 -0
- data/lib/gpt3/builder/version.rb +7 -0
- data/lib/gpt3/builder.rb +23 -0
- metadata +99 -0
data/bin/khotfix
ADDED
@@ -0,0 +1,244 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
#NOTE: you may need change file permissions
|
4
|
+
# chmod +x bin/khotfix
|
5
|
+
|
6
|
+
# Create a Versioned HotFix in Git and update the VERSION #
|
7
|
+
# USAGE
|
8
|
+
#
|
9
|
+
# ./bin/khotfix.sh 'my brand new hot fix'
|
10
|
+
|
11
|
+
# HELPERS
|
12
|
+
clear
|
13
|
+
|
14
|
+
l_heading ()
|
15
|
+
{
|
16
|
+
heading=$1
|
17
|
+
|
18
|
+
echo "======================================================================"
|
19
|
+
echo "${heading}"
|
20
|
+
echo "======================================================================"
|
21
|
+
}
|
22
|
+
|
23
|
+
l_line ()
|
24
|
+
{
|
25
|
+
echo "----------------------------------------------------------------------"
|
26
|
+
}
|
27
|
+
|
28
|
+
l_title ()
|
29
|
+
{
|
30
|
+
title=$1
|
31
|
+
|
32
|
+
l_line
|
33
|
+
echo "${title}"
|
34
|
+
l_line
|
35
|
+
}
|
36
|
+
|
37
|
+
l ()
|
38
|
+
{
|
39
|
+
title=$1
|
40
|
+
|
41
|
+
echo "${title}"
|
42
|
+
}
|
43
|
+
|
44
|
+
lkv ()
|
45
|
+
{
|
46
|
+
title=$1
|
47
|
+
value=$2
|
48
|
+
|
49
|
+
printf "%-25s : %s\n" "$title" "$value"
|
50
|
+
}
|
51
|
+
|
52
|
+
|
53
|
+
# expect 1 argument
|
54
|
+
MESSAGE=$1
|
55
|
+
|
56
|
+
l_heading "Make Hotfix - '${MESSAGE}'"
|
57
|
+
|
58
|
+
|
59
|
+
# This environment var will prevent git from asking for merge messages
|
60
|
+
export GIT_MERGE_AUTOEDIT=no
|
61
|
+
|
62
|
+
# Set up colour support, if we have it
|
63
|
+
# Are stdout and stderr both connected to a terminal, If not then don't set colours
|
64
|
+
if [ -t 1 -a -t 2 ]
|
65
|
+
then
|
66
|
+
if tput -V >/dev/null 2>&1
|
67
|
+
then
|
68
|
+
C_RED=`tput setaf 1`
|
69
|
+
C_GREEN=`tput setaf 2`
|
70
|
+
C_BROWN=`tput setaf 3`
|
71
|
+
C_BLUE=`tput setaf 4`
|
72
|
+
C_RESET=`tput sgr0`
|
73
|
+
fi
|
74
|
+
fi
|
75
|
+
|
76
|
+
|
77
|
+
exit_error ()
|
78
|
+
{
|
79
|
+
# dont display if string is zero length
|
80
|
+
[ -z "$1" ] || echo "${C_RED}Error: ${C_BROWN} $1 ${C_RESET}"
|
81
|
+
exit 1
|
82
|
+
}
|
83
|
+
|
84
|
+
|
85
|
+
# check arguments
|
86
|
+
[ ! -z "${MESSAGE}" ] || exit_error "You must pass a message when making a hotfix"
|
87
|
+
|
88
|
+
# make sure we are in a git tree
|
89
|
+
[ "`git rev-parse --is-inside-work-tree`" = "true" ] || exit_error "NOT a git repository"
|
90
|
+
l "Repository check OK"
|
91
|
+
|
92
|
+
CURRENT_BRANCH=`git branch | awk '/^\*/{print $2}'`
|
93
|
+
|
94
|
+
lkv "CURRENT_BRANCH" "${CURRENT_BRANCH}"
|
95
|
+
|
96
|
+
# check that we are on develop or master
|
97
|
+
[ "${CURRENT_BRANCH}" = "master" -o "${CURRENT_BRANCH}" = "develop" ] || exit_error "You MUST be on either the master or development branch"
|
98
|
+
l "OK: Branch"
|
99
|
+
|
100
|
+
# check that the current branch is NOT clean (we need changes to make a hotfix)
|
101
|
+
[ ! -z "`git status --porcelain`" ] || exit_error "Working copy is clean - no hotfix"
|
102
|
+
l "OK: Working copy has hotfix ready"
|
103
|
+
|
104
|
+
# fetch from origin
|
105
|
+
git fetch origin || exit_error "Failed to fetch from origin"
|
106
|
+
l "OK: Fsetch from origin"
|
107
|
+
|
108
|
+
# get the current directory that this script is in.
|
109
|
+
# we assume that the other scripts are in the same directory
|
110
|
+
SYNC_SCRIPT=kgitsync
|
111
|
+
SCRIPT_DIR=`dirname $0`
|
112
|
+
|
113
|
+
# ----------------------------------------------------------------------
|
114
|
+
# GET VERSION NUMBER
|
115
|
+
# ----------------------------------------------------------------------
|
116
|
+
|
117
|
+
l_title 'GET VERSION NUMBER'
|
118
|
+
|
119
|
+
# Get the last tag version
|
120
|
+
VERSION=`git tag | sort -V | tail -1`
|
121
|
+
lkv "CURENT VERSION" "${VERSION}"
|
122
|
+
|
123
|
+
# NOTE, if you don't have a TAG then you might need to manually do
|
124
|
+
# git flow hotfix start v0.01.001
|
125
|
+
# git flow hotfix finish v0.01.001
|
126
|
+
|
127
|
+
# Verify its format
|
128
|
+
[[ "`git tag | sort -V | tail -1`" =~ ^v[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,3}$ ]] || exit_error "Bad version format, version=${VERSION}"
|
129
|
+
# extract the version components
|
130
|
+
IFS=. read MAJOR_VERSION MINOR_VERSION PATCH_VERSION <<< "${VERSION}"
|
131
|
+
|
132
|
+
# inc the patch version. note: force base 10 as leading 0 makes bash think this is octal
|
133
|
+
PATCH_VERSION=$((10#${PATCH_VERSION} + 1))
|
134
|
+
lkv 'Patch #' "${PATCH_VERSION}"
|
135
|
+
|
136
|
+
# assemble the new version
|
137
|
+
NEW_VERSION="${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}"
|
138
|
+
lkv "NEW VERSION" "${NEW_VERSION}"
|
139
|
+
|
140
|
+
./hooks/update-version "${NEW_VERSION}" || exit_error "could not update version.rb you may need to run [chmod +x hooks/update-version]"
|
141
|
+
|
142
|
+
l_line
|
143
|
+
l 'git add .'
|
144
|
+
git add .
|
145
|
+
|
146
|
+
l 'git status'
|
147
|
+
l_line
|
148
|
+
git status
|
149
|
+
|
150
|
+
# ----------------------------------------------------------------------
|
151
|
+
# Before doing anything, add the files and then run the commit hook
|
152
|
+
# Then stash any changes - working directory will be clean after this
|
153
|
+
# The commit hook is where we can check for any debugging code that has
|
154
|
+
# gone into the code and abort the commit
|
155
|
+
# ----------------------------------------------------------------------
|
156
|
+
|
157
|
+
l 'check for debugging code'
|
158
|
+
./hooks/pre-commit || exit_error "remove debugging code from the commit - then run again"
|
159
|
+
|
160
|
+
l "Stash changes to [git_make_hotfix: ${MESSAGE}]"
|
161
|
+
|
162
|
+
git stash save "git_make_hotfix: ${MESSAGE}"
|
163
|
+
|
164
|
+
l "OK: Stashed"
|
165
|
+
|
166
|
+
l_title 'Master/Develop Branches'
|
167
|
+
# ----------------------------------------------------------------------
|
168
|
+
# update develop
|
169
|
+
# ----------------------------------------------------------------------
|
170
|
+
|
171
|
+
lkv "Develop" "Synchronized"
|
172
|
+
git checkout develop || exit_error "Failed to checkout develop branch"
|
173
|
+
lkv "Develop" "Switched"
|
174
|
+
git merge origin/develop || exit_error "Failed to merge develop from origin"
|
175
|
+
lkv "Develop" "Merged and upto date"
|
176
|
+
|
177
|
+
# ----------------------------------------------------------------------
|
178
|
+
# update master
|
179
|
+
# ----------------------------------------------------------------------
|
180
|
+
|
181
|
+
lkv "Master" "Synchronized"
|
182
|
+
git checkout master || exit_error "Failed to checkout master branch"
|
183
|
+
lkv "Master" "Switched"
|
184
|
+
git merge origin/master || exit_error "Failed to merge master from origin"
|
185
|
+
lkv "Master" "Merged and upto date"
|
186
|
+
|
187
|
+
# ----------------------------------------------------------------------
|
188
|
+
# start the hotfix
|
189
|
+
# ----------------------------------------------------------------------
|
190
|
+
|
191
|
+
l_title 'Hotfix'
|
192
|
+
|
193
|
+
lkv "Version" "${NEW_VERSION}"
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
lkv "git flow hotfix start" "${NEW_VERSION}"
|
198
|
+
|
199
|
+
git flow hotfix start "${NEW_VERSION}" || exit_error "Failed to start hotfix ${NEW_VERSION}"
|
200
|
+
|
201
|
+
HOTFIX_BRANCH="hotfix/${NEW_VERSION}"
|
202
|
+
|
203
|
+
lkv "started" "${HOTFIX_BRANCH}"
|
204
|
+
|
205
|
+
# ----------------------------------------------------------------------
|
206
|
+
# commit the stashed changes
|
207
|
+
# ----------------------------------------------------------------------
|
208
|
+
|
209
|
+
lkv "git stash" "pop"
|
210
|
+
|
211
|
+
git stash pop || exit_error "Failed to pop the stash into the hotfix"
|
212
|
+
|
213
|
+
lkv "git commit" "${MESSAGE}"
|
214
|
+
|
215
|
+
git commit -a -m"${MESSAGE}" || exit_error "Failed to commit the stash to ${HOTFIX_BRANCH}"
|
216
|
+
|
217
|
+
lkv "git commit succesful" "${HOTFIX_BRANCH}"
|
218
|
+
|
219
|
+
# ----------------------------------------------------------------------
|
220
|
+
# close the hotfix
|
221
|
+
# ----------------------------------------------------------------------
|
222
|
+
|
223
|
+
lkv "git flow hotfix finish" "${NEW_VERSION}"
|
224
|
+
|
225
|
+
git flow hotfix finish "${NEW_VERSION}" -m"${MESSAGE}" || exit_error "Failed to close ${HOTFIX_BRANCH}"
|
226
|
+
|
227
|
+
lkv "finished" "${HOTFIX_BRANCH}"
|
228
|
+
|
229
|
+
# closing the hotfix leaves you on the development branch
|
230
|
+
# return to the master ???? or should we stay on development
|
231
|
+
|
232
|
+
lkv "git checkout" "master"
|
233
|
+
|
234
|
+
git checkout master || exit_error "Failed to return to master branch"
|
235
|
+
|
236
|
+
# ----------------------------------------------------------------------
|
237
|
+
# now push the changes
|
238
|
+
# ----------------------------------------------------------------------
|
239
|
+
|
240
|
+
l_title 'Push Changes and Synchronize Master/Develop'
|
241
|
+
|
242
|
+
${SCRIPT_DIR}/${SYNC_SCRIPT}
|
243
|
+
|
244
|
+
l_heading 'Success'
|
data/bin/setup
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/gpt3/builder/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.required_ruby_version = '>= 2.5'
|
7
|
+
spec.name = 'gpt3-builder'
|
8
|
+
spec.version = Gpt3::Builder::VERSION
|
9
|
+
spec.authors = ['David Cruwys']
|
10
|
+
spec.email = ['david@ideasmen.com.au']
|
11
|
+
|
12
|
+
spec.summary = 'Gpt3 Builder provides builder pattern for creating GTP3 questions and answers against OPENAI'
|
13
|
+
spec.description = <<-TEXT
|
14
|
+
Gpt3 Builder provides builder pattern for creating GTP3 questions and answers against OPENAI
|
15
|
+
TEXT
|
16
|
+
spec.homepage = 'http://appydave.com/gems/gpt3-builder'
|
17
|
+
spec.license = 'MIT'
|
18
|
+
|
19
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
20
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
21
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.' unless spec.respond_to?(:metadata)
|
22
|
+
|
23
|
+
# spec.metadata['allowed_push_host'] = "Set to 'http://mygemserver.com'"
|
24
|
+
|
25
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
26
|
+
spec.metadata['source_code_uri'] = 'https://github.com/klueless-io/gpt3-builder'
|
27
|
+
spec.metadata['changelog_uri'] = 'https://github.com/klueless-io/gpt3-builder/commits/master'
|
28
|
+
|
29
|
+
# Specify which files should be added to the gem when it is released.
|
30
|
+
# The `git ls-files -z` loads the RubyGem files that have been added into git.
|
31
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
32
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
33
|
+
f.match(%r{^(test|spec|features)/})
|
34
|
+
end
|
35
|
+
end
|
36
|
+
spec.bindir = 'exe'
|
37
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
38
|
+
spec.require_paths = ['lib']
|
39
|
+
# spec.extensions = ['ext/gpt3_builder/extconf.rb']
|
40
|
+
|
41
|
+
spec.add_dependency 'k_log' , '~> 0.0.0'
|
42
|
+
spec.add_dependency 'ruby-openai'
|
43
|
+
# spec.add_dependency 'k_type' , '~> 0.0.0'
|
44
|
+
# spec.add_dependency 'k_util' , '~> 0.0.0'
|
45
|
+
end
|
data/hooks/pre-commit
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'English'
|
5
|
+
|
6
|
+
# NOTE: you may need change file permissions
|
7
|
+
# chmod +x hooks/pre-commit
|
8
|
+
|
9
|
+
exit 0 if ARGV.include?('--no-verify')
|
10
|
+
|
11
|
+
warning_keywords = %w[console.log]
|
12
|
+
keywords = %w[binding.pry console.dir byebug debugger]
|
13
|
+
files_changed = `git diff-index --name-only HEAD --`.split
|
14
|
+
|
15
|
+
# puts '----------------------------------------------------------------------'
|
16
|
+
# puts remove files changed from the pre-commit checking if they are one of the following files
|
17
|
+
# puts '----------------------------------------------------------------------'
|
18
|
+
# files_changed = files_changed - ['hooks/pre-commit']
|
19
|
+
# files_changed = files_changed - ['hooks/update-version']
|
20
|
+
|
21
|
+
# byebug may need to be in these files
|
22
|
+
files_changed -= ['Gemfile']
|
23
|
+
files_changed -= ['Gemfile.lock']
|
24
|
+
files_changed -= ['.gitignore']
|
25
|
+
files_changed -= ['README.md']
|
26
|
+
|
27
|
+
files_changed = files_changed.reject { |f| f.downcase.end_with?('.json') }
|
28
|
+
files_changed = files_changed.reject { |f| f.downcase.end_with?('.yml') }
|
29
|
+
|
30
|
+
# ignore files from specific folders
|
31
|
+
|
32
|
+
file_groups = files_changed.select do |item|
|
33
|
+
item.start_with?('hooks') # ||
|
34
|
+
# item.start_with?('lib/generators')
|
35
|
+
end
|
36
|
+
|
37
|
+
files_changed -= file_groups
|
38
|
+
|
39
|
+
# remove files that are changed because they are deleted
|
40
|
+
files_changed = files_changed.select { |filename| File.file?(filename) }
|
41
|
+
|
42
|
+
# puts '----------------------------------------------------------------------'
|
43
|
+
# puts 'Files Changed'
|
44
|
+
# puts '----------------------------------------------------------------------'
|
45
|
+
# puts files_changed
|
46
|
+
# puts '----------------------------------------------------------------------'
|
47
|
+
|
48
|
+
unless files_changed.length.zero?
|
49
|
+
# puts "#{keywords.join('|')}"
|
50
|
+
# puts "#{files_changed.join(' ')}"
|
51
|
+
|
52
|
+
`git grep -q -E "#{warning_keywords.join('|')}" #{files_changed.join(' ')}`
|
53
|
+
|
54
|
+
if $CHILD_STATUS.exitstatus.zero?
|
55
|
+
puts '' # Check following lines:''
|
56
|
+
puts $CHILD_STATUS.exitstatus
|
57
|
+
files_changed.each do |file|
|
58
|
+
warning_keywords.each do |keyword|
|
59
|
+
# puts "#{keyword} ::: #{file}"
|
60
|
+
`git grep -q -E #{keyword} #{file}`
|
61
|
+
if $CHILD_STATUS.exitstatus.zero?
|
62
|
+
line = `git grep -n #{keyword} #{file} | awk -F ":" '{print $2}'`.split.join(', ')
|
63
|
+
puts "WARNING:\t\033[31m#{file}\033[0m contains #{keyword} at line \033[33m#{line}\033[0m."
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
`git grep -q -E "#{keywords.join('|')}" #{files_changed.join(' ')}`
|
70
|
+
|
71
|
+
if $CHILD_STATUS.exitstatus.zero?
|
72
|
+
puts '' # Check following lines:''
|
73
|
+
puts $CHILD_STATUS.exitstatus
|
74
|
+
files_changed.each do |file|
|
75
|
+
keywords.each do |keyword|
|
76
|
+
# puts "#{keyword} ::: #{file}"
|
77
|
+
`git grep -q -E #{keyword} #{file}`
|
78
|
+
if $CHILD_STATUS.exitstatus.zero?
|
79
|
+
line = `git grep -n #{keyword} #{file} | awk -F ":" '{print $2}'`.split.join(', ')
|
80
|
+
puts "ERROR :\t\033[31m#{file}\033[0m contains #{keyword} at line \033[33m#{line}\033[0m."
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
puts '# Force commit with --no-verify'
|
85
|
+
exit 1
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
# NOTE: you may need change file permissions
|
5
|
+
# chmod +x hooks/update-version
|
6
|
+
|
7
|
+
exit 1 if ARGV.empty?
|
8
|
+
|
9
|
+
version = ARGV[0]
|
10
|
+
version = version[1..-1] # revoke 'v' character, e.g. v0.1.1 becomes 0.1.1
|
11
|
+
|
12
|
+
namespaces = %w[Gpt3 Builder]
|
13
|
+
|
14
|
+
indent = 0
|
15
|
+
output = ['# frozen_string_literal: true', '']
|
16
|
+
|
17
|
+
namespaces.each do |namespace|
|
18
|
+
output.push "#{' ' * indent}module #{namespace}"
|
19
|
+
indent += 1
|
20
|
+
end
|
21
|
+
|
22
|
+
output.push "#{' ' * indent}VERSION = \'#{version}'"
|
23
|
+
indent -= 1
|
24
|
+
|
25
|
+
namespaces.each do
|
26
|
+
output.push "#{' ' * indent}end"
|
27
|
+
indent -= 1
|
28
|
+
end
|
29
|
+
|
30
|
+
output.push('')
|
31
|
+
|
32
|
+
printf "%-25<label>s : %<version>s\n", label: 'GEM VERSION', version: version
|
33
|
+
File.open('lib/gpt3/builder/version.rb', 'w+') { |f| f.write(output.join("\n")) }
|
@@ -0,0 +1,259 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Gpt3
|
4
|
+
module Builder
|
5
|
+
class Gpt3Builder
|
6
|
+
include KLog::Logging
|
7
|
+
|
8
|
+
attr_reader :client
|
9
|
+
|
10
|
+
attr_accessor :prompt
|
11
|
+
|
12
|
+
attr_accessor :access_token
|
13
|
+
attr_accessor :engine
|
14
|
+
|
15
|
+
attr_accessor :max_tokens
|
16
|
+
attr_accessor :temperature
|
17
|
+
attr_accessor :top_p
|
18
|
+
attr_accessor :frequency_penalty
|
19
|
+
attr_accessor :presence_penalty
|
20
|
+
|
21
|
+
# response = client.completions(engine: engine,
|
22
|
+
# parameters: {
|
23
|
+
# prompt: prompt,
|
24
|
+
# })
|
25
|
+
|
26
|
+
|
27
|
+
# def self.build
|
28
|
+
# init.build
|
29
|
+
# end
|
30
|
+
|
31
|
+
# Create and initialize the builder.
|
32
|
+
#
|
33
|
+
# @return [Builder] Returns the builder via fluent interface
|
34
|
+
def self.init() #configuration = nil)
|
35
|
+
builder = new() #configuration)
|
36
|
+
|
37
|
+
yield(builder) if block_given?
|
38
|
+
|
39
|
+
builder
|
40
|
+
end
|
41
|
+
|
42
|
+
# assigns a builder hash and defines builder methods
|
43
|
+
def initialize() # configuration = nil)
|
44
|
+
@access_token = ENV['OPENAI_ACCESS_TOKEN'] # ENV['OPENAI_SECRET_KEY']
|
45
|
+
@client = OpenAI::Client.new(access_token: access_token)
|
46
|
+
|
47
|
+
@engine = 'davinci-codex'
|
48
|
+
|
49
|
+
@max_tokens = 50
|
50
|
+
@temperature = 0
|
51
|
+
@top_p = 1
|
52
|
+
@frequency_penalty = 0
|
53
|
+
@presence_penalty = 0
|
54
|
+
|
55
|
+
@prompt = ''
|
56
|
+
|
57
|
+
# @target_folders = configuration.target_folders.clone
|
58
|
+
# @template_folders = configuration.template_folders.clone
|
59
|
+
end
|
60
|
+
|
61
|
+
# @return [Hash/StrongType] Returns data object, can be a hash
|
62
|
+
# or strong typed object that you
|
63
|
+
# have wrapped around the hash
|
64
|
+
def build
|
65
|
+
raise NotImplementedError
|
66
|
+
end
|
67
|
+
|
68
|
+
# def to_h
|
69
|
+
# {
|
70
|
+
# target_folders: target_folders.to_h,
|
71
|
+
# template_folders: template_folders.to_h
|
72
|
+
# }
|
73
|
+
# end
|
74
|
+
|
75
|
+
# def debug
|
76
|
+
# log.subheading 'kbuilder'
|
77
|
+
|
78
|
+
# log.kv 'current folder key' , current_folder_key
|
79
|
+
# log.kv 'current folder' , target_folder
|
80
|
+
# target_folders.debug(title: 'target_folders')
|
81
|
+
|
82
|
+
# log.info ''
|
83
|
+
|
84
|
+
# template_folders.debug(title: 'template folders (search order)')
|
85
|
+
# ''
|
86
|
+
# end
|
87
|
+
|
88
|
+
# ----------------------------------------------------------------------
|
89
|
+
# Fluent interface
|
90
|
+
# ----------------------------------------------------------------------
|
91
|
+
|
92
|
+
def start(message)
|
93
|
+
@started = true
|
94
|
+
add_block(message)
|
95
|
+
|
96
|
+
self
|
97
|
+
end
|
98
|
+
|
99
|
+
def human(message)
|
100
|
+
@human_question = true
|
101
|
+
add_block("You: #{message}")
|
102
|
+
|
103
|
+
self
|
104
|
+
end
|
105
|
+
alias dude human
|
106
|
+
|
107
|
+
def example(example = nil, file: nil)
|
108
|
+
example = example || ''
|
109
|
+
example = File.read(file) if file
|
110
|
+
|
111
|
+
add_block(example)
|
112
|
+
|
113
|
+
self
|
114
|
+
end
|
115
|
+
|
116
|
+
def add_file(file, **opts)
|
117
|
+
# move to command
|
118
|
+
full_file = opts.key?(:folder_key) ? target_file(file, folder: opts[:folder_key]) : target_file(file)
|
119
|
+
|
120
|
+
# Need logging options that can log these internal details
|
121
|
+
FileUtils.mkdir_p(File.dirname(full_file))
|
122
|
+
|
123
|
+
content = process_any_content(**opts)
|
124
|
+
|
125
|
+
file_write(full_file, content, on_exist: opts[:on_exist])
|
126
|
+
|
127
|
+
# Prettier needs to work with the original file name
|
128
|
+
run_prettier file if opts.key?(:pretty)
|
129
|
+
# Need support for rubocop -a
|
130
|
+
|
131
|
+
self
|
132
|
+
end
|
133
|
+
alias touch add_file # it is expected that you would not supply any options, just a file name
|
134
|
+
|
135
|
+
# def make_folder(folder_key = nil, sub_path: nil)
|
136
|
+
# folder_key = current_folder_key if folder_key.nil?
|
137
|
+
# folder = target_folder(folder_key)
|
138
|
+
# folder = File.join(folder, sub_path) unless sub_path.nil?
|
139
|
+
|
140
|
+
# FileUtils.mkdir_p(folder)
|
141
|
+
|
142
|
+
# self
|
143
|
+
# end
|
144
|
+
|
145
|
+
# def add_clipboard(**opts)
|
146
|
+
# # move to command
|
147
|
+
# content = process_any_content(**opts)
|
148
|
+
|
149
|
+
# begin
|
150
|
+
# IO.popen('pbcopy', 'w') { |f| f << content }
|
151
|
+
# rescue Errno::ENOENT => e
|
152
|
+
# if e.message == 'No such file or directory - pbcopy'
|
153
|
+
# # May want to use this GEM in the future
|
154
|
+
# # https://github.com/janlelis/clipboard
|
155
|
+
# puts 'Clipboard paste is currently only supported on MAC'
|
156
|
+
# end
|
157
|
+
# end
|
158
|
+
|
159
|
+
# self
|
160
|
+
# end
|
161
|
+
# alias clipboard_copy add_clipboard
|
162
|
+
|
163
|
+
# def vscode(*file_parts, folder: current_folder_key)
|
164
|
+
# # move to command
|
165
|
+
# file = target_file(*file_parts, folder: folder)
|
166
|
+
|
167
|
+
# rc "code #{file}"
|
168
|
+
|
169
|
+
# self
|
170
|
+
# end
|
171
|
+
|
172
|
+
# Fluent adder for target folder (KBuilder::NamedFolders)
|
173
|
+
def add_target_folder(folder_key, value)
|
174
|
+
target_folders.add(folder_key, value)
|
175
|
+
|
176
|
+
self
|
177
|
+
end
|
178
|
+
|
179
|
+
# def run_cop(file, **opts)
|
180
|
+
# command = Commands::RuboCopCommand.new(file, builder: self, **opts)
|
181
|
+
# command.execute
|
182
|
+
|
183
|
+
# self
|
184
|
+
# end
|
185
|
+
|
186
|
+
# # Need to handle absolute files, see
|
187
|
+
# # /Users/davidcruwys/dev/printspeak/reference_application/printspeak-domain/.builders/presentation/presentation_builder/commands/copy_ruby_resource_command.rb
|
188
|
+
# def run_prettier(file, log_level: :log)
|
189
|
+
# # command = "prettier --check #{file} --write #{file}"
|
190
|
+
# command = "npx prettier --loglevel #{log_level} --write #{file}"
|
191
|
+
|
192
|
+
# run_command command
|
193
|
+
# end
|
194
|
+
|
195
|
+
# def run_command(command)
|
196
|
+
# # Deep path create if needed
|
197
|
+
# tf = target_folder
|
198
|
+
|
199
|
+
# FileUtils.mkdir_p(tf)
|
200
|
+
|
201
|
+
# build_command = "cd #{tf} && #{command}"
|
202
|
+
|
203
|
+
# puts build_command
|
204
|
+
|
205
|
+
# # need to support the fork process options as I was not able to run
|
206
|
+
# # k_builder_watch -n because it hid all the following output
|
207
|
+
# system(build_command)
|
208
|
+
# end
|
209
|
+
# alias rc run_command
|
210
|
+
|
211
|
+
# def file_write(file, content, on_exist: :skip)
|
212
|
+
# not_found = !File.exist?(file)
|
213
|
+
|
214
|
+
# if not_found
|
215
|
+
# File.write(file, content)
|
216
|
+
# return
|
217
|
+
# end
|
218
|
+
|
219
|
+
# return if on_exist == :skip || on_exist == :ignore
|
220
|
+
|
221
|
+
# if on_exist == :overwrite || on_exist == :write
|
222
|
+
# File.write(file, content)
|
223
|
+
# return
|
224
|
+
# end
|
225
|
+
|
226
|
+
# if on_exist == :compare
|
227
|
+
# # need to use some sort of caching folder for this
|
228
|
+
# ext = File.extname(file)
|
229
|
+
# fn = File.basename(file, ext)
|
230
|
+
# temp_file = Tempfile.new([fn, ext])
|
231
|
+
|
232
|
+
# temp_file.write(content)
|
233
|
+
# temp_file.close
|
234
|
+
|
235
|
+
# return if File.read(file) == content
|
236
|
+
|
237
|
+
# system("code -d #{file} #{temp_file.path}")
|
238
|
+
# sleep 2
|
239
|
+
# end
|
240
|
+
# end
|
241
|
+
|
242
|
+
def debug
|
243
|
+
puts "----------------------------------------------------------------------"
|
244
|
+
puts prompt
|
245
|
+
puts "----------------------------------------------------------------------"
|
246
|
+
end
|
247
|
+
|
248
|
+
private
|
249
|
+
|
250
|
+
def add_line(message)
|
251
|
+
self.prompt = self.prompt + message + "\n"
|
252
|
+
end
|
253
|
+
|
254
|
+
def add_block(message)
|
255
|
+
self.prompt = self.prompt + message + "\n\n"
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
259
|
+
end
|