-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_working_directory.rb
executable file
·180 lines (120 loc) · 4.51 KB
/
create_working_directory.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env ruby
# encoding: utf-8
# frozen_string_literal: true
##########################################################
###
## File: create_working_directory.rb
## Desc: Create a git-based working directory for a specific ticket
## By: Dewayne VanHoozer (dvanhoozer@gmail.com)
#
# This program depends on the ability to do `git up` which is
# provided by the gem "git-up" so: gem install git-up
#
# System Environment Variables Used:
# PROJECT_DIR The absolute path to the top-level git project directory
if ENV['PROJECT_DIR'].nil? || ENV['PROJECT_DIR'].empty?
ENV['PROJECT_DIR'] = ENV['HOME'] + '/sandbox/git_repos/athn/advoy-rails'
end
# TODO: bring these branch names in from system environment variables
source_branch = 'review' # NOTE: 'development' and 'develop' are more common
update_branches = %w[ master release ] << source_branch
working_dir_name = 'working'
require 'amazing_print'
require 'debug_me'
include DebugMe
require 'cli_helper'
include CliHelper
configatron.version = '0.0.1'
NOT_PROVIDED = '** Not Provided **'
def not_provided?(thing)
thing == NOT_PROVIDED
end
HELP = <<EOHELP
Important:
The default value for the project root directory is:
#{ENV['PROJECT_DIR']}
EOHELP
cli_helper("Create a git-based working directory for a specific ticket") do |o|
o.string '-b', '--branch-name', 'Branch Name [required]', default: NOT_PROVIDED
o.path '-p', '--project-dir', 'Project Directory Root', default: Pathname.new(ENV['PROJECT_DIR'])
end
# Error check your stuff; use error('some message') and warning('some message')
if not_provided?(configatron.branch_name)
error 'The branch name is a required parameter; use -h or --help for usage instructions'
end
if String == configatron.project_dir.class
configatron.project_dir = Pathname.new(configatron.project_dir)
end
unless configatron.project_dir.exist? && configatron.project_dir.directory?
error "Project directory root is invalid: #{configatron.project_dir}"
end
abort_if_errors
######################################################
# Local methods
def do_command(command)
if debug? || verbose?
puts "Command: #{command} ..."
end
result = `#{command}`
end
######################################################
# Main
at_exit do
puts
puts "Done."
puts
end
ap configatron.to_h if verbose? || debug?
# Make sure that the source working directories are up to date.
# There is a base director where all work on this project is happening
# In this case its $HOME/sandbox/git_repos/athn/advoy-rails
# Call this $PROJECT_DIR
PROJECT_DIR = configatron.project_dir
WORKING_ROOT = PROJECT_DIR + working_dir_name
update_branches.each do |branch|
do_command "cd #{PROJECT_DIR + branch} && git up"
end
SOURCE_DIR = PROJECT_DIR + source_branch
target_branch = configatron.branch_name
TARGET_DIR = WORKING_ROOT + target_branch
# do_command "mkdir -p #{TARGET_DIR}"
# recurisvely copy the source WD to the target WD
# may need to mkdir [feature|bugfix|chore] if it does not already exist
do_command "cp -R #{SOURCE_DIR} #{TARGET_DIR}"
# GoTo the new WD and do a git checkout of the target branch
# This assumes that the target branch already exists
# TODO: configatron.branch_name is already a branch? If not,
# then need to create the branch before checking it out.
do_command "cd #{TARGET_DIR} && git checkout #{target_branch}"
# Do a git status to update all of the junk in the .git directory
do_command "cd #{TARGET_DIR} && git status"
# If a file '.envrc' exists then do 'direnv allow'
ENVRC = TARGET_DIR + '.envrc'
if ENVRC.exist?
do_command "cd #{TARGET_DIR} && direnv allow"
end
PREP_SCRIPT = 'prepare_default_config_for_development'
PREP_FILE = TARGET_DIR + PREP_SCRIPT
if PREP_FILE.exist? && PREP_FILE.file? && PREP_FILE.executable?
do_command "cd #{TARGET_DIR} && ./#{PREP_SCRIPT}"
end
# TODO: on previous project-specific version of this kind of script
# there was some additional steps that followed on like
# bundle install; database rebuild
# Might have a project-specific script at the working root that
# gets invoked with the branch name as a parameter. The script
# would then cd into the branch's working directory and do initialization
# stuff.
__END__
PROJECT_DIR
|-- update_branch_1 'master'
|-- update_branch_2 'release'
|-- source_branch 'review' | 'development' | 'develop'
|__ working
|-- simple_ticket_with_no '/'
|-- 'bugfix'
|__ 'feature'
| |__ ticket_id
|__ level_1
|__ level_2 ...
|__ ticket_id