Skip to content

Commit

Permalink
first commit gaes
Browse files Browse the repository at this point in the history
  • Loading branch information
kucingapes committed Sep 19, 2020
0 parents commit c906c91
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/sepack.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2020 Muhammad Utsman

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<h1 align="center">
Android Sepack
</h1>

<p align="center">
<img src="https://images.unsplash.com/photo-1511854289476-81c95d2a62c6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=80"/>
</p>

<p align="center">
<a href="LICENSE"><img alt="License" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a>
<a href="https://github.com/utsmannn/sepack/pulls"><img alt="Pull request" src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat"></a>
<a href="https://twitter.com/utsmannn"><img alt="Twitter" src="https://img.shields.io/twitter/follow/utsmannn"></a>
<a href="https://github.com/utsmannn"><img alt="Github" src="https://img.shields.io/github/followers/utsmannn?label=follow&style=social"></a>
<h3 align="center">Tool for generate android project base on MVVM</h3>
</p>


## Under Construction

```
Copyright 2020 Muhammad Utsman
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```
283 changes: 283 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
package main

import (
"bytes"
"fmt"
"github.com/go-errors/errors"
"github.com/go-git/go-git"
"github.com/manifoldco/promptui"
"github.com/urfave/cli"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)

const (
TEMPLATE_1 = "MVVM Basic"
TEMPLATE_2 = "MVVM Basic With Glide"
TEMPLATE_3 = "MVVM Basic RecyclerView"
)

func main() {
app := &cli.App{
Name: "init",
Version: "0.1.1",
Description: "Build android MVVM project with templates",
Usage: "Input application name, package name and select template",
Action: actionBuf,
}

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}

func cloneRepo(appName string, repo string) {
appName = strings.Replace(appName, " ", "-", -1)
_, err := git.PlainClone(appName, false, &git.CloneOptions{
URL: repo,
Progress: os.Stdout,
})

if err != nil {
log.Fatal(err)
}
}

func actionBuf(*cli.Context) error {
userInput()
return nil
}

func userInput() {
appName, errAppName := settingUpName("Enter application name", false)
if errAppName != nil {
log.Fatal(errAppName)
}

packageName, errPackageName := settingUpName("Enter package name", true)
log.Println("appname is ", appName)
log.Println("package name is ", packageName)

if errPackageName != nil {
log.Fatal(errPackageName)
}

promptTemplate := promptui.Select{
Label: "Select Template",
Items: []string{TEMPLATE_1, TEMPLATE_2, TEMPLATE_3},
}

_, result, err := promptTemplate.Run()
if err != nil {
log.Fatal(err)
}

switch result {
case TEMPLATE_1:
var repo = "https://github.com/utsmannn/sepack-basic"
setupClone(appName, repo, packageName)
case TEMPLATE_2:
var repo = "https://github.com/utsmannn/sepack-basic-glide"
setupClone(appName, repo, packageName)
case TEMPLATE_3:
var repo = "https://github.com/utsmannn/sepack-basic-recyclerview"
setupClone(appName, repo, packageName)
}
}

func setupClone(appName string, repo string, packageName string) {
fmt.Println("Cloning repo...")
cloneRepo(appName, repo)
removeUnusedDir(appName)
fmt.Println("Configuring...")
readFile(appName, packageName)
fmt.Println("Removing old dir...")
removeOldDir(appName, packageName)

goos := runtime.GOOS
switch goos {
case "windows", "darwin":
openAndroidStudio(appName)
}
}

func openAndroidStudio(appName string) {
prompt := promptui.Prompt{
Label: "Open project on Android Studio? (y/n)",
}

result, err := prompt.Run()
if err != nil {
log.Fatal(err)
}

if result == "y" || result == "yes" {
openStudio(appName)
} else {
fmt.Println("done..")
}
}


func openStudio(appName string) {
appName = strings.Replace(appName, " ", "-", -1)
goos := runtime.GOOS
switch goos {
case "windows":
var command = "start \"\" \"C:\\Program Files\\Android\\Android Studio\\bin\\studio64.exe\""
runCommand(command, appName)
case "darwin":
var command = "open -a /Applications/Android\\ Studio.app"
runCommand(command, appName)
}
}

func runCommand(command string, appName string) {
fmt.Println("Opening...")
cmd, err := exec.Command("bash", "-c", command + " " + appName).Output()
if err != nil {
log.Fatal(err)
}

log.Println(string(cmd))
}

func removeOldDir(appName string, packageName string) {
if !strings.Contains(packageName, "com.utsman") {
appName = strings.Replace(appName, " ", "-", -1)
oldDirAndroidTest := appName + "/app/src/androidTest/java/com/utsman"
oldDirMain := appName + "/app/src/main/java/com/utsman"
oldDirTest := appName + "/app/src/test/java/com/utsman"

err1 := os.RemoveAll(oldDirAndroidTest)
err2 := os.RemoveAll(oldDirMain)
err3 := os.RemoveAll(oldDirTest)

if err1 != nil {
log.Fatal(err1)
}

if err2 != nil {
log.Fatal(err2)
}

if err3 != nil {
log.Fatal(err3)
}
}
}

func removeUnusedDir(path string) {
path = strings.Replace(path, " ", "-", -1)
fmt.Println("Removing ds_store...")
err := os.Remove(path + "/.DS_Store")
fmt.Println("Removing idea config...")
err = os.RemoveAll(path + "/.idea")
fmt.Println("Removing git config...")
errGit := os.RemoveAll(path + "/.git")

if errGit != nil {
log.Fatal(errGit)
}

if err != nil {
log.Fatal(err)
}
}

func settingUpName(command string, dot bool) (string, error) {
prompt := promptui.Prompt{
Label: command,
}

result, err := prompt.Run()
validation := allowedChar(result, dot)
if !validation {
msg := "character not allowed"
errVal := errors.New(msg)
return result, errVal
}

return result, err
}

func readFile(appName string, packageName string) {
appNameSlash := strings.Replace(appName, " ", "-", -1)
err := filepath.Walk(filepath.Base(appNameSlash),
func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatal(err)
return err
}
dir, err := os.Stat(path)
if err != nil {
log.Fatal(err)
}
switch mode := dir.Mode(); {
case mode.IsDir():
// dir
case mode.IsRegular():
if path != "main.go" {
replace(path, appName, packageName)
newPath := pathFixing(path, packageName)

pathMkdir := strings.Replace(newPath, info.Name(), "", -1)
mkErr := os.MkdirAll(pathMkdir, os.ModePerm)
if mkErr != nil {
log.Fatal(mkErr)
}

err := os.Rename(path, newPath)
fmt.Println("Configuring file: ", newPath)
if err != nil {
log.Fatal(err)
}
}
}
return nil
})

if err != nil {
log.Fatal(err)
}
}

func pathFixing(path string, packageName string) string {
pathFixer := strings.Replace(packageName, ".", "/", -1)
newPath := strings.Replace(path, "com/utsman/sepack", pathFixer, -1)
return newPath
}

func replace(path string, appName string, packageName string) {
input, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}

input = bytes.Replace(input, []byte("com.utsman.sepack"), []byte(packageName), -1)
input = bytes.Replace(input, []byte("Sepack-mvvm"), []byte(appName), -1)
err = ioutil.WriteFile(path, input, 0666)
if err != nil {
log.Fatal(err)
}
}

func allowedChar(s string, dot bool) bool {
allowed := "abcdefghijklmnopqrstuvwxyz "
if dot {
allowed = "abcdefghijklmnopqrstuvwxyz. "
}

for _, char := range s {
if !strings.Contains(allowed, strings.ToLower(string(char))) {
return false
}
}
return true
}
Binary file added sepack
Binary file not shown.

0 comments on commit c906c91

Please sign in to comment.