-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
187 lines (159 loc) · 5.39 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
package main
import (
"fmt"
"path/filepath"
"strings"
lic "github.com/bilginyuksel/cph/licence"
"github.com/bilginyuksel/cph/generator"
"github.com/bilginyuksel/cph/parser"
"github.com/bilginyuksel/cph/reader"
"github.com/bilginyuksel/cph/tsc"
"github.com/alecthomas/kong"
)
func main() {
prepareCliParser()
}
func prepareCliParser() {
ctx := kong.Parse(&cli)
err := ctx.Run(&Context{Debug: cli.Debug})
ctx.FatalIfErrorf(err)
}
// SyncPluginXML ...
func SyncPluginXML(path string) error {
if path == "" {
path = "."
}
plugin, err := parser.ParseXML(fmt.Sprintf("%s/plugin.xml", path))
if err != nil {
return err
}
sourceFiles, _ := reader.FilePathWalkDir("src", []string{})
plugin.Platform.NewSourceFrom(sourceFiles)
jsModules, _ := reader.FilePathWalkDir("www", []string{})
plugin.NewJsModulesFrom(jsModules)
if len(plugin.Platform.ConfigFiles) == 0 {
pluginName := strings.ReplaceAll(plugin.Name, "HMS ", "")
configFile := parser.ConfigFile{Target: "config.xml", Parent: "/*", Features: []parser.Feature{parser.Feature{Name: "HMS" + pluginName, Params: []parser.Param{parser.Param{Name: "android-package", Value: fmt.Sprintf("com.huawei.hms.cordova.%s.HMS%s", strings.ToLower(pluginName), pluginName)}}}}}
plugin.Platform.ConfigFiles = append(plugin.Platform.ConfigFiles, configFile)
}
// ADD HOOKS
if len(plugin.Platform.Hooks) == 0 {
hooks := parser.Hook{Src: "hooks/before_plugin_uninstall.js", Type: "before_plugin_uninstall"}
plugin.Platform.Hooks = append(plugin.Platform.Hooks, hooks)
hooks = parser.Hook{Src: "hooks/after_plugin_install.js", Type: "after_plugin_install"}
plugin.Platform.Hooks = append(plugin.Platform.Hooks, hooks)
}
// ADD HOOKS
if len(plugin.Platform.Frameworks) == 0 {
plugin.Platform.Frameworks = append(plugin.Platform.Frameworks, parser.Framework{Custom: true, Type: "gradleReference", Src: "src/android/plugin.gradle"})
}
err = parser.CreateXML(plugin, "plugin.xml")
return err
}
// AddLicenceTo ...
func AddLicenceTo(path string, ignored []string) error {
fmt.Println(ignored)
if path == "" {
path = "."
}
files, _ := reader.FilePathWalkDir(path, ignored)
for _, p := range files {
lic.Write(p)
}
return nil
}
// PluginGenerator ...
func PluginGenerator(project string, include bool, tsutils bool, layoutUtils bool) error {
if len(project) > 0 && include {
generator.IncludeFramework(project, false)
} else if tsutils {
generator.CreateTSUtil()
} else if len(project) > 0 && layoutUtils {
generator.CreateLayoutUtilJavaClass(project)
} else {
generator.CreateHMSPlugin(project)
}
return nil
}
// Run ...
func (pl *PluginXMLCmd) Run(ctx *Context) error {
return SyncPluginXML(pl.Path)
}
// Run ...
func (l *AddLicenseCmd) Run(ctx *Context) error {
return AddLicenceTo(l.Path, l.Ignore)
}
// Run ...
func (p *PluginCmd) Run(ctx *Context) error {
return PluginGenerator(p.Project, p.Include, p.TSUtils, p.LayoutUtils)
}
func getJavaFileNames(files []string) []string {
javaFiles := []string{}
for _, val := range files {
ext := filepath.Ext(val)
if ext == ".java" {
javaFiles = append(javaFiles, val)
}
}
return javaFiles
}
func getAllCormetReferences(javaFiles []string) []tsc.CormetRef {
corRefList := []tsc.CormetRef{}
for _, val := range javaFiles {
_, value := filepath.Split(val)
value = strings.Replace(value, ".java", "", -1)
content := reader.ReadFile(val)
if tsc.HasCormet(content) {
corRefList = append(corRefList, *tsc.GetCormetRef(content, value))
}
}
return corRefList
}
// Run ...
func (g *GenerateCmd) Run(ctx *Context) error {
if g.TypeScript {
files, _ := reader.FilePathWalkDir(".", []string{})
javaFiles := getJavaFileNames(files)
cormetRefList := getAllCormetReferences(javaFiles)
if g.Single && len(g.Filename) > 0 {
tsc.WriteCormetRefListToFile(g.Filename, cormetRefList)
} else {
tsc.WriteCormetRefListToFiles(cormetRefList)
}
}
return nil
}
// Context ...
type Context struct {
Debug bool
}
// PluginXMLCmd ...
type PluginXMLCmd struct {
Path string `help:"The folder path to sync."`
}
// AddLicenseCmd ...
type AddLicenseCmd struct {
Path string `name:"path" help:"Paths to list." type:"path" short:"p"`
Ignore []string `name:"ignore" help:"list of ignored folders" short:"i"`
}
// PluginCmd ...
type PluginCmd struct {
Create bool `group:"choice" xor:"choice"`
Include bool `group:"choice" xor:"choice"`
LayoutUtils bool `name:"layout-utils" help:"It creates the util files to help your layout management in cordova." short:"lu"`
TSUtils bool `name:"ts-utils" help:"Creates the base ts utils file." short:"ti"`
Project string `name:"project" help:"Project name for the plugin."`
}
// GenerateCmd ...
type GenerateCmd struct {
TypeScript bool `required:"" name:"typescript" short:"t"`
Single bool `name:"single" short:"s" help:"Create all references inside one file instead of seperate files."`
Filename string `name:"filename" short:"f" help:"To use single command you have to write the filename."`
}
var cli struct {
Debug bool `help:"Enable debug mode."`
Plugin PluginCmd `cmd:"" help:"Use this command to create a cordova plugin from scratch."`
PluginXML PluginXMLCmd `cmd:"" help:"You can use the functions in that command to manipulate plugin.xml file under cordova plugin root project directory."`
Generate GenerateCmd `cmd:"" help:"Auto generation tool."`
AddLicense AddLicenseCmd `cmd:"" help:"Add license to files."`
}