-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate.go
71 lines (58 loc) · 1.97 KB
/
create.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
package main
import (
_ "embed"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
//go:embed template/AndroidManifest.xml
var manifestTmpl string
//go:embed template/MainActivity.java
var activityTmpl string
//go:embed template/styles.xml
var stylesTmpl string
//go:embed template/main.xml
var layoutTmpl string
// create command sets up the project with the template files in template/ dir
func create() {
rootDir := "."
if len(os.Args) > 2 && os.Args[2] != "." {
rootDir = os.Args[2]
}
pkg := prompt("Package name (com.myapp): ", "com.myapp")
name := prompt("App name (My App): ", "My App")
mustMkdir := func(path string) {
if err := os.Mkdir(path, 0755); err != nil {
LogF("create", err)
}
}
mustMkdir(rootDir)
src := filepath.Join(rootDir, "src")
mustMkdir(src)
for _, part := range strings.Split(pkg, ".") {
src = filepath.Join(src, part)
mustMkdir(src)
}
mustMkdir(filepath.Join(rootDir, "lib"))
mustMkdir(filepath.Join(rootDir, "lib", "arm64-v8a"))
mustMkdir(filepath.Join(rootDir, "lib", "armeabi-v7a"))
mustMkdir(filepath.Join(rootDir, "lib", "x86"))
mustMkdir(filepath.Join(rootDir, "lib", "x86_64"))
mustMkdir(filepath.Join(rootDir, "res"))
mustMkdir(filepath.Join(rootDir, "res", "layout"))
mustMkdir(filepath.Join(rootDir, "res", "values"))
mustWriteFile := func(path string, content string) {
if err := ioutil.WriteFile(path, []byte(content), 0644); err != nil {
LogF("create", err)
}
}
activity := strings.ReplaceAll(activityTmpl, "{{pkgname}}", pkg)
mustWriteFile(filepath.Join(src, "MainActivity.java"), activity)
manifest := strings.ReplaceAll(manifestTmpl, "{{appname}}", name)
manifest = strings.ReplaceAll(manifest, "{{pkgname}}", pkg)
mustWriteFile(filepath.Join(rootDir, "AndroidManifest.xml"), manifest)
mustWriteFile(filepath.Join(rootDir, "res", "layout", "main.xml"), layoutTmpl)
mustWriteFile(filepath.Join(rootDir, "res", "values", "styles.xml"), stylesTmpl)
mustWriteFile(filepath.Join(rootDir, ".gitignore"), "build/\n")
}