-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
244 lines (204 loc) · 5.55 KB
/
main.go
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
////////////////////////////////////////////////////////////////////////////////
// The MIT License (MIT)
//
// Copyright (c) 2017 Mark LaPerriere
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
package main
import (
"flag"
"fmt"
"html/template"
"os"
"path/filepath"
)
// Version is the current semver of this project
const Version = "0.1.2"
// GoProjexDir is the directory used for the activate script
const GoProjexDir = ".gopjx"
// DefaultFileMod is the default set of permissions
const DefaultFileMod = 0755
var workspaceSkel = []string{
GoProjexDir,
"scripts",
"src",
}
var sourceSkel = []string{
"build",
"cmd",
"configs",
"docs",
"examples",
"scripts",
"test",
"vendor",
}
var errSkeletonInitNotSafe = fmt.Errorf("skeleton not safe to initialize")
// Project describes the project
type Project struct {
Name string
}
// NewProject creates a new project value
func NewProject(name string, ws *Workspace) *Project {
if name == "" {
name = filepath.Base(ws.Path)
}
return &Project{name}
}
// Skel describes a directory with other directories in it
type Skel struct {
Path string
Dirs []string
}
// IsSafe determines if it's save to create the direcotry tree described by Skel
func (s Skel) IsSafe() bool {
for _, d := range s.Dirs {
_, err := os.Stat(filepath.Join(s.Path, d))
if err == nil || !os.IsNotExist(err) {
return false
}
}
return true
}
// Create creates all the skeleton directories
func (s *Skel) Create() error {
for _, d := range s.Dirs {
path := filepath.Join(s.Path, d)
if err := os.MkdirAll(path, os.FileMode(DefaultFileMod)); err != nil {
return fmt.Errorf("failed to create workspace skeleton dir: %s", d)
}
fmt.Println("created:", path)
}
return nil
}
// Workspace describes the workspace Skel
type Workspace struct {
Skel
}
// NewWorkspace sets the path depending on the supplied argument
func NewWorkspace(path string, dirs []string) (*Workspace, error) {
if path == "" {
cwd, err := os.Getwd()
if err != nil {
cwd = "."
}
path = cwd
}
path, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("could not determine absolute path of workspace")
}
return &Workspace{Skel{path, dirs}}, nil
}
// CreateActivateScript creates the activate script
func (w *Workspace) CreateActivateScript(projectName, srcPath string) error {
tmpl, err := template.New("activate").Parse(activateTmplSrc)
if err != nil {
return fmt.Errorf("failed to compile activate template")
}
path := filepath.Join(w.Path, GoProjexDir, "activate")
script, err := os.Create(path)
if err != nil {
return err
}
defer script.Close()
err = script.Chmod(DefaultFileMod)
if err != nil {
return err
}
vars := struct {
Name string
GoPath string
SrcPath string
}{
projectName,
w.Path,
srcPath,
}
err = tmpl.Execute(script, vars)
if err != nil {
return err
}
fmt.Println("created activate script:", path)
return nil
}
// Source describes the source directory Skel
type Source struct {
Skel
}
// NewSource creates a new Source skel
func NewSource(ws *Workspace, path string, dirs []string) *Source {
if path == "" {
workspace := filepath.Base(ws.Path)
parent := filepath.Base(filepath.Dir(ws.Path))
path = filepath.Join(parent, workspace)
}
return &Source{
Skel{
filepath.Join(ws.Path, "src", path),
dirs,
},
}
}
// GoProjex creates the goprojex directory structure
func GoProjex(wsPath, srcPath, name string) error {
ws, err := NewWorkspace(wsPath, workspaceSkel)
if err != nil {
return err
}
if !ws.IsSafe() {
return fmt.Errorf("one or more workspace directories exist")
}
src := NewSource(ws, srcPath, sourceSkel)
if !src.IsSafe() {
return fmt.Errorf("one ore more workspace source directories empty")
}
err = ws.Create()
if err != nil {
return err
}
err = src.Create()
if err != nil {
return err
}
proj := NewProject(name, ws)
err = ws.CreateActivateScript(proj.Name, src.Path)
if err != nil {
return err
}
return nil
}
func main() {
ws := flag.String("ws", "", "path to create the workspace (othwerwise CWD)")
src := flag.String("src", "", "source path to create within the workspace (otherwise derived from workspace path")
name := flag.String("name", "", "name to display in shell prompt (otherwise base of the workspace path)")
version := flag.Bool("version", false, "print version and exit")
flag.Parse()
if *version {
fmt.Println(Version)
return
}
err := GoProjex(*ws, *src, *name)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}